$(document).ready(function() {
	
	// ===== INIT =====
	$("#content_text").hide();
	$(".text_container").hide();
	
	menu_id = "work";
	
	img_w = 580;
	img_h = 300;
	
	slide_off_opacity = 0.3;
	
	$(".slide_image").css({ opacity: slide_off_opacity });
	$("li.slide.current > .slide_image").css({ opacity: 1 });
	$(".slide_content").hide();
	$("li.slide.current > .slide_content").show();
	
	switchSlide("s1");
		
	// ===== NAVIGATION SLIDER =====
	
	var slide_nbr = $(".slide_image").length * img_w;
	$("#slider > ul").width(slide_nbr);
	
	function switchSlide(target) {
		// init
		var current = $("li.slide.current").attr("id");
		// action si ID différent
		if (target != current) {
			// indexes
			var currentIndex = current.substr(1);
			var targetIndex = target.substr(1);
			var diff = currentIndex - targetIndex;
			// change class
			$("#" + current).removeClass("current");
			$("#" + target).addClass("current");
			// change opacity
			$(".slide_content").fadeOut(150);
			$("#" + current + " > .slide_image").animate({
				opacity: slide_off_opacity
			}, 500);
			$("#" + target + " > .slide_image").animate({
				opacity: 1
			}, 500);
			// transition
			var moveX = img_w * diff;
			$("#slider").animate({
				left: '+=' + moveX
			}, 500, function() {
				// change opacity
				$(".slide_content").fadeOut(150);
				$("#" + target + " > .slide_content").fadeIn(250);
			});
		}
		// adapt #content height
		var contentHeight = $("li.slide.current > .slide_content").height() + 335;
		if (contentHeight > 425) $("#content").css("min-height", contentHeight);
		else $("#content").css("min-height", 425);
	}
	
	function slidePrev() {
		var target = ($("li.slide.current").hasClass("first")) ? $("li.slide.last").attr("id") : $("li.slide.current").prev("li.slide").attr("id");
		switchSlide(target);
	}
	
	function slideNext() {
		var target = ($("li.slide.current").hasClass("last")) ? $("li.slide.first").attr("id") : $("li.slide.current").next("li.slide").attr("id");
		switchSlide(target);
	}
	
	// prev button
	$("#prev").click(function() {
		slidePrev();
	});
	
	// next button
	$("#next").click(function() {
		slideNext();
	});
	
	// key press
	$(document).keypress(function(e) {
		if ($("#sb-container").css("display") == "none" && $("#slider_nav").css("display") == "block") {
			if (e.keyCode == 37) {
				slidePrev();
			} else if(e.keyCode == 39) {
				slideNext();
			}
		}
	});
	
	// click sur slide
	$("li.slide").click(function() {
		var target = $(this).attr("id");
		switchSlide(target);
	});
		
	// ===== MENU =====
	$(".menu_item").click(function() {
		menu_id = $(this).attr("id");
		
		$("#slider_nav").fadeOut(250);
		
		$(".menu_item").removeClass("current");
		$(this).addClass("current");
		
		if (menu_id == "work") {
			$("#content_text").fadeOut(250, function() {
				$("#slider").fadeIn(250, function() {
					switchSlide("s1");
				});
				$("#slider_nav").fadeIn(250);
			});
		} else {
			if (menu_id == "contact") showMap();
			$("#slider").fadeOut(250, function() {
				$(".text_container").hide();
				$("#content_" + menu_id).show();
				$("#content_text").fadeIn(250);
				if (menu_id == "contact") showMap();
			});
		}
	});
		
	// ===== ACCESS MAP =====
	function showMap() {
		var latlng = new google.maps.LatLng(50.8346564, 4.3933777);
		
		var myOptions = {
			zoom: 15,
			mapTypeControl: true,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		var map = new google.maps.Map(document.getElementById("accessmap"), myOptions);
		
		var contentString = '<p><strong>Van piperzeel</strong></p>'+
			'<p>The first agency to name itself after the consumer.</p>';
		
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});
		
		var image = 'images/map_marker.png';
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			icon: image
		});
		
		google.maps.event.addListener(marker, 'click', function() {
		  infowindow.open(map,marker);
		});
	}
	
});

