
var badIE = 0;
/*@cc_on
badIE = (@_jscript_version < 7);
@*/

/*  Lightbox
* This is a script for creating modal dialog windows (like the ones your operating
* system uses)
*
* edited version - remove all dependencies on protoype for performance reasons
*/

function Lightbox(element) {
  if(typeof(element)!='object') {
    this.element = document.getElementById(element);
  } else { this.element = element; }
  this.element.lightbox = this;
  //lightboxes_hide();
  //lightboxes.push(this.element);
  this.options = {
    lightboxClassName : 'lightbox',
    closeOnOverlayClick : false,
    externalControl : false
  };

}

function deb(x) {
  var el = document.createElement('div');
  el.innerHTML = x;
  el.style.height = '20px';
  document.body.appendChild(el);
}

Lightbox.prototype.show = function(){
  if(badIE) {
    // do evil IE 6 hacks
    this.getScroll();
    this.prepareIE('100%', 'hidden');
    this.setScroll(0,0);
  } else {
    this.element.style.position = 'fixed';
  }
  this.element.className = this.options.lightboxClassName;
  //show the overlay
  var overlay = document.createElement('div');
  overlay.setAttribute('id','overlay');
/*  if(document.all) {
    overlay.style.marginRight = '-16px';
    if(document.body) {
      document.body.style.marginRight = '16px';
    } else {
      document.documentElement.style.marginRight = '16px';
    }
  }*/
  document.body.insertBefore(overlay,document.body.firstChild);
//  this.element.parentNode.insertBefore(overlay,this.element);
  var closer = document.createElement('img');
  closer.setAttribute('id','lightbox_closer');
  closer.src = '/images/popup-close.gif';
  closer.alt = 'Close';
  closer.title = 'Close this window';
  this.element.insertBefore(closer,this.element.firstChild);
  $(closer).addEvent('click',this.hide.bind(this));
  document.body.className = document.body.className+' no_flash';
  this.center();
  this.element.style.display = 'block';
  return false;
}

/* functions to avoid issues in IE */
Lightbox.prototype.prepareIE = function(height, overflow){
  bod = document.getElementsByTagName('body')[0];
  bod.style.height = height;
  bod.style.overflow = overflow;

  htm = document.getElementsByTagName('html')[0];
  htm.style.height = height;
  htm.style.overflow = overflow;
  if(window.XMLHttpRequest) return; // IE 7 isn't this stupid...
  if(height == '100%') {
    bod.className = 'hide_selects '+bod.className;
  } else {
    bod.className = bod.className.replace(/hide_selects[^ ]*/,'');
  }
}

Lightbox.prototype.getScroll = function() {
  var yScroll;
  if (self.pageYOffset) {
      yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
  }
  this.yPos = yScroll;
}

Lightbox.prototype.setScroll = function(x, y) {
  window.scrollTo(x, y);
}

Lightbox.prototype.hide = function() {
  if(badIE) {
    // do evil IE hacks
    this.setScroll(0,this.yPos);
    this.prepareIE("auto", "auto");
    if(document.body) {
      document.body.style.marginRight = '0px';
    } else {
      document.documentElement.style.marginRight = '0px';
    }
  }
  this.element.className = '';
  this.element.style.display = 'none';
  //remove the overlay element from the DOM completely
  var el = document.getElementById('overlay');
  el.parentNode.removeChild(el);
  el = document.getElementById('lightbox_closer');
  el.onclick = null;
  el.parentNode.removeChild(el)
  document.body.className = document.body.className.replace('no_flash','');
  return false;
}

Lightbox.prototype.center = function() {
  /* with 'reasonable' options' the box is 400x400px..
    this function takes ~0.1ms, doing it dynamically takes ~60ms */
  var el_width = 400;
  var el_height = 400;
  this.element.style.zIndex   = 99;
  /* get window size... */
  if ( typeof( window.innerWidth ) == 'number' ){
    my_width  = window.innerWidth;
    my_height = window.innerHeight;
  }else if ( document.documentElement &&
       ( document.documentElement.clientWidth ||
         document.documentElement.clientHeight ) ){
    my_width  = document.documentElement.clientWidth;
    my_height = document.documentElement.clientHeight;
  }
  else if ( document.body &&
      ( document.body.clientWidth || document.body.clientHeight ) ){
    my_width  = document.body.clientWidth;
    my_height = document.body.clientHeight;
  }
  var setX = ( my_width  - el_width  ) / 2;
  var setY = ( my_height - el_height ) / 2;
  setX = ( setX < 0 ) ? 0 : setX;
  setY = ( setY < 0 ) ? 0 : setY;
  var scrollY = 0;
  if(badIE) {
    if ( document.documentElement && document.documentElement.scrollTop ){
      scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
      scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
      scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
      scrollY = window.scrollY;
    }
    setY+= scrollY;
  }
  this.element.style.left = setX + "px";
  this.element.style.top  = setY + "px";
}

Lightbox.prototype.centerSlow = function() {
  var my_width  = 0;
  var my_height = 0;
  if ( typeof( window.innerWidth ) == 'number' ){
    my_width  = window.innerWidth;
    my_height = window.innerHeight;
  }else if ( document.documentElement &&
       ( document.documentElement.clientWidth ||
         document.documentElement.clientHeight ) ){
    my_width  = document.documentElement.clientWidth;
    my_height = document.documentElement.clientHeight;
  }
  else if ( document.body &&
      ( document.body.clientWidth || document.body.clientHeight ) ){
    my_width  = document.body.clientWidth;
    my_height = document.body.clientHeight;
  }
//  this.element.style.position = 'absolute';
  this.element.style.zIndex   = 99;
  var scrollY = 0;

  if ( document.documentElement && document.documentElement.scrollTop ){
    scrollY = document.documentElement.scrollTop;
  }else if ( document.body && document.body.scrollTop ){
    scrollY = document.body.scrollTop;
  }else if ( window.pageYOffset ){
    scrollY = window.pageYOffset;
  }else if ( window.scrollY ){
    scrollY = window.scrollY;
  }

  var els = this.element.style;
  if (els.display != 'none') {
    var el_width =  element.offsetWidth;
    var el_height = element.offsetHeight;
  } else {
    var originalVisibility = els.visibility;
    var originalPosition = els.position;
    els.visibility = 'hidden';
    els.position = 'absolute';
    els.display = '';
    var originalWidth = this.element.clientWidth;
    var originalHeight = this.element.clientHeight;
    els.display = 'none';
    els.position = originalPosition;
    els.visibility = originalVisibility;
    el_width =  originalWidth;
    el_height = originalHeight;
  }
  var setX = ( my_width  - el_width  ) / 2;
  var setY = ( my_height - el_height ) / 2;
  if(badIE) {
    setY +=  scrollY;
  }

  setX = ( setX < 0 ) ? 0 : setX;
  setY = ( setY < 0 ) ? 0 : setY;

  this.element.style.left = setX + "px";
  this.element.style.top  = setY + "px";
}

