var mobileDevice=false; //this variable is used with mobileCheck.asp to determine whether or the user is on a mobile device
var alpha=null;
var faded=[];
var overlaydiv=null;

//This code preloads all the images so it doesn't look choppy when the lightbox appears.
tlc = new Image();
trc = new Image();
blc = new Image();
brc = new Image();
ls = new Image();
rs = new Image();
ts = new Image();
bs = new Image();
tlc.src = "images/tlc.gif";
trc.src = "images/trc.gif";
blc.src = "images/blc.gif";
brc.src = "images/brc.gif";
ls.src = "images/lbleft-bg.gif";
rs.src = "images/lbright-bg.gif";
ts.src = "images/lbtop-bg.gif";
bs.src = "images/lbbottom-bg.gif";

function initLightBox(){
    //dynamically insert our lb.css file so we don't have to manually add it to each html doc
//    insertCSS();
}

function showLightBox(div,overlay,step,fadeTo){
    if(!mobileDevice){
        if(navigator.appName=="Microsoft Internet Explorer"){step+=10;}
        overlaydiv=overlay;
        initOverlay(overlay);
        showDiv(overlay);
        fade(div,step,fadeTo);
    }
}

function hideLightBox(div,overlay,step,fadeTo){
    if(navigator.appName=="Microsoft Internet Explorer"){step+=10;}
    fade(div,step);
}

function fade(div,step,fadeTo){
    if(faded[0]==div){
        if(faded[1]=="in"){
            fadeOut(div,step);
        }else{
            showDiv(div);
            center(div);
            fadeIn(div,step,fadeTo);
        }
    }else{
        showDiv(div);
        center(div);
        fadeIn(div,step,fadeTo);
    }
}

function fadeOut(div,step,fadeTo){
    //document.body.style.overflow="scroll";
    if(alpha==null){alpha=100;}
    if(step==null){step=1;}
    if(fadeTo==null){fadeTo=0;}
    alpha-=step;
    setOpacity(div,alpha);
    if(alpha>fadeTo){
        setTimeout("fadeOut('"+div+"',"+step+","+fadeTo+")", 0);
    }else{
        faded[0]=div;
        faded[1]="out";
        hideDiv(div);
        if(overlaydiv!=null){hideDiv(overlaydiv);}
    }
}

function fadeIn(div,step,fadeTo){
    //document.body.style.overflow="hidden";
    if(alpha==null){alpha=0;}
    if(step==null){step=1;}
    if(fadeTo==null){fadeTo=100;}
    alpha+=step;
    setOpacity(div,alpha);
    if(alpha<fadeTo){
        setTimeout("fadeIn('"+div+"',"+step+","+fadeTo+")", 0);
    }else{
        faded[0]=div;
        faded[1]="in";
    }
}

function setOpacity(bgDiv,opacity){
    var e = document.getElementById(bgDiv).style;
    e.opacity = (opacity / 100); 
    e.MozOpacity = (opacity / 100); 
    e.KhtmlOpacity = (opacity / 100);
    e.filter = "alpha(opacity=" + opacity + ")"; 
    e.MsFilter = "'alpha(opacity=" + opacity + ")'";
}

function setBGcolor(bgDiv,bgString){
    document.getElementById(bgDiv).style.backgroundColor=bgString;
}

function insertCSS(){
    var headID = document.getElementsByTagName("head")[0];         
    var cssNode = document.createElement('link');
    cssNode.type = 'text/css';
    cssNode.rel = 'stylesheet';
    cssNode.href = 'lb.css';
    cssNode.media = 'screen';
    headID.appendChild(cssNode);
}

function showDiv(div){
    document.getElementById(div).style.display="block";
    document.getElementById(div).style.visibility="visible";
}

function hideDiv(div){
    document.getElementById(div).style.display="none";
    document.getElementById(div).style.visibility="hidden";
}

function initOverlay(div){
    var d=document.getElementById(div);
    if(navigator.appName=="Microsoft Internet Explorer"){
        d.style.width=document.body.scrollWidth;
        d.style.height=document.body.scrollHeight;
        if(navigator.appVersion.indexOf('MSIE 8.0')>0) d.style.height=document.body.scrollHeight+29;
    }else if(navigator.userAgent.indexOf("Chrome")>0){
        d.style.width=window.innerWidth+"px";
        d.style.height=document.body.scrollHeight+"px";
    }else{
        d.style.width=document.documentElement.scrollWidth+"px";
        d.style.height=document.documentElement.scrollHeight+"px";
    }
}

function center(div){
    var d=document.getElementById(div);
    var divW,divH;

    divW=d.offsetWidth;
    divH=d.offsetHeight;
    //alert("divW: "+divW+"\ndivH: "+divH+"\nwindowW: "+document.body.clientWidth+"\nwindowH: "+document.body.clientHeight+"\n\nleft: "+((document.body.clientWidth-parseInt(divW))/2)+"\ntop: "+((document.body.clientHeight-parseInt(divH))/2));
    if(navigator.appName=="Microsoft Internet Explorer"){
        d.style.left=(((document.body.clientWidth)-parseInt(divW))/2)+"px";
        d.style.top=(((document.documentElement.clientHeight)-parseInt(divH))/2)+document.documentElement.scrollTop+"px";
    }else if(navigator.userAgent.indexOf("Chrome")>0){
        d.style.left=((window.innerWidth-parseInt(divW))/2)+"px";
        d.style.top=((window.innerHeight-parseInt(divH))/2)+document.body.scrollTop+"px";
    }else{
        d.style.left=((window.innerWidth-parseInt(divW))/2)+"px";
        d.style.top=((window.innerHeight-parseInt(divH))/2)+document.documentElement.scrollTop+"px";
    }
}