$(document).ready(function()
{
	upgrade_form.initialise();
});

var upgrade_form =
{
	initialise: function()
	{
		this.set_up_product_events();
		this.select_initial_product();
		this.set_up_why_upgrade();
		this.set_up_extra_info();
	},
	
	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')
					.children('input[name=M_prodprice]')
					.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
			.children('input[name=M_prodprice]:checked')
			.parent('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];
				li.append('<div class="content '+what+'">...</div>');
				content = li.find('div.content.'+what);
				content.hide();				
				content.load(page+' div.js_load', 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;
		});
	}
};
