/*sortlakrids.dk*/
var pfullheight;
var pdefheight = 386;
var pagestotal = 0;
var slidelength = 400;

$(document).ready(function() {
	var $links = $("#products a");
	
	$links.each(function(i) {
		$(this).data("targethref",$(this).attr("href"));
		$(this).attr("href","javascript:;");
		$(this).click(openProduct);
	});
	
	
	enableProductPager();
});

function enableProductPager() {
	
	window.animationinprogress = false;
	
	while ($("#products > a").length > 0) {
		$("#products > a:lt(6)")
			.wrapAll("<div class='page'></div>");
	}
	
	pagestotal = $("#products div.page").length;
	$("<div></div>").addClass("pagecounter").appendTo("#products");
	updatePageCounter();
			
	$("#products > div.page:eq(0)").addClass("active");
	$("#products > div.page:gt(0)").css("display","none");
	
	$("<a></a>")
		.addClass("productarrow").addClass("leftarrow")
		.appendTo("#products");
		
	$("<a></a>")
		.addClass("productarrow").addClass("rightarrow")
		.appendTo("#products");
	
	function updatePageCounter() {
		$("#products div.pagecounter").empty();
		var cp = $("#products > div.active").prevAll().length + 1;
		
		for (var i = 1;i<=pagestotal;i++) {
			var $marker = $("<div></div>").addClass("pagemarker").appendTo("#products div.pagecounter");
			
			(i == cp) ?  $marker.addClass("activemarker") : $marker.addClass("inactivemarker");
			
			$marker.data("eq",i-1);
			var direction = (i < cp) ? -1 : 1;
			$marker.data("direction",direction);
			
			if (i != cp) {
				$marker.click(function(e) {
					/*alert("i am going to shift from the active to " + $(this).data("eq") + ", which is direction " + $(this).data("direction"));*/
					pageShift($("#products > div.active:eq(0)"),$("#products > div.page:eq(" + $(this).data("eq") + ")"),$(this).data("direction"));
				});
			}
		}
	}
	
	function pageShift($active,$next,direction) {
		if (window.animationinprogress) {
			return false;
		}
		window.animationinprogress = true;
		
		$active.removeClass("active");
		$next.addClass("active").show();
		
		var npos = $("#products").width() * direction;
		
		$next.css("left",npos + "px");
		
		$active.animate({"left":(npos*-1)+"px"},slidelength,function() {
			$(this).hide();
			window.animationinprogress = false;
		});
		
		$next.animate({"left":"0px"},slidelength);
		
		updatePageCounter();
	}
	
	$(".productarrow").click(function(e) {
		
		e.preventDefault();
		e.stopPropagation();
		
		var $active = $("#products > div.active");
		var direction = 0;
		
		if ($(this).hasClass("rightarrow")) {
			var $next = $active.next();
			
			if (!$next.hasClass("page")) {
				$next = $("#products div.page:first");
			}
			
			direction = 1;
		} else if ($(this).hasClass("leftarrow")) {	
			var $next = $active.prev();
			
			if (!$next.hasClass("page")) {
				$next = $("#products div.page:last");
			}
			
			direction = -1;
		}
		
		pageShift($active,$next,direction);
	});
	
	$(".productarrow").mousedown(function(e) {
		e.preventDefault();
		return false;
	})
}

function openProduct() {
	$.get($(this).data("targethref"),{rid:Math.random()},function(data,textStatus) {
		$(data).appendTo("#productview");		
		
		$("#productview").show();		
		$("#products").hide();
		$(".header_menu").hide();
		$("#sb_scrollbar").hide();
		
		var $backlink = $("#productview a.backlink");
		$backlink.attr("href","javascript:;");
		
		$("a.zoom").zoomify({'fullframe':true});
		
		$backlink.click(function() {
			$("#productview").empty();
			$("#productview").hide();
			$("#products").show();
			$(".header_menu").show();
			$("#sb_scrollbar").show();
			var scrollvalue = (pfullheight - pdefheight) - ($("#slider").slider('option','value'));
			$("#products").scrollTop(scrollvalue);
		});
	},'html');
}
