LITBox = Class.create();
LITBox.prototype = {
   initialize: function(mes){
   this.mes = mes;
   this.options = Object.extend({
      width: 600,
      height: 500,
      type: 'window',
      func: null,
      draggable: false,
      resizable: false,
      overlay: true,
      opacity: 1,
      left: false,
      top: false
   }, arguments[1] || {});
   this.setup();
   },
   setup: function(){
      this.rn = ( Math.floor ( Math.random ( ) * 100000000 + 1 ) );
      this.getWindow();
      switch(this.options.type){
         case 'window' : 
		 	this.d4.innerHTML = this.getAjax(this.mes);				 
           	break;
		
		case 'iframe' : 
			this.iframe = document.createElement('iframe');
			this.iframe.src = this.mes
			this.iframe.className = 'LB_iframe';
			this.iframe.frameBorder = "0";
			this.d4.appendChild(this.iframe)
		break
        
		case 'alert' : 
			var al = new Element('div', { 'class': 'alert-content'}).update(this.mes);
			$(this.d4).update(al)
	    break;
        
		case 'confirm' : 
			var msg = new Element('div', { 'class' : 'confirm-content'}).update(this.mes)
			$(this.d4).update(msg)
            this.button_y = document.createElement('input');
            this.button_y.type='button';
            this.button_y.value='Ja';
			this.button_y.className="confirm";
            this.d4.appendChild(this.button_y);
            this.button_y.d= this.d; this.button_y.d2 = this.d2;
            this.button_y.temp = this.options.func;
            this.button_y.onclick=this.remove;
            this.button_n = document.createElement('input');
            this.button_n.type='button';
            this.button_n.value='Nein';
			this.button_n.className="confirm";			
            this.d4.appendChild(this.button_n);
            this.button_n.d= this.d; this.button_n.d2 = this.d2;
            this.button_n.onclick=this.remove;
		break;
      }
      this.display();
   },
   getWindow: function(){
      this.over = null;
      if(this.options.overlay == true){
      this.d = document.createElement('div');
      document.body.appendChild(this.d);
      this.d.className = 'LB_overlay';
      this.d.id = 'LB_overlay_id';    
      this.d.style.display = 'block';
      this.d.onclick=this.remove;
      }
      this.d2 = document.createElement('div');
      document.body.appendChild(this.d2);
      this.d2.className = 'LB_window';
      this.d2.style.height = parseInt(this.options.height) + 'px';

      this.d3 = document.createElement('div');
      this.d2.appendChild(this.d3);
      this.d3.className='LB_closeAjaxWindow';
	  if(this.options.type == 'confirm'){
		$(this.d3).update(new Element('span', { 'class': 'title'}).update("Please confirm:"))  
	  }
	  if(this.options.type == 'alert'){
		$(this.d3).update(new Element('span', { 'class': 'title'}).update("Alert:"))  
	  }
	  
      this.d3.d2 = this.d2;
      this.d3.over = this.over;
      this.d3.options = this.options;
      this.close = document.createElement('a');
      this.d3.appendChild(this.close);
      this.close.d = this.d;
      this.close.d2 = this.d2;
      this.close.onclick=this.remove;
      this.close.href='#';
      this.close.innerHTML='close';
      this.close.id='litebox_close_button';
      this.d4 = document.createElement('div');
      this.d4.className='LB_content';
      this.d4.style.height = parseInt(this.options.height) - 0 + 'px';
      this.d4.style.width = parseInt(this.options.width) + 'px';
      this.d2.appendChild(this.d4);
      this.clear = document.createElement('div');
      this.d2.appendChild(this.clear);
      this.clear.style.clear='both';
      if(this.options.overlay == true){
      this.d.d = this.d;
      this.d.d2 = this.d2;
      }
   },
   display: function(){
      Element.setOpacity(this.d2, 0);
      this.position();
      new Effect.Opacity(this.d2, {from:0,to:this.options.opacity,duration:.5});
   },
   position: function(){
      var de = document.documentElement;
   	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
   	var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
     	if (window.innerHeight && window.scrollMaxY) {	
   		yScroll = window.innerHeight + window.scrollMaxY;
   	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
   		yScroll = document.body.scrollHeight;
   	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
   		yScroll = document.body.offsetHeight;
     	}
      this.d2.style.width = this.options.width + 'px';
      this.d2.style.display = 'block';
      if(!this.options.left || this.options.left < 0){
   	this.d2.style.left = ((w - this.options.width)/2)+"px";
      }else{
      this.d2.style.left=parseInt(this.options.left)+'px';
      }
   	
      var pagesize = this.getPageSize();
      var arrayPageScroll = this.getPageScrollTop();
	//console.log(this.d2.offsetHeight,h)
   	if(this.d2.offsetHeight > h - 10){
         if(!this.options.top || this.options.top < 0){
   			this.d2.style.top = "45px";
         }else{
    	    this.d2.style.top=parseInt(this.options.top)+'px';
         }
   		this.d2.style.height=h-100 + 'px';
   		//this.d4.style.height=h-145 + 'px';
   		this.d4.style.overflow ='auto';
   	} else {   
      if(!this.options.top || this.options.top < 0){
   		this.d2.style.top = (arrayPageScroll[1] + ((pagesize[1]-this.d2.offsetHeight)/2))+"px";
       }else{
         this.d2.style.top=parseInt(this.options.top)+'px';
       }
   	}
   	if(this.d){this.d.style.height =   yScroll +"px";}
   },
   remove: function(){
      if(this.temp) this.temp();
   	new Effect.Opacity(this.d2, {from:1,to:0,duration:.5});
   	if(this.d){new Effect.Opacity(this.d, {from:.6,to:0,duration:.5});
   	Element.remove(this.d);}
      Element.remove(this.d2);
			if($('searchValue')) $('searchValue').focus()
      return false;
   },
   parseQuery: function(query){
      var Params = new Object ();
      if ( ! query ) return Params; // return empty object
      var Pairs = query.split(/[;&]/);
      for ( var i = 0; i < Pairs.length; i++ ) {
         var KeyVal = Pairs[i].split('=');
         if ( ! KeyVal || KeyVal.length != 2 ) continue;
         var key = unescape( KeyVal[0] );
         var val = unescape( KeyVal[1] );
         val = val.replace(/\+/g, ' ');
         Params[key] = val;
      }
      return Params;
   },
   getPageScrollTop: function(){
      var yScrolltop;
      if (self.pageYOffset) {
         yScrolltop = self.pageYOffset;
      } else if (document.documentElement && document.documentElement.scrollTop){    // Explorer 6 Strict
         yScrolltop = document.documentElement.scrollTop;
      } else if (document.body) {// all other Explorers
         yScrolltop = document.body.scrollTop;
      }
      arrayPageScroll = new Array('',yScrolltop) 
      return arrayPageScroll;
   },
   getPageSize: function(){
      var de = document.documentElement;
      var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
      var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
      
      arrayPageSize = new Array(w,h) 
      return arrayPageSize;
   },
   getAjax: function(url){
      var xmlhttp=false;
      /*@cc_on @*/
      /*@if (@_jscript_version >= 5)
      // JScript gives us Conditional compilation, we can cope with old IE versions.
      // and security blocked creation of the objects.
        try {     
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
         try {       
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (E) {
          xmlhttp = false;
         }  
        }   
      @end @*/
      if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp = new XMLHttpRequest();
      if(xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml');
      if(url != ""){
         xmlhttp.open("GET", url, false);
         xmlhttp.send(null);
         return xmlhttp.responseText;
      }
   }

}

