/* 
COPYRIGHT 4D INTERNET SOLUTIONS, INC. 2008. ALL RIGHTS RESERVED.
UNLESS YOU PURCHASED THIS SCRIPT FROM 4DI SOLUTIONS, MY STORE SOLUTIONS OR SPRING STUDIO, YOU DO NOT HAVE PERMISSION TO USE. THIS SCRIPT MAY NOT BE COPIED OR TRANSFERED.
*/
var mssQvImageVersion = 1.0;
var mssQvImage = new Class({
    options: {
        index: 0,
        cols: 4,
        element: false,
        caption: '',
        pnum: 0,
        itemsperpage: 20,
        container: 'container',
        waitText: 'Loading... Please wait...',
        qvOffestLeft: 160,
        qvOffestTop: -50,
        qvOffestLeft2: -415,
        qvOffestTop2: -50
    },

    initialize: function (options) {
        this.setOptions(options),
		this.index = this.options.index,
		this.cols = this.options.cols,
		this.element = $(this.options.element),
		this.caption = this.options.caption,
		this.pnum = (this.options.pnum == 0) ? 0 : this.options.pnum - 1,
		this.itemsperpage = this.options.itemsperpage,
		this.container = $(this.options.container) || null,
		this.waitText = this.options.waitText || "",
		this.mod = this.index % this.cols,
		this.left = (this.mod < 1) ? true : false,
		this.qvOffestLeft = this.options.qvOffestLeft || 0,
		this.qvOffestTop = this.options.qvOffestTop,
		this.qvOffestLeft2 = this.options.qvOffestLeft2 || 0,
		this.qvOffestTop2 = this.options.qvOffestTop2,
		this.qv = null,
		this.qviShell = null,
		this.qvOverlay = null,
		this.waiting = null,
		this.cLeft = 0,
		this.cTop = 0,
		this.prepQvWin(this.element.childNodes[0])
    },

    prepQvWin: function (el) {
        this.cLeft = (this.left) ? this.findPos(el)[0] + this.qvOffestLeft : this.findPos(el)[0] + this.qvOffestLeft2;
        this.cTop = (this.left) ? this.findPos(el)[1] + this.qvOffestTop : this.findPos(el)[1] + this.qvOffestTop2;
        var sideClass = (this.left) ? 'mssQviLeft' : 'mssQviRight';
        var arrowId = (this.left) ? 'arrowLeft' : 'arrowRight';
        if ($('qviShell')) { $('qviShell').remove(); }
        this.qviShell = new Element('div', { 'id': 'qviShell', 'class': sideClass, 'styles': { 'top': this.cTop, 'left': this.cLeft} }).injectInside(document.body);
        this.qviTop = new Element('div', { 'id': 'qviTop' }).injectInside(this.qviShell);
        this.qviCaption = new Element('span', { 'id': 'qviCaption' }).injectInside(this.qviTop);
        this.qviContainer = new Element('div', { 'id': 'qviContainer' }).injectInside(this.qviShell);
        this.qviBottom = new Element('div', { 'id': 'qviBottom' }).injectInside(this.qviShell);
        this.qviArrow = new Element('div', { 'id': arrowId }).injectInside(this.qviShell);

        this.imgIndex = (this.pnum == 0) ? this.index : this.index + (this.itemsperpage * this.pnum);

        var pid = getPidFromUrl(this.element.href);

        this.img = new Element('img', { 'src': mssImgRefs[mssImgIDs.indexOf(pid.toUpperCase())] }).injectInside(this.qviContainer);
        //this.img = new Element('img', { 'src': mssImgRefs[this.imgIndex] }).injectInside(this.qviContainer);
        //if(window.gecko){ this.qviShell.setStyles({'top': this.cTop - this.img.offsetHeight}) }

        this.qviCaption.innerHTML = this.caption;
        this.showQvWin(el);
    },

    findPos: function (obj) {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
        }
        return [curleft, curtop];
    },

    showQvWin: function (thumb) {
        var fadeInFx = new Fx.Style('qviShell', 'opacity', { duration: 300 }).start(0, 1);
        thumb.parentNode.addEvent('mouseleave', this.closeQv.bind(this));
    },

    closeQv: function () {
        if (!this.qviShell) return;
        var fadeOutFx = new Fx.Style('qviShell', 'opacity', { duration: 600 }).start(1, 0);
        if ($('qviShell')) { $('qviShell').remove(); this.qviShell = false; }
    }

});
mssQvImage.implement(new Options, new Events, new Chain);
Element.extend({
    mssQvImageNow: function (ind, caption, pnum) {
        var el = $(this);

        return new mssQvImage(({ index: ind, pnum: pnum, caption: caption, element: el }));
    }

});
function getPidFromUrl(url) {
    var page = url.substr(url.lastIndexOf("/") + 1);
    return page.substr(0, page.length - 5);
}
function addQvEvent(imgs){
	imgs.each(function(im, i){
			var caption = qvNames[i].innerHTML
			im.addEvent('mouseenter',function(){im.mssQvImageNow(i, caption, pnum)})
		});
}

function updateimgArray(pnum){
	var qvNames = $$('.name-row a');
	var qvImgs = $$('.img-row a');
	
	qvImgs.each(function(im, i){
			var caption = qvNames[i].innerHTML
			im.addEvent('mouseenter',function(){im.mssQvImageNow(i, caption, pnum)})
		});
	$$('#pagelinks a').each(function(a, i){
			var pnum = a.innerHTML * 1;
			a.addEvent('click',function(){updateimgArray(pnum)})
		});
}
// ### on domready ###
window.addEvent('load', function(){ 
	updateimgArray(0);
});

