$(document).ready(function(){ 
	
	$("#basketItemsWrap li:first").hide();
	$("#slidingTopContent").hide();
	
	$("#slidingTopTrigger").live("click", function(event) {
      	$("#slidingTopContent").slideToggle("slow", function(){
			if ($("#slidingTopContent").is(":visible")) {
				$("#slidingTopFooterLeft").html('<img src="assets/images/arrow-up.png" alt="Hide Downloads" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Hide Downloads</a>');
				$("#slidingTopFooter").css("background-color","#ebebeb");
				//$("#slidingTopTrigger").css("color","#666666");
				$('span.welcome').css("z-index","9");
				
			} else {
				var countLI2 = $('#basketItemsWrap ul li').length;
				countLI2=countLI2-1;
				$("#slidingTopFooterLeft").html('<img src="assets/images/arrow-down.png" alt="Show Downloads" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Show Downloads ('+countLI2+')</a>');
				$("#slidingTopFooter").css("background-color","#000000");
				//$("#slidingTopTrigger").css("color","#F0F0F0");
				$('span.welcome').css("z-index","11");
			}
			
		});
    }); 
	
$('#basketItemsWrap ul li').each(function(index) {
	if(index>0){
	var productIDValSplitterX 	= (this.id).split("_");
		var productIDValX 			= productIDValSplitterX[1];
		var countLI = $('#basketItemsWrap ul li').length;
		countLI=countLI-1;
		var linkText=$(this).attr("title");
		/*if($("#productImageWrapID_" + productIDValX)){
		var parentTag = $("#productImageWrapID_" + productIDValX).parent().get(0).tagName;
		if(parentTag=="TD"){
			linkText="";
		}
		}*/
		$("#slidingTopFooterLeft").html('<img src="assets/images/arrow-down.png" alt="Show Downloads" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Show Downloads ('+countLI+')</a>');
		$("ul#downloadMain #productImageWrapID_" + productIDValX).html( "<img class='processing_"+productIDValX+"' src='assets/images/check.gif'>" );
		$("table#DownloadTable #productImageWrapID_" + productIDValX +" span").html( "<span><img class='processing_"+productIDValX+"' src='assets/images/check.gif'>"+linkText+"</span>" );
		$("table#DownloadTable #productImageWrapID_" + productIDValX +" a").contents().unwrap();

		$("#productImageWrapIDF_" + productIDValX).html( "<img class='processing_"+productIDValX+"' src='assets/images/check.gif'>"+linkText  );
		$("#productImageWrapIDR_" + productIDValX).html( "<img class='processing_"+productIDValX+"' src='assets/images/check.gif'>"+linkText  );
	}
    
  });

	/*$( "#email-cart,#email-cart2" )
			.button()
			.click(function() {
				$( "#email-form" ).dialog( "open" );
			});*/


	$(".productPriceWrapRight a img, .productLink a").live("click", function(event) {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var linkText=$(this).text();
		if(linkText==""){
			if($(this).attr("title")){
			linkText=$(this).attr("title");
			}
		}
		var holdLink0=$("#productImageWrapID_" + productIDVal).html();
		var holdLink1=$("#productImageWrapIDF_" + productIDVal).html();
		var holdLink2=$("#productImageWrapIDR_" + productIDVal).html();
		$.ajax({  
			type: "POST",  
			url: "./controllers/functions.php",  
			data: { downloadID: 0, action: "checkForCart"},  
			success: function(basketStatus) {
				if(basketStatus<1){
					cookie_value=getCookie('explanation');
					if (cookie_value=="") {
						
						$( "#basketConfirm" ).dialog({
							resizable: false,
							height:400,
							width:500, 
							modal: true,
							buttons: {
								"OK": function() {
									$( this ).dialog( "close" );
								}
							}
						});
					}
					setCookie('explanation','CartExplanation',365)
				}
				if(basketStatus>20){
				alert("To limit the strain on server resources, please limit your Download Basket to 20 items.");
				event.stopPropagation();
				}
			
				if(basketStatus<21){
					if ($("#slidingTopContent").is(":visible")) {
						
						$("#productImageWrapID_" + productIDVal).html( "<img class='processing_"+productIDVal+"' src='assets/images/loading.gif'>"+linkText  );
						$("#productImageWrapIDF_" + productIDVal).html( "<img class='processing_"+productIDVal+"' src='assets/images/loading.gif'>"+linkText  );
						$("#productImageWrapIDR_" + productIDVal).html( "<img class='processing_"+productIDVal+"' src='assets/images/loading.gif'>"+linkText  );

						$("#notificationsLoader").html('<img src="assets/images/loading.gif">');
					
						$.ajax({  
							type: "POST",  
							url: "./controllers/functions.php",  
							data: { downloadID: productIDVal, action: "addToBasket"},  
							success: function(theResponse) {
								
								if(theResponse=="1"){
									alert("Adding this file would make your basket too large to process.  Please Download the cart now. You can clear the basket and add new items after you download the current cart.");
									$("#productImageWrapID_" + productIDVal).html( holdLink0 );
									$("#productImageWrapIDF_" + productIDVal).html(  holdLink1  );
									$("#productImageWrapIDR_" + productIDVal).html(  holdLink2  );
									$("#notificationsLoader").empty();	
									event.stopPropagation();
								}else{
									if( $("#downloadID_" + productIDVal).length > 0){
										$("#downloadID_" + productIDVal).animate({ opacity: 0 }, 500);
										$("#downloadID_" + productIDVal).before(theResponse).remove();
										$("#downloadID_" + productIDVal).animate({ opacity: 0 }, 500);
										$("#downloadID_" + productIDVal).animate({ opacity: 1 }, 500);
										$("#notificationsLoader").empty();
										$(".processing_" + productIDVal).replaceWith('<img class="processing_'+productIDVal+'" src="assets/images/check.gif">');
										
									} else {
										$("#basketItemsWrap li:first").before(theResponse);
										$("#basketItemsWrap li:first").hide();
										$("#basketItemsWrap li:first").show("slow");  
										$("#notificationsLoader").empty();	
										$(".processing_" + productIDVal).replaceWith('<img class="processing_'+productIDVal+'" src="assets/images/check.gif">');
									}
								}
							}//end function  
						}); //end ajax

					} else {
						
						$("#slidingTopContent").slideToggle("slow", function(){		
																			 
							$("#slidingTopFooterLeft").html('<img src="assets/images/arrow-up.png" alt="Hide Downloads" /> <a href="aaa.htm" onclick="return false;" id="slidingTopTrigger">Hide Downloads</a>');
							
							$("#notificationsLoader").html('<img src="assets/images/loading.gif">');
							
							$("#productImageWrapID_" + productIDVal).html( "<img class='processing_"+productIDVal+"' src='assets/images/loading.gif'>"+linkText  );
							$("#productImageWrapIDF_" + productIDVal).html( "<img class='processing_"+productIDVal+"' src='assets/images/loading.gif'>"+linkText  );
							$("#productImageWrapIDR_" + productIDVal).html( "<img class='processing_"+productIDVal+"' src='assets/images/loading.gif'>"+linkText  );
						
							$.ajax({  
								type: "POST",  
								url: "./controllers/functions.php",  
								data: { downloadID: productIDVal, action: "addToBasket"},  
								success: function(theResponse) {
									if(theResponse=="1"){
										alert("Adding this file would make your basket too large to process.  Please Download the cart now. You can clear the basket and add new items after you download the current cart.");
										$("#productImageWrapID_" + productIDVal).html( holdLink0 );
										$("#productImageWrapIDF_" + productIDVal).html(  holdLink1  );
										$("#productImageWrapIDR_" + productIDVal).html(  holdLink2  );
										$("#notificationsLoader").empty();	
										event.stopPropagation();
									}else{	
										if( $("#downloadID_" + productIDVal).length > 0){
											$("#downloadID_" + productIDVal).animate({ opacity: 0 }, 500);
											$("#downloadID_" + productIDVal).before(theResponse).remove();
											$("#downloadID_" + productIDVal).animate({ opacity: 0 }, 500);
											$("#downloadID_" + productIDVal).animate({ opacity: 1 }, 500);
											$("#notificationsLoader").empty();
											$(".processing_" + productIDVal).replaceWith('<img class="processing_'+productIDVal+'" src="assets/images/check.gif">');
											
										} else {
											$("#basketItemsWrap li:first").before(theResponse);
											$("#basketItemsWrap li:first").hide();
											$("#basketItemsWrap li:first").show("slow");  
											$("#notificationsLoader").empty();			
											$(".processing_" + productIDVal).replaceWith('<img class="processing_'+productIDVal+'" src="assets/images/check.gif">');
										}
									}
								}  
							}); 
							
							
							$("#slidingTopTrigger").fadeTo(4000, 1, function(){
								$("#slidingTopContent").slideToggle("slow", function(){
									var countLI3 = $('#basketItemsWrap ul li').length;
									countLI3=countLI3-1;
									$("#slidingTopFooterLeft").html('<img src="assets/images/arrow-down.png" alt="Show Downloads" /> <a href="aaa.htm" onclick="return false;" id="slidingTopTrigger">Show Downloads ('+countLI3+')</a>');												 
								});
								
							});

						});												 
					}
				}
		}
		});		
		return false;
	});
	
	
	
	$("#basketItemsWrap li img,#basketItemsWrap li span").live("click", function(event) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
	
		$("#notificationsLoader").html('<img src="assets/images/loading.gif">');
	
		$.ajax({  
		type: "POST",  
		url: "./controllers/functions.php",  
		data: { downloadID: productIDVal, action: "deleteFromBasket"},  
		success: function(theResponse) {
			
			$("#downloadID_" + productIDVal).hide("slow",  function() {
				$(this).remove();
			});
			$("#notificationsLoader").empty();
			$(".processing_" + productIDVal).replaceWith('<img id="featuredProduct_' + productIDVal +'" alt="Undo Remove for basket" src="assets/images/undo.png">');
			$("#productImageWrapID_" + productIDVal).wrapInner('<a href="./controllers/functions.php?action=addToBasket&downloadID='+productIDVal+'" onclick="return false;"></a>');
			$("#productImageWrapIDF_" + productIDVal).wrapInner('<a href="./controllers/functions.php?action=addToBasket&downloadID='+productIDVal+'" id="featuredProduct_' + productIDVal +'"></a>');
			$("#productImageWrapIDR_" + productIDVal).wrapInner('<a href="./controllers/functions.php?action=addToBasket&downloadID='+productIDVal+'" id="featuredProduct_' + productIDVal +'"></a>');

		}
					
		});  	
		return false;
	});

	
	function setCookie(c_name,value,expiredays)
	{
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	} 

	function getCookie(c_name)
	{
		if (document.cookie.length>0)
		  {
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1)
			{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
			}
		  }
		return "";
	}

	function checkCookie(c_name)
	{
		cookie_value=getCookie(c_name);
		if (cookie_value=="") {
			$('#survey').dialog('open');
		}
	}

	function deleteCookie(c_name) {
		document.cookie = c_name +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
	}

	
});
