/******************************************************
	JQUERY CONFIGURATION
******************************************************/
jQuery.noConflict();
jQuery(document).ready( function() {


	/******************** USA TAX ************************/
	if(jQuery('select.current_country').val() == 'US') {
	
			
			function carl_update_price() {
				
				var current_window = window.location;
				
				var current_price = jQuery( '.pricedisplay:first' ).text();
					current_price = current_price.substr(1);
				
				var selected_region = jQuery('select.current_region').val();
				
				jQuery("select[title='shippingregion']").val(selected_region);			
				
				var new_price; 
				
				if(selected_region == 35) {
				
					current_price = parseFloat(current_price);
					
					new_tax = current_price * 0.07;   
					
					new_tax = new_tax.toFixed(2);
					
					new_tax = parseFloat(new_tax);
					
					price_add_up = new_tax + current_price;
					
					price_add_up = price_add_up.toFixed(2);
					new_price = '$' + price_add_up;
				} else {
					new_price = '$' + current_price; 
				}
			
				jQuery( '.red_small span' ).text(new_price);
			
			}
			
			carl_update_price();
	 
		jQuery('select.current_region').livequery('change', carl_update_price);
		
		jQuery("input[title='billingpostcode']").bind('keypress', function(e) {
			return true;
		  })
		  
		jQuery("input[title='shippingpostcode']").bind('keypress', function(e) {
			return true;
		  })
		  
		jQuery("input[title='billingpostcode']").bind('keypress', function(){
		
			return true;
			
		})
		
		jQuery("input[title='shippingpostcode']").bind('keypress', function(){
		
			return true;
			
		})
	} else {
	
		jQuery("input[title='billingpostcode']").bind('keypress', function(e) {
			return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
		  })
		  
		jQuery("input[title='shippingpostcode']").bind('keypress', function(e) {
			return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
		  })
		  
		jQuery("input[title='billingpostcode']").bind('keypress', function(){
		
			if(jQuery("input[title='billingpostcode']").val().length > 3 ) {
			 return false;
			}
			
		})
		
		jQuery("input[title='shippingpostcode']").bind('keypress', function(){
		
			if(jQuery("input[title='shippingpostcode']").val().length > 3 ) {
			 return false;
			}
			
		})
	
	}
	
	/******************** END USA TAX ************************/
	
	// Input fields using the clickclear and clickrecall functions, as defined below.
	jQuery('.widget_search input').focus ( function () {
		clickclear(this,'Search');	
	});
	
	jQuery('.widget_search input').blur ( function () {
		clickrecall(this,'Search');	
	});
	
	// Region selector Javascript.
	jQuery('#region_selector h6').click ( function (){
		
		jQuery('#region_selector .region_list').slideToggle();
		jQuery('#region_selector h6 a').toggleClass('open');
		
		return false;
		
	});
	
	if(jQuery('#menu ul li.first').hasClass('current_page_item')) {
		jQuery('#menu ul li:first-child a span').css("background-position", "left -34px");
	}
	
	if(jQuery('#menu ul li.last').hasClass('current_page_item')) {		
		jQuery('#menu ul li:last-child a span').css("background-position", "right -34px");
	}
	
	jQuery('#menu ul li:last-child').addClass('last');
	
	jQuery('#nav_secondary ul.last li:last-child').addClass('last');
	
	jQuery('.faq p').hide();
	
	jQuery('.faq h4').click(function() {
		
		  jQuery(this).next().slideToggle("fast");
		
	});
	
	jQuery(".slides").jCarouselLite({
		btnNext: ".btn_next",
		btnPrev: ".btn_prev", 
		visible: 1, 
		circular: true, 
		speed: 400
	});
	
	jQuery('#slider .inner').cycle({
		fx: 'scrollLeft', 
		timeout: 9000, 
   		speed: 1000, 
   		sync: 1, 
   		pager: '#pager'
	});
	
	// Update the cart count onclick
	jQuery('.wpsc_buy_button').click ( function (){
	
		jQuery('#update_cart_count').fadeOut('slow', function() {
			var current_value = parseInt(jQuery('#update_cart_count').html());
			
			current_value = current_value + 1;
		
			jQuery('#update_cart_count').html(current_value)
		
			jQuery('#update_cart_count').fadeIn('slow');
		});
		
	});
	
	jQuery('#fancy_notification_content span').html('Your item has been added to your cart');
	
	var current_country = jQuery('#current_country').val();
	var current_region = jQuery('#region').val();
	
	if(current_country == 0) {
		
		// Now make the suburb box display "Select One" as well
		jQuery('#region').val("0");
		
		jQuery('#region').attr('disabled', 'disabled');
		
	}
	
	if(current_region == 0 || current_country == 0) {
		jQuery('.wpsc_checkout_forms').hide();
		jQuery('.shippingtable').hide();
	}
	
	// Duplicate data for shipping and billing
	jQuery("#shippingSameBilling").click(
		function() {
		
			// Get the checked status
			var is_checked = jQuery(this).attr('checked');
			
			// We need to check wether or not the checkbox is checked
			if(is_checked) {
				var fname = jQuery("input[title='billingfirstname']").val();
				var lname = jQuery("input[title='billinglastname']").val();            
				var addr = jQuery("textarea[title='billingaddress']").val();
				var pcode = jQuery("input[title='billingpostcode']").val();
				var city = jQuery("input[title='billingcity']").val();
				var state = jQuery("select[title='billingregion']").val();
				
				jQuery("input[title='shippingfirstname']").val(fname);
				jQuery("input[title='shippinglastname']").val(lname); 
				jQuery("textarea[title='shippingaddress']").val(addr);			
				jQuery("input[title='shippingpostcode']").val(pcode);							
				jQuery("input[title='shippingcity']").val(city);							
				jQuery("select[title='shippingregion']").val(region);							
			} else {
				jQuery("input[title='shippingfirstname']").val('');
				jQuery("input[title='shippinglastname']").val(''); 
				jQuery("textarea[title='shippingaddress']").val('');			
				jQuery("input[title='shippingpostcode']").val('');							
				jQuery("input[title='shippingcity']").val('');							
			}
			
		}
	);
	
	jQuery('.paypal_express_form').before('<p class="cart_top">Your order is being processed...</p>');
	jQuery('.make_purchase').before('<p>Your payment will be securely processed by PayPal.</p>');
	jQuery('.paypal_express_form input[type="submit"]').trigger('click');
	
	// Hide the transaction results link on the left
	jQuery('.subpages-widget ul li span:contains("action")').addClass('displaynone');
	
	var selected_suburb = jQuery('#region option:selected').text();	
	var label_html = jQuery('.shipping_country_name').text();
	var new_label_html = label_html.replace(', Australia', ', ' + selected_suburb);
	
	jQuery('.shipping_country_name').text(new_label_html);
	
	// We need to change the city text
	if(jQuery('select.current_country').val() != 'US') {	
		jQuery("input[title='shippingcity']").val(selected_suburb);
	}
	
	form_html = jQuery('.wpsc_checkout_table label:last');
	
	label_trim = jQuery.trim(form_html.text());
	
	if(label_trim != 'Zip Code:') {
		form_html.hide();		
	} 
	
	if(label_trim == 'City:') {
		jQuery("input[title='shippingcity']").hide();		
	}
	
});


/******************************************************/
/******************************************************
	FUNCTIONS TO EMPTY AND RE-FILL A TEXT FIELD
******************************************************/

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
	thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
	thisfield.value = defaulttext;
	}
}

/******************************************************/


