var Carousel = Class.create();
Object.extend(Carousel.prototype, {
	initialize : function( container, options ) {
		this.options = Object.extend({
			delay: 0.01,
			offSetX: 1,
			direction: 1,
			continuous: true,
			mousestop: true,
			mousedirection: false,
			autostart: true
        },options || {});

		this.xoffset = 0;
		
		this.moving = this.options.autostart;
		this.mousein = false;
		this.container = $(container);
		this.posCount = 0;
		this.cList = $(container).down('ul');
		
		this.pos = {};
		this.pos.left = this.cList.cumulativeOffset().left;
		this.pos.right = this.cList.cumulativeOffset().left + this.container.getWidth();
		this.pos.center = ((this.pos.right - this.pos.left) / 2);
		
		if(this.options.mousestop)
		{
			$(container).observe('mouseover', this.mouseon.bindAsEventListener(this), false );
			$(container).observe('mouseout', this.mouseoff.bindAsEventListener(this), false );
		}
		
		if(this.options.mousedirection)
		{
			$(container).observe('mousemove', this.mousemove.bindAsEventListener(this), false );
		}
		
		this.current = 0;
//		Event.observe(window, 'load', function() {
			this.timeout = new PeriodicalExecuter( this.circulate.bind(this), this.options.delay );
//		}, false);	
	},
	mousepos: function( e )
	{
		var pos = {};
		pos.x = 0;
		pos.y = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			pos.x = e.pageX;
			pos.y = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			pos.x = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			pos.y = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		return pos;
	},
	mousemove : function( e )
	{
		var diff = (this.pos.center - this.mousepos( e ).x) / (this.container.getWidth() / 10);
		this.options.direction = diff; // (diff > 0) ? -diff : diff;
	},
	setdirection : function( dir )
	{
		this.options.direction = dir;
	},
	mouseon : function( e )
	{
		this.mousein = true;
	},
	mouseoff : function( e )
	{
		this.mousein = false;
	},
	circulate : function( pe )
	{		
		// console.log( 'circulate' );
		
		if(!this.moving || this.mousein) {return;}
		
		children = this.cList.childElements();
		
		var fchild = children.first();
		var lft = fchild.cumulativeOffset().left + fchild.getWidth();
		
		var lchild = children.last();
		var rgt = lchild.cumulativeOffset().left;
		var rgtw = lchild.cumulativeOffset().left + lchild.getWidth();
		
		if((this.options.direction > 0) && (fchild.cumulativeOffset().left >= this.pos.left) && !this.options.continuous)
		{
			// console.log('stop 1');
		}
		else if((this.options.direction < 0) && (rgtw <= this.pos.right) && !this.options.continuous)
		{
			// console.log('stop 2');
		}
		else if((this.options.direction < 0) && (this.pos.left >= lft) && this.options.continuous)
		{
			var li = this.cList.firstDescendant();
			li.remove();
			this.cList.insert(li);
			this.xoffset = 0; // this.xoffset + w;
		}
		else if((this.options.direction > 0) && (this.pos.right <= rgt) && this.options.continuous)
		{
			var w = lchild.getWidth();
			lchild.remove();
			this.cList.insert( { top: lchild } );
			this.xoffset = this.xoffset - w;
		}
		else
		{
			// console.log('add: '+this.options.offSetX+' : '+this.options.direction);
			this.xoffset += this.options.offSetX * this.options.direction;
		}
		
		// console.log( 'xoffset:'+ this.xoffset );
		this.cList.style.left = this.xoffset + "px";
	},
	stop : function()
	{
		this.moving = false;
	},
	start : function()
	{
		this.moving = true;
	}
} );
