/* 
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, iPage){
	this.target=elemTarget;
	this.time=0;
	this.duration=iDuration;
	this.position=iStartValue;
	
	if(window.innerWidth){
    width = window.innerWidth;
  }   else if(document.documentElement && document.documentElement.clientWidth){
    width = document.documentElement.clientWidth;
  }   else if(document.body && document.body.clientWidth){
    width = document.body.clientWidth;
  }
  	qw = 1009;
qw5 = 100900/width;
qw2 = (width - 961)/2;
qw3 = ((qw-qw2)*100)/width;
qw9=1009-qw2;
qw4 = qw5 - qw3;
u2=-qw*(iPage-1)+qw2;
//alert(w9);
  w1 = (width*33.2541567695100999524940617577197)/100;
  w2 = (width*11.876484560570071258907363420428)/100;
	u = ((iPage-1)*w1)-((iPage-2)*w2);
//alert(u2);
	//alert(iEndValue);
	this.finish=parseFloat(u2);
	//alert (this.finish);
	/* 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 && 0) // выключаем старый скрипт, т.к. он перестал работать в новых браузерах (7.11.11) вместо него теперь jquery.animate
				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 =9;
var iCurrentPage = 1;
var elemContainer, elemPageConatiner;
var oTween;

function NextPage(){
	GoToPage(iCurrentPage+1);
}

function PrevPage(){
	GoToPage(iCurrentPage-1);
}


function GoToPage(iPage){
    width = document.documentElement.clientWidth;
	w = 1009;
w5 = 100900/width;
w2 = (width - 961)/2;
w3 = ((w-w2)*100)/width;
w4 = w5 - w3;
	m=document.getElementById('m2');
	m.className = '';
	m=document.getElementById('m3');
	m.className = '';
	m=document.getElementById('m4');
	m.className = '';
	m=document.getElementById('m5');
	m.className = '';
	m=document.getElementById('m6');
	m.className = '';
	m=document.getElementById('m7');
	m.className = '';
	m=document.getElementById('m8');
	m.className = '';
	m=document.getElementById('m9');
	m.className = '';
	
	p=document.getElementById('p1');
	p.src = '/images/m2.jpg';
	p=document.getElementById('p2');
	p.src = '/images/m3.jpg';
	p=document.getElementById('p3');
	p.src = '/images/m4.jpg';
	p=document.getElementById('p4');
	p.src = '/images/m5.jpg';
	p=document.getElementById('p5');
	p.src = '/images/m6.jpg';
	p=document.getElementById('p6');
	p.src = '/images/m7.jpg';
	p=document.getElementById('p7');
	p.src = '/images/m8.jpg';
	p=document.getElementById('p8');
	p.src = '/images/m9.jpg';
	m=document.getElementById('m' + iPage);
	pr = iPage-1;
	p=document.getElementById('p' + pr);
	m.className = 'act' + iPage;
	p.src = '/images/m' + iPage + '.png';
	if(iPage > 0 && iPage <= iTotalPages && iCurrentPage != iPage)
  {
		if(oTween) oTween.stop();
		
		oTween=new Tween(elemPageConatiner, 'left', Math.easeInOutCirc, elemPageConatiner.offsetLeft, -elemContainer.offsetWidth*(iPage-1), 30, iPage);
		oTween.prc=-w5*(iPage-1)+w4;

		oTween.onMotionFinished=function(){
			//this.target.style.left=this.prc+'%';
		}
    $(elemPageConatiner).animate({'left':oTween.prc+'%'},'slow');
		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();
	}
}



