var isFF = (navigator.userAgent.indexOf("Firefox") > -1) ? true : false;
var fadeInTrigger;
var lightboxleftoffset = 0;
if (window.js_lightbox_leftoffset) {
   lightboxleftoffset = js_lightbox_leftoffset;
}


function getPageSize() {
  var w = h = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    w = window.innerWidth;
    h = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    w = document.documentElement.clientWidth;
    h = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    w = document.body.clientWidth;
    h = document.body.clientHeight;
  }
  return {width:w, height:h};
}

function getPageSize2(){
    var xScroll, yScroll;
    
    if (window.innerHeight && window.scrollMaxY) {  
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   
    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){  
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}

function getPageScroll(){
    var yScroll = 0;
    var xScroll = 0;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    
    return {x:xScroll,y:yScroll};
}

function hideLightBox() {
   var overlay = document.getElementById('lightboxoverlay');
   var lightbox = document.getElementById('lightbox');
   
   // if onbeforehide is defined and it resolves to false, then abort the hide operation
   if (lightbox.onbeforehide && !eval(lightbox.onbeforehide)) return;

   // hide the lightbox, and tweak it for cursor bugs in FF
   if (isFF) lightbox.style.position = 'absolute';   
   lightbox.style.display = 'none';

   // if onhide is defined, perform any actions defined in the string
   if (lightbox.onhide) eval(lightbox.onhide);

   // hide the overlay layer as well
   window.onresize = '';
   fadeOutOverlay();
      
   document.onkeydown = '';

   //var nodes = docment.getElementsByTagName('td');
   //td.innerHTML.
}

function getKey(e){
    var keycode = e ? e.which : event.keyCode;
    if(keycode == 27) { hideLightBox(); }
}

function listenKey () { document.onkeydown = getKey; }

function lightboxCallback(req) {
   var objLightbox = document.getElementById('lightbox');
   var objOverlay = document.getElementById('lightboxoverlay');
   var arrayPageSize = getPageSize2();
   var pagescroll = getPageScroll();  
   var div = document.createElement('div');
   
   var lbwidth = arrayPageSize[2]-(60+lightboxleftoffset);
   var lbheight = arrayPageSize[3]-60;
   var fheight = lbheight-80;
   
   objLightbox.style.top = '-5000px';
   objLightbox.style.display = '';
 
   while (objLightbox.firstChild)  { objLightbox.removeChild(objLightbox.firstChild); }
   div.innerHTML = req.responseText; //.replace(/##lbwidth##/g,lbwidth).replace(/##lbheight##/g,lbheight).replace(/##fheight##/g,fheight);
   objLightbox.appendChild(div);
   
   document.getElementById('lightbox_containertable').style.width = lbwidth + 'px';
   document.getElementById('lightbox_framediv').style.height = fheight + 'px';
   
   // execute script that was sent along with response
   var runmenow = document.getElementById('runmenow');
   if (runmenow) {
      var script = runmenow.innerHTML.replace(/amp;/g,'');
      var parent = runmenow.parentNode;
      parent.removeChild(runmenow);
      eval(script);
   }
   
   if (req.args.width != 0 && req.args.height != 0) {
      var size = [req.args.width, req.args.height];
   } else {
      var size = [div.offsetWidth, div.offsetHeight];
   }
/*   if (size[0] > arrayPageSize[0]) size[0] = arrayPageSize[0] - 100;
   if (size[1] > arrayPageSize[1]) size[1] = arrayPageSize[1] - 100;
   objLightbox.style.width = size[0];
   objLightbox.style.height = size[1];*/

   var lightboxTop = (isFF ? 0 : pagescroll.y) + ((arrayPageSize[3] - size[1]) / 2); //pagescroll + ((arrayPageSize[3] - size[1]) / 2);
   var lightboxLeft = (isFF ? 0 : pagescroll.x) + ((arrayPageSize[2] - size[0]) / 2);
 
   lightboxLeft += (lightboxleftoffset/2);
   objLightbox.style.top = (lightboxTop < 0)   ? '0px' : lightboxTop +  'px';
   objLightbox.style.left = (lightboxLeft < 0) ? '0px' : lightboxLeft + 'px';
   if (isFF) objLightbox.style.position = 'fixed';

   //objOverlay.style.width = arrayPageSize[2] + 'px';
   //objOverlay.style.height = arrayPageSize[3] + pagescroll + 'px';
   listenKey();
}

function resizeLightbox() {
   var pageWidth,pageHeight, windowWidth, windowHeight;
   var objOverlay = document.getElementById('lightboxoverlay');
   var objLightbox = document.getElementById('lightbox');
   var scroll = getPageScroll();
   var arrayPageSize = getPageSize2();
      
   objOverlay.style.display = '';
      
   if (document.documentElement && document.documentElement.clientHeight) {
      pageWidth = document.documentElement.clientWidth;
      if (document.body.clientWidth != document.documentElement.scrollWidth) pageWidth -= 32; //adjust for scrollbar
      pageHeight = document.documentElement.clientHeight;
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
   } else if (document.body) {
      pageWidth = document.body.clientWidth;
      pageHeight = document.body.clientHeight;
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
   }
   
   if (document.getElementById('lightbox_containertable')) {
      var lbwidth = arrayPageSize[2]-(60+lightboxleftoffset);
      var lbheight = arrayPageSize[3]-60;
      var fheight = lbheight-80;
      document.getElementById('lightbox_containertable').style.width = lbwidth + 'px';
      document.getElementById('lightbox_framediv').style.height = fheight + 'px';
   }
      
   var lwidth = objLightbox.offsetWidth;
   var lheight = objLightbox.offsetHeight;
   
   objOverlay.style.top = scroll.y + 'px';
   objOverlay.style.left = scroll.x + 'px';
   objOverlay.style.width = windowWidth + 'px';
   objOverlay.style.height = windowHeight + 'px';
   objLightbox.style.left = (lightboxleftoffset/2) + (windowWidth/2 - lwidth/2) + 'px';
   objLightbox.style.top = (windowHeight/2 - lheight/2) + (isFF ? 0 : scroll.y) + 'px';
}

function fadeInOverlay() {
   var objOverlay = document.getElementById('lightboxoverlay');
   //var opacity = parseFloat(objOverlay.style.opacity);
   //if (opacity < 0.75) {
   //   opacity += 0.1;
   //   objOverlay.style.opacity = opacity;
   //   objOverlay.style.filter = 'alpha(opacity=' + (opacity*100) + ')';
   //   setTimeout("fadeInOverlay()", 1);
   //} else {
   if (objOverlay.style.opacity) objOverlay.style.opacity = 0.75;
   if (objOverlay.style.filter)  objOverlay.style.filter = 'alpha(opacity=75)';
   fadeInTrigger();
   //}
}
   
function fadeOutOverlay() {
   var objOverlay = document.getElementById('lightboxoverlay');
   //var opacity = parseFloat(objOverlay.style.opacity);
   //if (opacity > 0) {
   //   opacity -= 0.1;
   //   objOverlay.style.opacity = opacity;
   //   objOverlay.style.filter = 'alpha(opacity=' + (opacity*100) + ')';
   //   setTimeout("fadeOutOverlay()", 1);
   //} else {
   if (objOverlay.style.opacity) objOverlay.style.opacity = 0;
   if (objOverlay.style.filter)  objOverlay.style.filter = 'alpha(opacity=0)';
   objOverlay.style.display = 'none';
   document.body.style.overflow = '';
   //}
}


function showLightBox(objLink, width, height) {
   var args = {};
   var objLightbox = document.getElementById('lightbox');
   
   //if (imageDown) imageDown();
   document.body.style.overflow = 'hidden';
   window.onresize = resizeLightbox;
   resizeLightbox();
   
   if (width || height) {
      args.width = width;
      args.height = height;
   } else {
      args.width = 0;
      args.height = 0;
   }
   var hidefn = objLink.getAttribute('onhide');
   var beforehidefn = objLink.getAttribute('onbeforehide');
   var url = objLink.getAttribute('href');
   objLightbox.onhide = hidefn;
   objLightbox.onbeforehide = beforehidefn;
   fadeInTrigger = function() { ajaxget(url, lightboxCallback, args); };
   //ajaxget(url, lightboxCallback, args);
   
   var objOverlay = document.getElementById('lightboxoverlay');
   objOverlay.style.display = '';
   objOverlay.style.opacity = '0';
   objOverlay.style.filter = 'alpha(opacity=0)';
   
   fadeInOverlay();
   return false;
}

