var current = 0;
var next = 1;
var mains;
var list;
var pos = new Array();
var len = 0;

$(document).ready(function() {
	mains = $("#carousel .hero");
	list = $("#sub li");
	$("#location").click(function() {
		if ($(this).hasClass("open")) {
			$(this).removeClass("open");
		} else {
			$(this).addClass("open");
		}
	});
	
});

$(window).load(function() {
	$(list).each(function(index, element) {
        pos.push($(element).position().top);
	});
	$(list).each(function(index, element) {
	$(element).css({
			top: pos[index] + "px",
			position:"absolute"
			});
	});
	len = $(list).length;
	var h = (len - 1) * 80;
	$("#sub ul").css("height", h);
	setInterval(moveToNext, 8000);
});

function moveToNext() {
	scrollToNext();
	fadeToNext();
	
}

function fadeToNext() {
    $(mains[current]).stop().fadeOut(250);
	$(mains[next]).stop().fadeIn(1500, function(){
		current = next;
		if (next < len - 1) {
			next++;
		} else {
			next = 0;
		}
		});
}

function scrollToNext() {
	$(list).each(function(index, element) {
		var newpos = parseInt($(element).css("top")) + 80;//$(element).outerHeight(true);
		$(element).stop().animate({
			top: newpos + "px"
		}, 1000, function() {
			$("#sub li").removeClass("current");
			var i = (len-1)-current;
			var cur = $(list)[i];
			var hidden = $(cur).outerHeight(true) * -1;
			$(cur).css("top", "-80px");
			
		})
    });
}
