/////////////////////////////////////////////////////////
// 	Author		Zbynek Strnad (Fastest Solution)	//
//	All Right Reserved							//
//	Dont use without permission					//
/////////////////////////////////////////////////////////
function getBrowser(){
		if (navigator.appName == 'Netscape'){
			return 'FF';
		} else if(navigator.appName == 'Microsoft Internet Explorer'){
			return 'IE';
		}
	}

	function Wndw(config){
		_this = this;
		this.config = config;
		this.caption = 'Popisek';
		
		Stamp = new Date();
		this.id = Stamp.getYear() +''+ Stamp.getMonth() +''+ Stamp.getDate() +''+ Stamp.getHours() +''+ Stamp.getMinutes() +''+ Stamp.getSeconds();
		this.index = this.getHIndex();
		this.init2();
		this.data();
		
	}
	
	Wndw.prototype.data = function(){
		this.ddiv.innerHTML = '<img src="/css/images/admin/preloader.gif" style="float:left"/><span style="display:block;float:left; margin: 3px 0px 0px 0px">Probíhá komunikace se serverem ...</span>';
		
	}
	
	Wndw.prototype.loadUrl = function(url){
		_this = this;
		new Ajax.Updater('container_div', url, { 
					method: 'post',
					onComplete: function(){
						_this.goCenter();
    						if (window.innerHeight > _this.fullHeight()){
    							_this.bgmask.style.height =  window.innerHeight + 'px';
    						} else {
    							_this.bgmask.style.height =   _this.fullHeight() + 'px';
    						}
  					},
  					onFailure: function(){
  						alert('Stránka ' + url + 'nebyla nalezena.');
  					}
  					,evalScripts: 'true'
		}
		);
		
	}
	
	Wndw.prototype.init2 = function(){
				
		// vytvoreni okna jako celku
		var InWin = document.createElement('div');
		InWin.className = 'win';
		InWin.id = this.id;
		
		// vytvoreni hlavicky
		var header = document.createElement('h2');									// vytvori H2
		header.appendChild(document.createTextNode(this.config['caption']));		// vlozi text do H2
		InWin.appendChild(header);
		this.addEvent(header,'mousedown',this.beginDrag);
		
		//vytvoreni closeButton
		var clsBtn = document.createElement('div');
		clsBtn.className = 'ClsButton';
		clsBtn.appendChild(document.createTextNode('X'));
		header.appendChild(clsBtn);
		this.addEvent(clsBtn,'click',
			function(){
				if (_this.config['onClose']){		
					eval(_this.config['onClose']);
				}
				_this.closeWndw();
				
			}
				
		);
		
		// vytvoreni plochy
		var ddiv = document.createElement('div');
		ddiv.id = 'container_div';
		ddiv.innerHTML = '';
		InWin.appendChild(ddiv);
		
		// vlozeni bgover
		if (getBrowser() == 'FF'){
			var bgmask = document.createElement('div');
			bgmask.className = 'bgmask';
			bgmask.style.zIndex = this.index;
			if (window.innerHeight > this.fullHeight()){
    				bgmask.style.height =  window.innerHeight + 'px';
    			} else {
    				bgmask.style.height =   this.fullHeight() + 'px';
    			}
			bgmask.id = this.id + '_iframe';
		} else {
			var bgmask = document.createElement('iframe');
			bgmask.className = 'bgmask';
			bgmask.style.zIndex = this.index;
			if (window.innerHeight > this.fullHeight()){
    				bgmask.style.height =  window.innerHeight + 'px';
    			} else {
    				bgmask.style.height =   this.fullHeight() + 'px';
    			}
			bgmask.src = '/js/bgmask.html';
			bgmask.id = this.id + '_iframe';
		}
		
		this.bgmask = bgmask;
		
		// vlozeni do dokumentu
		var doc = document.getElementsByTagName('body')[0];
		doc.appendChild(bgmask);
		doc.appendChild(InWin);
		
		// nastaveni vlastnosti Wndw
		if (_this.config['height'] && _this.config['height'] == 'auto'){ // automaticka vyska
			
		} else if (_this.config['height']){ // vyska dle config
			InWin.style.height =(parseInt(_this.config['height']) + 30 )+ 'px';
		}
		if (_this.config['width']){	InWin.style.width = parseInt(_this.config['width']) + 'px';} // set sirky pokud je v config
		
		
		
		InWin.style.zIndex = this.index;
		this.ddiv = ddiv;
		this.InWin = InWin;
		this.goCenter();
		if (_this.config['url'])
			this.loadUrl(this.config['url']);
		
	}
	
	Wndw.prototype.goCenter = function(){
		this.InWin.style.left = ((this.winWidth() - this.InWin.offsetWidth)/2) + 'px';
		wndwTop = ((this.winHeight() - this.InWin.offsetHeight)/2 + this.getScrollY()) + 'px';
		
		this.InWin.style.top = wndwTop;
		
	}
	
	Wndw.prototype.closeWndw = function(){				// zavrit iframe
		var objc = document.getElementById(_this.id);
		var mask = document.getElementById(_this.id + '_iframe');
		var doc  = document.getElementsByTagName('body')[0];
		doc.removeChild(objc);
		doc.removeChild(mask);
		if (_this.config['onCloseSpec']){
			eval(_this.config['onCloseSpec']);
		}
	}
	
	Wndw.prototype.getScrollY = function() {
		var scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {	//Netscape compliant
			scrOfY = window.pageYOffset;
		} else if( window.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { //DOM compliant
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {	//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;
	}
	
	Wndw.prototype.fullHeight = function(){				// cela vyska i se scroll under
		if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight 	+ window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
		}
		return yWithScroll;
	}
	
	
	
	Wndw.prototype.winHeight = function(){			// dej vysku okna
		myHeight = 0;
		if( typeof(window.innerHeight ) == 'number' ) {			//Non-IE
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 											//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible
			myHeight = document.body.clientHeight;
		}
		return myHeight;
	}
	
	Wndw.prototype.winWidth = function(){			// dej sirku okna
		myWidth = 0;
		if( typeof(window.innerWidth ) == 'number' ) {			//Non-IE
			myWidth = window.innerWidth;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 											//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible
			myHeight = document.body.clientWidth;
		}
		return myWidth;
	}
	
	Wndw.prototype.minmaxIframe = function(){		// minimalizace / maximalizace
		var objc = document.getElementById(_this.id);
		var ifrm = objc.getElementsByTagName('iframe')[0];
		if (this.status && this.status == 'mini'){
			objc.style.left = this.oldX + 'px';
			objc.style.top = this.oldY + 'px';
			this.status = null;
			this.oldX = null;
			this.oldY = null;
			ifrm.style.display = 'block';
			this.innerHTML = '_';
		} else {
			ifrm.style.display = 'none';
			this.status = 'mini';
			this.oldY = parseInt(objc.style.top);
			this.oldX = parseInt(objc.style.left);
			objc.style.left = '0px';
			
			objc.style.top = (_this.winHeight() - 30) + 'px';
			this.innerHTML = '=';
		}
	}
	
	Wndw.prototype.closeIframe = function(){				// zavrit iframe
		var objc = document.getElementById(_this.id);
		var mask = document.getElementById(_this.id + '_iframe');
		var doc  = document.getElementsByTagName('body')[0];
		doc.removeChild(objc);
		doc.removeChild(mask);
		if (_this.config['onClose']){
			
			eval(_this.config['onClose']);
		}
	}
	
	Wndw.prototype.getHIndex = function(){				// yiskej max Zindex
		var allElems = document.getElementsByTagName?document.getElementsByTagName("*"): document.all; 
		var maxZIndex = 0;
		for(var i=0;i<allElems.length;i++) {
			var elem = allElems[i];
			var cStyle = null;
			if (elem.currentStyle) {cStyle = elem.currentStyle;
			} else if (document.defaultView && document.defaultView.getComputedStyle){
				cStyle = document.defaultView.getComputedStyle(elem,"");
			}
			var sNum;
			if (cStyle) {
				sNum = Number(cStyle.zIndex);
			} else {
				sNum = Number(elem.style.zIndex);
			}
			if (!isNaN(sNum)) {
				maxZIndex = Math.max(maxZIndex,sNum);
			}
		}
		return maxZIndex;
	}
	
   	Wndw.prototype.addEvent = function( obj, type, fn ) { // pridani udalosti objektu
    	if (obj.attachEvent ) {
			obj['e'+type+fn] = fn;
        	obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
        	obj.attachEvent( 'on'+type, obj[type+fn] );
      	} else {
        	obj.addEventListener( type, fn, false );
      	}
    }
    
    Wndw.prototype.removeEvent = function( obj, type, fn ) { // odebrani udalosti objektu
    	if ( obj.detachEvent ) {
       		obj.detachEvent( 'on'+type, obj[type+fn] );
       		obj[type+fn] = null;
     	} else {
       		obj.removeEventListener( type, fn, false );
     	}
   	}

	Wndw.prototype.beginDrag = function(event){
		var deltaX = event.clientX - parseInt(this.parentNode.style.left);
		var deltaY = event.clientY - parseInt(this.parentNode.style.top);
		if (document.addEventListener){
			document.addEventListener("mousemove", moveHandler, true);
			document.addEventListener("mouseup", upHandler, true);
		} else if (document.attachEvent){
			document.attachEvent("onmousemove", moveHandler);
			document.attachEvent("onmouseup", upHandler);
		} else {
			var oldmovehandler = document.onmousemove;
			var olduphandler = document.onmouseup;
			document.onmousemove = moveHandler;
			document.onmouseup = upHandler;
		}
	
		if (event.stopPropagation) event.stopPropagation();
		else event.cancelBubble = true;
		if (event.preventDefault) event.preventDefault();
		else event.returnValue = false;
		var targetObject = this;
		targetObject = targetObject.parentNode;
				
		function moveHandler(e){
			if (!e) e = window.event;
			targetObject.style.left = (e.clientX - deltaX) + "px";
			targetObject.style.top = (e.clientY - deltaY) + "px";
			if (e.stopPropagation) e.stopPropagation();
			else e.cancelBubble = true;
		}
		
		function upHandler(e){
			if (!e) e = window.event;
			if (document.removeEventListener){
				document.removeEventListener("mouseup", upHandler, true);
				document.removeEventListener("mousemove", moveHandler, true);
			} else if (document.detachEvent){
				document.detachEvent("onmouseup", upHandler);
				document.detachEvent("onmousemove", moveHandler);
			} else {
				document.onmouseup = olduphandler;
				document.onmousemove = oldmovehandler;
			}
			if (e.stopPropagation) e.stopPropagation();
			else e.cancelBubble = true;
		}
	}
