var lightbox = new lb_class();

function lb_class() {
    this.clickedImage = this.container = null;
    this.allImages = [];
    this.stage = this.faded = 0;
    this.loaded = false;
    this.show = show;
    this.shown = shown;
    this.display = display;
    this.hidden = hidden;
    this.resized = resized;
    this.imgloaded = imgloaded;
    this.imageButtons = imageButtons;
    this.close = close;
    this.gone = gone;
    this.fixedSize = true;
    this.currentIndex = 0;
    this.bubble = bubble;
    this.bubbleIndex = 0;
    this.showBubble = showBubble;
    this.hideBubble = hideBubble;
    this.onresize = onresize;
    this.displayLoaded = displayLoaded;
    this.IE=isIE();
    this.withButtons=true;
    this.faded = -1;
    this.steps = this.IE?5:2;

    function show(srcObj, container) {
        if (flashobj = swfobject.getObjectById('video_player')) flashobj.pauseMovie();
        var imgObj = images[srcObj.id];
        //alert(srcObj.id);
        this.loaded = false;
        this.faded = -1;
        this.container = container;
        this.clickedImage = srcObj;
        this.currentIndex = 0;
        this.allImages = [];
        if (container !== null) {
            this.allImages = [];
            if (typeof(container) == "string") container = ob(container);
            var ims = container.childNodes;
            var j = 0;
            for (var i = 0; i < ims.length; i++) {
                if (ims[i].tagName) {
                    this.allImages.push(ims[i]);
                    if (ims[i] == srcObj) this.currentIndex = j;
                    j++;
                }
            }
        }else{
            var j = 0;
	        for(var key in images) {
	        	var nobj=null;
	        	if (nobj=ob(key)) {
	        		if (key==srcObj.id) this.currentIndex = j;
	        		this.allImages.push(nobj);
                    j++;
	        	}
	        }
        }
        var ins = "";
        fadeBackground(85);
        var src = imgObj["source"];
        var spacer = "<img src='/images/blank.gif' style='width:inherit;height:inherit' />";
        var des = (1 + this.currentIndex) + " of " + this.allImages.length + " - " + imgObj["description"];
        var lbcon = '<div style="width:300px;height:300px"></div>';
        var lbload = '';
        var trans = '';
        this.withButtons=!this.IE;
        if (src.match(/flv$/i)) {
            lbcon = "<div id='videodiv'><div id='videoplayerswf'><a href='http://www.adobe.com/go/getflash'><img src='http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash Player'  style='border:none'/></a></div></div>";
        } else {
        	withButtons=true;
            lbload = "<img src='" + src + "' onload='lightbox.imgloaded()' />";
        }
        ins += "<table id='lightbox'><tr><td><div class='box'><div id='lightboxheader'><h2 id='lb_title'>" + imgObj["name"] + "</h2><span id='lb_description'>" + des + "</span><div><div onclick='lightbox.close()'>close</div></div></div><div id='lightboxloader'>" + lbload + "</div><div id='lightboxbody'><div id='lightboximage' style=''>" + lbcon + "</div><div id='lightboxtransition'>"+trans+"</div><div id='imgloading'></div></div></div>";
        ins += "</td></tr></table>";
        pRH(ins, document.body);
        cO(ob('lightbox'));
        ob('imgloading').style.display='block';
        if (src.match(/flv$/i)) {
            this.display(this.currentIndex);
        }
        ob("lightbox").style.visibility="visible";
        fadeObject.fade(ob("lightbox"), 0, 100, this.steps, '');
    }
    function imageButtons() {
        var dims = 14;
        var w = this.allImages.length * (dims + 4);
        var ins = '';
        for (var i = 0; i < this.allImages.length; i++) {
            var src = images[this.clickedImage.id]["squareurl"];
            ins += "<div class='" + ((this.currentIndex == i) ? "clicked" : "button") + "' onmousemove='lightbox.showBubble(" + i + ",event)' onmouseout='lightbox.hideBubble()' onclick='lightbox.display(" + i + ");' id='lightboxbutton" + i + "' style='background:url(" + src + ")'></div>";
        }
        this.bubbleIndex = this.currentIndex;
        ins = this.bubble(images[this.allImages[this.currentIndex].id]["name"]) + "<div id='lightboxbuttons' style='width:" + w + "px;margin:0px auto'>" + ins + "</div>";
        return ins;
    }
    function onresize() {
        cO(ob('lightbox'));
    }
    function imgloaded() {
        this.loaded = true;
       ob("imgloading").style.display="none";
        if (this.faded < 0) setTimeout("lightbox.displayLoaded()",20);
    }
    function resized() {
        this.fixedSize = true;
    }
    function shown() {
    	if ((resizer.o.length==1 && fadeObject.o.length==0) || (resizer.o.length==0 && fadeObject.o.length==1)) {
        var transimg=ob("lightboxtransition").getElementsByTagName("IMG");
        if (transimg.length>0) {
		    var it = transimg[0];
		    var w=it.offsetWidth;
		    var h=it.offsetHeight;
		    var hs = '';
		    if (this.IE && iev<9) hs="top:"+(Math.round(h/2)-13)+"px";
			var ins = "<table id='imagebuttons' style='width:" + w + "px;height:" + h + "px;position:absolute' cellpadding=0 cellspacing=0><tr>" + ((this.currentIndex > 0) ? "<td onclick='lightbox.display(" + (this.currentIndex - 1) + ")' style='cursor:pointer' id='lblefttd'><div id=\"lbleft\" class=\"scrollleft\" style='"+hs+"'></div></td>" : "<td></td>") + "<td></td>" + ((this.currentIndex < this.allImages.length - 1) ? "<td id='lbrighttd' onclick='lightbox.display(" + (this.currentIndex + 1) + ")' style='cursor:pointer'><div id=\"lbright\" class=\"scrollright\" style='"+hs+"'></div></td>" : "<td></td>") + "</tr></table>";
		    ob("lightboximage").innerHTML = ins+"<img id='displayedImage' src='"+it.src+"' style='width:" + w + "px;height:" + h + "px' />";
			fader(it,0);
       }
        this.faded = 1;
        }
    }
    function display(n) {
        this.loaded = false;
        this.faded = 0;
        this.currentIndex = n;
        ob('lb_title').innerHTML = images[this.allImages[n].id]["name"];
        ob('lb_description').innerHTML = (1 + this.currentIndex) + " of " + this.allImages.length + " - " + images[this.allImages[n].id]["description"];
        var src = images[this.allImages[n].id]["source"];
        if (src.match(/flv$/i)) {
            this.loaded = true;
        } else {
            ob("lightboxloader").innerHTML = "<img src='" + src + "' onload='lightbox.imgloaded()' />";
        }
        if (ob('displayedImage')) {
		    ob('imgloading').style.display='block';
		    ob('imgloading').style.width=ob('displayedImage').offsetWidth+'px';
		    ob('imgloading').style.height=ob('displayedImage').offsetHeight+'px';
        }
        this.hidden();
    }
    function hidden() {
        this.faded = -1;
        if (this.loaded) setTimeout("lightbox.displayLoaded()",200);
    }
    function displayLoaded() {
        var src = images[this.allImages[this.currentIndex].id]["source"];
        if (src.match(/flv$/i)) {
            if (flashobj = swfobject.getObjectById('lb_player')) {
                if (this.withButtons) {
		            ob('lblefttd').style.display = (this.currentIndex > 0) ? "block" : "none";
		            ob('lblefttd').onclick=function(){lightbox.display(lightbox.currentIndex - 1)};
		            ob('lbrighttd').style.display = (this.currentIndex < this.allImages.length - 1) ? "block" : "none";
		            ob('lbrighttd').onclick=function(){lightbox.display((1*lightbox.currentIndex) + 1)};
               }
                playMovie("/" + src);
            } else {
                var c = ob("lightboxtransition");
                c.style.width = c.offsetWidth + "px";
                c.style.height = c.offsetHeight + "px";
                var ins = "<div id='videodiv'><div id='videoplayerswf'><a href='http://www.adobe.com/go/getflash'><img src='http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash Player' style='border:none'/></a></div>";
                if (this.withButtons) {
                	ins += "<div id='lblefttd' onclick='lightbox.display(" + (this.currentIndex - 1) + ")' style='display:" + ((this.currentIndex > 0) ? "block" : "none") + ";cursor:pointer;position:absolute;width:100px;top:0px;height:368px;left:0px'><div id='lbleft' class='scrollleft' style='position:absolute;left:10px;top:173px'></div></div>";
               		ins += "<div id='lbrighttd' onclick='lightbox.display(" + (this.currentIndex + 1) + ")' style='display:" + ((this.currentIndex < this.allImages.length - 1) ? "block" : "none") + ";cursor:pointer;position:absolute;width:100px;top:0px;height:368px;right:0px'><div id='lbright' class='scrollright' style='position:absolute;right:10px;top:173px'></div></div>";
               	}
                ins += "</div>";
                ob("lightboximage").innerHTML = ins;
                var flashvars = {};
                flashvars.startMovie = "/" + src;
                var params = {};
                params.menu = "false";
                params.allowScriptAccess = "sameDomain";
                params.swfliveconnect = "true";
                params.wmode = "opaque";
                params.bgcolor = "#000";
                var attributes = {};
                attributes.id = "lb_player";
                attributes.name = "lb_player";
                swfobject.embedSWF("/project/skin/largePlayer.swf", "videoplayerswf", "640", "368", "9.0.0", "/project/skin/expressInstall.swf", flashvars, params, attributes);
                resizer.resize([c,ob("videodiv")], true, true, c.offsetWidth, c.offsetHeight, 640, 368, ob("lightbox"), 20, 4, false, 'lightbox.resized()', false);
                this.fixedSize = false;
            }
        } else {
            var i = ob("lightboxloader").getElementsByTagName("IMG")[0];
            var c = ob("lightboximage");
            var cimg=c.getElementsByTagName("IMG");
            var ci=(cimg.length>0)?cimg[0]:c.childNodes[0];
            var cw=ci.offsetWidth;
            var ch=ci.offsetHeight;
            var transimg=ob("lightboxtransition").getElementsByTagName("IMG");
            if (transimg.length==0) {
            	ob("lightboxtransition").innerHTML="<img src='"+i.src+"' style='' />";
            	transimg=ob("lightboxtransition").getElementsByTagName("IMG");
            }
            var it = transimg[0];
           	ci.style.width = cw + "px";
            ci.style.height = ch + "px";
           	it.style.width = cw + "px";
            it.style.height = ch + "px";
            it.src=i.src;
            var w = i.offsetWidth;
            var h = i.offsetHeight;
            var mw = getStyle(c, "min-width").replace("px", "");
            var mh = getStyle(c, "min-height").replace("px", "");
            if (w < mw) w = mw;
            var w3 = w/3;
            if (h < mh) h = mh;
            var hs = '';
            if (this.IE && iev<9) hs="top:"+(Math.round(h/2)-13)+"px";
            this.fixedSize = false;
           var obs=[it];
           if (ob("displayedImage")) obs.push(ob("displayedImage"));
           //if (ob("imgloading")) obs.push(ob("imgloading"));
          removeTheNode('imagebuttons');
           if(cw!=w || ch!=h) {
           	resizer.resize(obs, true, true, cw, ch, w, h, ob("lightbox"), 20, 4, false, 'lightbox.shown()', false);
           }
        }
       	if (ob("displayedImage")) {
       		fader(it,100);
       		fadeObject.fade(ob("displayedImage"), 100, 0, this.steps, 'lightbox.shown()');
       	}else if(it) {
        	fadeObject.fade(it, 0, 100, this.steps, 'lightbox.shown()');
        }else{
        	this.shown();
        }
    }
    function close() {
    	if (flashobj = swfobject.getObjectById('lb_player')) swfobject.removeSWF("lb_player");
        loseBackground(85);
        fadeObject.fade(ob("lightbox"), 100, 0, 10, 'lightbox.gone()');
    }
    function gone() {
        removeTheNode("lightbox");
    }
    function bubble(txt) {
        var s = "<img src='/images/blank.gif' style='width:inherit;height:inherit' />";
        return "<div style='position:relative'><table id='lbbubble' cellpadding=0 cellspacing=0><tr><td class='bubbleleft'>" + s + "</td><td class='bubblecore'><div id='bubbletext'>" + txt + "</div><div id='bubblepoint'></div></td><td class='bubbleright'>" + s + "</td></tr></table>";
    }
    function showBubble(n, e) {
        var bub = ob('lbbubble');
        bub.style.display = 'block';
        var t = this.allImages[n].title;
        if (this.bubbleIndex != n) {
            ob('bubbletext').innerHTML = (t == '') ? "&nbsp;" : this.allImages[n].title;
            this.bubbleIndex = n;
        }
        var loc = eventLocation(e);
        var tl = gTL(ob('lightboxbuttons').parentNode);
        var lbtl = gTL(ob('lightbox'));
        var l = -Math.round(bub.offsetWidth / 2) + loc[0] - tl[1] - lbtl[1];
        bub.style.left = l + "px";
    }
    function hideBubble() {
        ob('lbbubble').style.display = 'none';
    }
}

