/*
Copyright Mikael Norling
www.mikaelnorling.se
License: http://creativecommons.org/licenses/by/3.0/
*/

$(function() {
	//$("#micke").hide();
	//$("#micke .desc").show();
	//$("#hint").show();
	//$("#portfolio-nav").show();
	//$("#transition").show().css("opacity","0");
	//$("#prev-next").show();
	//$("#prev a").hide();
	//$("#prev span").show();

	//Is there a better way to do this?
	//var loc = "http://mikaelnorling.se/";

	//Create the top bar if JS
	//$("h1").before("<nav><ul><li class=\"active\"><a href=\"#mikael\" id=\"n-mikael\">Mikael, han som jobbar med webb.</a></li><li><a href=\"#micke\" id=\"n-micke\">Micke, han som gillar tennis.</a></li></ul></nav>");
	
	//Show the top bars background and make sure there's no width jump when navigating
	//$("body").css("background-position","top left");
	
	//Convert spam avoiding text to normal
	$("#email").empty().append("<a href=\"mailto:mikael@mikaelnorling.se\">mikael@mikaelnorling.se</a>");
	//$("#msn").text("norling.mikael@gmail.com");
	//$("#address").wrap("<address></address>");
	
	//Switch from neutral phrase to professional if JS
	//$("#default-hello").text("Goddag.");
	
	//Nav
	/*$("#n-micke, #hint a").click(function() {
		if ($("#n-micke").closest("li").attr("class") != "active") {
			$("#n-micke").closest("li").attr("class","active");
			$("#n-mikael").closest("li").removeAttr("class");
		
			$("footer").fadeOut(200);
			$("#mikael").hide("drop", 600, function() {
				$("#hint").css("display","none");
				$("footer").fadeIn(600);
				$("#micke").show("drop", 600, function() {
					$("#title a span").fadeOut(200, function() {
						$("#title a span").text("Micke");
						$("#title a span").fadeIn(200);
					});
				});
			});
			
			location.href = loc + "#lifestream";
		
			return false;
		} else {
			return false;
		}
	});
	
	$("#n-mikael").click(function() {
		if ($(this).closest("li").attr("class") != "active") {
			$(this).closest("li").attr("class","active");
			$("#n-micke").closest("li").removeAttr("class");
		
			$("footer").fadeOut(200);
			$("#micke").hide("drop", 600, function() {
				$("footer").fadeIn(600);
				$("#mikael").show("drop", 600, function() {
					$("#title a span").fadeOut(200, function() {
						$("#title a span").text("Mikael");
						$("#title a span").fadeIn(200);
					});
				});
			});
			
			location.href = loc + "#portfolio";
		
			return false;
		} else {
			return false;
		}
	});*/
	
	//Make it possible to link directly to Micke
	/*if (location.href == loc + "#lifestream") {
		$("#micke").css("display","block");
		$("#mikael").css("display","none");
		$("#title a span").text("Micke");
		$("#hint").css("display","none");
		$("#n-micke").closest("li").attr("class","active");
		$("#n-mikael").closest("li").removeAttr("class");
		window.scroll(0,0);
	}*/
	
	//Portfolio
	/*var initbg = $("#portfolio-nav ul a:first").attr("href");
	var initclass = $("#portfolio-nav ul a:first").attr("class");
	$("#portfolio-nav ul li:first").addClass("current");
	
	$("#portfolio-wrap").addClass("js").css("background-image","url("+initbg+")").addClass(initclass);
	$(".portfolio-item").addClass("js").hide().css("left","900px");
	$(".portfolio-item").find(".thumb").remove();
	$(".portfolio-item:first").css("left","0").show();
	
	var container = $("#portfolio-nav");
	var ul = $("#portfolio-nav ul");
	var itemsWidth = ul.innerWidth() - container.outerWidth() + 6;
	
	$("#slider").slider({
		min: 0,
		max: itemsWidth,
		slide: function(ev, ui) {
			ul.css('left', '-' + ui.value + 'px');
		}
	});*/
	
	// Hack-ish solution to avoid handle overflow
	/*$("#click-left").click(function() {
		$(".ui-slider-handle").css("left","0");
		ul.css('left','0');
		return false;
	});
	
	$("#click-right").click(function() {
		$(".ui-slider-handle").css("left","100%");
		ul.css('left',itemsWidth + 'px');
		return false;
	});*/
	
	// Back and forward links
	/*$("#nav-back").click(function() {
		var slideVal = $("#slider").slider("option","value");
		$("#slider").slider("option","value", slideVal-(itemsWidth * 0.2));
		if (slideVal <= 0) {
			$("#slider").slider("option","value", 0);
		}
		
		ul.css("left",parseInt(ul.css("left"))+(itemsWidth * 0.2)+"px");
		if (parseInt(ul.css("left")) > 0) {
			ul.css("left","0");
		}
		
		return false;
	});
	
	$("#nav-fwd").click(function() {
		var slideVal = $("#slider").slider("option","value");
		$("#slider").slider("option","value", slideVal+(itemsWidth * 0.2));
		if (slideVal > itemsWidth) {
			$("#slider").slider("option","value", itemsWidth);
		}
		
		ul.css("left",parseInt(ul.css("left"))-(itemsWidth * 0.2)+"px");
		if (parseInt(ul.css("left")) < itemsWidth - (itemsWidth * 2)) {
			ul.css("left",itemsWidth - (itemsWidth * 2)+"px");
		}
		
		return false;
	});*/
	
	//var itemsCount = 0;
	//var wait = false;
	
	// Thumbnail clicks
	/*$("#portfolio-nav ul a").click(function() {
		
		if (wait == false) {
		
			wait = true;
		
			$("#portfolio-nav li").removeClass("current");
			$(this).parent().addClass("current");
			
			$("#portfolio-nav li").each(function() {
				if ($(this).attr("class") != "current") {
					itemsCount++;
				} else {
					return false;
				}
			});
		
			var bg = $(this).attr("href");
			var portfolio = $("#portfolio-wrap");
			var item = $(this).attr("class");
			
			if ($(".portfolio-item:visible").hasClass(item) == false) {
			
				$("#transition").animate({opacity: "1"}, 500, function() {
				
					portfolio.css("background-image","url("+bg+")").attr("class","js").addClass(item);
					
					$("#transition").animate({opacity: "1"}, 200, function() {
					
						$("#transition").animate({opacity: "0"}, 600);
						
					});
					
				});
			
				$(".portfolio-item:visible").animate({left: "-480px"}, 300, function() {
				
					$(this).css("left","900px");
					
					$(this).find("#prev-next").appendTo(".portfolio-item."+item);
					
					$(".portfolio-item").fadeOut(300);
					
					$(".portfolio-item."+item).show().animate({left: "0", easing: "easein"}, 700, function() {
						wait = false;
					});
					
					if (itemsCount == 0) {
						$("#prev span").show();
						$("#prev a").hide();
						$("#next a").show();
						$("#next span").hide();
					} else if ((itemsCount+1) == $("#portfolio-nav li").length) {
						$("#next span").show();
						$("#next a").hide();
						$("#prev a").show();
						$("#prev span").hide();
					} else {
						$("#prev a").show();
						$("#prev span").hide();
						$("#next a").show();
						$("#next span").hide();
					}
					
					itemsCount = 0;
					
				});
			} else {
				wait = false;
			}
			
			return false;
		
		} else {
			
			return false;
			
		}
		
	});*/
	
	// Global back click
	/*$("#prev a").click(function() {
	
		if (wait == false) {
		
			wait = true;
		
			$("#portfolio-nav li").each(function() {
				if ($(this).attr("class") != "current") {
					itemsCount++;
				} else {
					$(this).removeClass("current");
					return false;
				}
			});
			
			var prevItem = $("#portfolio-nav li:eq("+(itemsCount-1)+")");
			var bg = prevItem.find("a").attr("href");
			var portfolio = $("#portfolio-wrap");
			var item = prevItem.find("a").attr("class");
			
			prevItem.addClass("current");
			
			if ($(".portfolio-item:visible").hasClass(item) == false) {
				
				$("#transition").animate({opacity: "1"}, 500, function() {
				
					portfolio.css("background-image","url("+bg+")").attr("class","js").addClass(item);
					
					$("#transition").animate({opacity: "1"}, 200, function() {
					
						$("#transition").animate({opacity: "0"}, 600);
						
					});
					
				});
				
				$(".portfolio-item:visible").animate({left: "-480px"}, 300, function() {
				
					$(this).css("left","900px");
					
					$(this).find("#prev-next").appendTo(".portfolio-item."+item);
					
					$(".portfolio-item").fadeOut(300);
					
					$(".portfolio-item."+item).show().animate({left: "0", easing: "easein"}, 700, function() {
						wait = false;
					});
					
					if (itemsCount <= 1) {
						$("#prev span").show();
						$("#prev a").hide();
					} else {
						$("#prev a").show();
						$("#prev span").hide();
					}
					
					$("#next a").show();
					$("#next span").hide();
					
					itemsCount = 0;
					
				});
				
			} else {
				wait = false;
			}
	
			return false;
		
		} else {
		
			return false;
		
		}
		
	});*/
	
	
	// Global next click
	/*$("#next a").click(function() {
	
		if (wait == false) {
		
			wait = true;
		
			$("#portfolio-nav li").each(function() {
				if ($(this).attr("class") != "current") {
					itemsCount++;
				} else {
					$(this).removeClass("current");
					return false;
				}
			});
			
			if (itemsCount < $("#portfolio-nav li").length) {
			
				var nextItem = $("#portfolio-nav li:eq("+(itemsCount+1)+")");
				var bg = nextItem.find("a").attr("href");
				var portfolio = $("#portfolio-wrap");
				var item = nextItem.find("a").attr("class");
				
				nextItem.addClass("current");
				
				if ($(".portfolio-item:visible").hasClass(item) == false) {
					
					$("#transition").animate({opacity: "1"}, 500, function() {
					
						portfolio.css("background-image","url("+bg+")").attr("class","js").addClass(item);
						
						$("#transition").animate({opacity: "1"}, 200, function() {
						
							$("#transition").animate({opacity: "0"}, 600);
							
						});
						
					});
					
					$(".portfolio-item:visible").animate({left: "-480px"}, 300, function() {
					
						$(this).css("left","900px");
						
						$(this).find("#prev-next").appendTo(".portfolio-item."+item);
						
						$(".portfolio-item").fadeOut(300);
						
						$(".portfolio-item."+item).show().animate({left: "0", easing: "easein"}, 700, function() {
							wait = false;
						});
						
						if ((itemsCount+2) == $("#portfolio-nav li").length) {
							$("#next span").show();
							$("#next a").hide();
						} else {
							$("#next a").show();
							$("#next span").hide();
						}
						
						$("#prev a").show();
						$("#prev span").hide();
						
						itemsCount = 0;
						
					});
					
				}
				
			} else {
				wait = false;
			}
			
			return false;
		
		} else {
			return false;
		}
	});*/
});

