// =======================
//		Dynamic Window
// =======================
var global_dWindows = [];

function dWindow(){
	this.element = null;
	this.frame = null;
	this.shadow = null;
	this.content = null;
	this.exit = null;
	this.id = null;
	this.x = 0;
	this.y = 0;
	this.width = 0;
	this.height = 0;
	this.opened = false;
	this.opensCount = 0;

	this.init = function(id, width, height, ImgPath, innerHTML){
		var DIV = document.createElement("div");
		this.id = id;
		this.x = (-2)*width;
		this.y = (-2)*height;
		this.width = width;
		this.height = height;
		DIV.className = "dWindow";
		DIV.id = "dWindow_" + id;
		DIV.style.width = width + "px";
		DIV.style.height = height + "px";			
		DIV.style.position = 'absolute';	
		DIV.style.zIndex = '9995';		
		DIV.style.left = this.x + 'px';
		DIV.style.top = this.y + 'px';
		// if under layer will be SELECT or other Windwos control element we put IFRAME to cover this elements
		if (dhtml.IE){
			IFRAME = document.createElement('IFRAME');
			IFRAME.frameBorder = '0';
			IFRAME.scrolling = 'no';
			IFRAME.id = 'dWindow_frame_' + id;
			IFRAME.className = 'dWindow_frame'
			IFRAME.style.width = width + "px";
			IFRAME.style.height = height + "px";			
			IFRAME.style.position = 'absolute';	
			IFRAME.style.zIndex = '9996';
			//IFRAME.style.left = this.x + 'px';
			//IFRAME.style.top = this.y + 'px';
			this.frame = IFRAME;
			DIV.appendChild( IFRAME );
		}
		// create window content
		CONTENT = document.createElement("div");
		CONTENT.id = 'dWindow_content_' + id;
		CONTENT.className = 'dWindow_content'
		CONTENT.style.width = (dhtml.Mozilla)?(width-40):(width) + "px"; //take into value of padding:20px
		CONTENT.style.height = (dhtml.Mozilla)?(height-40):(height) + "px";		
		CONTENT.style.position = 'relative';	
		CONTENT.style.zIndex = '9998';
		CONTENT.innerHTML = innerHTML;
		CONTENT.style.visibility = "visible";
		
		// create window shadow
		IMG = new Image();
		IMG.id = 'dWindow_shadow_' + id;
		IMG.className = 'dWindow_shadow';
		IMG.style.position = 'absolute';
		IMG.style.zIndex = '9997';
		IMG.src = ImgPath + 'shadow.png';
		if (dhtml.IE)
		{
			IMG.runtimeStyle.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+ IMG.src +', sizingMethod=scale)'; 
			IMG.src = ImgPath + 'pix.gif';
		}

		//create dWindow close button
		EXIT = new Image();
		EXIT.id = 'dWindow_close_' + id;
		EXIT.style.position = 'absolute';
		EXIT.style.zIndex = '9999';
		EXIT.style.right = '4px';
		EXIT.style.top =  '4px';
		EXIT.style.cursor = 'pointer';
		EXIT.src = ImgPath + 'icons/popup_close.gif';
		
		//append childs to dWindow object
		this.shadow = IMG;
		DIV.appendChild( IMG );
		this.content = CONTENT;
		DIV.appendChild( CONTENT );
		this.exit = EXIT;
		CONTENT.appendChild( EXIT );
		
		//append dWindow to docuemnt
		document.body.appendChild(DIV); 	
		this.element = DIV;
		
		//add onlick event to exit button
		var o_dWindow = this;
		addEventToObject(
			EXIT,
			'onclick',
			function(){
				o_dWindow.Close();
				return false;
			}
		)
		//add this object to global array
		global_dWindows[id] = this;
	}
	
	this.changeContent = function(content){
		this.content.innerHTML = content;
		this.content.appendChild(this.exit);
	}
	
	this.Open = function(){
		for (key in global_dWindows)
		{
			if (global_dWindows[key].opened == true /*&& global_dWindows[key].id != this.id*/)
			{
				global_dWindows[key].Close();
				break;
			}
		}
		if (!this.opened)
		{
			this.opensCount++;
			this.element.style.visibility = 'visible';

			// Change Shadow width and height because content height and width can be biger
			clWidth = this.content.clientWidth;
			clHeight = this.content.clientHeight;
			xratio = 8.5;
			yratio = 5.5;
			if (clHeight < 300) yratio=3;
			wratio = Math.round(clWidth/xratio);
			hratio = Math.round(clHeight/yratio)
			this.shadow.style.width = (clWidth + wratio) + 'px'; 
			this.shadow.style.height = (clHeight + hratio) + 'px';
			this.shadow.style.left = (-1)*Math.round(wratio/2.2) + 'px';
			this.shadow.style.top =  (-1)*Math.round(hratio/4) + 'px';

			// Position window in center
			var x = (window.document.body.clientWidth - this.width) / 2  + window.document.body.scrollLeft;
			var y = (window.document.body.clientHeight  - this.height) / 2 + window.document.body.scrollTop;
			this.setElementPosition(x, y);

			// Seting state to open
			this.opened = true;
		} 
	}
	
	this.Close = function(){
		if (this.opened)
		{
			this.element.style.visibility = 'hidden';
			var x = (-2)*(this.width);
			var y = (-2)*(this.height);
			this.setElementPosition(x, y);
			this.opened = false;
		}				
	}
	
	this.setElementPosition = function(x, y){
		this.x = x;
		this.y = y;
		this.element.style.left = this.x + 'px';
		this.element.style.top = this.y + 'px';
	}
	
	return this;
}