function $(x)
{
	return document.getElementById(x);
}


/* - - - */

Math.easeInSine = function (t, b, c, d) {
	return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
};

Math.easeOutSine = function (t, b, c, d) {
	return c * Math.sin(t/d * (Math.PI/2)) + b;
};


/*- - -*/
function Tween(elemTarget, sStyleProperty, oEaseType, iStartValue, iEndValue, iDuration, addition){
	this.target=elemTarget;
	this.time=0;
	this.duration=iDuration;
	this.position=iStartValue;
	this.finish=parseFloat(iEndValue);

	if (!elemTarget.style)
		elemTarget.style = '';
	this._style=elemTarget.style;
	this._prop=sStyleProperty;
	this._ease=oEaseType;
	this._start=parseFloat(iStartValue);
	this._change=this.finish - this._start;
	this._animate=false;
	this._addition = addition ? addition : false;
	
	this.start();
}

Tween.aStack=[];
Tween._idPoll=null;
Tween._poll=function(){
	for(var i=0; i<this.aStack.length; i++){
		this.aStack[i]._onMotion();
	}
}

Tween._add=function(obj){
	this.aStack[this.aStack.length]=obj;
}

Tween._remove=function(obj){
	for(var i=0; i < this.aStack.length; i++){
		if(this.aStack[i] == obj){
			this.aStack.splice(i,1);
			break;
		}
	}
}

Tween._startPoll=function(){
	this._idPoll=setInterval('Tween._poll()', 30);
}

Tween._stopPoll=function(){
	clearInterval(this._idPoll);
	this._idPoll=null;
}

Tween._inStack=function(obj){
	for(var i=0; i<this.aStack.length; i++){
		if(this.aStack[i] == obj)
			return true;
	}
	return false;
}

Tween.prototype._onMotion=function(){
	if(this._animate){
		if(this.time < this.duration){
			this.time++;
			this.position=this._ease(this.time, this._start, this._change, this.duration, 50, 1.06);
			if(this._prop && this._style)
			{
				this._style[this._prop]=this.position + (this._addition ? this._addition : '');
			}
			this.onMotionChanged();
		}
		else{
			this.onMotionFinished();
			this.stop();
		}
	}
}

Tween.prototype.onMotionChanged=function(){
	return;
}

Tween.prototype.onMotionFinished=function(){
	return;
}

Tween.prototype.start=function(){
	if(!this._animate){
		this._animate=true;
		this.time=0;
		if(!this.constructor._inStack(this)){
			this.constructor._add(this);
		}
	}
	if(!this.constructor._idPoll)
		this.constructor._startPoll();
}

Tween.prototype.stop=function(){
	this._animate=false;
	this.constructor._remove(this);
	
	if(!this.constructor.aStack.length)
		this.constructor._stopPoll();
}

Tween.prototype.resume=function(){
	this._animate=true;
	if(!this.constructor._inStack(this))
		this.constructor._add(this);
}


/*- - -*/
















		
function toggleRoadWayText (tId)
{
	if (!$('roadSwitchers') || !$('roadTexts'))
		return false;
		
	var aSwitchers = $('roadSwitchers').getElementsByTagName('span');
	var aTexts = $('roadTexts').getElementsByTagName('p');
	
	var re = null;
	for (var i = 0; i < aSwitchers.length; i++)
	{
		re = new RegExp('^' + tId);
		if (matchClass(aSwitchers[i],'switcher'))
		{
			if (aSwitchers[i].id.match(re))
				addClass(aSwitchers[i],'selected');
			else
				removeClass(aSwitchers[i],'selected');
		}
		if (matchClass(aTexts[i],'roadWayText'))
		{
			if (aTexts[i].id.match(re))
				addClass(aTexts[i],'selected');
			else
				removeClass(aTexts[i],'selected');
		}
	}
}


var isRoadWayShown = false;

var roadWayTween = null;
function toggleRoadWay(preSet)
{

	if (roadWayTween || openADTween)
		return false;
	
	if (preSet === 0)
		isRoadWayShown = false;
	if (preSet === 1)
		isRoadWayShown = true;
	
	if (isopenADShown)
	{
		openADlocation();
	}
	
	if (!isRoadWayShown)
	{
		roadWayTween = new Tween($('ContactsContainer'),'left',Math.easeInSine,'-50','50',10,'%');
	}
	else
	{
		roadWayTween = new Tween($('ContactsContainer'),'left',Math.easeOutSine,'50','-100',10,'%');
	}
	roadWayTween.onMotionFinished = function ()
	{
		isRoadWayShown = !isRoadWayShown;
		roadWayTween = null;
	}
		
}

var isopenADShown = false;
var openADTween = null;
function openADlocation(preSet)
{

	if (openADTween || roadWayTween)
		return false;
	
	if (preSet === 0)
		isopenADShown = false;
	if (preSet === 1)
		isopenADShown = true;
	
	if (isRoadWayShown)
	{
		toggleRoadWay();
	}
	
	if (!isopenADShown)
	{
		$('openADContainer').style.display = 'block';
		openADTween = new Tween($('openAD'),'left',Math.easeInSine,'150','50',10,'%');
	}
	else
	{
		
		openADTween = new Tween($('openAD'),'left',Math.easeOutSine,'50','150',10,'%');
	}
	openADTween.onMotionFinished = function ()
	{
		isopenADShown = !isopenADShown;
		if (!isopenADShown)
		{
			$('openADContainer').style.display = 'none';
		}
		openADTween = null;
	}
		
}







