/* 
Quick port of Adobe Flash Tween class
Requires easing_equations.js

Author: Sergey Chikuyonok (gonarch@design.ru)
17.09.2006
*/ 

function Tween(elemTarget, sStyleProperty, oEaseType, iStartValue, iEndValue, iDuration){
	this.target=elemTarget;
	this.time=0;
	this.duration=iDuration;
	this.position=iStartValue;
	this.finish=parseFloat(iEndValue);
	
	/* private properties */
	this._style=elemTarget.style;
	this._prop=sStyleProperty;
	this._ease=oEaseType;
	this._start=parseFloat(iStartValue);
	this._change=this.finish - this._start;
	this._animate=false;
	
	this.start();
}

Tween.aStack=[];
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._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._prop]=this.position;
			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);
		}
	}
}

Tween.prototype.stop=function(){
	this._animate=false;
	this.constructor._remove(this);
}

Tween.prototype.resume=function(){
	this._animate=true;
	if(!this.constructor._inStack(this))
		this.constructor._add(this);
}

setInterval('Tween._poll()', 22);

/**
 * @author gonarch
 */
var iTotalPages =4;
var iCurrentPage = 3;
var elemContainer, elemPageConatiner;
var oTween;

function NextPage(){
	GoToPage(iCurrentPage+1);
}

function PrevPage(){
	GoToPage(iCurrentPage-1);
}


function GoToPage(iPage){
	if(iPage > 0 && iPage <= iTotalPages && iCurrentPage != iPage){
		if(oTween) oTween.stop();
		
		oTween=new Tween(elemPageConatiner, 'left', Math.easeOutElastic, elemPageConatiner.offsetLeft, -elemContainer.offsetWidth*(iPage-1), 30);
		oTween.prc=-100*(iPage-1);
		oTween.onMotionFinished=function(){
			this.target.style.left=this.prc+'%';

		}
		iCurrentPage=iPage;
	if(iCurrentPage<=iTotalPages){
		for(var i=1; i<=iTotalPages; i++){
			var pNav = document.getElementById('p'+i);
			pNav.className = (i!=iCurrentPage) ? 'link' : 'select';
		}
	}

	}
}

function AllowMouseScroll(){
	return (document.body.clientHeight == elemContainer.offsetHeight);
}


function MouseWheelCallback(evt){
	if((evt=checkEvent(evt)) && AllowMouseScroll()){
		if(evt.mouse_wheel_delta > 0)
			PrevPage();
		else
			NextPage();
	}
}
