var scroller={
	//automatic item switching
	autopilot_delay        :  7000, //milliseconds between each switch
	autopilot_resume_delay : 10000, //milliseconds before switching resumes, again, after user interaction
	autopilot_speed        :   0.08, //speed items slide while switching
	
	//user item switchng
	speed : 0.06, //speed items slide while switching

	init:function(){
		var self=this;
		this.width=0;this.item_count=$('#Scroller ul li').each(function(){self.width+=$(this).outerWidth();}).length;
		
		if(this.item_count>1){
			var item_first=$('#Scroller ul li:nth-child(1)');
			$('#Scroller ul').append('<li>'+item_first.html()+'</li>');
			$('#Scroller').append(
				'<div class="ui">'+
					'<a class="left" href="#" onclick="scroller.autopilot=false;setTimeout(\'scroller.autopilot=true\',scroller.autopilot_resume_delay);scroller.item_previous();return false"></a>'+
					'<a class="right" href="#" onclick="scroller.autopilot=false;setTimeout(\'scroller.autopilot=true\',scroller.autopilot_resume_delay);scroller.item_next();return false"></a>'+
					'<div class="bar"><a class="drag" href="#"></a></div>'+
				'</div>'
			);

			this.element=$('#Scroller');
			this.element_content=$('#Scroller ul');
			this.element_drag=$('#Scroller .ui .drag');
			this.element_drag.mousedown(function(event){
				var self=$(this);
				scroller.cursor_dragging=self;
				scroller.cursor_dragging_offset=[self.position().left-event.pageX,self.position().top-event.pageY];
				scroller.autopilot=false;
				return false;
			});
			if($.browser.msie)
				this.element_drag.attr('unselectable','on');
			this.element_drag.click(function(event){return false;});
			this.element_content.css('width',this.width+item_first.outerWidth()+'px');
			this.tick();
			setTimeout('scroller.autopilot_tick()',this.autopilot_delay);
		}
	},
	
	autopilot:true,
	phase:0,
	width:0,
	
	item_count:0,
	item_current:0,
	item_goto:function(id){
		this.update(this.item_count>1?(id%this.item_count)/(this.item_count-1):0.5);
	},
	item_next    :function(){if(this.item_current<this.item_count)this.item_current=(this.item_current+1)%(this.item_count+1);},
	item_previous:function(){this.item_current=(this.item_current>0?this.item_current:this.item_count)-1;},
	
	element:null,
	element_content:null,
	element_drag:null,
	element_drag_offset:[],
	
	cursor:[0,0],
	cursor_dragging:null,
	cursor_dragging_offset:[0,0],
	cursor_update:function(event){
		var delta=[event.clientX-this.cursor[0],event.clientY-this.cursor[1]];
		if(this.cursor_dragging){
			this.cursor_dragging.css('left',(event.pageX+this.cursor_dragging_offset[0])+'px');
			//this.cursor_dragging.css('top' ,(event.pageY+this.cursor_dragging_offset[1])+'px');
			this.update();
		}
		this.cursor=[event.clientX,event.clientY];
	},
	cursor_release:function(event){
		if(this.cursor_dragging){
			this.cursor_dragging=null;
			setTimeout('scroller.autopilot=true',scroller.autopilot_resume_delay);
			return false;
		}else
			return true;
	},
	
	update:function(phase,update_current){
		var area=this.element_drag.offsetParent().width()-this.element_drag.width();
		if(phase==undefined)phase=Math.min(Math.max(this.element_drag.position().left/area,0),1);
		if(update_current!==false)
			this.item_current=Math.round(phase*(this.item_count-1));
		var drag_phase=
			phase>=1?1-(phase-1)/(1/(this.item_count-1)):
			phase< 0?0-phase*(this.item_count-1):
			phase
		;
		this.element_drag.css('left',(drag_phase*area)+'px');
		this.element_content.css('left',-(phase*(this.width-this.element.innerWidth()))+'px');
		this.phase=phase;
	},
	tick:function(){
		if(!this.cursor_dragging){
			var phase=this.phase+((this.item_count>1?(this.item_current)/(this.item_count-1):0.5)-this.phase)*(this.autopilot?this.autopilot_speed:this.speed);
			if(this.item_current==this.item_count&&phase>=1+(1/(this.item_count-1))*0.9){
				this.item_current=0;
				phase-=1+1/(this.item_count-1);
			}
			this.update(phase,false);
		}
		setTimeout('scroller.tick()',30);
	},
	autopilot_tick:function(){
		if(this.autopilot){
			this.item_next();
			setTimeout('scroller.autopilot_tick()',this.autopilot_delay);
		}else
			setTimeout('scroller.autopilot_tick()',100);
	}
};

$(document).ready(function(){scroller.init();});
$(document).mousemove(function(event){scroller.cursor_update(event);});
$(document).mouseup(function(event){return scroller.cursor_release(event);});
