
(function(){

	/*
	 *
	 *      Copyright 2007 Azer Koçulu <http://azer.kodfabrik.com>
	 *		Fri Apr 27, 1:47 PM	
	 * 
	 *      This program is free software; you can redistribute it and/or modify
	 *      it under the terms of the GNU General Public License as published by
	 *      the Free Software Foundation; either version 2 of the License, or
	 *      (at your option) any later version.
	 *
	 *      This program is distributed in the hope that it will be useful,
	 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
	 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 *      GNU General Public License for more details.
	 *
	 *      You should have received a copy of the GNU General Public License
	 *      along with this program; if not, write to the Free Software
	 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
	 */

window.lib = {
	n: { ie:/MSIE/i.test(navigator.userAgent), ie6:/MSIE 6/i.test(navigator.userAgent), ie7:/MSIE 7/i.test(navigator.userAgent), op:/Opera/i.test(navigator.userAgent), ff:/firefox/i.test(navigator.userAgent) },
	e: function(att,val,tag)
	{
		return ((att=="id"?lib.p(document.getElementById(val)):(typeof att!="string" && typeof val!="string" && typeof tag=="string"?document.getElementsByTagName(tag):function(){
			
			var data = [];
			var tag = document.getElementsByTagName(typeof tag=="string"?tag:"*");
			for(x in tag)
					if(((parseInt(x))>=0 || lib.n.ie) && typeof tag[x]=='object' && tag[x].getAttribute(att) && (typeof val!="string" || tag[x].getAttribute(att)==val))
						data.push(tag[x]);
			return data;
				
		}())));
	},
	p: function(node)
	{
		node.make = function(tag,body,html)
		{
			var element = document.createElement(typeof tag=="string"?tag.split("::")[0]:"div");
			if(typeof body=="object")
			for(y in body)
				for(x in body[y])
					if(y=="css")
						element.style[(x=="cssFloat" && lib.n.ie?"styleFloat":x)] = body[y][x];
					else if(y=="att" || y=="on")
						element[y=="att"?"setAttribute":(lib.n.ie?"attachEvent":"addEventListener")](y=="att"?(x=="class" && lib.n.ie?"className":x):((lib.n.ie?"on":"")+x),body[y][x],false);
			
			try { element.innerHTML = typeof tag=="string" && tag.split("::").length>1?tag.split("::")[1]:(typeof html=="undefined"?"":html);}catch (e){}
			(node==document?document.body:(node==window?document.body:node)).appendChild(element);
			return lib.p(element);
		};
			
		node.css = function(property)
		{
			return (lib.n.ie==0?document.defaultView.getComputedStyle(node, null):node.currentStyle)[property];
		};
			
		node.on = function(ev,fu)
		{
			
			return node[lib.n.ie?"attachEvent":"addEventListener"]((lib.n.ie?"on":"")+ev,fu,1);
		}

		node.att = function(att,val,remove)
		{
			var ieFix = { "class":"className" };
			
			if(typeof val!="undefined" && typeof remove=="undefined") node.setAttribute(lib.n.ie?ieFix[att]:att,att=="class"?node.att(att)+' '+val:val);
			else if(typeof val!="undefined" && typeof remove!="undefined") node.setAttribute(lib.n.ie?ieFix[att]:att,node.att(att).replace(new RegExp(val,"gi"),""));
			
			var val = node.getAttribute(lib.n.ie?ieFix[att]:att);

			return val==null?"":val;
		}
			
		return node;
	},
	i: function(elm)
	{
		var dim = [0,0];
		while(elm && document.firstChild && elm.nodeName!='BODY')
		{
			dim = [dim[0]+elm.offsetLeft,dim[1]+elm.offsetTop];
			elm = elm.offsetParent;
		}
		return { x:dim[0], y:dim[1] };
	}
};

/* extras */


window.o2s = function(obj,html,depth)
{
	// converts objects to string
	depth = typeof depth=="undefined"?0:depth;
	var ch = typeof html=="undefined"?["","",""]:(html==1?["<br />","&nbsp;&nbsp;&nbsp;&nbsp;","&nbsp;&nbsp;&nbsp;&nbsp;"]:["\n","\t","\t"]);
	if(typeof html!="undefined")
	for(var i=0; i<depth; i++)ch[1]+=ch[1];
	var str = "{ ";
	for(x in obj)	
		str+=ch[0]+ch[1]+x+":"+(typeof obj[x]!="object"?(typeof obj[x]!="function"?obj[x]:"[Function]"):window.o2s(obj[x],html,depth+1))+", ";
	return str.substring(0,str.length-2)+ch[0]+ch[1].substring(0,ch[1].length-ch[2].length)+" }"
}

Date.prototype.get = function(){
	return { hour:{ am:this.getHours(), pm:Math.abs(12-this.getHours()) }, minute:this.getMinutes(), day:{ "ofWeek":this.getDay(), "ofMonth":this.getDate() }, week:{ firstDay:this.getDate()-this.getDay()-1, lastDay:this.getDate()-this.getDay()+6 }, month:{ "ofYear":this.getMonth(), "length":(this.getMonth()==1?(this.getFullYear()%4==0?29:28):(this.getMonth()<7?(this.getMonth()%2==0?31:30):(this.getMonth()%2==0?30:31))) }, year:this.getFullYear() }
}

window.runCss = function(query)
{
	if(typeof window.$__style!="undefined")window.$__style = document.make("style");
	if(lib.n.ie)$__style.styleSheet.addRule(query.split(/{|}/)[0],query.split(/{|}/)[1]);
	else $__style.sheet.insertRule(query,$__style.sheet.cssRules.length);
}

if(lib.n.ff==1)
{	
	Event.prototype.__defineGetter__('offsetX',function(){ return window.pageXOffset + this.pageX - lib.i(this.target).x; });
	Event.prototype.__defineGetter__('offsetY',function(){ return window.pageYOffset + this.pageY - lib.i(this.target).y; });
	Event.prototype.__defineGetter__('srcElement',function(){ return this.target; })
}


window.xhr = function(u,m,oC)
{
	var obj = {
		req: new XMLHttpRequest(),
		a: { url:u, method:typeof m!='undefined'?m:"GET" },
		init: function()
		{
			with(obj)
			{
				req.open(a.method,a.url,1);
				req.onreadystatechange = listen;
				req.send(null);
			}
		},
		listen: function()
		{
			with(obj){ if(req.readyState==4 && req.status==200)oc(); }
		}
	};
	return obj;
}


window.animate = function(ev,prd,stp,spd)
{
	var obj = {
		a: { event:ev, step:typeof stp!="undefined"?stp:1, iterator:0, interval:null, speed:typeof spd!="undefined"?spd:30, period:typeof prd!="undefined"?prd:10, errorLog:[] },
		init: function()
		{
			with(obj)
			{
				a.interval = setInterval(doo.run,a.speed);
				doo.event("load");
			}
		},
		doo: {
			event: function(key)
			{
				with(obj)
				{
					if(typeof a.event[key]=="function")
					try { a.event[key](a); }
					catch (e)
					{
						a.errorLog.push("External error:"+e);
					}
				}
			},
			run: function()
			{
				with(obj)
				{
					doo.event("run");
					a.iterator+=a.step;
					if(a.iterator<a.period)return;
					doo.event("complete");
					doo.kill();
				}
			},
			kill: function()
			{
				with(obj)
				{
					clearInterval(a.interval);
				}
			}
		}
	}
	return obj;
}

window.fader = function(elm,dir)
{
	var obj = {
	
		a:{ animation:null, cursor:0, elm:elm, direction:dir==-1?-1:1 },
		init: function()
		{
			with(obj)
			{
				a.period = 10;
				
				if(typeof elm.fader!="undefined")
				{
				
					a.period = a.direction==1?(a.period-elm.fader.a.cursor):(elm.fader.a.cursor);
					a.cursor = elm.fader.a.cursor;
					elm.fader.a.animation.doo.kill();
				}
				
				if(a.period==0)return;
				a.cursor = a.direction==-1?a.period:a.cursor;
				
				a.animation = new animate({ run:doo.fx },a.period,1,15);
				a.animation.a.event[a.direction==1?"load":"complete"] = doo.display;
				a.animation.init();
				
				elm.fader = obj;
			}
		},
		doo: {
			fx: function(fact)
			{
				with(obj)
				{
					
					if(a.direction==1)
					{
						a.cursor+=1;
						elm.style.opacity = ((a.cursor)<10?"0.":"")+(a.cursor);
						elm.style.filter = "Alpha(Opacity="+((a.cursor+1)*10)+")"
						return;
					}
					
					a.cursor-=1;
					elm.style.opacity = (a.period-fact.iterator<10?"0.":"")+(a.period-(fact.iterator));
					elm.style.filter = "Alpha(Opacity="+((a.period-(fact.iterator))*10)+")"
					
				}
			},
			display: function()
			{
				with(obj)
				{
					elm.style.display = a.direction==1?"block":"none";
				}
			}
		}
		
	}
	
	obj.init();
	return obj;
}


window.sizer = function(e,s,st,sp,oc)
{

	var obj = {
		a: { elm:e, onComplete:typeof oc=="function"?oc:0, step:typeof st=="number"?st:20, speed:typeof sp=="number"?sp:10, height:typeof s.height!="undefined"?[e.offsetHeight,s.height,1,1]:[0,0,0,0], width:typeof s.width!="undefined"?[e.offsetWidth,s.width,1,1]:[0,0,0,0], margin:typeof s.margin=="object"?s.margin:{ height:0, width:0 }, period:0 },
		init: function()
		{
			with(obj)
			{
				if(typeof a.elm.sizer!="undefined")
					a.elm.sizer.a.animation.doo.kill();
				
				
				a.height[0] -= a.margin.height;
				a.width[0] -= a.margin.width;
				
				
				var p = [Math.abs(a.height[0]-a.height[1]),Math.abs(a.width[0]-a.width[1])];
				a.period =p[0]<p[1]?p[1]:p[0];

				a.animation = new animate({ run:doo.fx, complete:doo.complete },a.period,a.step,a.speed);
				a.animation.init();

			}
		},
		doo: {
			fx: function(fact)
			{
				with(obj)
				{
					
					if(a.height[0]<a.height[1] && a.height[3])
					{
						a.elm.style.height = (a.height[0]+fact.iterator)+"px"
						if(a.height[0]+fact.iterator>=a.height[1])
						{
							a.elm.style.height = a.height[1]+"px";
							a.height[3]=0;
						}
						
					}
					else if(a.height[3])
						a.elm.style.height = (a.height[0]-fact.iterator)+"px";

					if(a.width[0]<a.width[1] && a.width[3])
					{
						a.elm.style.width = (a.width[0]+fact.iterator)+"px"
						if(a.width[0]+fact.iterator>=a.width[1])
						{
							a.elm.style.width = a.width[1]+"px";
							a.width[3]=0;
						}
						
					}
					else if(a.width[3])
						a.elm.style.width = (a.width[0]-fact.iterator)+"px";
					
				}
			},
			complete: function()
			{
				with(obj)
				{
					if(a.width[2]==1)a.elm.style.width = a.width[1]+"px";
					if(a.height[2]==1)a.elm.style.height = a.height[1]+"px";
				
					if(a.onComplete!=0)a.onComplete();
				}
			}
		}
	};
	obj.init();
	e.sizer = obj;
	return obj;
}

lib.p(document);
lib.p(window);

})();
/* */
