var activeSlide;
var intervalSlide;
function slide() {

	activeSlide = activeSlide.next();
	if(!activeSlide.size())
		activeSlide = $("#home_left #slides li:first");
	
	index = activeSlide.parent().find("li").index(activeSlide);
	
	$("#home_left #slides").animate({left: -index * 592});
	$("#home_left #nav li").removeClass("active");
	$("#home_left #nav li:eq(" + index + ")").addClass("active");

}

$(function() {
	
	
	$("#home_left").each(function() {
			
		$(this).append('<ul id="nav"></ul>');
		
		max = $("#slides li", this).size();
		
		for(var i=0; i<max; i++)
			$("#nav", this).append('<li><a href="#" title="Slide ' + (i + 1) + '"></a></li>');
			
		$("#nav li:first", this).addClass("active");
		
		activeSlide = $("#slides li:first", this);
		
		$("#nav a", this).click(function() {
		
			clearInterval(intervalSlide);
			intervalSlide = setInterval("slide()", 7000);
			
			index = $(this).parent().parent().find("li").index($(this).parent());
			activeSlide = $("#home_left #slides li:eq(" + index + ")");
			
			$("#home_left #slides").animate({left: -index * 592});
			$("#home_left #nav li").removeClass("active");
			$("#home_left #nav li:eq(" + index + ")").addClass("active");
		
		});

		intervalSlide = setInterval("slide()", 7000);
		
	});	
	
	$("#menu > ul > li").hover(function() {

		$("ul", this).addClass("hover");
        //$('#items').text(this.innerHTML);
	}, function() {

		$("ul", this).removeClass("hover");
        //$('#items').text(this.innerHTML);
	});
	
		
	

	$("#gallery").each(function() {

		$(">img", this).load(function() {

			$(this).css("margin-left", ($(this).parent().width()- $(this).width()) / 2 );

		});
		
		parefdaasdfnt = $(this);
		var prevLeft = 0;
		
		$("li", this).click(function() {
			
			parefdaasdfnt.find("li.active").removeClass("active");
			$(this).addClass("active");
			
			$("#gallery img:first").attr("src", $("img", this).attr("src"));
			
			size = $("#gallery ul img").size();
			index = $("#gallery ul img").index($("img", this));
			left = index * -99;
			
			if(left < (size - 1) * -99 + 448) {
				left = (size - 1) * -99 + 448;
			}
				
			if(left > 0) {
				left = 0;
			}
			
			if(left != prevLeft) {
				parefdaasdfnt.find("ul").stop(true).animate({left: left}, 350);
			}

			if(index == 0) {
				parefdaasdfnt.find(".left").css("cursor", "default").attr("title", "").find("span").stop(true).fadeTo(350, 0.5);
			}
			else {
				parefdaasdfnt.find(".left").css("cursor", "pointer").attr("title", "Vorige").find("span").stop(true).fadeTo(350, 1);
			}
			
			if(size - 1 == index) {
				parefdaasdfnt.find(".right").css("cursor", "default").attr("title", "").find("span").stop(true).fadeTo(350, 0.5);
			}
			else {
				parefdaasdfnt.find(".right").css("cursor", "pointer").attr("title", "Volgende").find("span").stop(true).fadeTo(350, 1);
			}
			
			prevLeft = left;
			
		});
		
		$(".right", this).click(function() {
		
			parefdaasdfnt.find("li.active").next().click();
		
		});
		
		$(".left", this).click(function() {
				
			parefdaasdfnt.find("li.active").prev().click();
		
		}).css("cursor", "default").attr("title", "").find("span").fadeTo(350, 0.5);
	
	});

});
