$("html").addClass("js");

$(function() {
	//contact
	$("#contact").each(function() {
		$(this).find("h2").click(function() {
			$("#contact div").hide();
			$(this).next().add("#cv-close").show();
		});
		$("#main").append($('<div id="cv-close"></div>').click(function() { $("#contact div").add(this).hide(); }));
	});
	
	$("#portfolio").each(function() {
		//preload images in div
		$("li>div", this).show().hide();
		
		var portfolioPopup;
		
		function createPortfolioPopup() {
			var popup = $('<div id="portfoliopopup-wrap"><div id="portfoliopopup-bg"></div><div id="portfoliopopup"><div class="content"></div><div class="close"></div><div class="prev"></div><div class="next"></div></div></div>');
			popup.appendTo("body");
			
			popup.find(".next,.prev").click(function() {
				var index = $("#portfolio div.selected").index("#portfolio li>div");
				if ($(this).is(".next")) {
					index++;
				}
				else {
					index--;
				}
				
				var total = $("#portfolio li>div").length;
				//todo: wel of niet circular
				index = (index + total) % total;
				
				openPortfolio($("#portfolio li>div").eq(index));
			});
			
			popup.find(".close").add("#portfoliopopup-bg").click(closePortfolio);
			
			$("#portfoliopopup-bg").css("opacity", 0.75);
			
			return popup.hide();
		}
		
		function scrollTo(obj) {
			if (timer != null)
				return;
			
			if (obj.offset().left < 0)
				$("#portfolio").animate({left: (-obj.index() * width) + "px"});
			else if (obj.offset().left + width > scrollWidth)
				$("#portfolio").animate({left: (-(obj.index()+1) * width + scrollWidth) + "px"});
		}
		
		function openPortfolio(obj) {
			if (!portfolioPopup) {
				portfolioPopup = createPortfolioPopup();
			}
			
			$("#portfolio li>div").parent().andSelf().removeClass("selected");
			
			var li = $(obj).closest("li");
			
			scrollTo(li);
			
			var content = $(obj).clone();
			var heading = $("h2", li).clone()
			content.prepend(heading);
			
			/*if (li.find(">div").length>1) {
				heading.append(' ' + $(obj).index() + '/' + li.find(">div").length);
			}*/
			
			portfolioPopup.attr("class", $(li).attr("class"));
			
			$("#portfoliopopup").attr("class", $(obj).attr("class"));
			
			$(obj).parent().andSelf().addClass("selected");
			
			portfolioPopup.fadeIn().find(".content").empty().append(content.show());
			
			var flashDiv = content.find(".flash");
			if (flashDiv.length) {
				var swf = flashDiv.attr("title");
				flashDiv.removeAttr("title");
				flashDiv.click(function() {
					flashDiv.css("backgroundImage", "none").flash({
						swf: swf,
						width: flashDiv.width(),
						height: flashDiv.height(),
						wmode: 'transparent'
					});
					flashDiv.unbind('click');
				});
			}
		}
		
		function closePortfolio(e) {
			if (portfolioPopup && e.target == e.currentTarget)
				portfolioPopup.hide();
			
			portfolioPopup.find('.content').empty();
			
			$("#portfolio li").removeClass("selected");
		}
		
		var width = $("#portfolio>li").outerWidth(true);
		var count = $("#portfolio>li").length;
		$("#portfolio").width(count * width).appendTo(
			$('<div id="portfoliowrap"><div class="prev button"></div><div class="next button"></div><div id="portfoliomask"></div></div>').appendTo("body").find("#portfoliomask")).find("li").click(function() {
			openPortfolio($(">div", this).eq(0));
		});
		

		var scrollable = 450;// aantal pixels links en rechts wat de muis "voelt"
		var speedPMS = 300/1000;// = 300 px/sec
		var speedClicked = 1.5;//speedFactor when clicked
		var timer;
		var g_speed = 0;
		var g_pos = 0;
		var scroller = $("#portfolio");
		
		var scrollWidth = $("#portfoliomask").width();
		var diarailWidth = $("#portfoliowrap").width();
		$(window).resize(function() {
			scrollWidth = $("#portfoliomask").width();
			diarailWidth = $("#portfoliowrap").width();
		});
		
		var total = scroller.width();
		
		function slide(speed) {
			scroller.stop();
			
			if (speed == 0) {
				if (timer != undefined) {
					clearInterval(timer);
					scrolling = false;
					timer = undefined;
				}
				return;
			}
			
			//$("#photoInfo").text(speed);
			g_speed = speed;
			if (timer == undefined) {
				var last = new Date();
				g_pos = scroller.position().left;
				scrolling = true;
				timer = setInterval(function() {
					var now = new Date();
					var past = now - last;
					last = now;
					if (!past)
						return;
						
					g_pos -= g_speed * past * speedPMS;
					if (g_pos > 0)
						g_pos = 0;
					else if (g_pos < -total + scrollWidth)
						g_pos = -total + scrollWidth;
					scroller.css("left", g_pos + "px");
				}, 13);
			}
		}
		
		var mousedown = false;
		function handleMouseMove(e) {
			if (mousedown) {
				return;
			}
			
			var pos = e.pageX - $(this).offset().left;
			var speed = 0;
			if (pos < scrollable)
				speed = pos - scrollable;
			else if ((pos-=diarailWidth) > -scrollable)
				speed = pos + scrollable;
			
			if (speed != 0) {
				speed = $.easing.easeInCubic(null, speed, 0, 1, scrollable);
			}
			
			slide(speed);
		}
		$("#portfoliowrap").mousemove(handleMouseMove).mouseleave(function() { slide(0);})
			.find(".button")
				.mousedown(function() { mousedown = true; slide(($(this).index() == 0) ? -speedClicked : speedClicked); /*return false;*/ } )
				.mouseup(function(e) { mousedown = false; handleMouseMove.call(this.parentNode, e); } )
	});
});

