(function($) {

	$.extend({
		get_finance_quotes: function(vrm, callback) 
		{
				
		var info = 'mode=get_finance_quotes&vrm=' + vrm + '&anti_cache=' + (Math.random() * 99999999);
		
		$.ajax({
			type: "GET",
			dataType: "html",
			url: "ajax_content.php",
			data: info,
			success: function(data){
				//console.log('Getting vehicle finance quote');
				$('#finance_quotes').html(data);			
			}			
		});

       		return false;
		},
 
		get_finance_listings: function(vrms) 
		{

			$.each(vrms, function(id, vrm) {
		      $('#finance_listing_' + vrm).html('<h3>Retrieving Finance Quotes...</h3><img src="templates/vauxhallclick/vauxhallclick_1/images/loading_animation.gif" alt="Loading Quote" />');
		    });

			
			var imploded_vrms = vrms.join('|');
			
			var info = 'mode=get_finance_listings&vrms=' + imploded_vrms + '&anti_cache=' + (Math.random() * 99999999);
			
			$.ajax({
				type: "GET",
				dataType: "json",
				url: "ajax_content.php",
				data: info,
				success: function(data){

					if(data.result == 'ok')
					{
						if(data.typical_apr != null)
						{
							$('#apr').html('<p>Typical APR ' + data.typical_apr + '%</p>');
						}
	
						$.each(vrms, function(id, vrm) {
					      
					      if(data.output[vrm] != null)
					      {

					      	$('#finance_listing_' + vrm).html(data.output[vrm]);
					      }
					      else
					      {
					      	$('#finance_listing_' + vrm).html('');
					      }
					      
					    });

					}
					else
					{
						//console.log('No OK result retrieved.');
					}				
				}			
			});

       		return false;
		},


		get_finance_quote: function(vrm, finance_type, callback) 
		{
				
		var info = 'mode=get_finance_quote&vrm=' + vrm + '&type=' + finance_type + '&anti_cache=' + (Math.random() * 99999999);
		
		$.ajax({
			type: "GET",
			dataType: "html",
			url: "ajax_content.php",
			data: info,
			success: function(data){
				//console.log('Getting ' + finance_type + ' finance quote for ' + vrm);
				$('#finance_quote_' + finance_type).html(data);			
			}			
		});

       		return false;
		},

		get_finance_listing: function(vrm) 
		{
		
			$('#finance_listing_' + vrm).html('<h3>Retrieving Finance Quotes...</h3><img src="templates/vauxhallclick/vauxhallclick_1/images/loading_animation.gif" alt="Loading Quote" />');
				
			var info = 'mode=get_finance_listing&vrm=' + vrm + '&anti_cache=' + (Math.random() * 99999999);
			
			$.ajax({
				type: "GET",
				dataType: "html",
				url: "ajax_content.php",
				data: info,
				success: function(data){
					//console.log('Getting finance listing for ' + vrm);
					$('#finance_listing_' + vrm).html(data);					
				}			
			});

       		return false;
		},

		get_insurance_quote: function (vrm, target_id)
		{
		
			$('#' + target_id).html('<h3>Retrieving Insurance Quotes...</h3>');
				
			var info = 'mode=get_insurance_quote&vrm=' + vrm + '&anti_cache=' + (Math.random() * 99999999);
			
			$.ajax({
				type: "GET",
				dataType: "html",
				url: "ajax_content.php",
				data: info,
				success: function(data){
					$('#' + target_id).html(data);					
				}			
			});

       		return false;
		}


	});	

	$(window).bind('load', function() {
		$("#finance_terms_submit").click(function(){

		//Make it look like its doing something.
		$('#finance_quote_hp').html('<h2>Recalculating HP Quote...</h2><img src="templates/vauxhallclick/vauxhallclick_1/images/loading_animation.gif" alt="Loading Quote" />');
		$('#finance_quote_lease_purchase').html('<h2>Recalculating Lease Purchase Quote...</h2><img src="templates/vauxhallclick/vauxhallclick_1/images/loading_animation.gif" alt="Loading Quote" />');
		$('#finance_quote_pcp').html('<h2>Recalculating PCP Quote...</h2><img src="templates/vauxhallclick/vauxhallclick_1/images/loading_animation.gif" alt="Loading Quote" />');

		var vrm = $("#finance_terms_vrm").val();
		
		var finance_deposit = $("#finance_deposit").val();
		var finance_term = $("#finance_term").val();
		var finance_annual_mileage = $("#finance_annual_mileage").val();
		//var vrm = $("#finance_terms_vrm").val();
		var info = 'mode=set_finance_terms&finance_deposit=' + finance_deposit + '&finance_term=' + finance_term + '&finance_annual_mileage=' + finance_annual_mileage;
			
		$.ajax({
			type: "GET",
			dataType: "html",
			url: "ajax.php",
			data: info,
			success: function(data){
				//console.log('Setting finance terms', info);
				//console.log('HTML: ' + data);
				$.get_finance_quote(vrm, 'hp');
				$.get_finance_quote(vrm, 'pcp');
				$.get_finance_quote(vrm, 'lease_purchase');				
			}			
		});
			


			return false;
		});
	});	
	
})(jQuery);

