$(document).ready(function() {
	$("#Menu").lavaLamp({ fx: "easeInOutQuart", speed: 700 });
	$("#SlideImages").slider($("#SlideNav"));
})


$.fn.slider = function(navigation)
{
	var SlideImagesObj = $(this);
	
//	alert (navigation.html())
	navigation.find("a").click(function(){
		
		var active = $(SlideImagesObj).children("li.active");
		
		var action = navigation.children("li").index($(this).parent("li"));
		var actionLastAvaliable = navigation.children("li").length;
		
		switch (action)
		{
		case 0:
			var next = (active.prev().length) ? active.prev():$(SlideImagesObj).children("li:last");
			break;
		case actionLastAvaliable-1:
			var next = (active.next().length) ? active.next():$(SlideImagesObj).children("li:first");
			break;
		default:
			var next = $(SlideImagesObj).children("li:eq("+(action-1)+")");
			break;
		}
		
		var contunue = true;
		
		if (SlideImagesObj.find(".imageDescription:animated").length)
			{
				contunue = false;
//				alert ("1: here");
			}
		if (SlideImagesObj.find("img:animated").length)
			{
				contunue = false;
//				alert ("2: here");
			}
		if ($(this).parent("li").is(".active"))
			{
				contunue = false;
			}
		
		if(contunue)
		{
			navigation.find(".active").removeClass("active");
			navigation.children("li:eq("+(SlideImagesObj.children("li").index(next)+1)+")").addClass("active");
			
			
			
			var nextImage = next.find("img").attr("src");
			
			active.css({"background-image": "url(\"" + nextImage +	"\")"});
			
			$(".imageDescription", active)
			.stop()
			.animate(
					{opacity:'0'},
					400,
					function(){
						
						next.find("img").css({opacity: 1});
						
						$(".imageDescription", next).css({opacity:0});
						
						active.find("img").animate(
								{opacity:'0'},
								800,
								function(){
									next.show();
									active.hide();
									$(".imageDescription", next)
									.stop()
									.css({opacity:'0'})
									.animate({opacity:'1'},{duration:400});
									
									active.removeClass("active");
									next.addClass("active");
								}
						);
					}
			);
		}
		return false;
	})	
}
