jQuery(document).ready(function($){
	var indx = 0;
	var zIndx = 1;
	var start = $(".headerhighlights-nav a").length;
	var timer;
	
	$(".headerhighlights-nav a").each(function(){
		$(this).attr("index", indx);
		$(this).click(function(){
			var thisIndex = $(this).attr("index");
			if(start==thisIndex) return false;
			
			$(".headerhighlights-element").eq(thisIndex).css({ zIndex: zIndx++, left: $(".headerhighlights-element").eq(thisIndex).find(".headerhighlights-image").width() }).filter(":not(:animated)").animate({ left:0 }, 'slow');
			$(".headerhighlights-element").eq(thisIndex).find(".headerhighlights-title").css({ zIndex:zIndx, right: -1 * $(".headerhighlights-title").eq(thisIndex).width() });
			start = thisIndex;
			
			return false;
		});
		indx++;
	});
	
	$(".headerhighlights-element .headerhighlights-title").each(function(){
		var titleElement = $(this);
		var toRight = -1 * $(this).width() + 20;
		
		$(titleElement).css({ right: toRight });
		
		$(this).hover(function(){
			if (!$(this).hasClass('animated')) {
				$(this).dequeue().stop().animate({ right:0 });
			}
		}, function() {
			var toRight = -1*$(this).width();
			$(this).addClass('animated').animate({ right: toRight }, function() {
				$(this).removeClass('animated').dequeue();
			});
		});
	});
	
	$(".headerhighlights-nav a").eq(0).trigger("click");
	
	
	$("#headerhighlights").mouseover(function(){
		clearInterval(timer);
	});
	$("#headerhighlights").mouseout(function(){
		timer = setInterval(function(){
			var thisInterval = start*1+1;
			if(thisInterval >= $(".headerhighlights-nav a").length) thisInterval = 0;
			
			$(".headerhighlights-nav a").eq(thisInterval).trigger("click");
		}, 3700);
	}).trigger('mouseout');
});
