
/* this is the class */	    

if(typeof (AC)=="undefined"){AC={};}

AC.drawerClass = Class.create();

Object.extend(AC.drawerClass.prototype,{containerHeight:515,handlerHeight:26,init:function(container,drawerHeight1){
		if(drawerHeight1>0){this.containerHeight=drawerHeight1;}
		this.myContainer = $(container);
		this.myDrawers = $$("#"+container+" .drawers>li");
		this.drawerCount = this.myDrawers.length;
		for (i=0; i<this.drawerCount; i++){
			this.myDrawers[i].style.top = i * this.handlerHeight + "px";
			this.myDrawers[i].style.display = "block";
			this.myDrawers[i].myY = i * this.handlerHeight ;
			if (i==0){
				this.myDrawers[i].myClassName = "first";
			} else if (i == this.drawerCount-1){
				this.myDrawers[i].myClassName = "last";
			} else {
				this.myDrawers[i].myClassName = "";
			}
			this.myDrawers[i].className = this.myDrawers[i].myClassName + "closed" ;
		}
		this.myDrawers[this.drawerCount-1].className = this.myDrawers[this.drawerCount-1].myClassName ;
		this.currentOpen = this.drawerCount-1;
		
		this.myDrawerHeads = $$("#"+container+" .drawers .drawer_handle");
		for (i=0; i<this.myDrawerHeads.length; i++){
			this.myDrawerHeads[i].myId = i;
			this.myDrawerHeads[i].myClass = this;
		}

		this.myDrawerHeads.each(function(c){
			Event.observe(c,'mouseup',function(e){
				c.myClass.openClose(c.myId);
			});
		});
		
		this.myDrawerContent = $$("#"+container+" .drawers .drawer_content");
		this.contentHeight = this.containerHeight - (this.drawerCount*this.handlerHeight);
		
		for (i=0; i<this.myDrawerContent.length; i++){
			this.myDrawerContent[i].style.height = this.contentHeight + "px";
		}
		
		for (i=0; i<this.drawerCount; i++){
			this.myDrawers[i].style.height = (this.contentHeight + this.handlerHeight) + "px";
		}
	},
	openClose:function(drawerId){
		if (drawerId != this.currentOpen){
			this.currentOpen = drawerId;
			window.clearInterval(this.animationId);
			for (i=0; i<this.drawerCount; i++){
				if (i<=drawerId){
					this.myDrawers[i].newY = i * this.handlerHeight;
				} else {
					this.myDrawers[i].newY = i * this.handlerHeight + this.contentHeight;
				}
			}
			
			this.myDrawers[drawerId].className = this.myDrawers[drawerId].myClassName ;
			

			window.drawerClass = this;
			this.animationId=window.setInterval(function(){
				var movingCount = 0;
				for (i=0; i<this.drawerClass.drawerCount; i++){
					if (this.drawerClass.myDrawers[i].newY != this.drawerClass.myDrawers[i].myY){
						movingCount++;
						this.drawerClass.myDrawers[i].myY=this.drawerClass.calculateDecel(this.drawerClass.myDrawers[i].myY,this.drawerClass.myDrawers[i].newY);
						this.drawerClass.myDrawers[i].style.top = this.drawerClass.myDrawers[i].myY + "px";
					}
				}
				
				if (movingCount == 0){
					window.clearInterval(this.drawerClass.animationId);
					for (i=0; i<this.drawerClass.drawerCount; i++){
						if (i != this.drawerClass.currentOpen){
							this.drawerClass.myDrawers[i].className = this.drawerClass.myDrawers[i].myClassName+"closed";
						}
					}
				}
														 
			},30);
		}
	},
	calculateDecel:function(from,to){
		var n=from-Math.floor((from-to)*.4);
		if(Math.abs(from-to)<4)return to;
		else return n;
	}
});

