/**
 * kPopupLayer
 * @author Krish <krish@freemail.hu>
 * @version 0.9
 */

function kPopupLayer(_params)
{
	if( arguments.length == 0 ){ _params = new Object(); }
	
	//_params.id
	this.contentID = _params.id == null ? "kPpopupLayer_Conatiner" : _params.id;
	
	this.type = _params.type == null ? "span" : _params.type; //span|div|iframe
	
	if( document.getElementById(this.contentID) == null )
	{
		/* IE don't liek this :(
		var _container  = document.createElement(this.type);
			_container.id = this.contentID;
			document.body.appendChild(_container); 
			if( this.type == "iframe" )
			{
				this.container.scrolling   = (_params.scrolling == null) ? "no" : _params.scrolling; 
				this.container.frameBorder = "0";
			}			
		*/
		
		var _attributes = " id='" + this.contentID + "' ";
		if( this.type == "iframe" )
		{
			//if( _params.width != null  ){ _attributes += "width='"+_params.width+" "; }
			//if( _params.height != null ){ _attributes += "height='"+_params.height+" "; }
			_attributes += " scrolling='" + (_params.scrolling == null ? "no" : _params.scrolling) + "' "; 
			_attributes += " frameBorder='0' ";
		}	
		document.write("<"+this.type+" "+_attributes+"></"+this.type+">");
	}
	
	this.container = document.getElementById(this.contentID);

	this.initStyle();
	
	if( _params.className != null )
	{
		this.container.className =  _params.className
	}
	else
	{
		this.setDefaultStyle();
	}
	
	//_params style
	if( _params.style != null )
	{
		var styleProp;
		var styleLines = _params.style.split(";");
		for(  i=0;i<styleLines.length; i++ )
		{
			styleProp = styleLines[i].split(":");
			this.container.setAttribute(this._strTrim(styleProp[0]),this._strTrim(styleProp[1])); 
		}
	}
	
	//content
	if( _params.content != null ){ this.container.innerHTML = _params.content; }//div,span
	if( _params.contentURL != null ){ this.container.src = _params.contentURL; }//iframe
	
	//position: offset
	if( _params.offset != null )
	{
		var offsets = _params.offset.split(",");
		this.offsetX = Number(offsets[0]);
		this.offsetY = Number(offsets[1]);
	}
	if(_params.offsetX != null ){ this.offsetX = _params.offsetX; } 
	if(_params.offsetY != null ){ this.offsetY = _params.offsetY; }
	
	//size
	this._setSize(_params);
}

kPopupLayer.prototype.x = 0;
kPopupLayer.prototype.y = 0;
kPopupLayer.prototype.offsetX = 0;
kPopupLayer.prototype.offsetY = 0;

kPopupLayer.prototype.width;
kPopupLayer.prototype.height;
kPopupLayer.prototype.contentID;

kPopupLayer.prototype.initStyle = function()
{
	this.container.style.zOrder     = "32322";
	this.container.style.position   = "absolute";
	this.container.style.visibility = "hidden";
}
kPopupLayer.prototype.setDefaultStyle = function(_obj)
{
	this.container.style.border = "1px solid #5a5a5a";
	if( this.type != "iframe" )
	{
		this.container.style.backgroundColor = "#ffff99";
		this.container.style.fontFamily      = "Verdana";
		this.container.style.fontSize        = "10px";
		this.container.style.padding         = "2px";
	}
}

kPopupLayer.prototype.setContent = function(_html)
{
	this.container.innerHTML = _html;
}

kPopupLayer.prototype._setSize = function(_params)
{
	//size
	if( _params.width != null  ){ this.container.style.width  = _params.width+"px"; }
	if( _params.height != null ){ this.container.style.height = _params.height+"px"; }	
}

kPopupLayer.prototype.show = function(_event,_params)
{
	if( arguments.length < 2 ){ _params = null; }
	if( arguments.length < 3 ){ _autohide = false; }
	if( _params == null ){ _params = new Object(); }

	if( window.event )
	{
		var _x = window.event.x+document.body.scrollLeft; 
		var _y = window.event.y+document.body.scrollTop;
	}
	else
	{
		var _x = _event.pageX; 
		var _y = _event.pageY;
	}
	
	//position
	if( _params.x != null ){ _x = _params.x; }
	if( _params.y != null ){ _y = _params.y; }
	
	if(  _params.bindPosObjId != null ){ _params.bindPosObj = document.getElementById(_params.bindPosObjId); }

	if(  _params.bindPosObj != null )
	{
		_x = this.getX(_params.bindPosObj);
		_y = this.getY(_params.bindPosObj);
	}
	
	if( _params.offset != null )
	{
		var offsets = _params.offset.split(",");
		this.offsetX = Number(offsets[0]);
		this.offsetY = Number(offsets[1]);
	}
	if(_params.offsetX != null ){ this.offsetX = _params.offsetX; } 
	if(_params.offsetY != null ){ this.offsetY = _params.offsetY; }
	
	_x = _x + this.offsetX;
	_y = _y + this.offsetY; 
	
	this.container.style.left = _x+"px";
	this.container.style.top  = _y+"px";
	
	this._setSize(_params);
	
	this.container.style.visibility = "visible";		
	
}
kPopupLayer.prototype.getX = function(obj)
{
	var x = 0;
	do
	{
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	while (obj);
	return x;
}
kPopupLayer.prototype.getY = function(obj)
{
	var y = 0;
	do
	{
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	while (obj);
	return y;
}
kPopupLayer.prototype.hide = function()
{
	this.container.style.visibility = "hidden";	
}
kPopupLayer.prototype._strTrim = function( str )
{
	return str.replace(/^[\s]+|[\s]+$/, '');
}
kPopupLayer.prototype.addEvent = function(obj, evType, fn) 
{
	if (obj.addEventListener) 
	{
		obj.addEventListener(evType, fn, true);
		return true;
	}
	else if (obj.attachEvent) 
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} 
	else 
	{
		return false;
	}
}
kPopupLayer.prototype.bindEvent = function(_targetID,_kPopupLayerObj,_eventsList,_mouseMoveParams)
{
	if( arguments.length < 3 ){ _eventsList      = "mousemove,mouseout"; }
	if( arguments.length < 4 ){ _mouseMoveParams = {}; }
	_targetObj = document.getElementById(_targetID);
	
	var _events = _eventsList.split(",")
	for( i=0; i<_events.length; i++ )
	{
		if( _events[i] == "mousemove" )
		{
			document.kPopupLayerEventPrameters = {obj:this}
			this.addEvent( _targetObj, "mousemove", function(_evt){ _kPopupLayerObj.show(_evt,_mouseMoveParams); } );
			
		}
		if( _events[i] == "mouseout" )
		{
			this.addEvent( _targetObj, "mouseout", function(_evt){ _kPopupLayerObj.hide(); } );
		}		
	}
}

