var iTotalPages=3;

var oImgSmallSize={w:184, h:38};
var oImgMedSize={w:209, h:44};
var oImgLargeSize={w:340, h:69};
var oMovePoint={x:57, y:90};

function EventSplitter(evt){
	if((evt=checkEvent(evt))){
		var oElem=evt.target;
		do{
			if(oElem.id)
				break;
		}while((oElem=oElem.parentNode))
		
		var _item=oAnS.Get(oElem.id);
		switch(evt.type){
			case 'mouseover':
				_item.MouseOver();
				break;
			case 'mouseout':
				_item.MouseOut();
				break;
			case 'click':
				_item.Click();
				break;
		}
	}
}

/* ************************************ */

function AnimationStack(){
	this.aStack={};
}

AnimationStack.prototype.sFunc='onAnimate';
AnimationStack.prototype.iIx=0;
AnimationStack.prototype.iFPS=30;

AnimationStack.prototype.Poll=function(){
	for(var a in this.aStack){
		if(this.aStack[a].onAnimate)
			this.aStack[a].onAnimate();
	}
}

AnimationStack.prototype.Add=function(oParam){
	var oObj;
	if(typeof(oParam) == 'string'){
		if(oObj = new AnimationItem(oParam)){
			this.aStack[oParam]=oObj;
			return this.aStack[oParam];
		}
	}
	else if(oParam){
		this.aStack[oParam.ID]=oParam;
		return this.aStack[oParam.ID];
	}
	
	return false;
}

AnimationStack.prototype.Get=function(sID){
	return (this.aStack[sID]) ? this.aStack[sID] : null;
}

AnimationStack.prototype.Iterate=function(){
	var i=0;
	for(var a in this.aStack){
		if(i == this.iIx){
			this.iIx++;
			return this.aStack[a];
		}
		i++;
	}
	this.iIx=0;
	return null;
}

/* ************************************ */

var MenuImage=function(sSmall, sMed, sLarge){
	this.img_small=new Image();
	this.img_small.src=sSmall;
	
	this.img_med=new Image();
	this.img_med.src=sMed;
	
	this.img_large=new Image();
	this.img_large.src=sLarge;
}

MenuImage.prototype.Small=function(){
	return this.img_small.src;
}

MenuImage.prototype.Medium=function(){
	return this.img_med.src;
}

MenuImage.prototype.Large=function(){
	return this.img_large.src;
}

/* ************************************ */

var MenuItem=function(sID, iPageNum, oImages){
	this.ID=sID;
	this.oPtr=document.getElementById(sID);
	this.oImgPtr=this.oPtr.getElementsByTagName('img')[0];
	this.oImages=oImages;
	this.iInitX=parseInt(this.oPtr.style.left);
	this.iPage=iPageNum;
	
	this.oSmallSize=oImgSmallSize;
	this.oMedSize=oImgMedSize;
	this.oLargeSize=oImgLargeSize;
	
	this.bAnimStarted=false;
	this.bBlocked=false;
	this.iEndCounter=0;
	this.onAnimationOver=this.onAnimationOverDefault;
	
	addEvent(this.oPtr, 'mouseover', EventSplitter);
	addEvent(this.oPtr, 'mouseout', EventSplitter);
	addEvent(this.oPtr, 'click', EventSplitter);
}

MenuItem.prototype.valueOf=function(){
	return this.ID;
}


MenuItem.prototype.MoveTo=function(x, y){
	this.oPtr.style.left=x+'%';
	this.oPtr.style.top=y+'px';
}

MenuItem.prototype.InitEaseIn=function(oPnt, f){
	this.tX=oPnt.x;
	this.tY=oPnt.y;
	this.f=f;
	this.cX=parseInt(this.oPtr.style.left);
	this.cY=parseInt(this.oPtr.style.top);
	this.onAnimate=this.EaseIn;
}

MenuItem.prototype.EaseIn=function(){
	this.cX=(this.tX-this.cX)*this.f + this.cX;
	this.cY=(this.tY-this.cY)*this.f + this.cY;
	
	if(Math.abs(this.tY-this.cY) < 1 && Math.abs(this.tX-this.cX) < 1){
		this.cX=this.tX;
		this.cY=this.tY;
		this.onAnimationOver();
	}
	
	this.MoveTo(this.cX, this.cY);
}

