var $$$ = jQuery;

$$$(document).ready(function() {
	tidy_menu();	 
	setup_contact_form();
	setup_mp3_group_selector();
	setup_basket_methods();
});

function tidy_menu() {
	$$$("#navigation ul li:first").addClass("first");
}

function setup_contact_form() {
	$$$("#contact-form").ajaxForm(function() {
		$$$("#contact-form").remove();
		$$$("#contact").append("<p>Thank you for sending us a message. Someone will be in touch soon!</p>");
	});
}

function setup_mp3_group_selector() {
	$$$("#mp3_group_select").change(function() {
		var str = "";
		
		str = $$$("#mp3_group_select option:selected").attr("class");
				
		$$$("#samples .selected").slideUp('slow', function() {				
			if(str != "") {
				$$$("#" + str).slideDown('slow').addClass("selected");
			}
		}).removeClass("selected");
	});
	
	// Repeat above code without the effects	
	//$$$("#mp3_group_select").change();
	
	var str = "";
		
	str = $$$("#mp3_group_select option:selected").attr("class");
			
	$$$("#samples .selected").css('display', 'none').removeClass("selected");
	
	if(str != "") {
		$$$("#" + str).css('display', 'block').addClass("selected");
	}
}

function setup_basket_methods() {
	// Block post-back from the product forms
	$$$(".product_form").submit(function() {
		return false;
	});
	
	// Add to the basket @ server, add HTML to the basket
	$$$(".btn_add_to_basket").click(function () {
		var desc = $$$(this).parent().find(".description").attr("value");
		var itemPrice = $$$(this).parent().find(".price").attr("value");
		var quantity = $$$(this).parent().find(".quantity").attr("value");

		if(quantity == 0) {
			alert("You can't add nothing to your basket!\nPlease increase the quantity and try again");
			return false;
		}
		
		var jsonUrl = "add_to_basket.ajax.php?description=" + escape(desc) + "&itemPrice=" + itemPrice + "&quantity=" + quantity;
		
		$$$.getJSON(jsonUrl, function(data) {
			if(data.item_id != false) {
				// Check whether this is the first item added
				initialise_basket();
				
				// Add to the basket view
				$$$("#basket table").append("<tr class=\"item\" id=\"" + data.item_id + "\"><td class=\"description\">" + desc + "</td><td class=\"quantity\">" + quantity + "</td><td class=\"item_price\">" + itemPrice + "</td><td class=\"delete\"><span class=\"del_item\">D</span></td></tr>");
				
				$$$("span.total").html(calculate_basket_total());				
						
				$$$(".del_item").click(del_item_click);
			}
		});
		
		$$$(this).parent().find(".quantity").attr("value", "0");
		
	});
	
	$$$(".del_item").click(del_item_click);
	
	// Auto-scroll the basket
	$$$(window).scroll(function(){
		var $basket = $$$("#sidebar #basket");
		
		if($basket.length > 0) {
			if($basket.css("marginTop").replace("px", "") < 850 || $$$(window).scrollTop() < 900) {
				$basket
					.stop()
					.animate({"marginTop": ($$$(window).scrollTop()) + "px"}, "slow" );
			}
		}
						
	});
	
}

function initialise_basket() {
	if($$$("#basket table").length == 0) {
		$$$("#basket p").remove();
		$$$("#basket").append("<table><tr><th>Description</th><th>Quantity</th><th colspan=\"2\">Price</th></tr></table>").append("<p>Basket Total: &pound;<span class=\"total\"></span></p><p><a href=\"checkout.php\">Go to the checkout</a></p>");
	}
}

function calculate_basket_total() {
	var totalPrice = 0;
				
	$$$("#basket table tr.item").each(function(i) {
		var itemQuantity 	= parseFloat($$$(this).find(".quantity").text());
		var itemPrice	 	= parseFloat($$$(this).find(".item_price").text());
		
		totalPrice += itemQuantity * itemPrice;
	});
	
	return totalPrice.toFixed(2);
}


function del_item_click() {
	$row = $$$(this).parent().parent();
		
	$$$.getJSON("delete_from_basket.ajax.php?id=" + $row.attr("id"), function(data) {
		if(data.deleted == "true") {
			$row.remove();
			
			if($$$("#basket .item").length == 0) {
				$$$("#basket").html("<h3>Your basket</h3><p>Your basket is empty</p>");
			}
			else {				
				$$$("span.total").html(calculate_basket_total());					
			}
		}
	});	
}