function menu(d) {
	var dd = document.getElementById(d);
	var origen = document.getElementById('iniciarsesion');
	for (var lx=0, ly=0;
         origen != null;
         lx += origen.offsetLeft, ly += origen.offsetTop, origen = origen.offsetParent);
	ly=ly-10;
	lx=lx+10;
	dd.style.top = ly+"px";
	dd.style.left = lx+"px";
	if(dd.style.display != "block") { 
		dd.style.display = "block";
		document.getElementById('iniciarsesion').innerHTML="";
	} else { 
		dd.style.display = "none"; 
		document.getElementById('iniciarsesion').innerHTML="<a onclick=\"menu('login'); return true;\" href=\"javascript:void(0);\">Iniciar sesión <img src=\"http://media.mapnia.com/imagenes/iconos/flecha_blanca.png\" border=\"0\" width=\"10\" height=\"9\" alt=\"Flecha Blanca\" /></a>";
	}
}

function altura() {
	var altura = document.getElementById('menu').offsetHeight-6;
	document.getElementById('map').style.height = altura+'px';
	map.checkResize();
	map.setCenter(new GLatLng(40.416691,-3.700345));
}

function centrar_mapa(x,y) {
	var zoom = 16 
	map.setCenter(new GLatLng(x,y), zoom);
}

function cargar(sec,alias,inicio,limite) {
	document.getElementById("menu").innerHTML="<p style='text-align:center;'>Cargando...<br /><img src='http://media.mapnia.com/imagenes/iconos/cargando.gif' width='220' height='19' alt='Cargando'></p>";
	$.post("/includes/menu.php", {"sec":sec,"alias":alias,"inicio":inicio,"limite":limite}, function(contenido){document.getElementById("menu").innerHTML=contenido;altura(); cluster.fitMapToMarkers();});		
}



function slideshow(){
	$('.slider').advancedSlider({width: 430, height: 280, skin: 'curved-square', scrollbarSkin: 'scrollbar-7-light', alignType: 'centerCenter', horizontalSlices: 7, verticalSlices: 5, navigationArrows: false, navigationButtons: false, fadeNavigationArrows: false, navigationButtonsNumbers: false, thumbnailsType: 'navigation', thumbnailSync: false, thumbnailOrientation: 'vertical', thumbnailArrows: false, thumbnailScrollbar: false, thumbnailMouseScroll: true, thumbnailMouseWheel: false});
}


function cargar_negocio(sec,idnegocio,inicio,limite) {
	document.getElementById("sec_contenido").innerHTML="<p style='text-align:center;'>Cargando...<br /><img src='http://media.mapnia.com/imagenes/iconos/cargando.gif' width='220' height='19' alt='Cargando'></p>";
	$.post("/includes/negocio.php", {"sec":sec,"idnegocio":idnegocio}, function(contenido){
		document.getElementById("sec_contenido").innerHTML=contenido;
		$('#celda_descripcion').removeClass('celda_on').addClass('celda_off');
		$('#celda_comentarios').removeClass('celda_on').addClass('celda_off');
		$('#celda_ofertas').removeClass('celda_on').addClass('celda_off');
		$('#celda_noticias').removeClass('celda_on').addClass('celda_off');
		$('#celda_eventos').removeClass('celda_on').addClass('celda_off');
		$('#celda_cerca').removeClass('celda_on').addClass('celda_off');
		$('#celda_tweets').removeClass('celda_on').addClass('celda_off');
		$('#celda_checkin').removeClass('celda_on').addClass('celda_off');
		$('#celda_'+sec).removeClass('celda_off').addClass('celda_on');
		if (sec=="descripcion") {
			slideshow();
		}
	});	
	
}

function contador(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}

function enlazar(alias){ 
	var tamano
	tamano = document.getElementById('tamano').selectedIndex;
	if (tamano==0) {
		document.getElementById('ancho').value="640";
		document.getElementById('alto').value="480";		
	}
	
	if (tamano==1) {
		document.getElementById('ancho').value="425";
		document.getElementById('alto').value="350";		
	}
	
	if (tamano==2) {
		document.getElementById('ancho').value="300";
		document.getElementById('alto').value="300";		
	}
	
	actualizar_codigo(alias);
} 

function enlazar_marcador(alias, marcador){ 
	var tamano
	tamano = document.getElementById('tamano').selectedIndex;
	if (tamano==0) {
		document.getElementById('ancho').value="640";
		document.getElementById('alto').value="480";		
	}
	
	if (tamano==1) {
		document.getElementById('ancho').value="425";
		document.getElementById('alto').value="350";		
	}
	
	if (tamano==2) {
		document.getElementById('ancho').value="300";
		document.getElementById('alto').value="300";		
	}
	
	actualizar_codigo_marcador(alias, marcador);
} 

