

//DETECTION NAVEGADOR
nav_detection=navigator.userAgent.search(/MSIE 6/)
if(nav_detection>0)var IE6=true
else var IE6=false


function pngFIX(){
                
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (document.body.filters)) 
{
   for(var i=0; i<document.images.length; i++)
   {
      var img = document.images[i]
      var imgName = img.src.toUpperCase()
      if (img.className=="pngfix" )
      {
         var imgID = (img.id) ? "id='" + img.id + "' " : ""
         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         var imgStyle = "display:inline-block;" + img.style.cssText 
         if (img.align == "left") imgStyle = "float:left;" + imgStyle
         if (img.align == "right") imgStyle = "float:right;" + imgStyle
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
         img.outerHTML = strNewHTML
                                i = i-1
      }
   }
}
}


/*SHADOWBOX Options*/
var shadowbox_options = {
   		handleOversize:		'resize',
        displayNav:         true,
        autoplayMovies:     false,
		initialWidth:		66,
		initialHeight:		66,
		resizeDuration:		0.40,
		fadeDuration:		0.40,
		overlayOpacity:		0.5,
		overlayColor:		'#000000',
		counterType:		'skip',
		counterLimit:		15
		
    };
	

/* FUNCIONES FRANJAS */

var abierta=null;

function array_search(needle,haystack,argStrict){var strict=!!argStrict;var key='';for(key in haystack){if((strict&&haystack[key]===needle)||(!strict&&haystack[key]==needle)){return key;}}
return false;}

function closeWin(operacion)
{
	// Cerrar la actual
	var elemento=$("#"+operacion);
	 alturas[elemento.attr("id")]=elemento.height();
	$("#"+operacion).html("<div class=\"mov2\"></div>");
	$("#"+operacion).animate({
		"width":  "142px",
		"height": "278px" }, velocidad, function() {
			$("#"+operacion).html(contenidos[operacion]);	
			asignaEvento(operacion);
			abierta=null;
			$("#"+operacion).css("backgroundColor","");
		});
	
	
}
function toggleFeaturedWin(operacion)
{
	// Abre o cierra del destacado
	var aux="";
	if (opfet)
	{
		// Cierra destacado	
		//$("#"+operacion).html("");
		aux=$("#"+operacion).html();
		$("#"+operacion).html("<div class=\"mov\"></div>");
		$("#"+operacion).animate({
		"width":  "369px",
		"height": "165px",
		"left": "568px"}, velocidad, function() {
			
			$("#"+operacion).html(destacado);
			destacado=aux;
			asignaEventoDest();
			$("#"+operacion).css("cursor","pointer");
		});
		$("#right").animate({"paddingTop":"0px"}, velocidad);
	} else {
		// Abre destacado	
		aux=$("#"+operacion).html();
		$("#"+operacion).html("<div class=\"mov\"></div>");
		$("#right").animate({"paddingTop":"679px"}, velocidad);
		$("#"+operacion).animate({
		"width":  "541px",
		"height": "844px",
		"left": "396px"
		}, velocidad, function() {
			$("#"+operacion).html(destacado);
			destacado=aux;
			$("#"+operacion).unbind("click");
			$("#"+operacion).css("cursor","default");
		});

		
	}
	opfet=!opfet;		
}

function toggleWin(operacion)
{
	// Cerrar la abierta
	if (abierta!=null)
	{
		$("#"+abierta).html("<div class=\"mov2\"></div>");
		$("#"+abierta).animate({
		"width":  "142px",
		"height": "278px"}, velocidad, function() {
			$("#"+abierta).html(contenidos[abierta]);
			cambia(operacion);
			asignaEvento(abierta);
			abierta=operacion;		
			$("#"+abierta).css("backgroundColor","");
		});
	} else {			
		cambia(operacion);
		abierta=operacion;
	}
}

function asignaEvento(id)
{
	// Vuelve a asignar un evento al clic de la caja
	$("#"+id).css("cursor","pointer");
	$("#"+id).click(function()
	{
		//$(this).css("backgroundColor","#FFF");
		toggleWin($(this).attr("id"));
	});
}

