//Specific to Prizebook

LITBox = Class.create();
LITBox.prototype = {
   initialize: function(mes){
	   this.mes = mes;
	   this.options = Object.extend({
	     	pred: null,
	     	potom: null,
			titulek: '',
	      width: 500,
	      height: 400,
	      type: 'window',
	      cssClass: 'mladez',
	      func: null,
	      draggable: true,
	      resizable: false,
	      overlay: true,
	      opacity: 1
	   }, arguments[1] || {});
	   this.setup();
	   
	   this.potom	=	this.options.potom;
   },
   setup: function(){
   	if (this.options.pred) this.options.pred();
   	
      this.rn = ( Math.floor ( Math.random ( ) * 100000000 + 1 ) );
      this.getWindow();
      switch(this.options.type){
//         case 'window' : this.d4.innerHTML = this.getAjax(this.mes);
         case 'window' : Element.update(this.d4,this.getAjax(this.mes));
            break;
         case 'alert' : this.d4.innerHTML = this.mes;
            break;
         case 'confirm' : this.d4.innerHTML = '<p>' + this.mes + '</p>';
            this.button_y = document.createElement('input');
            this.button_y.type='button';
            this.button_y.value='Yes';
            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='No';
            this.d4.appendChild(this.button_n);
            this.button_n.d= this.d; this.button_n.d2 = this.d2;
            this.button_n.onclick=this.remove;
      }
      
      this.display();
   },
   getId: function(){
      return this.d2.getAttribute('id');
   },
   getWindowElement: function(){
   	return $A($$('div#'+this.d2.getAttribute('id') + ' div.okno'))[0];
   },
   getWindow: function(){
      this.over = null;
	
      if(this.options.overlay == true){
	      this.d = document.createElement('div');
	      document.body.appendChild(this.d);
	      this.d.className = 'disabledOver';
	      this.d.onclick=this.remove;
      }
      
      this.d2 = document.createElement('div');
      document.body.appendChild(this.d2);
      this.d2.className = 'oknoRam';
      this.d2.setAttribute('id','LIG'+this.rn);
      this.d2.style.height = parseInt(this.options.height) + 'px';
		this.d2.style.width = parseInt(this.options.width) + 'px';

      this.d3 = document.createElement('div');
      this.d2.appendChild(this.d3);
      this.d3.className='prouzek';
      this.d3.d2 = this.d2;
      this.d3.over = this.over;
      this.d3.options = this.options;
      this.d3.onmouseover=this.getDraggable;
      this.d3.onmouseout=this.dropDraggable;
      this.close = document.createElement('span');
		this.close.className='close';
		this.close.potom = this.options.potom;
      this.text = document.createElement('span');
		this.text.className='text';
      this.d3.appendChild(this.close);
      this.d3.appendChild(this.text);
      this.close.d = this.d;
      this.close.d2 = this.d2;
      this.close.onclick=this.remove;
      this.text.innerHTML=this.options.titulek;
      this.close.innerHTML='zavřít';

      this.d4 = document.createElement('div');
      this.d4.className='okno '+this.options.cssClass;
      this.d4.style.height = parseInt(this.options.height) - 28 + '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;
      }
   },
   getDraggable: function(){
      if(this.options.draggable){
      if(this.resize)this.resize.destroy();
      if(!this.drag || (this.drag && !this.drag.dragging))
      this.drag = new Draggable(this.d2,{});
      }
   },
   dropDraggable: function(){
      if(this.options.draggable){
      if(!this.drag.dragging && this.drag){
      this.drag.destroy();
      }}
   },
   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';
   	this.d2.style.left = ((w - this.options.width)/2)+"px";
   	
      var pagesize = this.getPageSize();
      var arrayPageScroll = this.getPageScrollTop();
   	if(this.d2.offsetHeight > h - 100){
   		this.d2.style.top = "45px";
   		this.d2.style.height=h-100 + 'px';
   		//this.d4.style.height=h-145 + 'px';
   		this.d4.style.overflow ='auto';
   	} else {   
   		this.d2.style.top = (arrayPageScroll[1] + ((pagesize[1]-this.d2.offsetHeight)/2))+"px";
   	}
   	if(this.d){this.d.style.height =   yScroll +"px";}
   },
   remove: function(){
		if(this.potom)	this.potom();
      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);
   	}
   	if (enable()){
   		enable();
   	}
      Element.remove(this.d2);
   },
   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.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
         xmlhttp.send(null);         
         return xmlhttp.responseText;
      }
   }

}


var Resizers = {
  drags: [],
  observers: [],
  
  register: function(draggable) {
    if(this.drags.length == 0) {
      this.eventMouseUp   = this.endDrag.bindAsEventListener(this);
      this.eventMouseMove = this.updateDrag.bindAsEventListener(this);
      this.eventKeypress  = this.keyPress.bindAsEventListener(this);
      
      Event.observe(document, "mouseup", this.eventMouseUp);
      Event.observe(document, "mousemove", this.eventMouseMove);
      Event.observe(document, "keypress", this.eventKeypress);
    this.drags.push(draggable);

    }
  },
  
  unregister: function(draggable) {
    this.drags = this.drags.reject(function(d) { return d==draggable });
    if(this.drags.length == 1){this.drags.pop();}
    if(this.drags.length == 0) {
      Event.stopObserving(document, "mouseup", this.eventMouseUp);
      Event.stopObserving(document, "mousemove", this.eventMouseMove);
      Event.stopObserving(document, "keypress", this.eventKeypress);
    }
  },
  
  activate: function(draggable) {
    window.focus(); // allows keypress events if window isn't currently focused, fails for Safari
    this.activeDraggable = draggable;
  },
  
  deactivate: function() {
    this.activeDraggable = null;
  },
  
  updateDrag: function(event) {
    if(!this.activeDraggable) return;
    var pointer = [Event.pointerX(event), Event.pointerY(event)];
    // Mozilla-based browsers fire successive mousemove events with
    // the same coordinates, prevent needless redrawing (moz bug?)
    if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return;
    this._lastPointer = pointer;
    this.activeDraggable.updateDrag(event, pointer);
  },
  
  endDrag: function(event) {
    if(!this.activeDraggable) return;
    this._lastPointer = null;
    this.activeDraggable.endDrag(event);
    this.activeDraggable = null;
  },
  
  keyPress: function(event) {
    if(this.activeDraggable)
      this.activeDraggable.keyPress(event);
  },
  
  addObserver: function(observer) {
    this.observers.push(observer);
    this._cacheObserverCallbacks();
  },
  
  removeObserver: function(element) {  // element instead of observer fixes mem leaks
    this.observers = this.observers.reject( function(o) { return o.element==element });
    this._cacheObserverCallbacks();
  },
  
  notify: function(eventName, draggable, event) {  // 'onStart', 'onEnd', 'onDrag'
    if(this[eventName+'Count'] > 0)
      this.observers.each( function(o) {
        if(o[eventName]) o[eventName](eventName, draggable, event);
      });
  },
  
  _cacheObserverCallbacks: function() {
    ['onStart','onEnd','onDrag'].each( function(eventName) {
      Resizers[eventName+'Count'] = Resizers.observers.select(
        function(o) { return o[eventName]; }
      ).length;
    });
  }
}


