// JavaScript Document
function goto(index)
{
	 hCarousel.scrollTo(index); 
}
function Carousel(id, options){
	this.ul = jQuery(id);
	this.index = null;
	this.lis = null;
	this.timer = null;

	var self = this;
	
	this.init = function(){
		jQuery('.carousel_btns').css('z-index','3');
		this.index = 0;
		if(!this.ul.is("ul"))
			this.ul = jQuery(id+" ul");
		this.lis = this.ul.children();
		//hide all li
		this.lis.each(function(index) {
			jQuery(this).css('position', 'absolute');
			//except for first
			if(index != 0)
			{

				jQuery(this).css('z-index', '2');
				jQuery(this).hide();
			//	jQuery(this).css('left', options['width']);
				jQuery(this).css('left', '0');
			}
			else
			{
				jQuery(this).css('z-index', '1');
				jQuery(this).css('left', '0');
				jQuery(this).fadeIn();
			}
		  });
		//if horizontal - set overflow and with
		if(options['direction'] == 'horizontal')
		{
			this.ul.css('overflow', 'hidden');
			this.ul.css('width', options['width']*2);
			this.ul.css('height', options['height']);
		}
		
		if(options['autorotate'] == true)
		{
			this.autorotate(true);
			this.ul.mouseover(function() {
			  self.autorotate(false);
			});	
			this.ul.mouseout(function() {
			  self.autorotate(true);
			});	
		}
	//	jQuery(this.lis[0]).css('left', 0);
	};
	

	this.autorotate = function (flag)
	{
		
		if(flag)
			this.timer = setInterval( function () {
					self.scrollTo(self.index+1);
					//alert(self.index);
				}, options['slideduration']*1000);
		else
			clearInterval(this.timer)
	};
	
	this.scrollTo = function(int){	
		
		if(self.index != int)
		{
			
			
			//carousellinks[2].css('background-color', '#ff0000');
			if(int > self.lis.length-1)
				int = 0;
			var current = jQuery(self.lis[self.index]);
			

			
			
			/*current.animate({ 
				left: -options['width']
			  }, options['effectduration']*1000, function() {
				// Animation complete.
				self.index = int;
				current.css('left', options['width'] );
				current.hide();
			  });*/
			/*current.fadeOut( options['effectduration']*1000, function () {
				
			});*/
			
			
			var li_item = jQuery(self.lis[int]);
			li_item.css('z-index', '2');
			current.css('z-index', '1');
			li_item.hide();
			//li_item.css('left', options['width'] );
			li_item.css('left', '0');
			
			
			//li_item.show();
			/*li_item.animate({
				left: 0
			  }, options['effectduration']*1000, function() {
				// Animation complete.
			  });*/
			  li_item.fadeIn( options['effectduration']*1000, function (){
				  
				  
				  	self.index = int;
					current.css('left', '0' );
					current.hide();	
				  
				  });
						
			var carousellinks = jQuery('.carousel_btns a.carlinks');
			
			carousellinks.each(function(){
				  jQuery(this).css('background-image', 'none');
			  });
			jQuery(carousellinks[int]).css('background-image', 'url(/img/carousel-bg-nav.jpg)');
			
			
			clearInterval(this.timer)
			this.timer = setInterval( function () {
					self.scrollTo(parseInt(self.index)+1);
					//alert(self.index);
				}, options['slideduration']*1000);
				
			
		}
	};

	
};

