function LightBox(boxWidth, topPos){
    
    this.boxWidth = boxWidth ? boxWidth: 600;
    this.topPos = topPos? topPos : 100;    
}

LightBox.prototype.init = function(options){
    if($("lightb"))
        Element.remove($("lightb"))
    this.lightb  = document.createElement("DIV");
    this.lightb.id = "lightb";
    
    var width = options == null || options.width == null ? this.boxWidth : options.width    
    
    this.lightb.innerHTML = '\
    <div id="lightb_container">\
      <div id="lightb_msg" style="position: absolute; top: 0px; left: 0px;width:' + width + 'px;z-index: 11;">\
        <table cellpadding="0" cellspacing="0">\
          <thead>\
            <tr>\
              <th class="lft crn"></th>\
              <td class="brd"><a class="close" href="#" onclick="lightb.hide();return false;"></a></td>\
              <th class="rgt crn"></th>\
            </tr>\
          </thead>\
          <tbody>\
            <tr>\
              <th class="lft brd"></th>\
              <td id="lightb_td">\
                <div id="lb_msg_cntr">\
                </div>\
              </td>\
              <th class="rgt brd"></th>\
            </tr>\
          </tbody>\
          <tfoot>\
            <tr>\
              <th class="lft crn"></th>\
              <td class="brd"></td>\
              <th class="rgt crn"></th>\
            </tr>\
          </tfoot>\
        </table>\
      </div>\
    </div>';
  
    document.body.appendChild(this.lightb);    
}

LightBox.prototype.load = function(url, options){
    this.init(options);
    var ob = this;
    new Ajax.Updater('lb_msg_cntr', url, {asynchronous:true, evalScripts:true, method:'GET', onComplete:function(request){ob.show();}});    
}


LightBox.prototype.load_image = function(src, options){
    if(!options)
        options = {max_width : 800, max_height : 600}
    this.init();
    var ob = this;
    var img = new Image();
    img.src = src;
    var ob = this;    
    img.onload=function(){
        if(img.width > options.max_width)
            img.width = options.max_width;
        if(img.height > options.max_height)
            img.height = options.max_height;        
        $('lb_msg_cntr').appendChild(img);        
        ob.show(20);
        }
    
    //new Ajax.Updater('lb_msg_cntr', url, {asynchronous:true, evalScripts:true, method:'GET', onComplete:function(request){ob.show();}});    
}

LightBox.prototype.hide = function(){
    if(this.lightb)
        this.lightb.style.display="none";
}

LightBox.prototype.display = function(message, options){
    if(!options)
        options = {type:'error'};
    if(!this.lightb || this.lightb.style.display != 'block'){
      this.init();
      $('lb_msg_cntr').innerHTML = "<span class='" + options.type + "'>" + message + "</span>";        
      this.show();
      new Effect.Pulsate($('lightb_container'));
    }
  else{
      $('lb_msg_cntr').innerHTML += '<br/>' +  "<span class='" + options.type + "'>" + message + "</span>";    
      if(options.shake)
          new Effect.Pulsate($('lightb_container'));
  }
   
}


LightBox.prototype.setWidth = function(width){
    this.boxWidth = width;
}

LightBox.prototype.show = function(extra_width){
    if(!extra_width)
        extra_width = 0;
    this.lightb.style.height = document.body.offsetHeight + 'px';

    var scrollY = this.topPos;
    if(document.documentElement){
        scrollY=document.documentElement.scrollTop + this.topPos; 
    }
    else{
	if(document.body.scrollTop == null)
            scrollY = (window.pageYOffset + this.topPos);
	else 
            scrollY = document.body.scrollTop +this.topPos;
    }
    var w = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
    
    $('lightb_container').style.visibility = "hidden";    
    this.lightb.style.display = 'block';
    this.boxWidth = $('lb_msg_cntr').offsetWidth;
    $('lightb_msg').style.width = (this.boxWidth + extra_width) + 'px';   
    $('lightb_container').style.visibility = "visible";
    $('lightb_container').style.left = parseInt(((w - this.boxWidth))/2) + 'px';
    $('lightb_container').style.top = scrollY + 'px';    
}


function show_alert(message, options){       
    lightb.display(message, options)       
}


function exclaim(message){
    show_alert(message, {type : 'exclamation'})
}

function error_popup(message){
    show_alert(message, {type : 'error'})
}

function load_popup(url, options){
    lightb.load(url, options);
}

function hide_popup(){
    lightb.hide();
}

function load_image(src){
    lightb.load_image(src)
}

    
var lightb = new LightBox();
