var totWidth=0;
var positions = new Array();
var changeEvery = 5;
var current=1;

function autoAdvance()
{
     if(current==-1) return false;
    
    $('#menu ul li').eq(current%$('#menu ul li').length).trigger('mouseover',[true]);     
    current++;
}

$(document).ready(function(){
    
    $("a.widget_title_link").click(function(){
        window.location = $(this).attr("rel");
    })
 	 
    var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
	
	$('#slides .slide').each(function(i){
		positions[i]= totWidth;
		totWidth += $(this).width();
		if(!$(this).width())
		{
			return false;
		}
        
        $(this).mouseover(function(e,keepScroll){
            var pos = $(this).prevAll('.slide').length;
            $('#slides').stop().animate({marginLeft:-positions[pos]+'px'},10);
            e.preventDefault();
            if(!keepScroll) clearInterval(itvl);
        })
        
        $(this).mouseout(function(e,keepScroll){
            current=$(this).prevAll('.slide').length + 1;
            itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
            autoAdvance();
        })
		
	});
	
	$('#slides').width(totWidth);
    
    /*
	$('#menu ul li a').click(function(e,keepScroll){
		 
			$('li.menuItem').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			
			var pos = $(this).parent().prevAll('.menuItem').length;
			
			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
 			
			//e.preventDefault();
  			if(!keepScroll) clearInterval(itvl);
            
            return true;
	});
    */
    
	$('#menu ul li').mouseover(function(e,keepScroll){
		$('li.menuItem').removeClass('act').addClass('inact');
		$(this).addClass('act');
		
		var pos = $(this).prevAll('.menuItem').length;
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
 		
		e.preventDefault();
 		if(!keepScroll) clearInterval(itvl);
 	});
	
	$('#menu ul li').mouseout(function(e,keepScroll){
		current=$(this).prevAll('.menuItem').length + 1;
		itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
		autoAdvance();
 	});
    
	$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
     
});
