var ScrollMenu = Class.create();
ScrollMenu.prototype={
	initialize:function(width,displayCount,speed,maxWidth)
	{
		this.menu = $('photoList');
		this.btnPrev = $('btnPrev');
		this.btnNext = $('btnNext');
		this.items = this.menu.childElements();
		this.itemsCount = this.items.length;
		this.normalWidth = parseInt(this.items[0].childElements()[0].style.width);
		this.normalHeight = parseInt(this.items[0].childElements()[0].style.height);
		this.itemWidth = width;
		this.itemMaxWidth = maxWidth;
		this.displayCount = displayCount;
		this.currentItem = this.items[parseInt((this.displayCount-1)/2)];
		this.currentWidth = parseInt(this.currentItem.childElements()[0].style.width);
		this.currentHeight = parseInt(this.currentItem.childElements()[0].style.height);
		this.timer = null;
		this.speed = speed;
		this.moveWidth = 0;
		this.moveDir = '';
		this.doCount = 0;
		this.build();
	},
	build:function()
	{
		//events
		if(this.displayCount<this.itemsCount)
		{
			Event.observe(this.btnPrev,"click",this.prev.bindAsEventListener(this),false);
			Event.observe(this.btnNext,"click",this.next.bindAsEventListener(this),false);
			var _this = this;
			this.items.each(function(item){
				Event.observe(item,"click",_this.moveto.bindAsEventListener(_this,item),false);
			});
		}
		else
		{
			//set items align center
			
		}
	},
	prev:function()
	{
	    if(this.timer == null)
		{
			if(this.currentItem.nextSiblings().length==2)
			{
				//copy first to last elements and reset
				var first = this.menu.childElements()[0];
				this.menu.insert({bottom:first});
				$('photoContainer').scrollLeft -= this.itemWidth;
			}
			this.timer = new Timer(this);
			this.moveDir = 'left';
			this.currentItem.className = '';
			this.currentItem.nextSiblings()[0].className = 'current';
			this.timer.setInterval('move', 10);
		}
	},
	next:function()
	{
		if(this.timer == null)
		{
			if(this.currentItem.previousSiblings().length==2)
			{
				//copy last to first elements and reset
				var last = this.menu.childElements()[this.menu.childElements().length-1];
				this.menu.insert({top:last});
				$('photoContainer').scrollLeft +=this.itemWidth;
			}
			
			this.timer = new Timer(this);
			this.moveDir = 'right';
			this.currentItem.className = '';
			this.currentItem.previousSiblings()[0].className = 'current';
			this.timer.setInterval('move', 10);
		}
	},
	move:function()
	{
		if(this.moveDir == 'left')
		{
		    
			$('photoContainer').scrollLeft += this.speed;
			this.moveWidth += this.speed;
			this.currentItem.childElements()[0].style.width = this.currentWidth - (this.currentWidth-this.normalWidth)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.childElements()[0].style.height = this.currentHeight - (this.currentHeight-this.normalHeight)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.style.marginTop = 20 + (18)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.childElements()[0].setOpacity(1-0.5*this.moveWidth/this.itemWidth);
			this.currentItem.nextSiblings()[0].childElements()[0].style.width = this.normalWidth + (this.currentWidth-this.normalWidth)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.nextSiblings()[0].childElements()[0].style.height = this.normalHeight + (this.currentHeight-this.normalHeight)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.nextSiblings()[0].style.marginTop = 38 - (18)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.nextSiblings()[0].childElements()[0].setOpacity(0.5+0.5*this.moveWidth/this.itemWidth);
		}
		if(this.moveDir == 'right')
		{
			$('photoContainer').scrollLeft -= this.speed;
			this.moveWidth += this.speed;
			this.currentItem.childElements()[0].style.width = this.currentWidth - (this.currentWidth-this.normalWidth)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.childElements()[0].style.height = this.currentHeight - (this.currentHeight-this.normalHeight)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.style.marginTop = 20 + (18)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.childElements()[0].setOpacity(1-0.5*this.moveWidth/this.itemWidth);
			this.currentItem.previousSiblings()[0].childElements()[0].style.width = this.normalWidth + (this.currentWidth-this.normalWidth)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.previousSiblings()[0].childElements()[0].style.height = this.normalHeight + (this.currentHeight-this.normalHeight)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.previousSiblings()[0].style.marginTop = 38 - (18)*this.moveWidth/this.itemWidth + 'px';
			this.currentItem.previousSiblings()[0].childElements()[0].setOpacity(0.5+0.5*this.moveWidth/this.itemWidth);
		}
		if(this.moveWidth == this.itemWidth)
		{
			if(this.moveDir == 'left')
			{
				this.currentItem = this.currentItem.nextSiblings()[0];
				//this.currentItem.className = 'current';
			}
			else if(this.moveDir == 'right')
			{
				this.currentItem = this.currentItem.previousSiblings()[0];
				//this.currentItem.className = 'current';
			}
			this.timer.clearInterval(0);
			this.timer = null;
			this.moveDir = '';
			this.moveWidth = 0;
			if(this.doCount != 0)
			{
				if(this.doCount<0)
				{
					this.prev();
					this.doCount ++;
				}
				else if(this.doCount > 0)
				{
					this.next();
					this.doCount --;
				}
			}
		}
	},
	moveto:function(o,moveitem)
	{
		//
		if(this.doCount == 0 && this.timer == null)
		{
			if(moveitem == this.currentItem.previousSiblings()[0])
			{
				this.next();
			}
			else if(moveitem == this.currentItem.previousSiblings()[1])
			{
				this.doCount = 1;
				this.next();
			}
			else if(moveitem == this.currentItem.nextSiblings()[0])
			{
				this.prev();
			}
			else if(moveitem == this.currentItem.nextSiblings()[1])
			{
				this.doCount = -1;
				this.prev();
			}
		}
	}
};

