$(document).ready(function(){sectLnk();docInfo();bsktAdd();bsktRem();focusIn();});
function focusIn() {
	$("#show_hidden_documents a").live("click", function(e) {
		e.preventDefault();
		$("#show_hidden_documents").fadeOut("fast", function() {
			$("div[id^=product_item_]").slideDown();
		});
		
	});
	
	$("a[id^=document_focus_]").live("click", function(e) {
		e.preventDefault();
		var focusin="product_item_"+$(this).attr("id").substr(15);
		if($("div[id^=product_item_]:visible").length>1) {
			if($("#show_hidden_documents").is(':visible')){
				$("#show_hidden_documents").hide();
			} else {
				$("#product_list").append("<div id=\"show_hidden_documents\" style=\"display:none\"><h3>More documents are available in this section.</h3><p><a href=\"#\">Show hidden documents...</a></p></div>");
			}
			
			var match_req=$("div[id^=product_item_][id!="+focusin+"]:visible").length;
			var match_cur=0;
			$("div[id^=product_item_][id!="+focusin+"]").slideUp("",function() {
				match_cur++;
				if(match_cur===match_req) {
					if(!$("div[id="+focusin+"]").is(':visible')){
						$("div[id="+focusin+"]").slideDown("",function(){
							$("#show_hidden_documents").fadeIn("fast");
						});
					} else {$("#show_hidden_documents").fadeIn("fast");}
				}
			});
		} else {
			if($("div[id^=product_item_]:visible").attr("id")!==focusin) {
				$("#show_hidden_documents").hide();
				$("div[id^=product_item_][id!="+focusin+"]").hide();
				$("div[id="+focusin+"]").fadeIn("fast",function(){$("#show_hidden_documents").fadeIn("fast");});
			}
		}
	});
}
function sectLnk(){$("#section_links a").click(function(event) {$(this).addClass("loading");});}
function bsktAdd() {
	$("input[id^=document_link_add_]").live("click", function(e) {
		e.preventDefault();
		
		var moreinfo=$(this).parent().parent().parent().parent().parent().parent().children("div[id^=document_moreinfo_]");
		var addzone=$(this).parent().parent().parent().parent().parent().parent().children("div[id^=document_add_zone_]");
		if(moreinfo.is(':visible')) {
			$(this).html("More information...");
			moreinfo.slideUp();
		}
		
		$.ajax({type:"POST",url:"async.php",dataType:"json",cache:"false",data:({op:"ba",id:this.id}),
				beforeSend: function(xhr) {$(addzone).html("<img border=\"0\" src=\"/img/please-wait.gif\" width=\"16\" height=\"16\">");},	
				success: function(msg){
					$("#document_add_zone_"+msg["pid"]).html(msg["pidmsg"]);
					$("#show_hidden_documents").fadeOut("fast", function() {$("div[id^=product_item_]").slideDown();});
					$("#basket_summary").html(msg["basket_summary"]).slideDown();
						if(msg["basket_new_id"]!==undefined) {
							if($("#basket_items div[class=basket_item_last]").length===0) {
								$("#basket_items").prepend("<div class=\"basket_item_last\" id=\""+msg["basket_new_id"]+"\" style=\"display:none;\">"+msg["basket_new_item"]+"</div>");
							} else {
								$("#basket_items").prepend("<div class=\"basket_item\" id=\""+msg["basket_new_id"]+"\" style=\"display:none;\">"+msg["basket_new_item"]+"</div>");
							}
							$("#"+msg["basket_new_id"]).slideDown("fast");
						}
					}
				})
;});}
function bsktRem() {
	$("a[id^=basket_product_remove_]").live("click", function(e) {
		e.preventDefault();
		var ptr=this;
		var product_title=$(ptr).parent().parent().children("td[class=title]").html();
		var confirm_msg="Do you wish to remove the following item from your basket?\n\n"+product_title+"";

		if(confirm(confirm_msg.replace('&amp;','&'))) {	
			$.ajax({type:"POST",url:"async.php",dataType:"json",cache:"false",data:({op:"br",id:ptr.id}),
					beforeSend: function(xhr) {
						$(ptr).parent().replaceWith("<td class=\"wait\"><img src=\"../img/please-wait.gif\" width=\"16\" height=\"16\"></td>");},	
					success: function(msg){
			 			$("#basket_total_row_net").html(msg["basket_total_row_net"]);
				 		$("#basket_total_row_tax").html(msg["basket_total_row_tax"]);
				 		$("#basket_total_row_gross").html(msg["basket_total_row_gross"]);

						if($("#basket_total_row_gross td").length===0) {
							$("#basket_summary").hide();
							$("#basket_item_"+msg["ptr"]).parent().parent().replaceWith("<p>There are no items in your basket.</p><p><a href=\"/\">Click here to continue...</a></p>");
						} else {$("#basket_item_"+msg["ptr"]).remove();}
					}});
		}
	});
}
function docInfo() {
	$("a[id^=document_link_moreinfo_]")
	.live("click", function(event) {
		event.preventDefault();
		var moreinfo=$(this).parent().parent().parent().parent().parent().parent().children("div[id^=document_moreinfo_]");
		if(moreinfo.is(':visible')) {
			$(this).html("More information...");
			moreinfo.slideUp();
		} else {
			var ptr=this;
			$.ajax({type:"POST",url:"async.php",dataType:"json",cache:"false",data:({op:"mi",id:this.id}),
			  beforeSend: function(xhr) {
			  	$("#please_wait").remove();
			  	$(ptr).replaceWith("<div id=\"please_wait\"><img border=\"0\" src=\"/img/please-wait.gif\" width=\"16\" height=\"16\"></div>");
			  },
			  success: function(msg){
				$("#please_wait").replaceWith("<a id=\""+msg["more_info_link_id"]+"\" href=\"#\"><strong>&lt;</strong>&nbsp;Hide information...</a>");
				$.each(msg,function(key,val){if(key!="more_info_link_id"){$("#"+key).html(val);$("#"+key).slideToggle("normal");}});}
			});
		}
	});
}