var baseUrl="";

$(function() {

	if (window.location.protocol=="https:") {
		baseUrl = "https://"+window.location.host+"/haggusandstookles/";
	}
	
	$("#hideCartDialog").click(function(e) {
		e.preventDefault();
		$("#cartDialog").fadeOut(200);

		//IE6 display bug workaround
		$("select").each(function() {
			$(this).show();
		});
	});
	
	$("#friend").click(function(e){
		e.preventDefault();
		window.open( $(this).attr('href'),width=350,height=250 );
	});

 
   $("#giftwrapbutton").click(function(e) {

	   e.preventDefault();

		var prodid;
		var giftcardtype;
		var giftname;
		var message;
		var result;

		// get values
		var xmasgiftwrap = $("input[name='xmasgiftwrap']:checked").val(); 
		var xmasgiftname = $("input[name='xmasgiftwrap']:checked").attr("rel"); 

		var xmasgifttag = $("input[name='xmasgifttag']:checked").attr("rel"); 

		var giftwrap = $("input[name='giftwrap']:checked").val(); 
		var giftname = $("input[name='giftwrap']:checked").attr("rel"); 

		var giftcard = $("input[name='giftcard']:checked").val(); 
		var gifttag = $("input[name='gifttag']:checked").val(); 
		var message = "Message:<br/>" + $("textarea[name='giftmessage']").val(); 

		//var special = $("input[name='wholeorder']:checked").val(); 
		var specialmessage = $("textarea[name='giftspecial']").val(); 

		//if(special == "false")
	    //{
			message += "<br/><br/>Special Instructions:<br/>" + specialmessage;
		//}

		// they need to select a gift wrap option and a gift tag OR card to proceed
		if((!giftwrap && !xmasgiftwrap) || (!giftcard && !gifttag && !xmasgifttag) || !message)
	    {
			alert("Please select the Gift Wrap and Gift Tag or Card you would like to order and then type in a message");
			$("input[name='giftwrap']").each( function() {
					this.checked = false;
			});
			$("input[name='xmasgiftwrap']").each( function() {
					this.checked = false;
			});
			return false;
	    }
		else if(giftwrap && giftcard && gifttag)
	    {
			alert("Please select only one of either a Large Gift Card or Small Gift Card/Tag to go with your Gift Wrap.");

			$("input[name='giftcard']").each( function() {
					this.checked = false;
			});
			$("input[name='gifttag']").each( function() {
					this.checked = false;
			});
			$("input[name='xmasgifttag']").each( function() {
					this.checked = false;
			});
			return false;
	    }
		else if(giftwrap && xmasgifttag || (xmasgiftwrap && (giftcard || gifttag)))
	   {
			alert("Christmas wrap and tags cannot be mixed with regular gift wrap, cards and tags.");

			$("input[name='giftcard']").each( function() {
					this.checked = false;
			});
			$("input[name='gifttag']").each( function() {
					this.checked = false;
			});
			$("input[name='xmasgifttag']").each( function() {
					this.checked = false;
			});
			$("input[name='giftwrap']").each( function() {
					this.checked = false;
			});
			$("input[name='xmasgiftwrap']").each( function() {
					this.checked = false;
			});

			return false;
	   }


		if (giftcard)
		{
			prodid = '2858';
			giftname += " - " + $("input[name='giftcard']:checked").attr("rel");
		}
		else if (gifttag)
	    {
			prodid = '2857';
			giftname += " - " + $("input[name='gifttag']:checked").attr("rel");
	    }
		else
	   {
			prodid = '2859';
			giftname = xmasgiftname + " - " + $("input[name='xmasgifttag']:checked").attr("rel");

	   }

		$.get("ajaxcart.php", { prod_id: prodid, action: "add", pdetails: giftname, pdesc: message},function(d) {
				$("#shopCartTable").load("cartTable.php");
				window.location="cart.php";
			});


  });

   $("#btnAddToCart").click(function(e) {
	   var proceed = true;
	   e.preventDefault();
	   $("select.option_id").each(function (i) {
		   //console.log($(this).find("option:selected").text()+": "+$(this).val());
		   //console.log($(this).attr("rel")+": "+$(this).val());

		   if ($(this).val().length=="") {
			   alert($(this).attr("rel")+" is required");
			   proceed = false;
			   return false;
		   }
		   $("#btnAddToCart").attr("href", $("#btnAddToCart").attr("href")+"&option_"+i+"="+$(this).val());
	   });
	   if (proceed){
		   $("#btnAddToCart").attr("href", $("#btnAddToCart").attr("href")+"&prodqty="+$("#prodqty").val());
			$.get('ajax'+$(this).attr("href"),function(d) {
				if (d != "") alert(d);
				$("#cartDialog").fadeIn(200);

				//IE6 display bug workaround
				$("select").each(function() {
					$(this).hide();
				});

			});
			$("#shopCartTable").load("cartTable.php");

	   }
   });
	
	$("table.cart").find("td").each(function(i) {
		if ($(this).text().substring(0,1)=="$" || $(this).text().substring(0,2)=="-$") 
		{
			$(this).css("text-align", "right");
			$(this).css("font-size", "14px");
		}
	});

	$('input:checkbox').click(function () {
		if ($(this).attr("name")=="yesgiftwrap" && this.checked == true) {
			window.location="giftwrapping.php?cat_link=Gifts&sub_link=Gift-wrapping";
		}
	});


	$("#referral").change(function(){
		switch($("#referral").val()){
			case "friend":
				$("#moreinfo").show();
				$("#moreinfo").val("Please enter the person's name");
				break;
			case "online":
				$("#moreinfo").show();
				$("#moreinfo").val("Please enter the site");
				break;
			case "magazine":
				$("#moreinfo").show();
				$("#moreinfo").val("Please enter the magazine's name");
				break;
			case "kindergarten":
				$("#moreinfo").show();
				$("#moreinfo").val("Please enter the kindergarten's name");
				break;
			case "other":
				$("#moreinfo").show();
				$("#moreinfo").val("Where did you hear about us");
				break;
			default:
				$("#moreinfo").hide();
		}
	});

	$("#moreinfo").focus(function(){
		$("#moreinfo").val("");
	});

	$("#asabove").click(function(){
		$("#postal").slideDown(400);
		$("#postal_first_name").val($("#first_name").val()); 
		$("#postal_surname").val($("#surname").val()); 
		$("#postal_address").val($("#address").val()); 
		$("#postal_city").val($("#city").val());
		$("#postal_state").val($("#state").val());
		$("#postal_postcode").val($("#postcode").val());
		$("#postal_country").val($("#country").val());
	});
	$("#notasabove").click(function(){
		$("#postal").slideDown(400);
		$("#postal_first_name").val(""); 
		$("#postal_surname").val("");  
		$("#postal_address").val(""); 
		$("#postal_city").val("");
		$("#postal_state").val("");
		$("#postal_postcode").val("");
		$("#postal_country").val("Australia");
	});

	/*

	$(".birthdaypicker").datepicker({ yearRange: '-18:+00', dateFormat: 'dd MM yy' });
	*/
	$(".birthdays").hide();

	$("#yes_kids").click(function(){
		$("#birthday1").slideDown(400);
	});
	$("#no_kids").click(function(){
		$(".birthdays").slideUp(400);
	});

	$(".birthdays a").click(function(e){
		e.preventDefault();
		var div = "#"+ $(this).attr("rel");
		$(div).slideDown(400);
	
	});


	$("#showspecial").hide();

	$("#yes_whole").focus(function(){
		$("#showspecial").hide("slow");
	});
	$("#no_whole").focus(function(){
		$("#showspecial").show("slow");
	});



	startList();
	
	$('#another_tip').hide();
	$("#tip_submit").click(function(){
		$.post("lodge_tips.php",
			{author: $('#author').val(), email: $('#email').val(), subject: $('#subject').val(), tip: $('#tip').val(), security_code: $('#security_code').val()},
			function(data){
				if(data == "<p>Incorrect security code, please try again</p>"){
					$('#response').html(data);
				}else{
					$('#response').html(data);
					$('#another_tip').show();
					$('#lodge_tip').hide();
				}
		})
	})

	$("#another_tip a").click(function(e){
		e.preventDefault();
		$('#lodge_tip').show();
		$('#another_tip').hide();
	});



	// Slide toggle sub menus in left hand menu
	$("#navlist").find("a").each(function() {
		$(this).click(function(e) {
			if ($(this).siblings(".subnavlist").length>0) {
				e.preventDefault();
				$(this).siblings(".subnavlist").slideToggle(1000);
			}
		});
	});

});