jQuery(function($){
	
	//Datepicker elements
	$("input[name=publishDate], input[name=startDate], input[name=endDate]").datepicker({"dateFormat" : "yy-mm-dd"});
	
	/* COOKIE */
	if($("#remember").length > 0)
	{
		$.each($("#introContent > a"), function(){
			$(this).attr("href", $(this).attr("href") + "&r=false");
		});
		$("#remember").live("click", function(){
			if($(this).attr("checked") == true)
			{
				$.each($("#introContent > a"), function(){
					$(this).attr("href", $(this).attr("href").replace("false", "true"));
				});
			}
			else
			{
				$.each($("#introContent > a"), function(){
					$(this).attr("href", $(this).attr("href").replace("true", "false"));
				});
			}
		});
	}
	
	/* MIN MAX */
	$(".minmax").children(".minmax-body").hide();
	$(".minmax").live("click", function(){
		$(".minmax-body").slideUp("fast");
		$(".minmax-head").css("background-image", "url(assets/media/min.png)");
		if($(this).children(".minmax-body").is(":hidden")){
			$(this).children(".minmax-body").slideDown("fast");
			$(this).children(".minmax-head").css("background-image", "url(assets/media/max.png)");
		}
		else{
			$(this).children(".minmax-body").slideUp("fast");
			$(this).children(".minmax-head").css("background-image", "url(assets/media/min.png)");
		}
	});
	
	/* SLIDESHOW */
	if($("#slideshowContainer").length > 0)
		$("#slideshowContainer").slideshow();
	
	/* MEGA MENU */
	$("#header > #containerLogo > #menu > ul > li > a").live({
		"click" : function(event){
			var self = $(this);
			event.preventDefault();
			$.ajax({
				"type" : "post",
				"url" : "assets/snippets/ajax_get_megamenu.php",
				"data" : "urlName=" + self.attr("href"),
				"success" : function(data){
					if(data.length <= 1)
					{
						location.href = self.attr("href");
					}
				}
			});
		}
	});
	$("#header > #containerLogo > #menu > ul > li").live({
		"mouseenter" : function(){
			if($(this).data('over') != true)
			{
				$(this).data('over', true);
				$.each($(this).siblings("li"), function(){
					clearTimeout($(this).data('timeoutId'));
					$(this).css("padding-bottom", 0);
				});
				
				clearTimeout($(this).data('timeoutId'));
				
				var self = $(this);
				$.ajax({
					"type" : "post",
					"url" : "assets/snippets/ajax_get_megamenu.php",
					"data" : "urlName=" + self.children("a").attr("href"),
					"success" : function(data){
						if(data.length > 1)
						{
							self.css("padding-bottom", 35);
							
							var selfPos = self.position();
							var newLeft = (selfPos.left + self.outerWidth()/2) - 15;
							
							if($("#megamenu-wrapper").length == 0)
							{
								self.children("a").after('<div id="megamenu-pointer"></div><div id="megamenu-wrapper">' + data + '</div>');
								$("#megamenu-pointer").css("left", newLeft);
								$("#megamenu-wrapper, #megamenu-pointer").show();
							}
							else
							{
								$("#megamenu-wrapper").html(data);
								self.children("a").after($("#megamenu-wrapper"));
								$("#megamenu-pointer").css({
									"left" : newLeft
								});
							}
						}
						else if($("#megamenu-wrapper, #megamenu-pointer").length > 0)
						{
							$("#megamenu-wrapper, #megamenu-pointer").remove();
						}
					}
				});
			}
		},
		"mouseleave" : function(){
			var self = $(this);
			$(this).data('over', false);
			var timeoutId = setTimeout(function(){
				$("#megamenu-wrapper, #megamenu-pointer").remove();
				self.siblings().css("padding-bottom", 0);
				self.css("padding-bottom", 0);
			}, 350);
			$(this).data('timeoutId', timeoutId);
		}
	});
	
	/* CHILD MENU */
	$("#menuWrapper .isMenuGroup").live("click", function(event){
		
	});
	
	/* NEWS CAROUSEL */
	if($("#newsOfferContainer.start").length > 0)
	{
		$("#newsOfferContainer.start").append('<span class="news-backward">Tidigare nyheter</span><span class="news-forward">Nyare nyheter</span>');
		
		var firstId = $(".newsItem:first").attr("id");
		var lastId = $(".newsItem:last").attr("id");
		checkNewsButtons(firstId, lastId);
		fixPosition();
		
		// Forward click
		$(".news-forward.active").live("click", function(event){
			$(".news-forward").removeClass("active");
			
			$("#newsItems").append('<div class="newsItem replaceThisDiv" style="left: 768px; position: absolute; margin-left: 4px;"></div>');
			
			$.get("assets/snippets/ajax_get_newsitem.php?lastId=" + lastId, function(data){
				// Animate first
				$(".newsItem:first").animate({
					"margin-left" : "-252px"
				}, 300, function(){
					// Remove the first newsItem since its out side of the view
					$(this).remove();
				});
				
				var style = $(".replaceThisDiv").attr("style");
				$(".replaceThisDiv").replaceWith(data);
				$(".newsItem:last").attr("style", style);
				lastId = $(".newsItem:last").attr("id");
				firstId = $(".newsItem:eq(1)").attr("id");
				
				// Animate the last items
				$(".newsItem").animate({
					"margin-left" : "-252px"
				}, 300, function(){
					checkNewsButtons(firstId, lastId);
					fixPosition();
				});
			});
		});
		
		// Backward click
		$(".news-backward.active").live("click", function(){
			$(".news-backward").removeClass("active");
			$("#newsItems").prepend('<div class="newsItem replaceThisDiv" style="left: -256px; position: absolute;"></div>');
			
			$.get("assets/snippets/ajax_get_newsitem.php?firstId=" + firstId, function(data){
				// Animate last
				$(".newsItem:last").animate({
					"margin-left" : "252px"
				}, 300, function(){
					// Remove the first newsItem since its out side of the view
					$(this).remove();
				});
				
				var style = $(".replaceThisDiv").attr("style");
				$(".replaceThisDiv").replaceWith(data);
				$(".newsItem:first").attr("style", style);
				firstId = $(".newsItem:first").attr("id");
				lastId = $(".newsItem:eq(2)").attr("id");
				
				// Animate the last items
				$(".newsItem").animate({
					"margin-left" : "252px"
				}, 300, function(){
					checkNewsButtons(firstId, lastId);
					fixPosition();
				});
			});
		});
	}
	
	
	function fixPosition()
	{
		// Position the items
		$.each($(".newsItem"), function(){
			var index = $(".newsItem").index($(this));
			$(this).css({
				"position" : "absolute",
				"left" : index * 256 + "px",
				"margin-left" : "4px"
			});
		});
	}
	
	function checkNewsButtons(firstId, lastId)
	{
		$.get("assets/snippets/ajax_check_news.php?firstId=" + firstId + "&lastId=" + lastId, function(data){
			data = String(data);
			// alert(data);
			if(data.indexOf('forwards') > 0)
			{
				$(".news-forward").addClass("active");
			}
			if(data.indexOf('backwards') > 0)
			{
				$(".news-backward").addClass("active");
			}
		});
	}
	//control if the checkbox is touched.
	$("#rememberMe :checkbox").click(function()
	{
		//trigger the event if the checkbox is checked.
		if ($('#rememberMe :checkbox').attr('checked') == true) 
		{
			$("#various1").trigger("click");
		}	
	});
	//init the fancybox.
	$("#various1").fancybox({
				'titlePosition'		: 'inside',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
			
	$(".pt_small").mousemove(function(e){
		var id = $(this).attr("id");
		$("#pt_id_" + id).show();
		$("#pt_id_" + id).css({
			top: (e.pageY - ($("#pt_id_" + id).outerHeight()/2)+40) + "px",
			left: (e.pageX)-(($("html").outerWidth()/2))+500 + "px"
		});
	});
	$(".pt_small").mouseout(function(e){
		var id = $(this).attr("id");
		$("#pt_id_" + id).hide();
	});
});
