var PBase = Class.create;
PBase.prototype = {
	lastObjectId: 1,
	initialize: function()
	{
		this.objectId = PBase.prototype.lastObjectId;
		PBase.prototype.lastObjectId++;
		
		if (!window.pObjects)
			window.pObjects = [];
			
		window.pObjects[this.objectId] = this;	
	}
};


var PUtil = 
{
	getDomHead: function()
	{
		var tmp = $$('head');
		return tmp[0];
	},
		
	includeJs: function(url)
	{
		var domScript = document.createElement('script');
		domScript.setAttribute('type', 'text/javascript');
		domScript.setAttribute('src', url);
		
		
		this.getDomHead().appendChild(domScript);
	},
	
	includeCss: function(url)
	{
		var domLink	= document.createElement('link');
		domLink.setAttribute('type', 'text/css');	
		domLink.setAttribute('rel', 'stylesheet');
		domLink.setAttribute('href', url);
				
		this.getDomHead().appendChild(domLink);
	},
	
	getPHPSessionId: function()
	{
		var s = document.cookie;
		res = s.match(/PHPSESSID=(.*?)(;|$)/);
		return res[1];
	},
	
	randomString: function(length)
	{
		var alph = "abcdefghijklmnopqrstuvwxyz0123456789";
		
		var res = "";
		for (var i = 0; i < length; i++)
		{
			var j = Math.round(Math.random()*(alph.length - 1));
			res += alph.substr(j, 1);
		}
		
		return res;
	},
	
	numberFormat: function(number, decimals, dec_point, thousands_sep)
	{
		var exponent = "";
		var numberstr = number.toString ();
		var eindex = numberstr.indexOf ("e");
		if (eindex > -1)
		{
			exponent = numberstr.substring (eindex);
			number = parseFloat (numberstr.substring (0, eindex));
		}

		if (decimals != null)
		{
			var temp = Math.pow (10, decimals);
			number = Math.round (number * temp) / temp;
		}
		var sign = number < 0 ? "-" : "";
		var integer = (number > 0 ?
		Math.floor (number) : Math.abs (Math.ceil (number))).toString ();

		var fractional = number.toString ().substring (integer.length + sign.length);
		dec_point = dec_point != null ? dec_point : ".";
		fractional = decimals != null && decimals > 0 || fractional.length > 1 ?
		(dec_point + fractional.substring (1)) : "";
		if (decimals != null && decimals > 0)
		{
			for (i = fractional.length - 1, z = decimals; i < z; ++i)
			fractional += "0";
		}

		thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ?
		thousands_sep : null;
		if (thousands_sep != null && thousands_sep != "")
		{
			for (i = integer.length - 3; i > 0; i -= 3)
			integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
		}

		return sign + integer + fractional + exponent;
	}
};

//init cursor pos autodetection

var CursorPos = 
{
	x: 0,
	y: 0,
	
	update: function(event)
	{
		if (Event.pointerX)
		{
			this.x = Event.pointerX(event);
			this.y = Event.pointerY(event);
		}
	}
};


new PeriodicalExecuter(function(pe) 
{
	if (!document.body)
		return;
	
	Event.observe(document.body, 'mousemove', CursorPos.update.bindAsEventListener(CursorPos));
	pe.stop();

}, 0.5);


//some custom functions

Event.observeDelayed = function(element, event, func, timeout)
{
	if (!timeout)
		timeout = 50;
	
	var newFunc = function() {setTimeout(func, timeout);};
	Event.observe(element, event, newFunc);	
}