function actualizar_codigo(alias){ 
	document.getElementById('codigo').value="<iframe width=\""+document.getElementById('ancho').value+"\" height=\""+document.getElementById('alto').value+"\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"http://www.mapnia.com/enlazar/"+document.getElementById('ancho').value+"/"+document.getElementById('alto').value+"/"+alias+"\"></iframe>";
}

function actualizar_codigo_marcador(alias,id){ 
	document.getElementById('codigo').value="<iframe width=\""+document.getElementById('ancho').value+"\" height=\""+document.getElementById('alto').value+"\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"http://www.mapnia.com/enlazarmarcador/"+document.getElementById('ancho').value+"/"+document.getElementById('alto').value+"/"+id+"/"+alias+"\"></iframe>";
}

function toggleClustering() {
	cluster.clusteringEnabled=!cluster.clusteringEnabled;
	cluster.refresh(true);	
}

function noenter() {return !(window.event && window.event.keyCode == 13);}
  
function mostrar(d) {
	if(d.length < 1) { return; }
	if(document.getElementById(d).style.display == "none") {document.getElementById(d).style.display = "block";}
	else {document.getElementById(d).style.display = "none";}
}

function click_email() {	
	if (document.getElementById("email_login").value== "E-mail") {
		document.getElementById("email_login").value="";
	}
}

function click_pass() {	
	if (document.getElementById("password_login").value== "Password") {
		document.getElementById("password_login").value="";
	}
}


function validar_login() {
  if (document.getElementById("email_login").value=="") {
	  alert("Debes decirnos tu e-mail");
	  document.getElementById("email_login").focus();
	  return false;
  }
 
	var filtro = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filtro.test(document.getElementById("email_login").value)) {
		alert('Debes decirnos tu e-mail');
		document.getElementById("email_login").focus
	return false;
	}
   
  if (document.getElementById("password_login").value=="" || document.getElementById("password_login").value=="Password" ) {
	  alert("Debes decirno tu password");
	  document.getElementById("password_login").focus();
	  return false;	
  }
  document.formulario.submit();
}


function validar_buscador() {
  if (document.getElementById("q").value=="") {
	  alert("Debes introducir el texto que quieres buscar");
	  document.getElementById("q").focus();
	  return false;
  }

  if (document.getElementById("q").value.length<2) {
	  alert("Debes introducir una cadena con más de 1 carácteres");
	  document.getElementById("q").focus();
	  return false;
  } 
	
  document.buscador.submit();
}

function cerrarsesion() {
    if(confirm("¿Seguro que quieres cerrar la sesión?")) {
        document.location.href= '/acciones/cerrarsesion.php';
    }
} 

function validar_anadir_evento() {
  if (document.getElementById("evento").value=="") {
	  alert("Debes decirnos el título del evento");
	  document.getElementById("evento").focus();
	  return false;
  }
  
  if (document.getElementById("fecha_inicio").value=="") {
	  alert("Debes indicar la fecha y hora de inicio del evento");
	  document.getElementById("fecha_inicio").focus();
	  return false;
  }

  
  if (document.getElementById("fecha_fin").value=="") {
	  alert("Debes indicar la fecha y hora de fin del evento");
	  document.getElementById("fecha_fin").focus();
	  return false;
  }
  
   CadenaFecha1=document.getElementById("fecha_inicio").value;
   CadenaFecha2=document.getElementById("fecha_fin").value;
   var miFecha1 = new Date(CadenaFecha1)  
   var miFecha2 = new Date(CadenaFecha2)  
   if (miFecha1>miFecha2) { 
   	alert("La fecha inicial no puede ser posterior"); 
	return false;
	}
  
  if (document.getElementById("tipo").selectedIndex=="0") {
		alert("Debe seleccionar el tipo de evento");
	 	document.getElementById("tipo").focus();
	  	return false;
 	}	
  
  document.formulario.submit();
}		

function porcentaje_de_mayusculas(texto) {
val = 1;
var matchList = "";
var regexp = /[A-Z]/g;
while ((match = regexp.exec(texto)) != null) {
	matchList += val;
}
porcentaje_mayusculas = matchList.length/texto.length * 100
return porcentaje_mayusculas.toFixed(2);
} 


function mostrar_alerta(texto) {
$(function () {
	window.scrollTo(0,0);
	var $alerta = $('#alerta');
	$alerta.html('<div id="alerta_mensaje">'+texto+'</div>');
	if($alerta.length)
	{
		var alerttimer = window.setTimeout(function () {
			$alerta.trigger('click');
		}, 3000);
		
		$alerta.animate({height: '62'}, 200)
		.click(function () {
			window.clearTimeout(alerttimer);
			$alerta.animate({height: '0'}, 200);
		});
	}
});
}
