// init
var czu = new Object();

czu.webcoreWeb = WEBCORE_WEB_PATH;


// common init
czu.common = new common();

// tools init
czu.tools = new Object();
czu.tools.toolTip = new toolTip();
czu.tools.wpLayer = new wpLayer();
czu.efects = new efects();


function common () {
	
	this.callMethodInScope = function( object, method )
	{
		var fnc = object[method];
		return function() { return fnc.apply( object, arguments ) }
	}
	
	this.findPos = function( obj )
	{
		var curleft = curtop = 0;
		
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return [curleft,curtop];
	}

	this.setEvent = function( obj, event, func )
	{
		if( event == 'onEnd') {
			obj.onEnd = func;
		}
	}

}


/** 
 * Efekty pro JS
 */
function efects()
{
	
	this.wipeIn = function( elmName, callback )
	{
		elm = document.getElementById( elmName );
		elm.style.overflow = "hidden";
		elm.style.height = 0;
	
		anim = new czu.animation();
		anim.set( elmName, 0, elm.scrollHeight, 'h' );
		
		if(typeof callback == "function") {
			czu.common.setEvent( anim, 'onEnd', function(e) {
												elm.style.overflow = "visible";
												if(callback)	callback(elm);
												 }  
								);
	}
	
	anim.play();
	}


	this.wipeOut = function( elmName, callback )
	{
		elm = document.getElementById( elmName );
		elm.style.overflow = "visible";

		anim = new czu.animation();
		anim.set( elmName, elm.offsetHeight, 0, 'h' );

		elm.style.overflow = "hidden";
		
		if(typeof callback == "function") {
			czu.common.setEvent( anim, 'onEnd', function(e) {
												
												if(callback)	callback(elm);
												 }  
								);
		}

		anim.play();
	}
}

/**
 * zobrazi tool tip nad zadanymi souradnicemi
 */
function toolTip( ) {
	this.elm = 'toolTipBox';
	
	this.create = function( width, height, posX, posY, content )
	{

		var content =  '<div style="position: absolute; top: 0; left: 0; z-index: 1;">' +
							
							/* levy horni roh */	
							'<img style="position: absolute; top: 0px; left: 0px;" src="'+ czu.webcoreWeb +'img/toolTip/cor_1.png" width="14" height="10" />' +  
							
							/* spojnice mezi hornim levym a hornim pravym rohem */
							'<img style="position: absolute; top: 0px; left: 14px; width: '+ (width - (2* 14) ) +'px;" src="'+ czu.webcoreWeb +'img/toolTip/cor_b1.png" width="14" height="10" />' + 	
							
							/* pravy horni roh */
							'<img style="position: absolute; top: 0px; left: '+ ( (width - 14) ) +'px;" src="'+ czu.webcoreWeb +'img/toolTip/cor_2.png" width="14" height="10" />' + 
							
							/* spojnice mezi hornim pravym a spodnim pravym rohem */
							'<img style="position: absolute; top: 10px; left: '+ (width-10) +'px; height: '+ (height - (2* 10) ) +'px;" src="'+ czu.webcoreWeb +'img/toolTip/cor_b2.png" width="10" height="14" />' + 	
							
							/* pravy spodni roh */
							'<img style="position: absolute; top: ' + (height - 10) + 'px; left: '+ ( width - 14 ) +'px;" src="'+ czu.webcoreWeb +'img/toolTip/cor_4.png" width="14" height="10" />' + 
							
							/* spojnice mezi pravym spodnim a levym spodnim rohem */
							'<img style="position: absolute; top: '+ (height - 10 ) +'px; left: 14px; width: '+ (width - (2* 14) ) +'px;" src="'+ czu.webcoreWeb +'img/toolTip/cor_b3.png" width="14" height="10" />' + 	
							
							/* levy spodni roh */
							'<img style="position: absolute; top: '+ (height-10) +'px; left: 0px" src="'+ czu.webcoreWeb +'img/toolTip/cor_3.png" width="14" height="10" />' + 
							
							/* spojnice mezi levym spodnim a levym hornim rohem */
							'<img style="position: absolute; top: 10px; left: 0px; height: '+ (height - (2* 10) ) +'px;" src="'+ czu.webcoreWeb +'img/toolTip/cor_b4.png" width="10" height="14" />' + 	
							
							'<img style="position: absolute; top: '+ (height-2) +'px; left: '+ ( (width/2) - 25) +'px" src="'+ czu.webcoreWeb +'img/toolTip/cor_x1.png" width="50" height="24" />' +
							
							'<div style="position: absolute; top: 10px; left: 10px; width: '+ (width - (2*10)) +'px; height: '+ (height - (2*10)) +'px;" class="toolTipContainer"> '+ content +' </div>' +
						'</div>';
		
		poziceBoxuX = posX - (width/2) + 25;
		poziceBoxuY = posY - height - 24;
		
		document.getElementById( this.elm ).style.top = poziceBoxuY + 'px';
		document.getElementById( this.elm ).style.left = poziceBoxuX + 'px';
		document.getElementById( this.elm ).style.display = 'block';
		document.getElementById( this.elm ).innerHTML = content;
	}
	
	this.hide = function()
	{
		if( this.elm != 'undefined' ) {
			document.getElementById( this.elm ).style.display = 'none';
		}
	}
}

