$(document).ready(function() {

  $("#basket-wrapper").corner("bottom 8px").parent().css({'padding' : '0 8px 8px 8px'}).corner("bottom 14px");
  
  $("#main-content").corner("8px");

  $("#categories").corner("top 8px");
  
  $("#category-holder").corner("bottom 8px");
  
  $("#filters").corner("top 8px");
  
  $("#filters-holder").corner("bottom 8px");
  
  $("#cat-download").corner("8px");
  
  $("#searchForm").validate({
	messages: {
			q: "Please enter something to search for!"
		},			
	errorPlacement: function(error, element) {
     error.appendTo( element.parent() );
    },
    submitHandler: function(form) {
    form.submit();
    
    }
  });
  
  $("#login_existing").validate({
		rules: {
			email: {
				required: true,
				email: true
			},
			password: {
				required: true,
				minlength: 5
			}
		},
		messages: {
			password: {
				required: "Please enter your password",
				minlength: "Your password must be at least 5 characters long"
			}
		}
  });
		
	$("#login_new").validate({
		rules: {
			password1: {
				required: true,
				minlength: 5
			},
			confirm_password: {
				required: true,
				minlength: 5
			}
		},
		messages: {
			password1: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long"
			},
			confirm_password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long",
				equalTo: "Please enter the same password as above"
			}
		}
	});
	
  $(function(){
	        $("div.carousel").carousel();
	    });
	    	    
	//$("input[name='account']").click(function(){
	//	$("input[name='account']:checked").val() == 'yes' ? $('#new-account').show('slow') : $('#new-account').hide('slow');
	//});
	
  $("#newuser_form").validate({
  });

	$("#postage-options").change(function () {

		var postage_cost = parseFloat($("select#postage-options option:selected").val()) || 0;
		var postage_options = $("select#postage-options option:selected").text();
		var basket_cost = parseFloat($("#subtotal").val()) || 0;
		var donation_cost = parseFloat($("#donation").val()) || 0;
        
        postage_cost = Math.round(postage_cost*100)/100;
	  	postage_cost = postage_cost.toFixed(2);
	
	  	postage_options = postage_options.split(" ");
	  	postage_options =  postage_options[0].toLowerCase();

	  	$("#tmppost").text(postage_cost);
	  	$("#postage_cost").val(postage_cost);
	  	$("#postage_options").val(postage_options);
	  	 
	  	var total = parseFloat(basket_cost) + parseFloat(postage_cost) + parseFloat(donation_cost);
        total = Math.round(total*100)/100;
	  	total = total.toFixed(2);
	  	$("#ttl").text(total);
	  	$("#total").val(total);
	  	 	  	 	  	 
	  	 switch (postage_options) {
	  	 
	  	 	default:
	  	 	case "1st":
	  	 	
	  	 		var postie = "Estimated delivery 7 to 14 days";
	  	 	
	  	 	break;
	  	 	
	  	 	case "2nd":
	  	 	
	  	 		var postie = "Estimated delivery 14 to 21 days";
	  	 	
	  	 	break;
	  	 	
	  	 	case "special":
	  	 	
	  	 		var postie = "If ordered by 12noon, your order will go out the same day. Otherwise it will go out the next working day.";
	  	 	
	  	 	break;
	  	 
	  	 	case "overseas":
	  	 	
	  	 		var postie = "Please allow up to 28 days for delivery to non-UK addresses.";
	  	 	
	  	 	break;
	  	 
	  	 	case "eu":
	  	 	
	  	 		var postie = "Please allow up to 28 days for delivery to non-UK addresses.";
	  	 	
	  	 	break;
	  	 
	  	 }
	  	 
	  	 $("#post_desc").text(postie);
	
  })	
  .change();

	$("#donation").bind("change keyup", function () {

		var postage_cost = parseFloat($("select#postage-options option:selected").val()) || 0;
		var postage_options = $("select#postage-options option:selected").text();
		var basket_cost = parseFloat($("#subtotal").val()) || 0;
		var donation_cost = parseFloat($("#donation").val()) || 0;
        
        postage_cost = Math.round(postage_cost*100)/100;
	  	postage_cost = postage_cost.toFixed(2);
	  	postage_options = postage_options.split(" ");
	  	postage_options =  postage_options[0].toLowerCase();

	  	$("#tmppost").text(postage_cost);
	  	$("#postage_cost").val(postage_cost);
	  	$("#postage_options").val(postage_options);
	  	 
	  	var total = parseFloat(basket_cost) + parseFloat(postage_cost) + parseFloat(donation_cost);
        total = Math.round(total*100)/100;
	  	total = total.toFixed(2);
	  	$("#ttl").text(total);
	  	$("#total").val(total);
	
  })	

	// currency changes
	$("#curr").change(function () {
	
		//change default currency symbol to avoid duplicates
		$('span.currency-symbol').text("");
		var currency_choosen = $("select#curr option:selected").text();
		var currency_rate = $("select#curr option:selected").val();
		
		switch (currency_choosen) {
		
			case "Euros":
			
				var currency_symbol = "\u20ac";
				var currency_code = "978";
			
			break;
			
			case "US Dollars":
			
				var currency_symbol = "US$";
				var currency_code = "840";
			
			break;
			
			
			case "Sterling":
			
				var currency_symbol = "\u00A3";
				var currency_code = "826";
			
			break;
		
		}

		var postage_cost = parseFloat($("select#postage-options option:selected").val()) || 0;
		var postage_options = $("select#postage-options option:selected").text();
		var basket_cost = parseFloat($("#subtotal").val()) || 0;
		var donation_cost = parseFloat($("#donation").val()) || 0;
		
		var IDs = $("#cartSummary span.prod-price[id]") // find spans with ID attribute
		  .map(function() { return this.id; }) // convert to set of IDs
		  .get(); // convert to instance of Array (optional)
		
		$.each(IDs, function() {
			var tempy = $("#" + this).text();
			var myID = $("#" + this).attr('id');
			// price is stored in the ID
			original_price = myID.split("_");
			original_price = original_price[1];
			original_price = original_price.replace("_", ".");
			original_price = parseFloat(original_price);
			tmp = original_price * currency_rate;
		    tmp = Math.round(tmp*100)/100;
		  	tmp = tmp.toFixed(2);
     		 $("#" + this).text(tmp);
		    });	
		         
	  	//update all amounts
	  	postage_cost = postage_cost * currency_rate;
	  	total = total * currency_rate;
	  	
        postage_cost = Math.round(postage_cost*100)/100;
	  	postage_cost = postage_cost.toFixed(2);
        total = Math.round(total*100)/100;
	  	total = total.toFixed(2);
	  	
	  	postage_options = postage_options.split(" ");
	  	postage_options =  postage_options[0].toLowerCase();

	  	//write-in values
	  	$("#_currency").val(currency_code);
	  	$("#tmppost").text(postage_cost);
	  	$("#postage_cost").val(postage_cost);
	  	$("#postage_options").val(postage_options);
	  	$("span.currency-symbol").text(currency_symbol);
	  	 
	  	var total = parseFloat(basket_cost) + parseFloat(postage_cost) + parseFloat(donation_cost);
        total = Math.round(total*100)/100;
	  	total = total.toFixed(2);
	  	$("#ttl").text(total);
	  	$("#total").val(total);
	
  })	
  .change();
  
  // toggles the slickbox on clicking the noted link  
  $('#hider').hide();
  $('a#shower').click(function() {
    $('#hider').toggle(400);
    return false;
  });

  // toggles the slickbox on clicking the noted link  
  $('#cur-hide').hide();
  $('a#cur-show').click(function() {
    $('#cur-hide').toggle(400);
    return false;
  });

      
});