heroSlideshow = {
	current : 1,
	first : 1,
	last : 4,	
	timer : 0,
	moveNext : function() { 
		if(this.current < this.last) { 
			this.current++;
		} else {
			this.current=this.first;
		}
		if(document.getElementById('hero'+this.current+'Ctrl') != null)
   		this.activatePanel(document.getElementById('hero'+this.current+'Ctrl'));
	},
	activatePanel : function(a) {
		$('#controls a').removeClass('current'); 
        $(a).addClass('current');
        
        $('.heroImg').removeClass('current');
        $('.heroImg').hide();
        var hero = "#"+a.id.split('Ctrl')[0];
        $(hero).fadeIn("slow");
        $(hero).addClass('current');
		
		var pos = a.id.split('Ctrl')[0];
		var pos = pos.split('hero')[1];
		this.current=pos;
	},
	toggleAutoplay : function() {
		if ($('#controls .autoPlay').hasClass('play'))  {		
			$('#controls .autoPlay').removeClass('play');
			heroSlideshow.startAutoplay();
		}
		else {
			heroSlideshow.stopAutoplay();
		}
	},
	startAutoplay : function() {
		this.timer = setInterval('heroSlideshow.moveNext();',5000);
	},
	stopAutoplay : function() {
		clearInterval(this.timer);
		$('#controls .autoPlay').addClass('play');
	},	
	init : function() {
		$('#controls a').bind("click",function(e) {
			heroSlideshow.stopAutoplay();
			heroSlideshow.activatePanel(this);
	        e.preventDefault();
		});
		
		$('#controls .autoPlay').bind("click",function(e) {
			heroSlideshow.toggleAutoplay();
		});
		
		heroSlideshow.startAutoplay();
											  
	}
}

carousel = function() {
	
	var carousel_length = 0;
	 if(document.getElementById("productSlides") != null)
	  carousel_length = document.getElementById("productSlides").getElementsByTagName("LI").length;
  
	$('.totalSlides').html(carousel_length);
	var offset = 333;
	var pos_current = 0;
	var num_visible = 1;
	var pos_max = parseInt((carousel_length) / num_visible);
	
	if (carousel_length % num_visible == 0)
		pos_max -= 1;	
	
	$('#prevBtn').bind("click",function(e) {
		if((pos_current-1) >= 0) {
			pos_current -= 1;
			move_carousel(pos_current);			
		}
		
		else {
			pos_current = pos_max;
			move_carousel(pos_current);
		}								
        e.preventDefault();
    });
	
	$('#nextBtn').bind("click",function(e) {
		if((pos_current+1) <= pos_max) {
			pos_current += 1;			
			move_carousel(pos_current);								
		}
		
		else {
			pos_current = 0;
			move_carousel(pos_current);
		}					
        e.preventDefault();
	});
	
	function move_carousel(a) {			
		var newPos = (a*offset*-1)+'px';
		$('#productSlides').animate({ left:newPos }, 1500 );
		$('.currentSlide').html(a+1);
	}
	
	
}



$(function() {
	heroSlideshow.init();
	carousel();
});
