jQuery(document).ready(function($){
//$(document).ready(function(){
	
	// Some vars
	var currentItem = 1; // We start with 1, even though there is a item 0
	var maxItems = 7;
	var autoSwitch = 1; // Should we switch automatically
	var ul = $("#switchmenu ul");
	var li = $("#switchmenu li");
	var h3 = $("#switchmenu li h3");
	var firstimgsrc = li.children("img:eq(0)").attr("src");
	var secondimgsrc = li.children("img:eq(1)").attr("src");
	var x = 2;
	
	// Start by putting the divs into the h3 (for the animation)
	h3.each(function(){
		$(this).wrapInner("<div class='textwrap'></div>").prepend("<div class='normal'></div>").prepend("<div class='active'></div>");
	});
	
	li.each(function(){
		var itemLink = $(this).find("a").attr("href");
		$(this).children("h3").wrapInner("<a href='"+itemLink+"'></a>");
	});
	
//	$("#switchmenu").append("<div id='bg1'></div>").append("<div id='bg2'></div>");
	$("#switchmenu").prepend("<div id='bg1'></div>").prepend("<div id='bg2'></div>");
		
	// Add class to first and last h3 and li
	h3.filter(":last").addClass("lasth3");
	h3.filter(":first").addClass("firsth3").addClass("act");
	li.filter(":last").addClass("lastli");
	li.filter(":first").addClass("firstli");
		
	// Hide non-used elements, except for first
	li.children("img").hide();
	//li.children("p:gt(0)").hide();
	li.children("p:eq(0)").show();
	// Add background image to the 2 divs next to the ul
	$("#bg1").css({'background-image':'url('+firstimgsrc+')'});
	$("#bg2").css({'background-image':'url('+secondimgsrc+')'}).hide();
	
	// Function for changing menuitems
	function switchMenu(cur_h3, animate){
	
		// Set everything to zero
		li.children("img").hide();
		li.children("p").hide();

		// Some vars
		var img = cur_h3.siblings("img");
		var p = cur_h3.siblings("p");
		
		h3.removeClass("act");
		cur_h3.addClass("act");
		
		x = x % 2;
		if(animate == 1) {
			p.show(250);
			
			if(x == 1){
				$("#bg2").fadeOut("slow");
				$("#bg1").css({'background-image':'url('+img.attr("src")+')'}).fadeIn("slow");
			} else {
				$("#bg1").fadeOut("slow");
				$("#bg2").css({'background-image':'url('+img.attr("src")+')'}).fadeIn("slow");
			}
		
			h3.find(".active").hide();
			h3.find(".normal").show();
			cur_h3.find(".normal").hide();
			cur_h3.find(".active").show(250);
		} else {
			p.show();
		
			if(x == 1){
				$("#bg2").hide();
				$("#bg1").css({'background-image':'url('+img.attr("src")+')'}).show();
			} else {
				$("#bg1").hide();
				$("#bg2").css({'background-image':'url('+img.attr("src")+')'}).show();
			}
		
			h3.find(".active").hide();
			h3.find(".normal").show();
			cur_h3.find(".normal").hide();
			cur_h3.find(".active").show();
		}
		x++;
				
		if(currentItem < (maxItems - 1)){
			currentItem++;
		} else {
			currentItem = 0;
		}		
		
	}
	
	// Switch manually
	h3.each(function(i){
		$(this).hover(function(){
			//Kill the automatic switch
			autoSwitch = 0;
			// Set current h3 as currentItem
			currentItem = i;
			// Switch manually
			switchMenu($(this), 0);
		}, function(){
			// Activate autoswitch
			autoSwitch = 1;
			// Set current h3 as currentItem
			if(currentItem != 0){
				currentItem = i+1;
			}
		});
	});
	
	li.children("p").each(function(i){
		$(this).hover(function(){
			//Kill the automatic switch
			autoSwitch = 0;
		}, function(){
			autoSwitch = 1;
		});
	});
	
	// Switch automatically
	setInterval(function(){
		if(autoSwitch == 1){
			switchMenu(h3.filter(":eq("+currentItem+")"), 1);
		}
	}, 3000);
	
});