$(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"));
		document.location.hash = $(this).attr("name");
	});
	
	toggleNews = function(year) {
		if ($("ul." + year).is(":hidden")) {
			$("ul." + year).slideDown("400").show("500");
		} else {
			$("ul." + year).slideUp("400").hide("500");
		}
	}
	
	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) {
		if (srcFile.length <= 0) {
			srcFile = 'home';
		}
		/* $("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("600");
			    },
				success: function(html){
					var url = srcFile;

					$("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("600");
					
					// $("div.imageDiv").click(function() {
						// $("p.imageAlt").fadeOut("fast");
						// $("div.imageDiv").cycle('toggle');
						// setTimeout(function() { 
						// var images = $("div.imageDiv img");
						// for(i = 0; i < images.length; i++){
							// var image = images[i];
							// if (!$(image).is(":hidden")) {
								// if ($(image).attr("alt").length > 0 && $(image).attr("alt") != "image") {
									// $('.imageAlt').html($(image).attr("alt"));
									// $("p.imageAlt").fadeIn("slow");
								// } else {
									// $('.imageAlt').html("");
								// }
							// }
						// }}, 2200);
					// }).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).wrap('<span class="imgframe" />');
							$(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();
							$('div.imageDiv').append('<p class="imageAlt"></p>');
							if ($('img').attr("alt").length > 0 && $('img').attr("alt") != "image") {
								$('.imageAlt').html($('img').attr("alt"));
							}
				    		$('#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);
				    	}
				    }); */
					
					
					$('.imageDiv').addClass('ad-gallery');
					
					$('.imageDiv img').each(function(index) {
						$(this).wrap('<li><a href="' + $(this).attr('src') + '"></a></li>');
					});
					
					$('.imageDiv').wrapInner('<div class="ad-nav"><div class="ad-thumbs"><ul class="ad-thumb-list"></ul></div></div>');
					
					$('.ad-nav').before('<div class="ad-image-wrapper"></div>');
					
					  $(function() {
						var galleries = $('.imageDiv').adGallery();
						$('#switch-effect').change(
						  function() {
							galleries[0].settings.effect = $(this).val();
							return false;
						  }
						);
						$('#toggle-slideshow').click(
						  function() {
							galleries[0].slideshow.toggle();
							return false;
						  }
						);
						$('#toggle-description').click(
						  function() {
							if(!galleries[0].settings.description_wrapper) {
							  galleries[0].settings.description_wrapper = $('#descriptions');
							} else {
							  galleries[0].settings.description_wrapper = false;
							}
							return false;
						  }
						);
					  });
				}
			});
		} 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>");
	
	function locationHashChanged() {
		switchTo(document.location.hash.replace("#", ""));
	}

	window.onhashchange = locationHashChanged;
	
});
