(function(jQuery){
	jQuery.fn.fuelWidget = function(options){
		var defaults = {
			costLitre:128.9,
			petrol:128.9,
			diesel:134.9,
			costTank:45.0,
			fuelType:'Unleaded',
			fuelTypeSelect:'.fuelType > a',
			fuelPrice:'#costLitre , #costLitreView',
			totalMileage:350
            
		};

		var options = jQuery.extend(defaults, options);
		
		//if(this.nodeName=='FORM'){
			this.submit(function(e){
				e.preventDefault()
				options.costTank=$('#costFullTank').val();
				options.totalMileage=$('#totalMileage').val();
				calculateMPG();
				return false;
			})
		//}
		
		//set the title to the fuel type retuirned from price lookup
		//important petrol is retruened as unleaded so check the title is coprrect if not getting anything
		$(options.fuelTypeSelect).click(function(e){
			e.preventDefault();
			$(options.fuelTypeSelect).removeClass('active');
			options.fuelType=$(this).attr('title');
			$(this).addClass('active');
			update();
			
		})
		
		var havePrices=false;
		var prices=null;
		var calculate = false;
		if(this.length){
			$.ajax({
			  url:'/frontend-operations/fuel-prices/',
			  dataType: 'json',
			  success: function(data){
					prices=data;
					calculate=true;
					update();
				},
				error: function( objRequest ){
					calculate=false;
				}
			});
		}
		
		//loop over fuel prices and match current selected fuel type 
		//update the input box with the value
		function update(){
			if(prices!=null){
				$.each(prices,function(index,value){
					if (index==options.fuelType){
						$(options.fuelPrice).each(function(){
							if (this.nodeName=='INPUT'){
								$(this).val('&pound;' + value[1]);
							}else{
								$(this).html('&pound;' + value[1]);
							}
						})
					}
				})
			}
		}
		
		function calculateMPG(){
			errors='';
			if(calculate==false){
				errors+='No fuel prices are currently available\n';
			}
			
			if( isNaN( options.costTank ) || options.costTank == '' || options.costTank == 0 ) {
				errors+='Please complete the Full Tank Cost with numbers only\n';
				//calculate = false;
			}

			if( isNaN( options.totalMileage ) || options.totalMileage == '' || options.totalMileage == 0 ) {
				errors+='Please complete the Mileage with numbers only\n';
				//calculate = false;
			}
			if(errors!=''){
				alert(errors);
			}else{
				options.costLitre=prices[options.fuelType];
				var tankLitres=parseFloat(options.costTank) / (parseInt(options.costLitre) / 100);
				var tankGalons=tankLitres / 4.546;
				var mpg = (options.totalMileage / tankGalons).toFixed(1);
				
				var costPerMile = (( options.costLitre * 4.546 ) / mpg).toFixed(2) ;
				$('#mpgResults').html(mpg);	
				//calculateEcoSaving(costPerMile,totalMileage);

			}
		}

		//not needed here but maybe for other sites
		//needs converting / testing
		function calculateEcoSaving(mycostmile,totalMileage){

			/*if ( {car[pencePerMile]} > 0 ) {
		
				var mycost  = mycostmile*totalMileage;
				var carcost = {car[pencePerMile]}*totalMileage;
				var saving  =  mycost-carcost;
				if ( saving > 0 ) {
					saving = saving/100;
					$('myEconomySave').style.display='block';
					$('myEconomySaveAmount').innerHTML=saving;
				} else {
					$('myEconomySave').style.display='none';
				}
			}*/

		}

	}	
})(jQuery);

