var upgrade_form =
{
	initialise: function()
	{
		$('input[type=radio].checked').attr('checked', 'checked');
		upgrade_form.set_up_product_events();
		upgrade_form.select_initial_product();
		upgrade_form.set_up_worldpay_impressions();
		upgrade_form.set_up_why_upgrade();
		upgrade_form.set_up_extra_info();
	},
	
	set_up_worldpay_impressions: function()
	{	
		$("#checkout_button").click(function()
		{
			$(this).val('Please wait...');
			
			var submitForm = function() {
				$('#checkout_button').parents('form').submit();
			};
			
			if ( $("input[name='payment_method']:checked").val() == 'worldpay' )
			{
				setTimeout(submitForm, 1000); // Backup form submission - just in case.
				$.get("/flatshare/worldpay_impression.pl?instId="+$('input[name=instId]').val(), submitForm);
				return false;
			}
			else {
				return true;		
			}
		}); 
	},
	
	set_up_extra_info: function()
	{
		$('#upgradeform ul.extra_info span.link').each(function()
		{
			$(this).css({'text-decoration':'underline'});
			var extra = $(this).next('span.extra_info');
			extra.hide();
			$(this).click(function()
			{
				extra.toggle();
			});
		});
	},
	
	set_up_product_events: function()
	{
		/*
			This script wraps around the old update form javascript
			so as to not affect any existing shared functionality.
			
			All it does is make divs that contain a product clickable.
			When clicked, the radio input associated with it is checked.
		*/
	
		var self = this;
		
		// Different styles if javascript is enabled
		$('#upgradeform').addClass('js');
		
		this.products = $('#upgradeform.new div.panel.product');
		
		// Set up onclick events
		this.products
			.click(function()
			{
				self.products.removeClass('on');
				var product = $(this);
				product
					.addClass('on')
					.find('input[name=M_prodprice], input[name=prod_id]')
					.attr('checked', 'checked');
				
				// Refers to global function in old script
				updatetotal();
				
				// Continue where old script left off
				self.update_form();
			});
		
		// Reflect hidden form field value
		$('#extra_listings').change(self.update_form);
	},
	
	select_initial_product: function()
	{
		this.products
			.find('input[name=M_prodprice]:checked, input[name=prod_id]:checked')
			.parents('div.panel')
			.trigger('click');
	},
	
	update_form: function()
	{
		/*
			The old updatetotal script affects hidden form fields.
			This new script grabs those values and uses them.
		*/
		
		switch (UPGRADE_JS)
		{
			// upgrade2.js doesn't seem to update the description field
			case 2:
				$('input[name=desc]').val($('input[name=M_prodprice]:checked').parent('.product').attr('title'));
			break;
		}
		
		$('.discount_total').text($('input[name=amount]').val());
		$('.extra_listings_total').text($('input[name=extralistingscost]').val());
	},
	
	set_up_why_upgrade: function()
	{
		$('li a.js_load').click(function()
		{
			var li = $(this).parent('li');
			var content = li.find('div.content');
			if (content.length)
			{
				content.toggle();
			}
			else
			{
				var page = $(this).attr('href');
				var what = $(this).attr('class').match(/js_load (\w+)$/)[1];
				var part = $(this).attr('class').match(/js_load_part/);
				li.append('<div class="content '+what+'">...</div>');
				content = li.find('div.content.'+what);
				content.hide();				
				content.load(page+' .js_load'+((part)?'.'+what:''), function()
				{
					content.prepend('<a class="cross_close" href="#">X</a>');
					content.find('a.cross_close').click(function()
					{
						content.toggle();
						return false;
					});
					
					// Custom actions
					switch (what)
					{
						case 'why_upgrade':
							content.find('a.bluebuttontextlink').remove();	
							break
						case 'compare_packages':
							// Do something
							break;
					}
					content.slideDown('fast');
				});
			}
			return false;
		});
	}
};

$(document).ready(upgrade_form.initialise);

