$(document).ready(function(){

	$(".js_hide").hide();
	
	$(".lightbox").lightBox();
	$("div#monthly_specials_slideshow").cycle();
	$("div#current_sales").cycle({
		next: '#next',
		prev: '#prev'
	});
	
	/* Search field */
	$('#search_field').pSearch();

	// On focus remove the default text
	$("#search_field").focus(function() {
		if($(this).val() == "Enter product name") {
			$(this).val("");
		} 
	});
	// On blur, if no content there, set default content
	$("#search_field").blur(function() {
		if($(this).val() == "") {
			$(this).val("Enter product name");
		}
	});

	// Product special countdown of time remaining	
	if($("#special_countdown").length) {
		var liftoff = $("#special_countdown").attr('rel');
		$("#special_countdown").countdown({until:liftoff, format: 'DHMS'});
	}
	
	// Order page
	// On load hide payment elements that aren't being used.
	$("tr.cc_gateway:not(.current)").hide();
	//$("tr.paypal").hide();
	$("tr.ddeposit:not(.current)").hide();
	
	// Show Credit Card elements
	$("input#payment_type_cc").click(function() {	
		$("tr.payment_type").hide(); // Hide all current details, show correct.
		if($(this).is(':checked')) {
			$("tr.cc_gateway").show();
		}	
	});
	// Show PayPal elements
	$("input#payment_type_pp").click(function() {
	
		$("tr.payment_type").hide(); // Hide all current details, show correct.
		
		if($(this).is(':checked')) {
			$("tr.paypal").show();
		}
	
	});
	// Show Direct Deposit elements
	$("input#payment_type_dd").click(function() {

		var result = true;

		// Check if there are any CC info entered, and confirm if so
		if($("input.cc_details").val() != "") {
			result = confirm("You have entered some credit card information. Are you sure you want to change payment type?");
			if(result == false) {
				$("input#payment_type_cc").attr("checked", true);
			}
		}
	
		// Confirm user want's to change from CC then do it
		if(result == true) {
			$("tr.payment_type").hide(); // Hide all current details, show correct.
		
			if($(this).is(':checked')) {
				$("tr.ddeposit").show();
			}
		}
	
	});

	$("input#same_as_shipping").click(function() { 
		if($(this).is(':checked')) {
			$("#billing_address").val($("#street").val());
			$("#billing_address2").val($("#street2").val());
			$("#billing_suburb").val($("#suburb").val());
			$("#billing_postcode").val($("#postcode").val());
			$("#billing_state").val($("#state").val());
		} else {
			$("#billing_address").val("");
			$("#billing_address2").val("");
			$("#billing_suburb").val("");
			$("#billing_postcode").val("");
			$("#billing_state").val(0);
		}				
	});
	
	$("#customer_details").submit(function() {
	
		if(!$("#accept_terms").is(':checked')) {
			alert("You must accept the Terms and Conditions before making your order.");
			return false;
		} else {
			return true;
		}
	
	});	

});

/* FUNCTION: Product Search (pSearch) 
// PURPOSE: Uses an AJAX call to provide the user with a product
// search to speed and decrease the loading of each product selector drop down. */
(function($){ 

	// Create Product Search jQuery function called psearch	
	$.fn.pSearch = function() {
		return this.each(function() {
		
			var input_element = $(this);
			
			// Create correct HTML markup for this function
			input_element.wrap('<div class="psearch"></div>').parent()
				.append('<span class="psearchpreview"></span>')
				.append('<ul class="psearchresults"></ul>');
			input_element.attr('autocomplete', 'off');
		
			// Listen to any key stroke added to the text input 
			input_element.bind('keydown', function(e) { 
				// Load and set required elements from the DOM
				var return_url = '/includes/product_search.php';
				var search_input = $(this);
				var search_results_list = search_input.parent().children('ul.psearchresults');
				// Set keycode from what user has clicked
				var keyCode = e.keyCode || e.which;					
				// If the user typed an escape or enter, remove the box
				if (keyCode == 9 || keyCode == 27) {
					search_results_list.hide();
				// If the length is 3 more more characters then do a JSON search
				} else if(search_input.val().length >= 3) {
					var items = [];
					search_results_list.show();
					// Load JSON response from server to get all "searched" results
					$.getJSON(return_url, { 'search': search_input.val() }, function(data) {
						$.each(data, function(key, val) {
							items.push('<li id="ps_' + key + '"><a href="' + val['url'] + ' ">' +  val['name'] + '</a></li>');
						});
						search_results_list.empty().append(items.join(''));
					});
				}
			});
						
		});
	}

})(jQuery);
