// Courtesy of http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// Originally from http://hostroute.com/legacy/resources/script_vat.html
// Modified by Aral Balkan.
function custRound(x,places) {
	x= (Math.round(x*Math.pow(10,places)))/Math.pow(10,places);
	s = String(x);
	dot = s.indexOf('.')
	if (dot == -1)
	{
		//s = s + '.00'
		//console.log(s);
	}
	else
	{
		numDecimalPlaces = (s.substr(dot, 3)).length -1 ;
		if (numDecimalPlaces == 1) s = s + '0';		
	}
	return s
}


function priceString(num){
	// Returns a properly formatted price string for the passed amount.
	price = custRound(num,2);
	parts = price.split('.');
	
	if (parts[1] == undefined) 
	{
		parts[1] = '';
	}
	else
	{
		parts[1] = '.' + parts[1]
	}
	
	price = parts[0] + '<small>' + parts[1] + '</small>';
	return currency + price;
}

// Gets the latest prices - mock. The actual data will be written by the
// server directly into the HTML file and overwrite this function.
function getServerPrices(){
	prices = {
		fromServer: false,
		ticket: 99,
		microSponsorship:50
	}
	return prices
}
	
function toggleAdditionalInfo(id){
	$("#" + id).slideToggle("fast", function(){
		if ($(this).is(":visible")) {
			$("#" + id + "_link").html('Hide details.');
		} else {
			$("#" + id + "_link").html("What's this?");
		}
	});
	
	// Don't let the actual link fire.
	return false;
}
	
function termsCheck(){
	var disabled = !$("#id_terms").attr("checked")
	$("#paypal_button").attr("disabled", disabled)
	if (disabled){
		$("#paypal_button").attr("src", "/images/paypal_button_disabled.gif");
	}
	else
	{
		$("#paypal_button").attr("src", "https://www.paypal.com/en_GB/i/btn/btn_xpressCheckout.gif")
	} 
}
	
function updateTotal(evt)
{		
	prices = getServerPrices()
	ticketPrice = prices.ticket;
	microSponsorshipPrice = prices.microSponsorship;

	currency = '$'

	var numTickets = $("#id_number_of_tickets").attr("value");
	var total = ticketPrice * numTickets;
	
	// Check for micro-sponsorship
	microSponsor = $("#id_micro_sponsor").attr("checked");
	if (microSponsor){
		total = total + microSponsorshipPrice;
	}
	
	//
	// Handle display strings
	//
	ticketPriceForDisplay = priceString(ticketPrice) + " <small>(inc. VAT)</small>";
	microSponsorshipPriceForDisplay = priceString(microSponsorshipPrice);
	
	$(".ticket_price").html(ticketPriceForDisplay);
	$("#micro_sponsorship_price").html(microSponsorshipPriceForDisplay);
	
	// Due to our pricing structure, the total will never have a decimal bit
	$("#total").html(priceString(total))
}


$(document).ready(function(){
	// Preload the full-color PayPal image from PayPal's servers as it takes a moment to load.
	$.preloadImages("https://www.paypal.com/en_GB/i/btn/btn_xpressCheckout.gif");
	
	updateTotal();
	
	$("#controlBar").append('<input type="image" id="paypal_button" src="/images/paypal_button_disabled.gif" alt="Checkout with PayPal." name="checkout">');
	$("#id_number_of_tickets").change(updateTotal)
	$("#id_number_of_tickets").keyup(updateTotal)
	$("#id_micro_sponsor").click(updateTotal)

	$("#calculate_total").hide();
	
	$("#id_number_of_tickets").focus();

	// Additional information box toggles
	$("#micro_sponsor_info_link").click(function(){toggleAdditionalInfo("micro_sponsor_info"); return false;});
	$("#terms_info_link").click(function(){toggleAdditionalInfo("terms_info"); return false;});

	$("#id_terms").click(termsCheck)
	termsCheck();
 });

