$(document).ready(function(){

	refreshBasket();

	//Sub navigation horizontal slide

	
	
	
	var config = {    
		     sensitivity: 9, // number = sensitivity threshold (must be 1 or higher)    
		     interval: 100, // number = milliseconds for onMouseOver polling interval    
		     over: enter, // function = onMouseOver callback (REQUIRED)    
		     timeout: 1000, // number = milliseconds delay before onMouseOut    
		     out: leave // function = onMouseOut callback (REQUIRED)    
		};

	
	$("#subnav").hoverIntent(config);
	function enter(){
		
		$("#subnav").animate({ width: '306px' }, 550);

	};
	
	function leave(){
		
		$("#subnav").animate({ width: '69px' }, 500);
	};
	
	
	 $("#menu-nav").children("li").children("span.section").toggle(
		function () {
			$(this).text("−")
				.parent("li").addClass("on").end()
				.siblings("ol").fadeIn("fast");
		},
		function () {
			$(this).text("+")
				.parent("li").removeClass("on").end()
				.siblings("ol").fadeOut(200);
		}
	);
	$(".section li.on").trigger("click");

	//all other elements should be closed except for the selected 
	$("ol span.subSection").toggle(
			function () {
				$(this).text("−").parent().addClass("on");
				$(this).parent().children("ol").fadeIn("fast");
			},
			function () {
				$(this).text("+").parent().removeClass("on");
				$(this).parent().children("ol").fadeOut(200);
			}
		);
		$("ol.subSection li:not(.current) ol").hide();
		//$(".section li.on").trigger("click");

	
	
	
	// delete all basket items
	$("p.delete a").click(function() {
		var deleteurl = $(this).attr("href").split("?");
		$.ajax({
			type: "POST",
			url: deleteurl[0],
			data: deleteurl[1],
			success: function(msg){
				refreshBasket();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert(XMLHttpRequest);
				//alert(textStatus);
				//alert(errorThrown);
			}
		});
		return false;
	});
	
	// delete single cart item
	$("body").not("#print-basket").droppable({
		accept: "#cart-status img",
		drop: function(ev, ui) {
			var deleteurl = $("p.delete a").attr("href").split("?");
			var deleteid = ui.draggable.parent("a").attr("href").split("?id=");
			var currentid =  $("div.cart-links a").attr("href").split("?id=");
			$.ajax({
				type: "GET",
				url: deleteurl[0],
				data: "deleteId=" + deleteid[1] + "&id=" + currentid[1],
				success: function(msg){
					refreshBasket();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					//alert(XMLHttpRequest);
					//alert(textStatus);
					//alert(errorThrown);
				}
			});
		}
	});
	
	// add page to basket
	$("#thumbnail-slot img, #single-slot img").draggable({
		helper: "clone",
		cursorAt: { top: 30, left: 26 },
		opacity: 0.5
	});

	$("#print-basket").droppable({
		hoverClass: 'droppable-hover', 
		accept: "#thumbnail-slot img, #single-slot img",
		tolerance: "pointer",
		drop: function(ev, ui) {
			if ($("#thumbnail-slot").length > 0) {
				$.ajax({
					url: ui.draggable.parent("a").attr("href").replace("?","action/addBasket/?"),
					cache: false,
					success: function(html){
						refreshBasket();
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						//alert(XMLHttpRequest);
						//alert(textStatus);
						//alert(errorThrown);
					}
				});
			} else if ($("#single-slot").length > 0) {
				$.ajax({
					url: $("#nav a.single-page").attr("href").replace("?","action/addBasket/?"),
					cache: false,
					success: function(html){
						refreshBasket();
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						//alert(XMLHttpRequest);
						//alert(textStatus);
						//alert(errorThrown);
					}
				});
			}
		}
	});
	
	
	// search text
	var searchtext = $("#query").val();
	$("#query").focus(function() {
		if ($(this).val() == searchtext) {
			$(this).val("");
		};
	});
	$("#query").blur(function() {
		if ($(this).val() == "") {
			$(this).val(searchtext);
		};
	});

	$("#emailForm").validate();

	// IE6 alpha PNG fix
	//$('img[@src$=.png], #secondaryContent, #subnav, #subnav div.tab, #subnav div.wrap > div').ifixpng();

});


function refreshBasket() {
	var currentTime = new Date();
	var basketjson = $("#logo a").attr("href") + "action/basket/?mode=json&time=" + currentTime.getTime();
	$.getJSON(basketjson, function(data){
		$("#cart-status a").remove();
		
		$.each(data, function(i,item){
			$("#cart-status").append(item.thumbnail);
		});
		$("#cart-status img").draggable({
			helper: "clone"
		});
		
		var p = "pages"
		if (data.length == 1) {
			p = "page"
		};
		$("div.cart-links small").remove();
		if (data.length > 0) {
			$("div.cart-links a").text("Print PDF").after(" <small>(" + data.length + " " + p + ")</small>");
			if ($("p.add:visible")) {
				$("p.add").hide();
				$("p.delete").show();
			};
		} else {
			$("div.cart-links a").text("Print basket is empty");
			if ($("p.delete:visible")) {
				$("p.delete").hide();
				$("p.add").show();
			};
		};
	});
}