/* Begin: TragicMedia.com - LightBox 
/* created 4/08 by Rich Rudzinski
/*------------------------------------------*/
/*
<script type="text/javascript">
    var lb = new TragicLightbox();  //automatically finds all links with a class of lightBox. Image tags must be wrapped in <a> with the href of the full size image, non-image tags must have a rel of the container id of the ajaxed page.
</script>
*/

function TragicLightbox() {

    var obj = this;

    this.links = TM.getElementsByClassName('lightbox', 'a');

    for(i=0; i<this.links.length; i++) {

        this.links[i].onclick = function() {

            obj.el = this;
            
            if(!obj.overlay)
            {
                obj.createLightbox();
                obj.makeElements();
            }           

            obj.elNum = i;

            obj.manageElements();

            if(obj.overlay.style.display == 'none') obj.overlay.style.display = 'block';

            return false;

        }

    }
    
window.onresize = TM.bindFunction(this, this.resizeLightbox);

}

function show_notice(skinItLB,notice){
    obj=skinItLB;
    obj.el = skinItLB;
    if(!obj.overlay)
    {
        obj.el.createLightbox();
        obj.el.makeElements();
    }
    obj.el.href="/show_notice.php?notice="+notice+"&";
    obj.manageElements();
    if(obj.overlay.style.display == 'none') obj.overlay.style.display = 'block';
    window.onresize = TM.bindFunction(obj, obj.resizeLightbox);
    }

function show_url(skinItLB,url){
    obj=skinItLB;
    obj.el = skinItLB;
    if(!obj.overlay)
    {
        obj.el.createLightbox();
        obj.el.makeElements();
    }
    obj.el.href=url;
    obj.manageElements();
    if(obj.overlay.style.display == 'none') obj.overlay.style.display = 'block';
    window.onresize = TM.bindFunction(obj, obj.resizeLightbox);
    }

