$(document).ready(function() {
	var add_products = "";
	var currentitems = $(".add-products").val();
	$('div.photo a').fancyZoom({scaleImg: true, closeOnClick: true});

	//Default Action
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	$(".main-expert").hide();

	$("#basics-click").click(function() {
		$(".main-basics").fadeIn();
		$(".main-expert").hide();
		$("#basics-click").addClass("suggest-active")
		$("#expert-click").removeClass("suggest-active")
	});
	$("#expert-click").click(function() {
		$(".main-basics").hide();
		$(".main-expert").fadeIn();
		$("#basics-click").removeClass("suggest-active")
		$("#expert-click").addClass("suggest-active")
	});


	var myArray = currentitems.split("|");  // Get the product ID
	for (var i=0; i < myArray.length; i++) {
		if (myArray[i].match('free')) {
		}
		else if (myArray[i].match('nil')) {
		}
		else {
		$("#" + myArray[i] + "-added").show();
		}
	}


	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(".tab_content").hide(); //Hide all tab content
		$(activeTab).fadeIn(); //Fade in the active content


		return false;
	});


	$("ul.main-additional-product-chosen li").hide(); //Hide all the products first tab
	$(".product-prices").hide();
	
	// On click event for products
	$("ul.main-additional-product-list li").click(function() {
		var display = $(this).attr('id'); //get the ID of the product being added
		$(this).hide();		// Hide this product

		var mySplitResult = display.split("-");  // Get the product ID
		var display_name = mySplitResult[0];

		add_products = $(".add-products").val();
		add_products = add_products + "|" + display_name;
		$(".add-products").val(add_products);

		display_price = $("." + display_name).html(); // get the price before we change the name
		save_price = $("." + display_name + "-save").html(); // get the save / rrp price before we change the name

		display_name = display_name + "-added";
		$("#" + display_name).show();		
		$(".main-image-bottominfotext").html("");


		// Change total price
		var totalprice = $(".total-price").html();
		var totalsave = $(".total-rrp").html();

		//Change to numbers
		totalprice = parseFloat(totalprice);
		totalsave = parseFloat(totalsave);
		display_price = parseFloat(display_price);

		save_price = parseFloat(save_price)
	
		totalprice = totalprice + display_price;
		totalsave = totalsave + save_price;

		totalprice = totalprice.toFixed(2);
		totalsave = totalsave.toFixed(2);

		var totalpercent = totalsave + totalprice;
		totalpercent = parseFloat(totalpercent);
		totalpercent = (totalsave / totalpercent) * 10;
		totalpercent = totalpercent.toFixed(0);

		$(".total-rrp").html(totalsave)
		$(".total-price").html(totalprice)
		$(".total-percent").html(totalpercent)
		
		
		
	});

	// On click event for products
	$("ul.main-additional-product-chosen li").click(function() {
		var display = $(this).attr('id'); //get the ID of the product being added
		$("#" + display).hide();
	
		var mySplitResult = display.split("-");
		var display_name = mySplitResult[0];

		add_products = $(".add-products").val();
		add_products = add_products.replace("|" + display_name, "");
		$(".add-products").val(add_products);
	
		display_price = $("." + display_name).html(); // get the price before we change the name
		save_price = $("." + display_name + "-save").html(); // get the save / rrp price before we change the name

		display_name = display_name + "-avail";
		$("#" + display_name).show();
	
		// Change total price
		var totalprice = $(".total-price").html();
		var totalsave = $(".total-rrp").html();

		//Change to numbers
		totalprice = parseFloat(totalprice)
		totalsave = parseFloat(totalsave);
		display_price = parseFloat(display_price)

		save_price = parseFloat(save_price)
		
		totalprice = totalprice - display_price;
		totalsave = totalsave - save_price;

		totalprice = totalprice.toFixed(2);
		totalsave = totalsave.toFixed(2);
		$(".total-rrp").html(totalsave)

		$(".total-rrp").html(totalsave)
		$(".total-price").html(totalprice)

	});


});
