var precios = [0.1,0.1,0.68,0.0421,2.0904,1.3072];
var consumos = [1.43,10,0.997,10,0.391,0.511];
var mascaraprecios = "####0.0000"
var diverrorie7=""

$(document).ready(function () {
	diverrorie7 = $("#errorie7").html();
	$("#btnCalcular").click(function () {
		calcular();
		return false;
	});
	$("#metros").numeric();
	$("#precio").numeric(".");
	$("#combustible").val(0);
	$("#metros").val('');
	$("#precio").val(0);
	$("#b3").html(precios[0]);
	$("#b3").format({format:mascaraprecios});
	$('#combustible').change(function(){
		$("#precio").val(precios[$("#combustible :selected").val()]);
		$("#precio").format({format:mascaraprecios});
		$("#btnCalcular").trigger('click');
		//calcular();
	});
	$("#metros").blur(function() {
		$("#metros").format({format:"####0"});
	});
	$("#precio").blur(function() {
		$("#precio").format({format:mascaraprecios});
	});

	//tooltip
	$("[title]").mbTooltip({ // also $([domElement]).mbTooltip
		opacity : .85,       //opacity
		wait:800,           //before show
		cssClass:"default",  // default = default
		timePerWord:70,      //time to show in milliseconds per word
		hasArrow:false,            // if you whant a little arrow on the corner
		hasShadow:true,
		imgPath:"images/",
		anchor:"mouse", //or "parent" you can ancor the tooltip to the mouse  or to the element
		shadowColor:"black", //the color of the shadow
		mb_fade:200 //the time to fade-in
	});

	// formularios marcados
	$(".marcados").focus(function() {
		$(this).parent().addClass("curFocus")
	});
	$(".marcados").blur(function() {
		$(this).parent().removeClass("curFocus")
	});
	// Validacion y Envio del form
	jQuery("#contacto").validate({
		messages:{
			name:'Rellene el nombre',
			comment:'Rellene el campo',
			email:{required:'Rellene el mail',email:'El mail introducido no es v&aacute;lido'}
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#contact_form"
			});
		}
	});
	jQuery("#reset").click(function() {
		jQuery("#contacto").clearForm();
		jQuery("#contacto").resetForm();
	});

$('#example1,#example2').FontEffect({
    shadow:true,
       shadowColor     :"#000",// The color of the shadow
    shadowOffsetTop :3,     // The top offset position (px)
    shadowOffsetLeft:1,     // The left offset position (px)
    shadowBlur      :1,     // The shadow blur 1=none,2=low,3=high
    shadowOpacity   :0.5,   // The opacity of the shadow (0=none,1=all)
    outline:true,
   outlineColor1:"#3b3b3b"
})







});

function calcular() {
	var metros = $("#metros").val();
	var precio = 	$("#precio").val();;
	var consumo = consumos[$("#combustible :selected").val()]
	var coste = precio * consumo;
	$("#b4").html(((($("#b3").html()*consumos[0])*12*182)/100)*metros);
	$("#c4").html(((coste*12*182)/100)*metros);

	$("#b4").format({format:"#####0.00"});
	$("#c4").format({format:"#####0.00"});
	var ahorro = ($("#c4").html())-($("#b4").html())
	if (ahorro < 0) ahorro = 0 ;
	$("#t4").html("<h4 style='text-align:right'>"+format_number(ahorro,2)+"</h4>");
	$("#errorie7").html(diverrorie7);



}

function format_number(pnumber,decimals){
	if (isNaN(pnumber)) { return 0};
	if (pnumber=='') { return 0};

	var snum = new String(pnumber);
	var sec = snum.split('.');
	var whole = parseFloat(sec[0]);
	var result = '';

	if(sec.length > 1){
		var dec = new String(sec[1]);
		dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
		dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
		var dot = dec.indexOf('.');
		if(dot == -1){
			dec += '.';
			dot = dec.indexOf('.');
		}
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	} else{
		var dot;
		var dec = new String(whole);
		dec += '.';
		dot = dec.indexOf('.');
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	}
	return result;
}

function inicializarmapa() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_NORMAL_MAP);
		var bounds = new GLatLngBounds();

		var geocoder = new GClientGeocoder();
		var address = "C/ Méjico 6 ,45004 Toledo"
		geocoder.getLatLng(  address ,
		function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				//map.setCenter(point, 13);
				bounds.extend(point);

				var marker = new GMarker(point);
				marker.openInfoWindowHtml("<p style='color:red'>CAPTERSOL<br>C/ Méjico 6 1º Izq.<br>45004 - Toledo</p>", { maxWidth: 20,noCloseOnClick:true });
				map.addOverlay(marker);
				map.setCenter(bounds.getCenter());
				map.setZoom(map.getBoundsZoomLevel(bounds));
			}
		}
		);
		//		map.setZoom(0)
	}

}