MenuItem.prototype.InitResize=function(oSPnt, oTPnt, iDecay){
	this.sW=oSPnt.w;
	this.sH=oSPnt.h;
	this.tW=oTPnt.w;
	this.tH=oTPnt.h;
	this.d=iDecay;
	
	this.oSnapInit={w: Math.min(oSPnt.w, oTPnt.w), h: Math.min(oSPnt.h, oTPnt.h)}
	
	this.oSnapPoint={x: this.sW/2, y: this.sH/2};
	this.onAnimate=this.Resize;
}

MenuItem.prototype.Resize=function(){
	this.sW+=(this.tW-this.sW)*this.d;
	this.sH+=(this.tH-this.sH)*this.d;
	
	if(Math.abs(this.tH-this.sH) < 2 && Math.abs(this.tW-this.sW) < 2){
		this.sW=this.tW;
		this.sH=this.tH;
		this.onAnimationOver();
	}
	
	this.SizeTo(this.sW, this.sH);
	this.Snap();
}

MenuItem.prototype.Snap=function(){
	var dx=this.oSnapPoint.x*(1 - this.sW/this.oSnapInit.w);
	var dy=this.oSnapPoint.y*(1 - this.sH/this.oSnapInit.h);
	
	this.oImgPtr.style.left=dx+'px';
	this.oImgPtr.style.top=dy+'px';
}

MenuItem.prototype.SizeTo=function(w, h){
	this.oImgPtr.style.width=w+'px';
	this.oImgPtr.style.height=h+'px';
}

MenuItem.prototype.MoveResized=function(){
	this.Resize();
	this.EaseIn();
}

MenuItem.prototype.MouseOver=function(evt){
	if(!this.CanStartAnim())
		return;
	this.oImgPtr.src=this.oImages.Medium();
	this.InitResize(this.oSmallSize, this.oMedSize, .4);
	this.onAnimationOver=this.onAnimationOverDefault;
}

MenuItem.prototype.MouseOut=function(evt){
	if(!this.CanStartAnim())
		return;
	this.oImgPtr.src=this.oImages.Small();
	this.InitResize(this.oMedSize, this.oSmallSize, .4);
	this.onAnimationOver=this.onAnimationOverDefault;
}

MenuItem.prototype.Click=function(evt){
	if(this.bBlocked)
		return;
	this.StartAnim();
	var _a;
	while((_a=oAnS.Iterate())){
		if(_a != this && _a.bBlocked)
			_a.Restore();
	}
	this.bBlocked=true;
	this.oImgPtr.src=this.oImages.Large();
	this.InitEaseIn(oMovePoint, .4);
	this.InitResize(this.oMedSize, this.oLargeSize, .4);
	this.onAnimate=this.MoveResized;
	this.onAnimationOver=function(){
		this.onAnimationOverStacked();
		this.ShowPage();
	};
}

MenuItem.prototype.Restore=function(){
	this.bBlocked=false;
	this.oImgPtr.src=this.oImages.Small();
	this.InitEaseIn({x:this.iInitX, y:0}, .3);
	this.InitResize(this.oLargeSize, this.oSmallSize, .4);
	this.onAnimate=this.MoveResized;
	this.onAnimationOver=this.onAnimationOverStacked;
}

MenuItem.prototype.ShowPage=function(){
	for(var i=1; i<=iTotalPages; i++){
		document.getElementById('page'+i).style.display=(i == this.iPage) ? 'block' : 'none';
	}
}

MenuItem.prototype.onAnimationOverDefault=function(){
	delete this.onAnimate;
	this.StopAnim();
}

MenuItem.prototype.onAnimationOverStacked=function(){
	this.iEndCounter++;
	if(this.iEndCounter == 2){
		this.iEndCounter=0;
		delete this.onAnimate;
		this.StopAnim();
	}
}

MenuItem.prototype.StartAnim=function(){
	this.bAnimStarted=true;
}

MenuItem.prototype.StopAnim=function(){
	this.bAnimStarted=false;
}

MenuItem.prototype.CanStartAnim=function(){
	return (!this.bAnimStarted && !this.bBlocked);
}