/* create overlay container */
TragicLightbox.prototype.createLightbox = function() {
    if(navigator.appVersion.match('Macintosh') && navigator.appCodeName.match('Mozilla')) {
        this.flash = (TM.$('cheerFlash')) ? TM.$('cheerFlash') : null;
    }
    if(!TM.$('overlay')) {
        this.overlay = document.createElement('div');
        this.overlay.id = 'overlay';
        this.overlay.onclick = TM.bindFunction(this, this.closeLightbox);  //this.overlay.onclick = function() { if(this.firstChild.getAttribute('rel') != 'noClick') {return this.closeLightBox();} }
        if(!window.innerWidth) {
            this.overlay.style.width = document.body.clientWidth + 'px';
           // this.overlay.style.minWidth = 1000 + 'px';
            this.overlay.style.height = document.body.clientHeight + 'px';
            if(navigator.appVersion.match('MSIE 6.0')) {
                this.overlay.style.height = document.body.clientHeight + 27 + 'px';
                //document.getElementsByTagName('select').item(0).style.display="none";
            }
        } else {
            this.scrollbarWidth = window.innerWidth - document.body.offsetWidth;
            this.overlay.style.width = $(document).width() + 'px';
            this.overlay.style.minWidth = 1000 + 'px';
            this.overlay.style.height = $(document).height() + 'px';
        }
        this.overlay.style.display = 'none';
        if(document.all) {
            var tempCont = document.body.all[0];
            document.body.insertBefore(this.overlay, tempCont);
        } else {
            document.body.insertBefore(this.overlay, document.body.firstChild);
        }
    }
}
/* reevaluate height/width on resize */
TragicLightbox.prototype.resizeLightbox = function() {
    if (!this.overlay)
    {
        return false;
    }
    if(!window.innerWidth) {
        this.overlay.style.width = document.body.clientWidth + 'px';
        this.overlay.style.height = document.body.clientHeight + 'px';
        if(navigator.appVersion.match('MSIE 6.0')) {
            this.overlay.style.height = document.body.clientHeight + 27 + 'px';
            //document.getElementsByTagName('select').item(0).style.display="none";
        }
    }
    else {
        this.scrollbarWidth = window.innerWidth - document.body.offsetWidth;
        this.overlay.style.width = $(document).width() + 'px';
        this.overlay.style.height = $(document).height() + 'px';
    }
    if(this.overlay.style.display != 'none') this.manageElements();
}
/* creates the lightbox elements, and handles their display */
TragicLightbox.prototype.makeElements = function() {
    var obj = this;
    if(!this.LBCont) {
        this.LBCont = document.createElement('div');
        this.LBCont.id = 'LBContainer';
        this.LBCont.align = 'center';
        TM.insertAfter(this.LBCont, this.overlay);
        this.LBCont.style.visibility = 'hidden';
    } 
    
    //create loading image
    if(!this.loading) {
        this.loading = document.createElement('img');
        this.loading.id = 'loading';
        this.loading.src = '/images/backgrounds/loading.gif';
        this.loading.style.top = this.overlay.style.height.replace('px', '')/2 - 30 + 'px'; //30 is half the height/width of the loading image
        this.loading.style.left = this.overlay.style.width.replace('px', '')/2 - 30 + 'px';
        this.overlay.appendChild(this.loading);
    } else {
        this.loading.style.visibility = 'visible';
        if(navigator.appVersion.match('MSIE 6.0') && document.getElementsByTagName('select').length) {
            document.getElementsByTagName('select').item(0).style.display="none";
        }
    }
    this.makeButtons();
    this.imgTag = new Image();
    this.imgTag.setAttribute('id', 'lightboxImage');
    this.LBCont.appendChild(this.imgTag);
    //initialize opacity variables
    this.LBCont.endOpacity = 1;
    this.LBCont.style.opacity = 0;
    this.LBCont.style.filter = 'alpha(opacity='+0+')';
}
//handles the displaying of the image/ajax
TragicLightbox.prototype.manageElements = function() {
    if (document.getElementById("customizer"))
    {
    document.getElementById("customizer").style.visibility="hidden";
    }
    if(navigator.appVersion.match('MSIE 6.0')) {
        if (document.getElementsByTagName('select').item(0)) {
            document.getElementsByTagName('select').item(0).style.display="none";
        }
        if (document.getElementsByTagName('select').item(1)) {
            document.getElementsByTagName('select').item(1).style.display="none";
        }
        if (document.getElementsByTagName('select').item(2)) {
            document.getElementsByTagName('select').item(2).style.display="none";
        }
    }
    if(this.flash) this.flash.style.display = 'none';
    this.loading.style.visibility = 'visible';
    this.LBCont.style.opacity = 0;
    this.LBCont.style.filter = 'alpha(opacity='+0+')';
    var executeAjax = new Ajax(this.el.href, 'LBContainer', TM.bindFunction(this, this.processData));
    executeAjax.update();
}
//process the Ajax
TragicLightbox.prototype.processData = function(id,responseText,status) {
   if(status == 200)
    {
        TM.$(id).innerHTML = responseText;
        exeJavascript(responseText);
        this.ajaxCont = TM.$(TM.$(id).id);
        this.overlay.style.position = 'absolute';
        if(window.pageYOffset) {
            if(this.ajaxCont.id.match('pincard')) 
                this.LBCont.style.top = 76 + 'px';
            else    
                this.LBCont.style.top = window.pageYOffset + 100 + 'px';
        } else {
            if(this.ajaxCont.id.match('pincard') || true) 
                this.LBCont.style.top = 76 + 'px';
            else    
                this.LBCont.style.top = document.documentElement.scrollTop + 100 + 'px';        
        }
        var adjustPos;
        this.LBCont.style.opacity = 0;
        this.LBCont.style.filter = 'alpha(opacity='+0+')';
        this.LBCont.style.height = this.ajaxCont.offsetHeight + 'px';
        this.LBCont.style.width = this.ajaxCont.offsetWidth + 'px';
        if (parseInt(this.overlay.style.width)<1000)
        	this.LBCont.style.left = (1000 - this.ajaxCont.offsetWidth)/2 + 'px';
        else
        	this.LBCont.style.left = (parseInt(this.overlay.style.width) - this.ajaxCont.offsetWidth)/2 + 'px';
        if(parseInt(this.LBCont.style.top) + parseInt(this.LBCont.style.height) + 50 > parseInt(this.overlay.style.height)) 
        this.LBCont.style.top = parseInt(this.LBCont.style.top) - ((parseInt(this.LBCont.style.height) + parseInt(this.LBCont.style.top) + 50) - parseInt(this.overlay.style.height)) + 'px';
        this.loading.style.visibility = 'hidden';
        this.LBCont.style.visibility = 'visible';
        TM.animateOpacity(this.LBCont);
        //create close button
        this.closeBtn = document.createElement('a'); // create close button
        this.closeBtn.setAttribute('id', 'closeBtn');
        this.closeBtn.setAttribute('href', '#');
        this.closeBtn.innerHTML = 'CLOSE';
        this.closeBtn.title = 'CLOSE';
        //append close button
        this.LBCont.appendChild(this.closeBtn);
        this.closeBtnWidth = TM.getStyle(this.closeBtn, 'width');
        this.closeBtnHeight = TM.getStyle(this.closeBtn, 'height');
        //manage click event
        this.closeBtn.onclick = TM.bindFunction(this, this.closeLightbox);
        TM.resetFields();
        this.checkLinks();
    }
}
/* functions makes the close, next, previous buttons */
TragicLightbox.prototype.makeButtons = function() {
    var obj = this;
    
    //create close button
    this.closeBtn = document.createElement('a'); // create close button
    this.closeBtn.setAttribute('id', 'closeBtn');
    this.closeBtn.setAttribute('href', '#');
    this.closeBtn.innerHTML = 'CLOSE';
    this.closeBtn.title = 'CLOSE';
    //append close button
    this.LBCont.appendChild(this.closeBtn);
    this.closeBtnWidth = TM.getStyle(this.closeBtn, 'width');
    this.closeBtnHeight = TM.getStyle(this.closeBtn, 'height');
    //manage click event
    this.closeBtn.onclick = TM.bindFunction(this, this.closeLightbox);
}
/* check links */
TragicLightbox.prototype.checkLinks = function() {
    var obj = this;
    var links = TM.getElementsByClassName('lightbox', '*', this.ajaxCont);
    for(i=0; i<links.length; i++) {
        if(links[i].className.match('lightbox')) {
            links[i].onclick = function() {
                obj.el = this;
                obj.elNum = i;        
                obj.manageElements();
                if(obj.overlay.style.display == 'none') obj.overlay.style.display = 'block';
                return false;
            }
        }
    }
}
/* removes elements inside overlay container */
TragicLightbox.prototype.closeLightbox = function() {
	if (this.ajaxCont.id == 'licensingLightbox' && $(".gray_out").size() == 0)
	{
			location.reload(true);
			exit;
	}
    if (document.getElementById("customizer"))
    {
    document.getElementById("customizer").style.visibility="visible";
    }
    var video = document.getElementById('flashcontent');
    if (video)
    {
    	video.parentNode.removeChild(video);
    }
    this.overlay.style.display = 'none';
    this.LBCont.style.visibility = 'hidden';
    this.loading.style.visibility = 'hidden';
    if(this.lightboxCount) this.lightboxCount = '';
    
    if(this.flash) this.flash.style.display = 'block';
    if(navigator.appVersion.match('MSIE 6.0')) {
        if (document.getElementsByTagName('select').item(0)) {
            document.getElementsByTagName('select').item(0).style.display="inline";
        }
        if (document.getElementsByTagName('select').item(1)) {
            document.getElementsByTagName('select').item(1).style.display="inline";
        }
        if (document.getElementsByTagName('select').item(2)) {
            document.getElementsByTagName('select').item(2).style.display="inline";
        }
    }
    return false;
}



    
