﻿jQuery(function() {
    var p = jQuery("#formContact").validate({
        submitHandler: function(form) {
            send.mailSend();
        },
        errorLabelContainer: null
    });
    // show a simple loading indicator
    var loader = jQuery('<div id="loader">Enviando...</div>')
			    .css({ position: "fixed", top: "1em", textAlign: "center" })
			    .appendTo("body")
			    .hide();
    jQuery().ajaxStart(function() {
        var backItem = $("#BackAlertLay");
        backItem.show();
        loader.show();
    }).ajaxStop(function() {
        loader.hide();
        p.resetForm();
    }).ajaxError(function(a, b, e) {
        throw e;
    });
});

var send = {
    mailSend: function() {
        var f = $("#formContact");
        var serializedForm = f.serialize();
        $.post("/Contacto/SendMail", serializedForm, function() { openWin.init(['alertOK', 'Información de contacto', 'Tu mensaje se ha enviado correctamente.', ""]); });
        return false;

    }
}

/******* Mapa *************/
var map = null;
var geocoder = null;

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 15);
        geocoder = new GClientGeocoder();
    }
}

function showAddress() {
    var address = "Av. Manoteras 18, 6º 28050 Madrid";    
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  alert(address + " not found");
              } else {
                  map.setCenter(point, 15);
                  var marker = new GMarker(point);
                  map.addOverlay(marker);
                  marker.openInfoWindowHtml("<dl><dt>Zeppelin Televisión</dt><dd> Av. Manoteras 18, 6º</dd><dd> 28050 Madrid</dd></dl>");
              }
          }
        );
    }
}
/******* Mapa *************/


$(document).ready(function() {
    if ($("div#map_canvas").length != 0) {
        initialize();
        GUnload();
        showAddress();
    }
});