function wpLayer() {
	
	this.active = false;
	this.layerID = '';
	this.layerContentID = '';
	this.layerBoxID = '';
	
	this.execute = function() 
	{
		if( this.layerID == '') {
			this.layerID = document.getElementById('wpLayer');	
			this.layerContentID = document.getElementById('wpLayerContent');	
			this.layerBoxID = document.getElementById('wpLayerBox');
		}

		if( !this.active ) {
			this.active = true;
			
			this.configure();
			this.show();
		} else {
			this.active = false;
			this.hide();
		}
	}
	
	this.show = function() 
	{
		this.layerID.style.display = 'block';
	}


	this.hide = function() 
	{
		this.layerID.style.display = 'none';
	}
	
	// nastaveni rozmeru okna
	this.configure = function()
	{
		topPosition = 10;
		if (document.compatMode && document.compatMode != "BackCompat") {
		   width = document.documentElement.clientWidth;
		   height = document.documentElement.clientHeight;
		   
			if( document.documentElement.scrollTop > 0 ) {
				topPosition += document.documentElement.scrollTop;
			}

		} else {
		   width = document.body.clientWidth;
		   height = document.body.clientHeight;

			if( document.documentElement.scrollTop > 0 ) {
				topPosition += document.body.scrollTop;
			}

		}
		

		
		
		this.layerID.style.left = '10px';
		this.layerID.style.width = (width - 20) + 'px';
		
		this.layerID.style.top = topPosition + 'px';
		this.layerID.style.height = (height - 20) + 'px';
		
		// umisteni boxu
//		layerBoxWidth = this.layerBoxID.width;
		this.layerBoxID.style.left =  ( (width/2) - (250)) + 'px';
		this.layerBoxID.style.top =  ( (height/2) - (150)) + 'px';
		
		if( navigator.appName == 'Microsoft Internet Explorer' ) {
			this.layerID.style.filter = 'alpha(opacity=90)';
		} else {
			this.layerID.style.MozOpacity = '0.9';
		}
		
		 
	}
}

czu.animation = function() {
	// private
	this._timer = null;				// ukazatel na timeout id
	this._curPos = 0;				// soucasna pozice
	this._curSize = 0;				// rozmer/delka ktery posouvam
	this._isActive = false;			// ukazatel zda je prave pustena animace	
	
	this._animElement = null;			// prvek ktery ma byt animovan
	this._animID = null;				// ukazatel na prvek ktery ma byt animovan
	this._animPosFrom = 0;				// startovni pozice
	this._animPosTo = 0;				// koncova pozice
	this._animDirection = 'h';			// smer w - do sirky, h - do delky
	
	// handlers
	this.onEnd = null;
	this.onStart = null;
	
	
	this.set = function( elm, from, to, direction )
	{
		this._curPos = 0;
		this._curSize = Math.abs( to - from );
		this._animElement = elm;
		this._animElementID = document.getElementById(elm);
		this._animPosFrom = from;
		this._animPosTo = to;
		this._animDirection = direction;
		
		return null;
	}
	
	this.play = function()
	{
		this._anime();
	}
	
	this._anime = function()
	{
		clearTimeout(this._timer);
	
		if( this._curPos < this._curSize ) {
			
			this._curPos = this._curPos + Math.ceil( ( this._curSize - this._curPos) / 5);
			
			if( this._animPosFrom > this._animPosTo ) { 
				if( this._animDirection == 'h' ) 	{this._animElementID.style.height = ( this._animPosFrom - this._curPos) + 'px';} 
				else 								{this._animElementID.style.width = (this._animPosFrom - this._curPos) + 'px';}
			} else {
				if( this._animDirection == 'h' ) 	{this._animElementID.style.height = (this._animPosFrom + this._curPos) + 'px';} 
				else 								{this._animElementID.style.width = (this._animPosFrom + this._curPos) + 'px';}
			}
			
			this._timer = setTimeout( czu.common.callMethodInScope( this, '_anime' ) , 50 );
		} else {
			e = new Object();
			e.type = 'onEnd';
			if(typeof this.onEnd == 'function') {
				this.onEnd(e); 
			}
		}
	}
}
