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 e(d){return document.getElementById(d)}

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+=5}
    overlaydiv=overlay;
    initOverlay(overlay);
    showDiv(overlay);
    fade(div,step,fadeTo);
  }
}

function hideLightBox(div,overlay,step,fadeTo){
    if(navigator.appName=="Microsoft Internet Explorer"){step+=10;}
    hideDiv(div);
    hideDiv(overlay);
		document.body.style.overflow="auto";
    //fade(div,step);
}

function fade(div,step,fadeTo){
    if(faded[0]==div){
        if(faded[1]=="in"){
            fadeOut(div,step);
        }else{
            showDiv(div);
            centerH(div);
            fadeIn(div,step,fadeTo);
        }
    }else{
        showDiv(div);
        centerH(div);
        fadeIn(div,step,fadeTo);
    }
}

function fadeOut(div,step,fadeTo){
    document.body.style.overflow="auto";
    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.media = 'screen';
    headID.appendChild(cssNode);
    if(BrowserDetect.browser=="Explorer"){
      cssNode.href = 'Themes/_DefaultTemplates/_css/lbie.css';
    }else{
      cssNode.href = 'Themes/_DefaultTemplates/_css/lbff.css';
    }
}

function showDiv(div,option){
    document.getElementById(div).style.display="block";
    document.getElementById(div).style.visibility="visible";
		if(option){
				//this option allows the div to be able to fade/hide again and again: hideDiv('lb',1)
        faded[0]=div;
        faded[1]="in";
        if(overlaydiv!=null){showDiv(overlaydiv);}
				alpha=100;
				//document.body.style.overflowY="hidden";
		}
}

function hideDiv(div,option){
    document.getElementById(div).style.display="none";
    document.getElementById(div).style.visibility="hidden";
		if(option){
				//this option allows the div to be able to fade/hide again and again: hideDiv('lb',1)
        faded[0]=div;
        faded[1]="out";
        hideDiv(div);
        if(overlaydiv!=null){hideDiv(overlaydiv);}
				alpha=0;
				//document.body.style.overflowY="scroll";
		}
}

function initOverlay(div){
    var addW=30;
    var d=document.getElementById(div);
    if(navigator.appName=="Microsoft Internet Explorer"){
        d.style.width=document.body.scrollWidth;
        if(navigator.appVersion.indexOf('MSIE')>0){
          d.style.height=document.documentElement.scrollHeight;
          d.style.width=document.documentElement.scrollWidth+addW;
        }
    }else if(navigator.userAgent.indexOf("Chrome")>0){
        d.style.width=window.innerWidth+addW+"px";
        d.style.height=document.body.scrollHeight+"px";
    }else{
        //alert(document.documentElement.scrollHeight+"px");
        //d.style.width=document.documentElement.scrollWidth+"px";
        //d.style.height=document.documentElement.scrollHeight+"px";
    }
}
function centerH(d){
  var divH=e(d).offsetHeight;
  e(d).style.marginTop="-"+(divH/2)+"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";
    }
}