//<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
/*
possible Init data {}
headerClass and/or headerId
bodyClass and/or bodyId
footerClass and/or footerId
containerClass and/or containerId
closeButton, closeButtonClass and/or closeButtonId
iframe, iframeClass and/or iframeId
clickOutClose
*/
function Panel(initData) {
	var shown = false;
	this.isShown = function PIsShow(){return shown;}
	var height = 0;
	this.getHeight = function PGetHeight(){return height;}
	var width = 0;
	this.getWidth = function PGetWidth(){return width;}
	var closeOnClickOut = false;

	var container = CDiv();

	this.setWidth = function(_width)
	{
		container.style.width = _width + 'px';
		width = _width;
	}	

	this.disableCloseOnClickOut = function()
	{
		closeOnClickOut = false;
	}
	
	this.setHeight = function(_height)
	{
		container.style.height = _height + 'px';
		height = _height;
	}	

	if (initData.containerClass) container.className = initData.containerClass;
	if (initData.containerId) container.id = initData.containerId;
	//height = getObjectCurrentHeight(container);
	width = getObjectCurrentWidth(container);
	var header = CDiv(container);
	if (initData.headerClass) header.className = initData.headerClass;
	if (initData.headerId) header.id = initData.headerId;
	this.getHeaderReferense = function PGetHeaderReferense(){return header;}
	
	var bodyDiv = CDiv(container);
	if (initData.bodyClass) bodyDiv.className = initData.bodyClass;
	if (initData.bodyId) bodyDiv.id = initData.bodyId;
	this.getBodyReferense = function PGetBodyReferense(){return bodyDiv;}
	
	var footer = CDiv(container);
	if (initData.footerClass) footer.className = initData.footerClass;
	if (initData.footerId) footer.id = initData.footerId;
	this.getFooterReferense = function PGetFooterReferense(){return bodyDiv;}
	
	var lastClickY = 0;
	var operaFirstClickMarker = false;
	this.setOperaFirstClickMarker = function PSetOperaFirstClickMarker(){operaFirstClickMarker=true;}
	
	if (initData.iframe) 
	{
		var fakeFrame = CEAC('iframe');
		if (initData.iframeClass) fakeFrame.className = initData.iframeClass;
		if (initData.iframeId) fakeFrame.id = initData.iframeId;
		if (!initData.iframeClass && !initData.iframeId) createFakeFrameStyle();
	}
	else var fakeFrame = false;
	if (initData.clickOutClose) closeOnClickOut = true;
	function createFakeFrameStyle(){
		// TODO take current style from container and apply its 
		// top, left width height to frame
	}
	// 
	this.showCurrent = function PShowCurrent()
	{
		if (fakeFrame) fakeFrame.style.display = 'block';
		container.style.display = 'block';
		container.style.visibility = 'visible';
		shown = true;
		if (closeOnClickOut) addHandlerToNode(document,'onclick',this.clickOutCloseHandler,'document');
	}
	this.showXY = function PShowCurrent(x,y)
	{
		if (fakeFrame) 
		{
			fakeFrame.style.left = x + 'px';
			fakeFrame.style.top = y + 'px';
		}
		container.style.left = x + 'px';
		container.style.top = y + 'px';
		this.showCurrent();
	}
	// node : node relavite to what panel must be plased
	// position : upper, lower, auto
	this.showRelative = function PShowRelative(node,position)
	{
		// TODO later refactor this to a set of XBrowser functions or object that will 
		// generate any coordinatos relative to any thing maybe expand prototype of NODE 
		// maybe find it in sone lib
		var pos = getElementPosition(node,false);
		var nodeHeight = getObjectCurrentHeight(node);
		/***********************************************************************************
		!!!!! IE 6.0 and earlier have a bug then geting offsetTop of LI and divs in it !!!!!
		remove or change detection to 6.0 and earlier then orienting to 7.0
		************************************************************************************/
		if ('fixedLower' == position)
		{
			this.showXY(pos.left,pos.top+nodeHeight+3);
			return;
		}
		height = getObjectCurrentHeight(container);
		if (getObjectCurrentHeight(bodyDiv) > height) height = getObjectCurrentHeight(bodyDiv);
		if (nodeHeight < node.clientHeight || !nodeHeight) nodeHeight = node.clientHeight;
		if (!nodeHeight) nodeHeight = node.offsetHeight;
		// hack for opera bug
		if (isOpera && nodeHeight>50) nodeHeight=20;
		if ('auto' == position) {
			var nodeScreenTop = pos.top - getBodyScrollTop();
			if (nodeScreenTop - 3 - height < 0) {
				position = 'lower';
			}else{
				var windowHeight = document.body.parentNode.clientHeight == 0 ? document.documentElement.clientHeight : document.body.parentNode.clientHeight;
				if (nodeScreenTop + nodeHeight + 8 + height > windowHeight){
					position = 'upper';
				}else{
					position = 'lower';
				}
			}
		}
		var mozilaAddUper =0;
		if (isMozilla) mozilaAddUper = 10;
		if (pos.left + width> document.body.clientWidth) pos.left -= pos.left + width -document.body.clientWidth+25;
		if ('upper' == position){
			this.showXY(pos.left,pos.top-3-height-mozilaAddUper);
		}else{
			this.showXY(pos.left,pos.top+nodeHeight+3);
		}
	}
	this.hide = function PHide()
	{
		container.style.visibility = 'hidden';
		shown = false;
		if (closeOnClickOut) removeHandlerFromNode(document,'onclick',this.clickOutCloseHandler,'document');
	}
	if (initData.closeButton)
	{
		var closeButton = CDiv(header);
		if (initData.closeButtonClass) closeButton.className = initData.iframeClass;
		if (initData.closeButtonId) closeButton.id = initData.closeButtonId;
		closeButton.onclick = this.hide;
	}
	this.updateByContent = function PUpdateByContent()
	{
		//curentHeight = getObjectCurrentHeight(container);
	}
	_this = this;
	this.clickOutCloseHandler = function (_event){
		if (operaFirstClickMarker && isOpera) {
			operaFirstClickMarker = false;
			return false;
		}
		if (isMozilla || isOpera) event = _event;
		firer = event.target?event.target:event.srcElement;
		if (firer == container) return;
		if (firer.no_close_mark)
		{
			firer.no_close_mark = false;
			return;
		}
		if (firer.tagName == "HTML" || firer.tagName == "BODY" || firer == container) 
		{
			_this.hide();
			return;
		}
		while (firer = firer.parentNode){
			if (firer == container) return;
			if (firer.tagName == "HTML" || firer.tagName == "BODY" || firer == container) 
			{
				_this.hide();
				return;
			}
		}
	}
}
