var slidingHideHtml = '<img src="images/arrow-up.png" alt="隐藏购物车" /> <a href="#" onclick="return false;" id="slidingTopTrigger">隐藏购物车</a>';
var slidingShowHtml = '<img src="images/arrow-down.png" alt="显示购物车" /> <a href="#" onclick="return false;" id="slidingTopTrigger">显示购物车</a>';

$(document).ready(function(){ 
	
	$("#basketItemsWrap li:first").hide();
	$("#slidingTopContent").hide();
	
	$("#slidingTopTrigger").live("click", function(event) {
      	$("#slidingTopContent").slideToggle("slow", function(){
			if ($("#slidingTopContent").is(":visible")) {
				$("#slidingTopFooterLeft").html(slidingHideHtml);
			} else {
				$("#slidingTopFooterLeft").html(slidingShowHtml);
			}
		});
    }); 
	
	$("#slidingReloadCart").click(function() {
		$("#notificationsLoader").html('<img src="images/loader.gif">');
		$("#basketItemsWrap ul").empty();
		$.ajax({  
		type: "POST",  
		url: "api/cart.php",  
		data: { time: Math.random, action: "reloadCart"},  
		success: function(theResponse) {
			
			$("#basketItemsWrap li").before(theResponse);
			$("#basketItemsWrap li").hide();
			$("#basketItemsWrap li").show("slow");  
			$("#notificationsLoader").empty();
			
		}  
		}); 
	});
	
		
	$("#slidingEmptyCart").click(function() {
		$("#notificationsLoader").html('<img src="images/loader.gif">');
		$.ajax({  
		type: "POST",  
		url: "api/cart.php",  
		data: {time: Math.random, action: "emptyCart"},  
		success: function() {
			$("#basketItemsWrap li").empty();
			$("#notificationsLoader").empty();
			}  
		}); 
	});

	$(".rightinfo_button a img").click(function() {
		var articleIDValSplitter 	= (this.id).split("_");
		var articleIDVal 		= articleIDValSplitter[1];

		
		if ($("#slidingTopContent").is(":visible")) {

			$("#notificationsLoader").html('<img src="images/loader.gif">');
		
			$.ajax({  
			type: "POST",  
			url: "api/cart.php",  
			data: {time: Math.random, articleID: articleIDVal, action: "addToCart"},  
			success: function(theResponse) {
				
				if( $("#articleID_" + articleIDVal).length > 0){
					$("#articleID_" + articleIDVal).animate({ opacity: 0 }, 500);
					$("#articleID_" + articleIDVal).before(theResponse).remove();
					$("#articleID_" + articleIDVal).animate({ opacity: 0 }, 500);
					$("#articleID_" + articleIDVal).animate({ opacity: 1 }, 500);
					$("#notificationsLoader").empty();
					
				} else {
					$("#basketItemsWrap li:first").before(theResponse);
					$("#basketItemsWrap li:first").hide();
					$("#basketItemsWrap li:first").show("slow");  
					$("#notificationsLoader").empty();			
				}
				
			}  
			}); 

		} else {
			
			$("#slidingTopContent").slideToggle("slow", function(){		
																 
				$("#slidingTopFooterLeft").html(slidingHideHtml);
				$("#notificationsLoader").html('<img src="images/loader.gif">');
			
				$.ajax({  
				type: "POST",  
				url: "api/cart.php",  
				data: {time: Math.random, articleID: articleIDVal, action: "addToCart"},  
				success: function(theResponse) {
					
					if( $("#articleID_" + articleIDVal).length > 0){
						$("#articleID_" + articleIDVal).animate({ opacity: 0 }, 500);
						$("#articleID_" + articleIDVal).before(theResponse).remove();
						$("#articleID_" + articleIDVal).animate({ opacity: 0 }, 500);
						$("#articleID_" + articleIDVal).animate({ opacity: 1 }, 500);
						$("#notificationsLoader").empty();
						
					} else {
						$("#basketItemsWrap li:first").before(theResponse);
						$("#basketItemsWrap li:first").hide();
						$("#basketItemsWrap li:first").show("slow");  
						$("#notificationsLoader").empty();			
					}
					
				}  
				}); 
				
				
				$("#slidingTopTrigger").fadeTo(5000, 1, function(){
					$("#slidingTopContent").slideToggle("slow", function(){
						$("#slidingTopFooterLeft").html(slidingShowHtml);												 
					});
					
				});

			});												 
		}
				
	});
	
	
	
	$("#basketItemsWrap li img").live("click", function(event) { 
		var articleIDValSplitter 	= (this.id).split("_");
		var articleIDVal 			= articleIDValSplitter[1];	
	
		$("#notificationsLoader").html('<img src="images/loader.gif">');
	
		$.ajax({  
		type: "POST",  
		url: "api/cart.php",  
		data: { articleID: articleIDVal, action: "delFromCart"},  
		success: function(theResponse) {
			
			$("#articleID_" + articleIDVal).hide("slow",  function() {
				$(this).remove();
			});
			$("#notificationsLoader").empty();
		
		}
					
		});  	
		
	});

});
