$(document).ready(function(){
	var totWidth=0;
    var totElements=0;
	var positions = new Array();
    var currentSlide=0;
	$('#slides .slide').each(function(i){
		/* Loop through all the slides and store their accumulative widths in totWidth */
		positions[i]= totWidth;
		totWidth += $(this).width();
        totElements++;
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});

	$('#slides').width(totWidth);
            
	$('#whatsNewMenu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
        
    var c=1;
    var t;
    var timer_is_on=0;
    function timedCount() {
        $('#slides').stop().animate({marginLeft:-positions[c]+'px'},450);
		$('li.menuItem').removeClass('act').addClass('inact');
		$('li.menuItem').eq(c).addClass('act');
        currentSlide=c;
        //$('.c1').append(c + ' ');
        c=c+1;
        if (c == totElements) {
            c=0;
        }
        t=setTimeout(timedCount,5000);
    }

    function doTimer() {
    if (!timer_is_on) {
        timer_is_on=1;
        timedCount();
        }
    }
    
	//$('#slides').mouseover(function(e){
    //    clearTimeout(t);
        //timer_is_on=0;
        //$('#slides').mouseout(function(e){
        //    setTimeout(doTimer,2000);
        //});    
    //});    
    
	$('#whatsNewMenu').click(function(e){
        clearTimeout(t);
    });    
    
    $('#whatsNewMenu ul li a').click(function(e){
		$('li.menuItem').removeClass('act').addClass('inact');
		$(this).parent().addClass('act');
		var pos = $(this).parent().prevAll('.menuItem').length;
        currentSlide = pos;
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
		e.preventDefault();
	});
    
    $('#prevItem a').click(function(e){
        clearTimeout(t);
        if (currentSlide > 0) {
            currentSlide = currentSlide - 1;
            $('#slides').stop().animate({marginLeft:-positions[currentSlide]+'px'},450);
            $('li.menuItem').removeClass('act').addClass('inact');
            $('li.menuItem').eq(currentSlide).addClass('act');
        }    
		e.preventDefault();
    });    
        
    $('#nextItem a').click(function(e){
        clearTimeout(t);
        if (currentSlide < (totElements - 1)) {
            currentSlide = currentSlide + 1;
            $('#slides').stop().animate({marginLeft:-positions[currentSlide]+'px'},450);
            $('li.menuItem').removeClass('act').addClass('inact');
            $('li.menuItem').eq(currentSlide).addClass('act');
        }
		e.preventDefault();
    });    
    
    t=setTimeout(doTimer,5000);
});
