window.$=function(id)
{
	return document.getElementById(id);
}

function getTarget(e)
{
	return window.event ? window.event.srcElement : e.currentTarget;
}

function show(id)
{
	var el=id;
	if (typeof id=='string')	el=$(id);
	
	el.style.display='block';
}

function hide(id)
{
	var el=id;
	if (typeof id=='string')	el=$(id);
	
	el.style.display='none';
}

function getWindowHeight()
{
	return document.body.clientHeight?document.body.clientHeight:document.documentElement.clientHeight;
}

function getWindowWidth()
{
	return window.innerWidth?window.innerWidth:(document.body.clientWidth?document.body.clientWindow:document.documentElement.clientWidth);
}

var _preload=[];
function preloadImage(url)
{
	var pre=new Image();
	pre.src=url;
	
	_pre.push(pre);
}

function existsClass(id, clname)
{
	var el=id;
	if (typeof id=='string')	el=$(id);
	
	var cl=el.className;
	
	var reg=new RegExp('(^| )'+clname+'( |$)', 'gi');
	
	if (reg.test(cl))	return true;
	else	return false;	
}

function addClass(id, clname)
{
	var el=id;
	if (typeof id=='string')	el=$(id);
	
	if(!existsClass(el, clname))
	{
		var cl=el.className;
		var sp=' ';
		if (cl=='')	sp='';
		
		el.className+=sp+clname;
	}
}

function delClass(id, clname)
{
	var el=id;
	if (typeof id=='string')	el=$(id);
	
	if(existsClass(el, clname))
	{
		var cl=el.className;
		
		var reg=new RegExp('(^| )'+clname+'( |$)', 'gi');
		cl=cl.replace(reg, '$1');
		cl=cl.replace(/  /, ' ');
		if (cl==' ')	cl='';
		
		el.className=cl;
	}
}

function getTop(id)
{
	var el=id;
	if (typeof id=='string')	el=$(id);

	var il=0;

	while(el.tagName!='BODY' && el.tagName!='HTML')
	{
		il+=el.offsetTop;
		el=el.offsetParent;
	}
	il+=el.offsetTop;
	
	return il;
}

function getLeft(id)
{
	var el=id;
	if (typeof id=='string')	el=$(id);

	var il=0;

	while(el.tagName!='BODY' && el.tagName!='HTML')
	{
		il+=el.offsetLeft;
		el=el.offsetParent;
	}
	il+=el.offsetLeft;

	return il;
}

function setOpacity(id, value)
{
	var el=id;
	if (typeof id=='string')	el=$(id);
	
	el.style.filter='alpha(opacity='+value+')';
	el.style.opacity=value/100;
}

var _g=[];
var _glen=0;

function _gNextFrame(id)
{
	_g[id].nextFrame();

	return;
}

function Changer(id, attr, startValue, endValue, off)
{
	if (typeof id == 'string')
	{
		this.elementId=id;
		this.element=$(id);
	}
	else
	{
		this.element=id;
		this.elementId=null;
	}
	
	if (!this.element)	return false;
	
	this.id=_g.length;
	this.attr=attr;

	this.startValue=startValue;
	this.endValue=endValue;
	
	this.value=this.startValue;
	
	_glen++;
	_g.push(this);

	if (startValue>endValue)	this.inc=false;
	else	this.inc=true;
	
	this.off=off;
	
	if (this.inc)
	{
		if (this.startValue==null)	this.value=0;
		if (this.endValue==null)	this.endValue=100;

		this.setValue();
		show(this.element);
	}
	else
	{
		if (this.startValue==null)	this.value=100;
		if (this.endValue==null)	this.endValue=0;

		this.setValue();
		show(this.element);

	}

	return true;
}

Changer.prototype.id=null;
Changer.prototype.element=null;
Changer.prototype.timerId=null;
Changer.prototype.timer=10;
Changer.prototype.value=0;
Changer.prototype.v=1;
Changer.prototype.a=0;
Changer.prototype.endValue=null;
Changer.prototype.startValue=null;
Changer.prototype.onEnd=null;
Changer.prototype.unit='px';
Changer.prototype.listener=null;
Changer.prototype.pauseEnd=0;
Changer.prototype.isPauseEnd=false;
Changer.prototype.autoKill=true;

Changer.prototype.start=function()
{	
	if (!this.element)	return false;
		//alert(this.value);
	
	if (this.startValue>this.endValue)	this.inc=false;
	else	this.inc=true;
	
	this.isPauseEnd=false;
	
	if (this.timerId)	clearTimeout(this.timerId);
	this.timeId=null;
	
	this.nextFrame();
	
	return true;
}

Changer.prototype.setValue=function()
{
	var style=this.element.style;
	var gvalue=''+this.value+this.unit;

	switch(this.attr)
	{
		case 'opacity':
		{
			style.filter='alpha(opacity='+this.value+')';
			style.opacity=(this.value/100);
		}
		break;
		case 'top':
			style.top=gvalue;
		break;
		case 'left':
			style.left=gvalue;
		break;
		case 'right':
			style.right=gvalue;
		break;
		case 'bottom':
			style.bottom=gvalue;
		break;
		case 'width':
			style.width=gvalue;
		break;
		case 'height':
			style.height=gvalue;
		break;
		case 'margin-left':
			style.marginLeft=gvalue;
		break;
		case 'margin-top':
			style.marginTop=gvalue;
		break;
		case 'margin-right':
			style.marginRight=gvalue;
		break;
		case 'margin-bottom':
			style.marginBottom=gvalue;
		break;
		case 'margin':
			style.margin=gvalue;
		break;
		case 'padding-left':
			style.paddingLeft=gvalue;
		break;
		case 'padding-top':
			style.paddingTop=gvalue;
		break;
		case 'padding-right':
			style.paddingRight=gvalue;
		break;
		case 'padding-bottom':
			style.paddingBottom=gvalue;
		break;
		case 'padding':
			style.padding=gvalue;
		break;
		case 'z-index':
			style.zIndex=gvalue;
		break;
		case 'font-size':
			style.fontSize=gvalue;
		break;		
		case 'border-width':
			style.borderWidth=gvalue;
		break;		
	}
}

Changer.prototype.nextFrame=function()
{
	if (!this.isPauseEnd)
	{
		this.v+=this.a;
		if (this.inc)	this.value+=this.v;
		else	this.value-=this.v;
		if (this.inc && this.value>this.endValue)	this.value=this.endValue;
		if (!this.inc && this.value<this.endValue)	this.value=this.endValue;
		this.setValue();
		if (this.listener!=null)	this.listener();
	}
	
	if ((this.inc && this.value==this.endValue) || (!this.inc && this.value==this.endValue))
	{
		if (!this.isPauseEnd)
		{
			if (this.pauseEnd)
			{
				this.isPauseEnd=true;
				this.timerId=setTimeout('_gNextFrame('+this.id+');', this.pauseEnd);
			
				return;
			}
		}
		
		this.isPauseEnd=true;
		
		if (this.isPauseEnd)
		{
			if (this.autoKill)
			{
				this.id=null;
				_glen--;
		
				if (_glen<=0)
				{
					_glen=0;
					_g=[];
				}
			}
			
			if (this.off)	hide(this.element);
			if (this.onEnd)	this.onEnd();			
		}
		
		return;
	}
	else	this.timerId=setTimeout('_gNextFrame('+this.id+');', this.timer);
}
