function init(){
	inputText();
}

// HACE UN PEDIDO DE TIPO JSON
function requestJson(file,method,params,fn){
	return $.ajax({
		url: file,
	  	type: method,
		data: params,
		dataType: 'json',
	  	success: fn
	});
}

// INPUTS TEXT
function inputText(){
	$('input[type="text"]').each(function(){
		var inputValue;
		$(this).focusin(function(){
			if( $(this).attr('value') == '' ) {
				inputValue = $(this).attr('value');
				$(this).attr('value',inputValue);
			}
			if( inputValue == null ){
				inputValue = $(this).attr('value');
				$(this).attr('value','');
			}
		});
		$(this).focusout(function(){
			if( $(this).attr('value') == '' ) {
				$(this).attr('value',inputValue);
				inputValue = null;
			}
		}); 
	});
}

// FORMULARIO
function submitContactForm(){
	$(".errors").html("");
	if( $("#nombre").val() == "Ingrese su nombre" ){ var nombre=false; }else{ var nombre = $("#nombre").val(); }
	if( $("#telefono").val() == "Ingrese su numero telefonico" ){ $("#telefono").attr('value','');var telefono=""; }else{ var telefono = $("#telefono").val(); }
	if( $("#email").val() == "Ingrese su E-mail" ){ var email=false; }else{ var email = $("#email").val(); }
	if( $("input:radio[name=motivo]:checked").val() == undefined ){ var motivo=false; }else{ var motivo = $("input:radio[name=motivo]:checked").val(); }
	if( $("#consulta").val() == "" ){ var consulta=false; }else{ var consulta = $("#consulta").val(); }
	var i = 0;
	if( nombre ){i++;}else{ $(".errors").append("<li class='flechitaN'>Debe ingresar un nombre</li>") }
	if( email ){i++;}else{ $(".errors").append("<li class='flechitaN'>Debe ingresar un email</li>") }		
	if( motivo ){i++;}else{ $(".errors").append("<li class='flechitaN'>Debe seleccionar un motivo de consulta</li>") }
	if( consulta ){	i++}else{ $(".errors").append("<li class='flechitaN'>Debe ingresar una consulta</li>") }
	
	function success(data, textStatus, jqXHR){
		if( data.name ){ $(".errors").append("<li class='flechitaN'>"+data.name+"</li>") }
		if( data.email ){ $(".errors").append("<li class='flechitaN'>"+data.email+"</li>") }
		if( data.comentario ){ $(".errors").append("<li class='flechitaN'>"+data.comentario+"</li>") }
		if( data.msg ){
			$(".errors").append("<li class='successForm'>"+data.msg+"</li>");
			$("#nombre").attr('value','Ingrese su nombre');
			$("#telefono").attr('value','Ingrese su numero telefonico');
			$("#email").attr('value','Ingrese su email');
			$("#consulta").attr('value','');
		}
	}	
	
	if( i == 4){
		requestJson('/wp-content/themes/rumbason/email.php','POST',{email:true,nombre:nombre,telefono:telefono,email:email,motivo:motivo,consulta:consulta},success)
	}
	
}

// CARGAR MAPA
function loadMap(){
    var rumbason = new google.maps.LatLng(-34.636241,-58.407101);
	var imagen = '/wp-content/themes/rumbason/images/maplogo.png';
	var contenido = "<b>Instituto Rumbason</b><br/>Rondeau 3072 - Parque Patricios<br/>Tel.: 4912.3105";
    
	var opcionesMapa = {
      zoom: 16,
      center: rumbason,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
	  mapTypeControl:false,
	  scaleControl:false,
	  navigationControl:true,
	  navigationControlOptions: {
	  	style: google.maps.NavigationControlStyle.SMALL,
		position: google.maps.ControlPosition.LEFT_RIGHT
	  }
    };
    var mapa = new google.maps.Map(document.getElementById("mapa"), opcionesMapa);
	
	var marcador = new google.maps.Marker({
	    position: rumbason,
	    map: mapa,
		icon: imagen
	});
	
	var ventanita = new google.maps.InfoWindow({
		content:contenido,
		size: new google.maps.Size(10,10)
	});
	
	google.maps.event.addListener(marcador, 'mouseover', function(){
		ventanita.open(mapa,marcador);
	});
	
	google.maps.event.addListener(mapa, 'zoom_changed', function(){
		mapa.setCenter(rumbason);
	});
	
}

$(document).ready( init );
