$(document).ready(function() {
	$("ul.colorSelector li").click(function() {
	var stylePath = 'style/theme_' + $(this).attr("class").replace(' colorSelected', '') + '.css';
	var curStylePath = $("head").children(":last").attr("href").replace(' colorSelected', '');
	if ($("head").children(":last").attr("href") != stylePath) {
		$("div.container").hide();
		changeTheme(stylePath);
		$("div.container").fadeIn(1000);
	}
	});
	
	changeTheme = function(stylePath) {
		if (stylePath.length == 0) { stylePath = 'style/theme_green.css'; }
		$("head").append("<link>");
	    css = $("head").children(":last");
	    css.attr({
			type: 'text/css',
			rel: 'stylesheet',
			href: stylePath
		});
	    stylePath = stylePath.replace('style/theme_', '');
	    stylePath = stylePath.replace('.css', '');
	    $("ul.colorSelector li.colorSelected").removeClass("colorSelected");
	    $("ul.colorSelector li." + stylePath).addClass("colorSelected");
	    var exdate=new Date();
	    exdate.setDate(exdate.getDate()+31);
	    document.cookie='selectedThemeColor=green; expires=Fri, 27 Jul 2001 00:00:00 UTC; path=/';
	    document.cookie="selectedThemeColor=" + stylePath + "; expires=" + exdate.toUTCString() + "; path=/";
	};
	
	shuffleImage = function () {
		$("div.imageDiv").cycle('toggle');
	};

	$("ul.topnav li a").click(function() {
		switchTo($(this).attr("name"));
	});
	
	doubleClick = function() {
		$("div.content").html('<h3>Dubbelklicka inte på webben.</h3><a href="#" onclick="switchTo(\'home\');">Till startsidan</a>');
	};
	
	$("ul.topnav li span").hover(function() {
		$(this).parent().find("ul.subnav").slideDown('fast').show();
		$(this).parent().hover(function() {
		}, function() {
			$(this).parent().find("ul.subnav").hide();
			$(this).parent().find('span').removeClass("subhover");
		});
	}).hover(function() {
		$(this).addClass("subhover");
		if ($(this).parent().find("ul.subnav").is(':visible')) {
			$(this).parent().addClass("menu-icon-hover");
		}
	}, function() {
		$(this).removeClass("subhover");
		$(this).parent().removeClass("menu-icon-hover");
	});
	
	$("ul.subnav").hover(function() {
		$(this).parent().find('span').addClass("subhover");
	});
	
	switchTo = function(srcFile) {
		$("div.imageDiv").cycle('destroy');
		$("div.content").slideUp("400").hide("500");
		if ($("ul.subnav").is(':visible')) { $("ul.subnav").fadeOut("slow"); }
		if ($("div.content").is(":hidden")) {
			$.ajax({
				dataType: "html",
				contentType: "charset=utf-8",
				url: srcFile + ".html",
				cache: true,
				error: function (request, status, error) {
					$("div.content").append('<h3>Sidan hittades ej. Fel URL?</h3><a href="#" onclick="switchTo(\'home\');">Till startsidan</a>');
					$("div.content").slideDown("400").show("500");
			    },
				success: function(html){
					var url = "?page=" + srcFile;
					document.location.hash = (url);
					$("div.content").html(html);
					$("div.imageDiv").hide();
					$("div.imageDiv").after('<p id="shuffleText" style="text-align: center; font-weight: bold;">Bildspel laddar...</p>');
					$("#shuffleText").before('<div class="imageDivPlaceHolder"></div>');
					
					
					var linkTotal = $("div.content").find("a").length;
				    var linkCount = 0;
			    	while (linkCount < linkTotal) {
			    		var link = $("div.content").find("a")[linkCount];
			    		if ($(link).attr("href").indexOf("http://") != -1 || $(link).attr("href").indexOf("https://") != -1) {
			    			$(link).attr("target", "_blank");
			    		}
			    		linkCount++;
			    	}
					
					$("div.content").slideDown("400").show("500");
					$("div.imageDiv").click(function() {
						$("div.imageDiv").cycle('toggle');
					}).dblclick(function() { doubleClick(); });
					var i = 0;
					while (i < $("div.content").find("img").length) {
						var img = $("div.content").find("img")[i];
						if ($(img).parent().attr("class") != "imageDiv") {
							$(img).attr("id", "00" + i);
							var loaderId = "loaderId" + $(img).attr("id");
							if ($(img).height() > 0) { imgHeight = $(img).height(); }
							$(img).after('<div id="' + loaderId + '" class="loader"></div>');
							resize($(img).attr("id"), loaderId);
							$(img).hide();
					        $(img).load(function () {
					        	var newLoadId = "loaderId" + $(this).attr("id");
					        	$("#" + newLoadId).remove();
					            $(this).fadeIn("slow");
					        });
						}
						i = i + 1;
					}
					var imageTotal = $('img').length;
				    var imageCount = 0;
				    $('img').load(function(){ 
				    	if(++imageCount == imageTotal) {
							$("div.imageDiv").cycle({ 
							    fx: 'shuffle',
							    timeout: 2000,
							    next: $('div.imageDiv')
							});
							$("#shuffleText").hide();
							$('div.imageDivPlaceHolder').hide();
				    		$('#shuffleText').html('Klicka på bilden för att se nästa.');
				    		$("div.imageDiv").fadeIn("slow");
				    		$("#shuffleText").fadeIn("slow");
							setTimeout(function() {  $("div.imageDiv").cycle('pause'); }, 400);
				    	}
				    });
				}
			});
		} else { setTimeout(function() { switchTo(srcFile); }, 200); }
	};
	resize = function(imgObjId, loaderObjId) {
		if ($('#'+imgObjId).width() > 0 || $('#'+imgObjId).height() > 0) {
			$('#'+loaderObjId).css({
				'width' : $('#'+imgObjId).width() + 'px',
				'height' : $('#'+imgObjId).height() + 'px'
			});
		}  else { setTimeout(function() { resize(imgObjId, loaderObjId); }, 200); }
	};
	var year = new Date().getFullYear();
	$("div.footer").html("<p>Copyright " + year + " © All rights reserved. Made By <b>Veronica Hall</b> | <a href=\"mailto:veronica.hall@mhm.se\">veronica.hall@mhm.se</a></p>");
});