$(document).ready(function() {
	
	$("#disc_table").hide();

	$("#disc").click(function(e) {
		e.preventDefault();
		applyDiscount();
	});

	function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}

	function applyDiscount(){
		/*
		if (e) {
			e.preventDefault();
		}
		*/
		$.getJSON("app_disc.php",{ total_price: $("#total_price").html(), coupon_code: $("#coupon_code").val()},function(data){
			dontSubmitMoreThanOnce=false;
			if ($("#totalprice").val() > data)
			{
				alert("Valid Coupon");
				var format_data = addCommas(data.toFixed(2));
				$("#disc_price").html("$"+format_data);				
				$("#discountedprice").val(format_data);
				$("#disc_table").slideDown(100);
			}
			else
			{
				alert("Invalid Coupon");
				$("#disc_table").slideUp(100);
			}
		});
	}

	if($("#quote_only").val() == 0){
		$("#quote_request").hide();
	}else{
		$("#sub_pay_table").hide();
		$("#not_instant").hide();
	}

	$("#country").change(function(){
		var country = $(this).val();
		var quote_only = $("#quote_only").val();
		if(quote_only == 0){
			if(country == 'Other'){
				var total_text = "Total Price (Excluding Freight)";
				var disc_text = "Discount Price (Excluding Freight)";
				$("#sub_pay_table").hide();
				$("#not_instant").hide();
				$("#quote_request").show();
			}else if(country == "New Zealand"){
				var total_text = "Total Price (Including Freight)";
				var disc_text = "Discount Price (Including Freight)";
				$("#sub_pay_table").show();
				$("#not_instant").hide();
				$("#quote_request").hide();
			}else{
				var total_text = "Total Price (Including Freight)";
				var disc_text = "Discount Price (Including Freight)";
				$("#sub_pay_table").show();
				$("#not_instant").show();
				$("#quote_request").hide();
			}

		$("#total_text").html(total_text);
		$("#disc_text").html(disc_text);
		}else{
			$("#sub_pay_table").hide();
			$("#not_instant").hide();
			$("#quote_request").show();
			}
	});

	
	var dontSubmitMoreThanOnce=false;
	$("#coupon_code").keyup(function(e) {
		if (e.keyCode==13) {
			if (dontSubmitMoreThanOnce==true) return;
			dontSubmitMoreThanOnce=true;
			var cleanText=$("#coupon_code").val().replace(/\n/g, '');
			
			//Hack to stop hitting Enter on alert box from resending Ajax request
			if ($("#coupon_code").val()==cleanText)
			{
				dontSubmitMoreThanOnce=false;
				return;
			}

			$("#coupon_code").val(cleanText);
			applyDiscount();
			//alert("do apply discount thing");
			return;
		}
	});
	
	$("ul#newsbar").liScroll(); 

});

/*!
 * liScroll 1.0
 * Examples and documentation at: 
 * http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
 * 2007-2009 Gian Carlo Mingati
 * Version: 1.0.1 (07-DECEMBER-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).width();
				});
				$strip.width(stripWidth);			
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};