jQuery.noConflict();
jQuery(document).ready(function($) {

	// Slideshow
	var slidecount = $('#slideshow a').length;
	var controlcount = 0;
	while (controlcount < slidecount){
		$('ul#slider_nav').append('<li><a href="#"></a></li>');
		controlcount += 1;
	}

	$('#slideshow').cycle({
        fx:     'fade',
        timeout: 5000,
	   speed: 1500,
        pager:  '#slider_nav',
	   cleartype:  true,
	   cleartypeNoBg:  true,
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#slider_nav li:eq(' + (idx) + ') a';
        }
    });

	// Drop Downs
	function addMega(){
	  $(this).addClass("hovering");
	  }
	function removeMega(){
	  $(this).removeClass("hovering");
	  }
	  
	var megaConfig = {    
     interval: 100,
     sensitivity: 4,
	over: addMega,
	timeout: 500,
	out: removeMega
	};
	
	$("#product-nav ul li").hoverIntent(megaConfig);
	
	$('#product-nav ul#product-nav-top li:last-child').addClass("rightside");
	
	$('#product-nav a').attr('title','');
	
	// Front page latest blog entries rotator
	$('ul#blogposts').cycle({
        fx:     'scrollUp',
        timeout: 3000,
	   speed: 200
	   });

// Shopping Cart Add Tax

	//First we add the content and hooks we need to pull this off.
	$('div#post-6 table#viewCartTable tr:last').before("<tr id='taxrow'></tr>"); // Only add it if we're on the checkout page.
	$('tr#taxrow').append('<td class="noBorder" style="text-align: right;" colspan=3>&nbsp;</td>\n<td class="noBorder" colspan="1" style="text-align: right; font-weight: bold;" valign="top">Tax:</td>\n<td class="noBorder" colspan="1" style="text-align: left; font-weight: bold;" valign="top">$<span id="tax">0.00</span></td>');

	//We add a class to these so we can key on their change later
	$('select#billing-state').addClass("changer");
	$('input#billing-zip').addClass("changer");
	$('select#shipping-state').addClass("changer");
	$('input#shipping-zip').addClass("changer");
	
	var totaltd = $('table#viewCartTable tr:last td:last').text();
	var total = parseFloat(totaltd.substr(totaltd.indexOf("$")+1));
	$('table#viewCartTable tr:last td:last').html('$<span id="total">' + total + '</span>');
	
	// Now that we have our rows set up with css hooks we can manipulate them when the state or zip code change.

	function roundit(rnum) {  //We'll use this later to make sure we round to 2 decimal places
		return Math.round(rnum*Math.pow(10,2))/Math.pow(10,2);
	}
	
	function stateandzip(){
		//Register function globals (total global declared outside of function)
		var state;
		var zip;
		var tax;
		
		//Get our state and zip vals based on shipping or billing address
		if ($('input#sameAsBilling:checked').val() > 0) {
			state = $('select#billing-state').val();
			zip = parseInt($('input#billing-zip').val());
		}else{
			state = $('select#shipping-state').val();
			zip = parseInt($('input#shipping-zip').val());
		}

		if(state == "AK" || state == "HI") { //Not eligible for flat rate
			var shippingagree = confirm("Additional shipping charges may apply for Alaska and Hawaii.  By clicking \"OK\" you agree to pay the actual cost of shipping for your order if it is more than $12.99.");
			if (shippingagree == true){
				$('div.shipping-disclaimer').remove();
				$('table#viewCartTable').after("<div class=\"shipping-disclaimer\">Please note: Your order is not elligible for our $12.99 Shipping Saver Flat Rate shipping program because it is being shipped to Alaska or Hawaii.  Additional shipping charges will be calculated based on your location and shipping method after you make your purchase through our website.  We will contact you to make shipping arrangements.  Your order will not be shipped until additional shipping charges have been paid.</div>");
				$('input#PHPurchaseCheckoutButton').show();
			}else{
				window.alert("We cannot process your order until you accept the additional shipping charges to Alaska or Hawaii.");
				$('select#billing-state').val("0");
				$('select#shipping-state').val("0");
				$('input#PHPurchaseCheckoutButton').hide();

			}
/*			var cartform = "<form id='shippingform' action='http://174.121.35.130/~kirksbre/store/cart/' method='post'><input type='hidden' name='task' value='updateCart'><input type='hidden' name='shipping_method_id' value='2'></form>";

			$('#header').append(cartform);
			window.alert("Not eligible for Shipping Saver Flat Rate");
			$('#shippingform').submit();
*/				
		}else{
			$('input#PHPurchaseCheckoutButton').show(); // If it's not AK or HI we need to be able to submit.
		}

		if(state == "NE") { //If they're in NE we need to charge tax.
			if (zip >= 68501 && zip <= 68588) { //If the zip is in our range, the rate is higher.
				tax = (total * .07).toFixed(2);
			}else{ //It's in NE but not in Lincoln
				tax = (total * .055).toFixed(2);	
			}
		}else{ //If the state isn't Nebraska, we know we don't need to charge tax.
			if (zip >= 68501 && zip <= 68588) { //Maybe they didn't set the state yet.
				tax = (total * .07).toFixed(2);
			}else{
				tax = 0;
			}
		}
		
		var newtotal = roundit((parseFloat(tax) + parseFloat(total)));
		newtotal = newtotal.toFixed(2);
		
		//Let's change the values displayed in the table
		if(tax == 0) {tax = "0.00"} //Looks better this way
		$('span#tax').text(tax);
		$('span#total').text(newtotal);
	}
	
	$('.changer').change(function() {
		stateandzip();
	});
	$('input#sameAsBilling').change(function(){
		stateandzip(); // Need to check when the checkbox is hit
    });
	
	$(window).load(function(){ //Catches page refresh and error refresh
		stateandzip();
    });
	
	// Zip Code Checks
	$('input#shipping-zip').change(function() {
		if ($(this).val().length < 5) {
			window.alert('Please verify the zip code you entered.  It seems to be too short.');
		}
	});
	
	$('input#billing-zip').change(function() {
		if ($(this).val().length < 5) {
			window.alert('Please verify the zip code you entered.  It seems to be too short.');
		}
	});
	
	// Freight Shipping Stuff
	
	$(window).load(function(){ //Catches page refresh and error refresh
		if (freight > 0)  {
			if($("table#viewCartTable select#shipping_method_id").val() == 1) { //See if it's already set so we don't create an infinite loop.
				$('table#viewCartTable select#shipping_method_id').val('2');
				$('#PHPurchaseCartForm').submit();
			}
			
			$('table#viewCartTable select#shipping_method_id').val('2').attr("disabled", "disabled"); //Disable the form so they can't change it.
			if($('div#post-8').length == 0) {
				$('div.shipping-disclaimer').remove();
				$('table#viewCartTable').after("<div class=\"shipping-disclaimer\">Please note: One or more of the products in your cart is not elligible for our $12.99 Shipping Saver Flat Rate shipping program.  Additional shipping charges will be calculated based on your location and shipping method after you make your purchase through our website.  We will contact you to make shipping arrangements.  Your order will not be shipped until additional shipping charges have been paid.</div>");
			}
			$('table#viewCartTable td#shipping-amount').html("TBD");
		}else{ //No freight?
			if($("table#viewCartTable select#shipping_method_id").val() == 2) { //See if it's already set so we don't create an infinite loop.
				$('table#viewCartTable select#shipping_method_id').val('1');
				$('#PHPurchaseCartForm').submit();
			}

			$("table#viewCartTable select#shipping_method_id option[value='2']").remove(); //Remove the bulk option.
//				$('table#viewCartTable select#shipping_method_id').val('1');

		}
	});

});
