$(document).ready(function() {
	$("a.account").click(function(){
		var txt = $(this).parent().find(".nd").html() + "<div class='up rgt'></div>";
		var pos = $(this).offset();
		$("#tt").slideUp(50,function(){
			$("#tt").html(txt).css("margin-left","0px").css("width","200px").css("top",(pos.top+23)).css("left",(pos.left-175)).slideDown(200);
		});
		return false;
	});
	$("a.share").click(function(){
		var txt = $(this).next("div").html();
		var pos = $(this).offset();
		$("#tt").slideUp(50,function(){
			$("#tt").html(txt).css("margin-left","0px").css("width","172px").css("top",(pos.top+23)).css("left",(pos.left)).slideDown(200);
		});
		return false;
	});

	$("a.wish").click(function(){
		var product = $(this).classData('prod'); 
		var option = $(this).classData('opt');
		var pos = $(this).offset();
		$("#tt").fadeOut(50,function(){
			$("#tt").html("<div id='ttcon'><div class='loading'></div></div><div class='up lft'></div>").css("margin-left","0px").css("width","172px").css("top",(pos.top+23)).css("left",pos.left).slideDown(200,function(){
				$.ajax({
					cache: false,
					type: "GET",
					url: "/php/shop/",
					dataType: "html",
					data: "action=wish&product="+product+"&option="+option+"&x=1",
					success: function(resultdata) {
						$("#ttcon").html(resultdata);
					}
				});				
			});
		});		
		return false;
	});
	$(".unwish").live('click', function() {
		var thislink = $(this);
		var idbits = $(this).attr("id").split("-");
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/",
			dataType: "html",
			data: "action=unwish&id="+idbits[1]+"&x=1",
			success: function(resultdata) {
				var parts = resultdata.split("|*|");
				$("#row-"+idbits[1]).slideUp(500, function() {
					$("#row-"+idbits[1]).remove();
				});
			}
		});
		return false;
	});



	$("a.enquire").click(function(){
		var product = $(this).classData('prod'); 
		var option = $(this).classData('opt');
		var pos = $(this).offset();
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/getenquirytext.php",
			dataType: "html",
			data: "product="+product+"&option="+option,
			success: function(resultdata) {
				var parts = resultdata.split("|*|");
				var emailbody = parts[0];
				var subject = parts[1];
				var email = parts[2];
				var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+emailbody;
				window.location = mailto_link;
			}
		});
		return false;
	});

	$("a.buy").click(function(){
		var product = $(this).classData('prod'); 
		var option = $(this).classData('opt');
		var type = $(this).classData('type');
		var pos = $(this).offset();

		$("#tt").fadeOut(50,function(){
			$("#tt").html("<div id='ttcon'><div class='loading'></div></div><div class='up lft'></div>").css("margin-left","0px").css("width","172px").css("top",(pos.top+40)).css("left",pos.left).slideDown(200,function(){
			
				$.ajax({
					cache: false,
					type: "GET",
					url: "/php/shop/",
					dataType: "html",
					data: "action=buy&product="+product+"&option="+option+"&type="+type+"&x=true",
					success: function(resultdata) {
						$("#ttcon").html(resultdata);
					}
				});				
			});
		});		
		return false;
	});

	$("a.signin").live("click",function(){
		var email = $(this).parent().parent().find('input[name="email"]').val();
		var password = $(this).parent().parent().find('input[name="password"]').val();
		alert(password);
		return false;
	});


	$("a.currency").click(function(){
		var thislink = $(this);
		$("#tt").slideUp(100,function(){
			var href= thislink.attr("href");
			var pos = thislink.offset();
			$("#tt").html("<p>Foreign currencies are indicational only. Please note that shop transactions will be made in GBP.</p><p class='mts centred'><a class='ctt' href='#'>Cancel</a>&nbsp;&nbsp;&nbsp;<a href='"+href+"'>OK</a></p><div class='up rgt'></div>").css("margin-left","0px").css("width","200px").css("top",(pos.top+23)).css("left",(pos.left-192)).slideDown(200);
		});
		return false;
	});

	$(".ctt").live("click",function(){
		$("#tt").fadeOut(200,function(){
			$(this).empty();
		});
		return false;
	});
	
	$("input.quant").focus(function(){
		$(this).parent().find(".nd").fadeIn(300);
	});


	$(".showoi").click(function (){
		$(this).parent().parent().parent().parent().find(".oilist").slideToggle(300, function() {
    		
  		});
		return false;
	});
	$(".shipcountrybskt").live('change', function() {
		var selectedcountry = $(this).val();
		window.location = "/php/shop/php/changecountry.php?country="+selectedcountry;
	});
	$("#sameaddress").live('change', function() {
		if ($(this).is(':checked')){
			//hide
			$(".hideifsame").slideUp(300);
			$(".hideifsame").find(".required").removeClass("required").addClass("req");
		} else {
			$(".hideifsame").slideDown(300);
			$(".hideifsame").find(".req").removeClass("req").addClass("required");
		}
	});
	$("#subscribe").live('change', function() {
		if ($(this).is(':checked')){
			//hide
			$(".hideifnoemail").fadeIn(50);
		} else {
			$(".hideifnoemail").fadeOut(50);
		}
	});
	$(".shippingmethod").live('change', function() {
		var shippingmethod = $(this).val();
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/changeshippingmethod.php",
			dataType: "html",
			data: "shippingmethod="+shippingmethod,
			success: function(resultdata) {
				var parts = resultdata.split("|*|");
				$(".shippingoptions").html(parts[0]);
				$(".basketlabels").html(parts[1]);
				$(".baskettotals").html(parts[2]);
			}
		});
	});
	$(".reset").live('click', function() {
		var loginform = $("form[name='login']");
		loginform.attr("action","/php/shop/php/reset.php");
		$("#password").parent().parent().remove();
		$("#remember").parent().parent().remove();
		$("#loginsubmit").attr("value","Reset");
		return false;
	});
	$(".saveitem").live('click', function() {
		var product = $(this).attr("id").replace("prod","");
		var action = "add";
		var thislink = $(this);
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/addtowishlist.php",
			dataType: "html",
			data: "product="+product+"&action="+action,
			success: function(resultdata) {
				var parts = resultdata.split("|*|");
				thislink.replaceWith(parts[0]);
				$("#saveditemslink").html(parts[1]);
			}
		});
		return false;
	});	
	$(".unwish").live('click', function() {
		var product = $(this).attr("id").replace("prod","");
		var action = "remove";
		var thislink = $(this);
		var thisitem = $(this).parent().parent().parent().parent();
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/addtowishlist.php",
			dataType: "html",
			data: "product="+product+"&action="+action,
			success: function(resultdata) {
				var parts = resultdata.split("|*|");
				thisitem.hide(300,function(){
					thisitem.remove();
				})
				$("#saveditemslink").html(parts[1]);
			}
		});
		return false;
	});		


	$(".request").live('click', function() {
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/request.php",
			dataType: "html",
			success: function(resultdata) {
				$("#checkoutarea").html(resultdata);
			}
		});
		return false;
	});	

	
	$(".basketupdate").live('click', function() {
		var thislink = $(this);
		var baskettype = thislink.closest(".bsk_wrap").attr("id");
		var idbits = $(this).attr("id").split("-");
		if (idbits[0] == "rem"){
			var quantity = 0;
		} else {
			var quantity = $("#quant-"+idbits[1]).val();
		}
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/updatebasketitem.php",
			dataType: "html",
			data: "oiid="+idbits[1]+"&quant="+quantity,
			success: function(resultdata) {
				var parts = resultdata.split("|*|");
/* 				$(".shippingoptions").html(parts[0]); */
				$("#"+baskettype+" .basketlabels").html(parts[1]);
				$("#"+baskettype+" .baskettotals").html(parts[2]);
				if (quantity <= 0){
					$("#row-"+idbits[1]).slideUp(500, function() {
						$("#row-"+idbits[1]).remove();
					});
				} else {
					$("#lt-"+idbits[1]).html(parts[3]);
				}
				if (parts[4]){
					var pos = thislink.offset();
					$("#tt").html(parts[4]+"<p class='mts centred'><a class='ctt' href='#'>OK</a></p><div class='up rgt'></div>").css("width","200px").css("top",(pos.top+23)).css("left",(pos.left-180)).slideDown(200);
				}
				$("#em-"+idbits[1]).html(parts[4]);
				$("#quant-"+idbits[1]).val(parts[5]);
				thislink.fadeOut(300);
			}
		});
		return false;
	});
	
	$(".clearbasket").click(function(){
		var type = $(this).closest(".bsk_wrap").attr("id");
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/clearbasket.php",
			dataType: "html",
			data: "type="+type,
			success: function(resultdata) {
				$("#"+type).html(resultdata);
			}
		});
		return false;
	});
	

	$(".billcountry, .shipcountry").change(function(){
		var ctry = $("option:selected", this).val();
		var statediv = $(this).parent().parent().parent().find('.state');
		if (statediv.find("input").length){
			var sv = statediv.find("input").val();
			var fn = statediv.find("input").attr("name");
		} else {
			var sv = statediv.find("option:selected").val();
			var fn = statediv.find("select").attr("name");
		}
		$.ajax({
			cache: false,
			type: "GET",
			url: "/php/shop/php/getstatefield.php",
			dataType: "html",
			data: "sv="+sv+"&country="+ctry+"&fn="+fn,
			success: function(resultdata) {
				$(".frmnpt",statediv).html(resultdata);
			}
		});
		if (ctry == 211){
			statediv.find("label").append("<span class='gold'>*</span>");
		} else {
			var label = statediv.find("label").html();
			var newlabel = label.replace('<span class="gold">*</span>','');
			statediv.find("label").html(newlabel);
		}
	});


	$(".submit").click(function(){
		var formid = $(this).classData("form");
		$("#"+formid).submit();
	});
	$("#register, #login, #editdetails, #editlogin").validate();
	$("#editlogin").validate({
		rules: {
		    email: {
		      required: true,
		      email: true
		    },
		    email_again: {
		      equalTo: "#email"
		    },
		    password: {
		    	required: false
		    },
		    password_again: {
		    	equalTo: "#password"	
		    }
		  }
	});
	$("#paymentform").validate({
		rules: {
		    cctype: {
		      required: true
		    }
		  },
		 submitHandler: function(form) {
		   makePayment();
		 }
	});
});



function makePayment(){
	var cctype = $("#cctype").val();
	var ccname = $("#ccname").val();
	var ccnumber = $("#ccnumber").val();
	var cccode = $("#cccode").val();
	var ccissue = $("#ccissue").val();
	var ccstartmonth = $("#ccstartmonth").val();
	var ccstartyear = $("#ccstartyear").val();
	var ccendmonth = $("#ccendmonth").val();
	var ccendyear = $("#ccendyear").val();
	var checkouttype = $("#checkouttype").val();
	var estimateno = $("#estimateno").val();
	$("#checkoutarea").html("<div class='midcol bg1'><div class='bp'>Please wait…</div></div>");

	if (cctype == "PAYPAL"){
		$("#checkoutarea", top.document).load("/php/shop/sagepay/transactionRegistration.php",{cctype:cctype, ccname:ccname});
	} else {
		$("#checkoutarea", top.document).load("/php/shop/sagepay/transactionRegistration.php",{cctype:cctype, ccname:ccname, ccnumber:ccnumber, cccode:cccode, ccissue:ccissue, ccstartmonth:ccstartmonth, ccstartyear:ccstartyear, ccendmonth:ccendmonth, ccendyear:ccendyear, checkouttype:checkouttype, estimateno:estimateno});
	}
}
