// JS Global de index
$(document).ready(function()
{
	$(document).pngFix();
	$('#navmenu-v li').mouseover(function() { $(this).stop().find('a').animate({backgroundPosition:"0 -32px"}, {duration:100}) })
										.mouseout(function() { $(this).stop().find('a').animate({backgroundPosition:"0 0"}, {duration:100}) });
										
	//Menu
	var config = {
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		interval: 100, // number = milliseconds for onMouseOver polling interval
		over: menuTopHover, // function = onMouseOver callback (REQUIRED)
		timeout: 100, // number = milliseconds delay before onMouseOut
		out: menuTopOut // function = onMouseOut callback (REQUIRED)
	};

	$('ul#topnav li .subnav').css({'opacity':'1'});
	$('ul#topnav li').hoverIntent(config);
	
	menuVertical();
	
	var v = $("#f_contact").validate({    
										 errorLabelContainer: $("#messages"),
                     submitHandler: function(form) {
											 				 signForm(form); 				 
											 				 
                              jQuery(form).ajaxSubmit({
                                     success: showResponse,
                                     dataType: 'json'
                              });
                     }
             });
	if (GBrowserIsCompatible()) {

									
/*			var myLatitude=19.373387;
			var myLongitude=-99.179682;*/
			var myLatitude=19.373304;
			var myLongitude=-99.179798;
			var latLngTelecomab = new GLatLng(myLatitude, myLongitude);
			var map = new GMap2(document.getElementById("telecomab-map"));
			map.setCenter(latLngTelecomab, 17);
			//point = new GLatLng(myLatitude, myLongitude);
			map.setUIToDefault();
//			map.enableScrollWheelZoom();
			
      var markerIcon = new GIcon(G_DEFAULT_ICON);
			markerIcon.iconSize = new GSize(40, 21);
//      markerIcon.shadowSize = new GSize(50, 16);
//			markerIcon.iconAnchor = new GPoint(9, 34);


      markerIcon.image = "http://dev.telecomabmex.com/telecomabmex/images/telecomab-map-marker.png";
			markerOptions = { icon:markerIcon};
				
			marker = new GMarker(latLngTelecomab, markerOptions);
			map.addOverlay(marker);
			
			var myPano = new GStreetviewPanorama(document.getElementById("telecomab-sv"));
      //fenwayPark = new GLatLng(42.345573,-71.098326);
      myPOV = {yaw:350.64659986187695,pitch:-10};
      myPano.setLocationAndPOV(latLngTelecomab, myPOV);
      GEvent.addListener(myPano, "error", handleNoFlash);
	} else  {
		$('#telecomab-map').html("<p>No se puede mostrar el mapa. Su navegador no es compatible</p>");
	}
	
});

function handleNoFlash(errorCode) {
      if (errorCode == 603) {
        //alert("Error: Street View Flash doesn't appear to be supported by your browser");
        $('#telecomab-sv').html('<p style="text-align:center">Error. Su navegador no soporta contenido Flash</p>');
        return;
      }
}

		
function signForm(form)
{
	$(form).append('<input type="hidden" name="auth" value="'+document.auth+'" />');
	return;
}

function showResponse(r)
{
	var $messages = $('#messages');
//	alert($messages.attr('class'));
	$messages.show().removeClass($messages.attr('class')).addClass(r.type).html(r.message);
	$("#f-contact").resetForm();
}

function menuTopHover() {
	
	$(this).find('.nav').animate({
		backgroundPosition: '0px -38px'
	}, 300);
	
	$(this).find('.nav').stop().animate({
		backgroundPosition:"0 -38px",
		marginTop:'-9px',
		height:'47px'
	},100);
	
	$(this).find('.subnav').stop().fadeTo('fast', .9).show();
	
	//Calculate width of all ul's
	(function($) {
		jQuery.fn.calcSubWidth = function() {
			rowWidth = 0;
			//Calculate row
			$(this).find('ul').each(function() {
				rowWidth += $(this).width();
			});
		};
	})(jQuery);
	
	if ( $(this).find('.row').length > 0) { //If row exists...
		var biggestRow = 0;
		//Calculate each row
		$(this).find(".row").each(function() {
			$(this).calcSubWidth();
			//Find biggest row
			if(rowWidth > biggestRow) {
				biggestRow = rowWidth;
			}
		});
		//Set width
		$(this).find('.subnav').css({'width' :biggestRow});
		$(this).find('.row:last').css({'margin':'0'});
	} else { //If row does not exist...
		$(this).calcSubWidth();
		//Set Width
		$(this).find('.subnav').css({'width' : rowWidth});
	}
}

function menuTopOut()
{
//	alert('Out');
//	$(this).animate({"height":50},200);
	$(this).find('.nav').animate({
		backgroundPosition:'0 0px',
		marginTop:'0',
		height:'38px'
	}, 100);
	$(this).find('.subnav').stop().fadeTo('fast', 0, function() {
		$(this).hide();
	});
}

function verticalMenuTopHover() {
	
	$(this).find('.nav').animate({
		backgroundPosition: '0px -38px'
	}, 300);
	
	$(this).find('.nav').stop().animate({
		backgroundPosition:"0 -38px",
		marginTop:'-9px',
		height:'47px'
	},100);
	
	$(this).find('.submenu').stop().fadeTo('fast', .9).show();
	
	//Calculate width of all ul's
	(function($) {
		jQuery.fn.calcSubWidth = function() {
			rowWidth = 0;
			//Calculate row
			$(this).find('ul').each(function() {
				rowWidth += $(this).width();
			});
		};
	})(jQuery);
	
	if ( $(this).find('.row').length > 0) { //If row exists...
		var biggestRow = 0;
		//Calculate each row
		$(this).find(".row").each(function() {
			$(this).calcSubWidth();
			//Find biggest row
			if(rowWidth > biggestRow) {
				biggestRow = rowWidth;
			}
		});
		//Set width
		$(this).find('.submenu').css({'width' :biggestRow});
		$(this).find('.row:last').css({'margin':'0'});
	} else { //If row does not exist...
		$(this).calcSubWidth();
		//Set Width
		$(this).find('.submenu').css({'width' : rowWidth});
	}
}

function verticalMenuTopOut()
{
//	alert('Out');
//	$(this).animate({"height":50},200);
	$(this).find('.nav').animate({
		backgroundPosition:'0 0px',
		marginTop:'0',
		height:'38px'
	}, 100);
	$(this).find('.submenu').stop().fadeTo('fast', 0, function() {
		$(this).hide();
	});
}