function abreFoto(id)
{
	// Abre la foto en un shadowbox
	Shadowbox.open({
        content:    id,
        player:     'img'
    });
}

function abreMapa(id,tipo)
{
	// Abre el mapa en un Shadowbox
	Shadowbox.open({
        content:    ruta+'content/comun/mapa.php?id='+id+"&tipo="+tipo,
        player:     'iframe',
		width:		500,
		height:		400,
        title:      'Detalle de ubicación'
    });

}

var alturas=new Array();

function array_key_exists ( key, search ) {
    // Checks if the given key or index exists in the array  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/array_key_exists
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Felix Geisendoerfer (http://www.debuggable.com/felix)
    // *     example 1: array_key_exists('kevin', {'kevin': 'van Zonneveld'});
    // *     returns 1: true
    // input sanitation
    if (!search || (search.constructor !== Array && search.constructor !== Object)){
        return false;
    }

    return key in search;
}

function cambia(operacion)
{
	// Cambia la posición de la caja a la primera de la fila
	// Luego abre la caja y pone en contenido ampliado
	// Guarda los contenidos originales
	contenidos[operacion]=$("#"+operacion).html();
	// Buscamos en que fila original va
	// Primero la posición en el array
	var posactual=array_search(operacion.substring(2),pss);
	// Buscamos la primera posición de la fila
	var destino=posactual-(posactual%maxcajas);
	var elemento;
	// Sustituimos una por la otra
	/*if (posactual!=destino)
	{
		var orig=$("#"+operacion);
		var tipo;
		if (orig.attr("class").indexOf("lugar")==-1)
		{
			tipo="ev"; // Tipo Evento
		} else {
			tipo="pl"; // Tipo Lugar
		}
		var clon=$("#"+operacion).clone(true);
		// Buscamos el primero para intercambiarlo
		var primhijo=$("#"+operacion).parent().find(".evento:first");
		primhijo.before(clon);
		orig.remove();
		// Intercambiamos las posiciones en el array
		var aux=pss[destino];
		pss[destino]=pss[posactual];
		pss[posactual]=aux;
		elemento=clon;
		
	} else {*/
		elemento=$("#"+operacion);
	//}
	// Esto solo tiene que hacerlo cuando abre el elemento
	// Elimina bind
	elemento.unbind("click");
	elemento.css("cursor","default");
	if (elemento.attr("class").indexOf("lugar")==-1)
	{
		var _ruta=ruta+"content/comun/get-content-event.php?id="+elemento.attr("id").substr(2)+"&ruta="+ruta;
	} else {
		var _ruta=ruta+"content/comun/get-content-place.php?id="+elemento.attr("id").substr(2)+"&ruta="+ruta	
	}
	elemento.removeClass("evhover");
	//elemento.html("<div class=\"mov2\"></div>"); /// Borramos el contenido
	$.ajax({
	  url: _ruta,
	  cache: true,
	  success: function(html){
		 // Obtiene la altura de resumen
		 elemento.css("visibility","hidden");
		 elemento.html(html);
		 if (!array_key_exists(elemento.attr("id"), alturas))
		 {
	 		/* var altura=elemento.height()+40;
			 salto2=salto-60;
	 		 var factor=parseInt(altura/salto2)+1;
			 altura=610+(base*factor)-base;
			if (altura==916) altura=906;
			 alturas[elemento.attr("id")]=altura;
			 //		 alert(factor);
			//		 alert(altura+" "+factor);*/
			altura=906;
		 } else {
			 altura=alturas[elemento.attr("id")];
		 }

		 elemento.html("<div class=\"mov2\"></div>");
		 elemento.css("visibility","visible");

		anima(elemento,altura,html);
			
	  }
	});

}	

function anima(elemento,altura,html)
{
	 elemento.animate({
		"width":  "499px",
		"height": altura+"px"}, velocidad, function() {
				elemento.css("height","auto");
				elemento.html(html);	
				var h=elemento.height();
				if (h>altura) {
					altura+=316;
					anima(elemento,altura,html);
				} else {
					elemento.css("height",altura+"px");
					alturas[elemento.attr("id")]=altura;
					elemento.css("backgroundColor","#FFF");	
					$(".evento").each(function() {
						if($(this).attr("id")!=elemento.attr("id")) $(this).css("backgroundColor","");
					});
				}
		});
}

function abreElemento(el,tipo)
{	
	var elemento=$("#"+tipo+el);
	var altura=elemento.height();
	 var factor=parseInt(altura/salto)+1;
	// alert(factor);
	 altura=600+(base*factor)-base;
	 elemento.css("width","499px");
	 elemento.css("height",altura+"px");
	 

}

function asignaEventoDest()
{

	$(".eventodest").click(function()
	{
		// Asigna el click al evento destacado
		toggleFeaturedWin($(this).attr("id"));
	});
}

function hovercajas()
{		
	// Define el hover de las cajas de elementos
	$(".evento").hover(function() {
		if(abierta!=$(this).attr("id") && $(this).attr("class").indexOf("rel")==-1) {
			$(this).addClass("evhover");							
		}
	}, function() {
		$(this).removeClass("evhover");							
	});
}

function anchorEvento(idev)
{
	var tipo=idev.substring(0,2);
	// Hace un anchor sobre el evento si no está abierto
	if(abierta!=idev) {
			document.location.href="#anchor"+idev;
	}
}
function reescala()
{
	var anchmin=$(window).width()-253;
	if (anchmin<300) anchmin=300;
	$("#right").css("width", anchmin+ "px");
}
function toggleSubscribe()
{
	if (subscribe)
	{
		$("#suscribete").fadeOut(300);
	} else {
		$("#suscribete").fadeIn(300);
	}
	subscribe=!subscribe;
}

function trim(str,charlist){var whitespace,l=0,i=0;str+='';if(!charlist){whitespace=" \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";}else{charlist+='';whitespace=charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g,'$1');}
l=str.length;for(i=0;i<l;i++){if(whitespace.indexOf(str.charAt(i))===-1){str=str.substring(i);break;}}
l=str.length;for(i=l-1;i>=0;i--){if(whitespace.indexOf(str.charAt(i))===-1){str=str.substring(0,i+1);break;}}
return whitespace.indexOf(str.charAt(0))===-1?str:'';}

