/*
Author: 		Petar Sabev
Description: 	Document tools
Modified Date: 	24.10.2008
*/

function getPosition(e)
{
	var left = 0;
	var top  = 0;

	while (e.offsetParent)
	{
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}
function stopBubble(e)
{
	e = e || window.event;
	
	e.cancelBubble = true;
	if(e.stopPropagation) 
		e.stopPropagation();
}
function mouseCoords(ev)
{
	if(ev.pageX || ev.pageY)
		return {x:ev.pageX, y:ev.pageY};
	return {x:ev.clientX + document.documentElement.scrollLeft, y:ev.clientY + document.documentElement.scrollTop}
}
function getCss(el,par) 
{
	if (el.currentStyle) 
		return parseInt(eval("el.currentStyle."+par));
	else if (document.defaultView.getComputedStyle)
		return parseInt(eval("document.defaultView.getComputedStyle(el,'')."+par));
	else 
		return;
}
function centerElCoords(el)
{	
	var width = getCss(el,"width");
	var height = getCss(el,"height");

	page_center_x = document.documentElement.clientWidth/2+document.documentElement.scrollLeft;
	page_center_y = document.documentElement.clientHeight/2+document.documentElement.scrollTop;

	return {y:parseInt(page_center_y-height/2), x:parseInt(page_center_x-width/2)};
}
function animation(object)
{
	this.object = object
	this.endframe=0;
	this.frame= new Array;
	
	this.addAnimation = function(par,begin,end,duration,fps,delay,easing)
					{
						easing = (easing) ? easing : this.easeInOutExpo;
						delay = (delay) ? delay : 0;
							
						var change = end-begin;
						var interval = Math.ceil(1000/fps);
						var totalFrames = Math.ceil(duration/interval);
						var step = change/totalFrames;
						
						for(i=0;i<=totalFrames;i++)
							this.doFrames(interval*i+delay,par+","+(easing(interval*i,begin,change,duration)));
						
						this.endframe = interval*i+delay;
					}
					
	this.addProp = function(par,value,delay)
					{	
						this.doFrames(delay,par+","+value);
					}
					
	this.setStyle = function(obj,prop,val) 
					{
  						if(prop=='opacity') 
  						{
  							obj.style.zoom = 1;
   							obj.style.filter = "alpha(opacity=" + parseFloat(val*100) + ")";
    						obj.style.opacity  = parseFloat(val);
    						return true;
  						}
  						if(prop=='background' || prop=='display')
  						{
  							obj.style[prop] = val;
  							return
  						}
  						if(prop=='float')   
     						prop = (window.attachEvent) ? 'styleFloat' : 'cssFloat';
 
  						unit='px';
  						obj.style[prop] = val+unit;
  						
					}	
					
	this.doFrames = function(curFrame,pars)
					{
						if(this.frame[curFrame])
							this.frame[curFrame] += ";"+pars;
						else
							this.frame[curFrame] = pars;
					}
				
	this.animate = function()
				{
					var obj = this.object;
					var arr = this.frame;
					var setStyle = this.setStyle;
					
					for(var i=0; i<arr.length; i++)
					{
						for(i in arr)
						{
							(function()
							{
								var frame = i;
								function inner()
								{
									var actions = arr[frame].split(";");
								
									for(var j=0; j<actions.length; j++)
									{
										params = actions[j].split(",");
										setStyle(obj,params[0],params[1]);
									}
								}
								timer[timer.length] = setTimeout(inner,frame);
							})();
						}
					}
					return true;
				}

	this.easeInQuad = function (t, b, c, d) {return c*(t/=d)*t + b;};
	this.easeOutQuad = function (t, b, c, d) {return -c *(t/=d)*(t-2) + b;};
	this.easeInOutQuad = function (t, b, c, d) {if ((t/=d/2) < 1) return c/2*t*t + b;return -c/2 * ((--t)*(t-2) - 1) + b;};
	this.easeOutCubic = function (t, b, c, d) {return c*((t=t/d-1)*t*t + 1) + b;};
	this.easeInOutCubic = function (t, b, c, d) {if ((t/=d/2) < 1) return c/2*t*t*t + b;return c/2*((t-=2)*t*t + 2) + b;};
	this.easeInQuart = function (t, b, c, d) {return c*(t/=d)*t*t*t + b;};
	this.easeOutQuart = function (t, b, c, d) {return -c * ((t=t/d-1)*t*t*t - 1) + b;};
	this.easeInOutQuart = function (t, b, c, d) {if ((t/=d/2) < 1) return c/2*t*t*t*t + b;return -c/2 * ((t-=2)*t*t*t - 2) + b;};
	this.easeInQuint = function (t, b, c, d) {return c*(t/=d)*t*t*t*t + b;};
	this.easeOutQuint = function (t, b, c, d) {return c*((t=t/d-1)*t*t*t*t + 1) + b;};
	this.easeInOutQuint = function (t, b, c, d) {if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;return c/2*((t-=2)*t*t*t*t + 2) + b;};
	this.easeInSine = function (t, b, c, d) {return -c * Math.cos(t/d * (Math.PI/2)) + c + b;};
	this.easeOutSine = function (t, b, c, d) {return c * Math.sin(t/d * (Math.PI/2)) + b;};
	this.easeInOutSine = function (t, b, c, d) {return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;};
	this.easeInExpo = function (t, b, c, d) {return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;};
	this.easeOutExpo = function (t, b, c, d) {return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;};
	this.easeInOutExpo = function (t, b, c, d) {if (t==0) return b;if (t==d) return b+c;if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;};
	this.easeInCirc = function (t, b, c, d) {return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;};
	this.easeOutCirc = function (t, b, c, d) {return c * Math.sqrt(1 - (t=t/d-1)*t) + b;};
	this.easeInOutCirc = function (t, b, c, d) {if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;};
	//a: amplitude (optional), p: period (optional)
	this.easeInElastic = function (t, b, c, d, a, p) {if (t==0) { return b; } if ((t/=d)==1) { return b+c; }if (!p) { p=d*.3; }if (a < Math.abs(c)) {  a=c; s=p/4; }else { a=Math.abs(c); s = p/(2*Math.PI) * Math.asin(c/a);}return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;};
	this.easeOutElastic = function (t, b, c, d, a, p) {if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;if (a < Math.abs(c)) { a=c; var s=p/4; }else {   a=Math.abs(c); var s = p/(2*Math.PI) * Math.asin (c/a);}return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;};
	this.easeInOutElastic = function (t, b, c, d, a, p) {if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);if (a < Math.abs(c)) { a=c; var s=p/4; }else {a=Math.abs(c);var s = p/(2*Math.PI) * Math.asin (c/a);}if (t < 1) {return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;}return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;};
	this.easeInBack = function (t, b, c, d, s) {if (s == undefined) s = 1.70158;return c*(t/=d)*t*((s+1)*t - s) + b;};
	this.easeOutBack = function (t, b, c, d, s) {if (s == undefined) s = 1.70158;return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;};
	this.easeInOutBack = function (t, b, c, d, s) {if (s == undefined) s = 1.70158; if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;};
	this.easeInBounce = function (t, b, c, d) {return c - easeOutBounce (d-t, 0, c, d) + b;};
	this.easeOutBounce = function (t, b, c, d) {if ((t/=d) < (1/2.75)) { return c*(7.5625*t*t) + b;} else if (t < (2/2.75)) { return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;} else if (t < (2.5/2.75)) { return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;}else { return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; }};
	this.easeInOutBounce = function (t, b, c, d) {if (t < d/2) return easeInBounce (t*2, 0, c, d) * .5 + b;return easeOutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;};
}
function darkenPage()
{
	if(blur = document.getElementById("darkenPage"))
	{
		document.body.removeChild(blur);
		return;
	}
	var screenAllX = '100%';
	
	if(window.innerHeight)
		heightAll = window.innerHeight;
	else
		heightAll = document.documentElement.clientHeight;
	
	if(document.body.scrollHeight>heightAll)
		var screenAllY = document.body.scrollHeight+"px";
	else
		var screenAllY = heightAll+"px";
		
	var blur = document.createElement("div");
	blur.style.position='absolute';
	blur.id='darkenPage';
	blur.style.zIndex='1';
	blur.style.top='0px';
	blur.style.left='0px';
	blur.style.opacity='0.8';
	blur.style.filter='alpha(opacity=80)';
	blur.style.cursor='pointer';
	blur.style.background='#000000';
	blur.style.width=screenAllX;
	blur.style.height=screenAllY;
	document.body.appendChild(blur);
}