/*
 Script: lightbox.js
 
 Author:
  Caleb Kniffen, <http://kniffenwebdesign.com>
 
 Version:
  1.4

 Required Files:
  - prototype.kLib.js
  - <kLib.js>
 
 Class: LightBox
  A nifty lighbox class.
*/
LightBox = Class.create();
LightBox.prototype = {
 /*
 Function: initialize
  Initializes the LightBox object.
  
  Arguments:
   header - <header>
   content - <content>
   options - <options>
 */
 initialize:function(header, content, options){
  this.headerText = header
  this.content = content
  this._setOptions(options);
  if(this.options.reBuild == true) this.show();
  else this._makeInterface();
 },
 classes:{
  backElem:{ filter:'alpha(opacity=70)',MozOpacity:'0.70',position:(isIE)?'absolute':'fixed',visibility:'hidden',top:'0px',bottom:'0px',right:'0px',left:'0px',backgroundColor:'#333', zIndex:'1'},
  frontElem:{ position:(isIE)?'absolute':'fixed',zIndex:'2', visibility:'hidden', height:'220px', width:'200px'  },
  iframe:{position: 'absolute', width:'100%', height:'100%', left: '0px', top: '0px', zIndex: '-1', backgroundColor:'transparent' }
 },
 /*
  Function: _makeInterface
   Creates the LightBox's HTML interface.
 */
 _makeInterface:function(){
  $S(document.body).height = '100%';
  elems = new Object;
  elems.back = makeAppendElem('div', document.body, this.classes.backElem);
  iframe = makeAppendElem('iframe',elems.back, this.classes.iframe);
  iframe.frameBorder = '0';
  iframe.src ='about:blank';
  iframe.scrolling = 'no';
  
  elems.front = document.createElement('div');
  Element.setStyle(elems.front, this.classes.frontElem);
  elems.header = makeAppendElem('div', elems.front, this.options.headerElem);
  $I(elems.header, this.headerText);
  elems.content = makeAppendElem('div', elems.front);
  (typeof(this.content)!='string') ? elems.content.appendChild(this.content) : $I(elems.content, this.content);

  elems.buttonContainer = makeAppendElem('div', elems.front, {textAlign:'center'});
  if(this.options.okButton){ elems.okButton = makeButton('Ok',elems.buttonContainer);}
  if(this.options.cancelButton) {elems.cancelButton = makeButton('Cancel',elems.buttonContainer);}
  if(!elems.okButton && !elems.cancelButton){$S(elems.buttonContainer).display='none';}
  document.body.appendChild(elems.front);
  this.elems = elems;
  
  if(isIE){
   this.setElemsIE();
   window.onresize = window.onscroll = this.setElemsIE.bindAsEventListener(this);
  }
  else{
   this.setElemsNetscape();
   window.onresize = this.setElemsNetscape.bindAsEventListener(this);
  }
  this._bindEvents();
 },
 /*
  Event: okButtonClick
   Called when okButton is clicked.
 */
 okButtonClick:function(){
  this.options.onOk();
  this.hide();
 },
 /*
  Event: cancelButtonClick
   Called when cancelButton is clicked.
 */
 cancelButtonClick:function(){
  this.options.onCancel();
  this.hide();
 },
 /*
  Function: setElemsIE
   Centers LightBox's elements, if browser is IE based.
 */
 
 setElems:function(){
   if(isIE){
   this.setElemsIE();
   window.onresize = window.onscroll = this.setElemsIE.bindAsEventListener(this);
  }
  else{
   this.setElemsNetscape();
   window.onresize = this.setElemsNetscape.bindAsEventListener(this);
  }
 },
 setElemsIE:function(){
   offsetLeft = (document.body.scrollLeft)? document.body.scrollLeft : document.documentElement.scrollLeft;
  offsetTop = (document.body.scrollTop)? document.body.scrollTop : document.documentElement.scrollTop;
  $S(this.elems.back).left = '0px';
  $S(this.elems.back).top = '0px';
  $S(this.elems.back).height = (document.body.clientHeight+offsetTop)+'px';
  $S(this.elems.back).width = (document.body.clientWidth)+'px';


  $S(this.elems.front).top = ((document.body.clientHeight - this.elems.front.offsetHeight )/2+offsetTop )+ 'px';
  $S(this.elems.front).left = ((document.body.clientWidth - this.elems.front.offsetWidth)/2 +offsetLeft )+'px';
 },
 /*
  Function: setElemsNetscape
   Centers LightBox's elements, if browser is Netscape based.
 */
 setElemsNetscape:function(){ 
  $S(this.elems.front).top = ((document.documentElement.clientHeight - this.elems.front.offsetHeight)/2)+ 'px';
  $S(this.elems.front).left = ((document.documentElement.clientWidth - this.elems.front.offsetWidth)/2)+'px';
 },
 /*
  Function: show
   Makes LightBox visible.
 */
 show:function(){
  if(this.options.reBuild == true){ this._makeInterface() };
  $S(this.elems.back).visibility = 'visible';
  $S(this.elems.front).visibility = 'visible';
 },
 /*
  Function: hide
   Makes LightBox disappear.
 */
 hide:function(){
  if(this.options.reBuild == true){ 
   Element.remove(this.elems.back);
   Element.remove(this.elems.front);
  }
  else{
   $S(this.elems.back).visibility = 'hidden';
   $S(this.elems.front).visibility = 'hidden';
  }
  window.onresize =window.scroll = null;
 },
 /*
  Function: setOptions
   Sets the LightBox's options.
  
  Arguments:
   options - Object, An options object.
 */
 _setOptions:function(options){
  this.options = {
   frontElem: {},
   headerElem: {},
   okButton:true,
   cancelButton:false,
   onOk:function(){},
   onCancel:function(){},
   buttons:null,
   reBuild:true
  };
  Object.extend(this.options, options || {});
  Object.extend(this.classes.frontElem, this.options.frontElem || {})
 },
 /*
  Function: _bindEvents
   Binds events to elements.
 */
 _bindEvents:function(){
  if(this.options.okButton) this.elems.okButton.onclick = this.okButtonClick.bindAsEventListener(this);
  if(this.options.cancelButton) this.cancelButton.onclick = this.cancelButtonClick.bindAsEventListener(this);
  if(this.options.buttons!=null){
   buttons = this.options.buttons;
   for(x=0;x<buttons.length;x++) $(buttons[x]).onclick = this.show.bindAsEventListener(this);
  }
 }
 /*
  Var: content
   string|HTML ELEMENT, The LightBox's content.
   
  Var: header
   string, The LightBox's header.
   
  Var: options
   object, An options object.
  
  Var: classes
   object, contains style objects for Lightbox's elements.
  
  Var: elems
   object, Contains LightBox's internal elements.
  
  Elem: back
   <div>, Handles opacity and disables clicking underneath elements.
  
  Elem: front
   <div>, The lightBox's core element.
  
  Elem: header
   <div>, Contains LightBox's header.
  
  Elem: content
   <div>, Contains LightBox's content.
  
  Elem: buttonContainer
   <div>, Contains okButton and/or cancelButton, otherwise it is hidden.
  
  Elem: okButton
   <button>
  
  Elem: cancelButton  
   <button>
   
  Option: frontElem
   object, Style object for the front element.
  
  Option: headerElem
   object, Style object for the header element.
  
  Option: okButton
   boolean, Whether or not there is an okButton, defaults to true.
  
  Option: cancelButton
   Whether or not there is a cancelButton, defaults to false.
  
  Option: onOk
   function, Called when okButton is clicked.
  
  Option: onCancel
   function, Called when cancelButton is clicked.
  
  Option: buttons
   object, The HTML elements that when clicked cause the LightBox to be created.
  
  Option: reBuild
   boolean, Whether or not to recreate the light box everytime it is shown.
 */
}