function lastPostFunc()
{
	// Llega al pie, carga los últimos eventos que no haya cargado
	var posi=null;
	$(".evento").each(function() {
		var html=trim($(this).html());
		if (html.length<150)
		{
			$(this).html("<img src=\""+ruta+"content/imgs/ajax-loader.gif\" />");
			// Busca el tipo
			var position=($(this).offset());
			if (posi==null) posi=position["top"];
			// Solo amplia los de la misma fila
			if (position["top"]==posi)
			{
				$(this).html("<img src=\""+ruta+"content/imgs/ajax-loader.gif\" />");
				var id=$(this).attr("id");
				var tip=id.substring(0,2);
				if (tip=="ev")
				{
					// Cargamos el contenido de un evento (en pequeñín)
					var html = $.ajax({ url: ruta+"content/comun/get-mini-event.php?ruta="+ruta+"&id="+id.substring(2),  async: false }).responseText;
					$(this).html(html);
				} else {
					var html = $.ajax({ url: ruta+"content/comun/get-mini-place.php?ruta="+ruta+"&id="+id.substring(2),  async: false }).responseText;
					$(this).html(html);
				}
			}
		}
	  });
							   
}

$(window).scroll(function(){
        if  ($(window).scrollTop() == $(document).height() - $(window).height()){
           lastPostFunc();
        }
}); 

function selcapa(capa)
{
	if ($("#l"+capa).css("display")=="none") $("#l"+capa).fadeIn(300);
	else $("#l"+capa).fadeOut(300);
}

function selecitem(capa,valor)
{
	$("#la"+capa).html($('#f'+capa+valor).html());
	eval("document.filtro."+capa+".value=valor");
	//$("#l"+capa).fadeOut(300);
	//
}
