/*
 * Dipendenze: $.geolocation  - http://code.google.com/p/geo-location-javascript/
 * */

$.widget("ui.cycle", {
	options : {
		interval:5000
	},
	_create : function() {
		this.currentStep = 0;
		var _self = this;
		this.steps = this.element.find('.steps li');		
		this.element.find('.box > li').mouseover(function(){
			_self.stop();
			if(parseInt($(this).attr('rel'))==_self.currentStep) return false;
			 			 
			 _self._next(parseInt($(this).attr('rel')));
		 }).mouseout(function(){
			 _self.start();
		 });
		
		
		this.pid = setInterval(function() {
			_self._next();
		}, this.options.interval);
	},
	_next : function(forceStep) {
		
		var _self = this;
		$(this.steps[this.currentStep]).fadeOut();	
		if(forceStep == undefined){
			this.currentStep++;
		}else{
			this.currentStep = forceStep;
		}
		if (this.currentStep > this.steps.length - 1)
			this.currentStep = 0;
		$(this.steps[this.currentStep]).fadeIn();
		var left = 0;
		switch(this.currentStep){
			case 1:
				left = 320;
				break;
			case 2:
				left = 640;
				break
			case 3:
				left = 0;
				break
			
		}
		this.element.find('.box li').removeClass('current');
		$('.selector').animate({ left:left }, 500,
				  "easeOutQuad", function(){
			_self.element.find('.box .box-'+_self.currentStep).addClass('current');
		});	
		/*
		if(this.currentStep == 1 || this.currentStep == 2){
			$('.selector').animate({ left:left }, 500,
					  "easeOutQuad", function(){
				_self.element.find('.box .box-'+_self.currentStep).addClass('current');
			});		
		}else{		
			$('.selector').fadeOut('fast',function(){
				$('.selector').css('left',0).fadeIn('fast',function(){
					_self.element.find('.box .box-'+_self.currentStep).addClass('current');					
				});				
			});
		}
		*/
			

	},
	stop:function(){
		this.pause = true;	
		clearTimeout(this.pid);
	},
	start:function(){
		var _self = this;
		
		this.pid = setInterval(function() {
			_self._next();
		}, this.options.interval);
	}

});

