/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

var lightbox = Class.create();
var lightbox_ready = false;
var lightbox_height = 0;

lightbox.prototype = {
 yPos : 0,
 xPos : 0,
 inner: "",

 initialize: function(ctrl, url) {
  if (!lightbox_ready) addLightboxMarkup();

  this.content = url || ctrl.href;
  if (ctrl)
  {
    Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
    ctrl.onclick = refalse;
  }
 },

 // Turn everything on - mainly the IE fixes
 activate: function(){
  this.displayLightbox("block");
 },

 // Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
 prepareIE: function(height, overflow){
  bod = document.getElementsByTagName('body')[0];
  bod.style.height = height;
  bod.style.overflow = overflow;
  if (!IEBrowserOld) bod.style.overflowY = (overflow=="hidden"?"scroll":"");


  htm = document.getElementsByTagName('html')[0];
  htm.style.height = height;
  htm.style.overflow = overflow;
 },

 // In IE, select elements hover on top of the lightbox
 hideSelects: function(visibility){
  selects = document.getElementsByTagName('select');
  for(var i = 0; i < selects.length; i++) {
   selects[i].style.visibility = visibility;
  }
 },

 // Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
 getScroll: function(){
  if (self.pageYOffset) {
   this.yPos = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){
   this.yPos = document.documentElement.scrollTop;
  } else if (document.body) {
   this.yPos = document.body.scrollTop;
  }
 },

 setScroll: function(x, y){
  window.scrollTo(x, y);
 },

 displayLightbox: function(display){
    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;
	}

   
  if(display != 'none'){
   this.loadInfo();
  }
  else
  {
    $('overlay').style.display = display;
    $('lightbox').style.display = display;
  }

  $('overlay').style.height = pageHeight+' px';

 },

 // Begin Ajax request based off of the href of the clicked linked
 loadInfo: function() {
   var myAjax = new Ajax.Request(
         this.content,
         {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
   );
 },

 // Display Ajax response
 processInfo: function(response){
  if (response.responseText=="") return false;
  if (IEBrowser){
   this.getScroll();
   this.prepareIE('100%', 'hidden');
   this.setScroll(0,0);
   this.hideSelects('hidden');
  }
  $('overlay').style.display = "block";
  $('lightbox').style.display = "block";
  this.inner=response.responseText;

  /*
  if ($('lbContent'))
  {
    //
    var myEffect = new fx.Height("lbContent" , {duration: 150, onComplete:
      this.processInfoFX.bindAsEventListener(this) }
     );
     myEffect.toggle();
  }
  else
  {
    this.processInfoFX();
  }
  */

  this.processInfoFX();
 },

 processInfoFX: function()
 {
  if ($('lbContent')) Element.remove($('lbContent'));
  var info = "<div id='lbContent'>" + this.inner + "</div>";
  new Insertion.Before($('lbLoadMessage'), info);

  $('lightbox').className = "done";
  $('lightbox').style.height=$("lbContent").offsetHeight+"px";

  var clse=$("lbClose");

  if (clse)
  {
    Event.observe(clse, 'click', this.deactivate.bindAsEventListener(this), false);
    clse.onclick = refalse;

    Event.observe(document, "keypress", this.deactivateESC.bindAsEventListener(this), false);
  }

  var clse=document.getElementsByClassName("laClose");
  if (clse) for (var i in clse)
  {
    Event.observe(clse[i], 'click', this.deactivate.bindAsEventListener(this), false);
    clse[i].onclick = refalse;
  }

  this.actions();
 },

 // Search through new links within the lightbox, and attach click event
 actions: function(){
  lbActions = document.getElementsByClassName('lbAction');

  for(var i = 0; i < lbActions.length; i++) {
   eval(lbActions[i].value);
   //Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
   //lbActions[i].onclick = function(){return false;};
  }

 },

 // Example of creating your own functionality once lightbox is initiated
 insert: function(e){
    link = Event.element(e).parentNode;
    Element.remove($('lbContent'));

    var myAjax = new Ajax.Request(
     link.href,
     {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
    );

 },

 // Example of creating your own functionality once lightbox is initiated
 deactivate: function(){
  var clse=$("lbClose");

  if (clse)
  {
    Event.stopObserving(clse, 'click', this.deactivate.bindAsEventListener(this), false);
    Event.stopObserving(document, 'keypress', this.deactivateESC.bindAsEventListener(this), false);
  }

  var clse=document.getElementsByClassName("laClose");
  if (clse) for (var i in clse)
  {
    Event.stopObserving(clse[i], 'click', this.deactivate.bindAsEventListener(this), false);
  }

  if ($('lbContent')) Element.remove($('lbContent'));

  if (IEBrowser){
   this.setScroll(0,this.yPos);
   this.prepareIE("", "");
   this.hideSelects("visible");
  }

  this.displayLightbox("none");
 },

 deactivateESC: function(e){
   if (e.keyCode==Event.KEY_ESC) this.deactivate();
 }

}

/*-----------------------------------------------------------------------------------------------*/

// Event.observe(window, 'load', initialize, false);

// Onload, make all links that need to trigger a lightbox active
function initialize(){
 addLightboxMarkup();
 lbox = document.getElementsByClassName('lbOn');
 for(var i = 0; i < lbox.length; i++) {
  valid = new lightbox(lbox[i]);
 }
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {
 if (lightbox_ready) return;
 lightbox_ready=true;
 var bod         = document.getElementsByTagName('body')[0];
 var overlay     = document.createElement('div');
 overlay.id  = 'overlay';

 if (IEBrowser)
 {
   overlay.style.backgroundImage="none";
   overlay.style.backgroundColor="#000";
   overlay.style.filter="alpha(opacity=70)";
 }
 else if (MozBrowser)
 {
   overlay.style.backgroundImage="";
   overlay.style.backgroundColor="#000";
   overlay.style.MozOpacity="0.7";
 }
 else if (OperaBrowser && OperaVersion>8)
 {
   overlay.style.backgroundImage="";
   overlay.style.backgroundColor="#000";
   overlay.style.opacity="0.7";
 }

 var lb          = document.createElement('div');
 lb.id       = 'lightbox';
 lb.className= 'loading';
 lb.innerHTML= '<div id="lbLoadMessage"><p>&nbsp;</p></div>';
 bod.appendChild(overlay);
 bod.appendChild(lb);

 if (IEBrowserPNG)
 {
   document.styleSheets[0].addRule(".lb_top", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='image', src='"+JSRoot+"lb_top_line.png');")
   document.styleSheets[0].addRule(".lb_head", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='scale', src='"+JSRoot+"lb_top_body.png');");
   document.styleSheets[0].addRule(".lb_line", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='image', src='"+JSRoot+"lb_line.png');");
   document.styleSheets[0].addRule(".lb_body", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='scale', src='"+JSRoot+"lb_bottom_body.png');");
   document.styleSheets[0].addRule(".lb_bottom", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='image', src='"+JSRoot+"lb_bottom_line.png');");
 }
}