//init



$(document).ready(function() {
    jQuery.easing.def = 'easeInOutCubic';
	$('.list_item').mouseenter(function() {
	  var id=$(this).attr('id');
	  var op=($(this).css('opacity'));
	  if (op==1) {
	  if ((op==1) && (document.getElementById(id+'f')) && (!document.getElementById(id+'videos'))) {
	  	$('#'+id+'f').animate({opacity: '0.1'},300);
	  	$('#'+id+'c').animate({opacity: '1.0'},300);
		
  	  }
      $('#'+id+'mas').animate({opacity: '1.0'},300);
	  $('#'+id+'col').animate({top: '0px'},200);
  	  }
	});
	$('.list_item').mouseleave(function() {
	  var id=$(this).attr('id');
	  if ((document.getElementById(id+'f')) && (!document.getElementById(id+'videos'))) {
	  	$('#'+id+'f').animate({opacity: '1.0'},300);
	  	$('#'+id+'c').animate({opacity: '0.0'},300);
		
	  }
	  $('#'+id+'mas').animate({opacity: '0.0'},300);
	  $('#'+id+'col').animate({top: '-10px'},200);
	});
	
	//$('.tipo_eventos').tooltip({offsetX:-20, offsetY:-20});
	
	$('div.gal_photo a').fancyZoom({scaleImg: false, closeOnClick: true});
	
	var fechaFin=fecha_hoy;
	//provisional quitar noticias
	//if ((ap=='noticias') || (ap=='agenda') || (subap=='agenda')) fechaFin='2999/12/12';
	if ((ap=='agenda') || (subap=='agenda')) fechaFin='2999/12/12';
	
	$('.turn-me-into-datepicker').datePicker({inline:true, showYearNavigation:false, startDate:'1999/01/01', endDate:fechaFin});
	$('.turn-me-into-datepicker').dpSetSelected(fecha);
	$('.turn-me-into-datepicker').bind(
		'dateSelected',
		function(e, selectedDate, $td)
		{
			sub='';
			if (subap!='') sub = subap+'/';
			if (ap=='geo') {
			//window.location='/'+ap+'/'+sub+'fecha/'+selectedDate.getFullYear()+'/'+(selectedDate.getMonth()+1)+'/'+selectedDate.getDate();
			window.location='/'+ap+'/agenda/fecha/'+selectedDate.getFullYear()+'/'+(selectedDate.getMonth()+1)+'/'+selectedDate.getDate();
			} else {	
			window.location='/'+ap+'/fecha/'+selectedDate.getFullYear()+'/'+(selectedDate.getMonth()+1)+'/'+selectedDate.getDate();
			}
		}
	)
	
	if (ap=='geo') $('#calendario').css('left','0px');
			
			
	$(document).bind('click', function(e) {  

	var $clicked = $(e.target);
	//alert($clicked.attr("id"));
	if (! $clicked.hasClass("calendario")) { $("#calendario").slideUp("fast"); }
	if ((! $clicked.hasClass("listacats")) && (! $clicked.hasClass("catnosel")) && (! $clicked.hasClass("catsel"))) { $("#catlist").hide(); }
	if ((! $clicked.hasClass("listainsts")) && (! $clicked.hasClass("instnosel")) && (! $clicked.hasClass("instsel"))) { $("#instlist").hide(); }
	if (! $clicked.hasClass("sharewindow")) { $('#sharewindow').animate({height: '0px'},200); }
	if (! $clicked.hasClass("searchfield")) { $('#searchfield').animate({width: '175px'},200); }
	
	
	if (! $clicked.hasClass("insertwindow")) { $('#insertwindow').animate({height: '0px'},200); }
	
	});
	
	//funcion detectar cursores
	$(document).keydown(function(e){
		if (!$('#searchlist').is(":hidden")) {

	    	if (e.keyCode == 40) { 
	       		
				sugpos++;
				changesugpos();
		   
	    	}
			if (e.keyCode == 38) { 
	       		
				sugpos--;
				changesugpos();
		   
	    	}
		}
	});
	
	$('#searchlist').mouseenter(function() {
		$('.slsel').attr('class','slnosel');
		sugpos=0;
	});
	
	//$(document).unbind('keydown');
	
	//funcion detectar enter
    $('#searchfield').keypress(function(e){
		 
		 str=$('#searchfield').val();
		 if (str.length>2) { $('#searchbot').css('opacity',1); 
			mostrarsugstimer(); 
		 } else { $('#searchbot').css('opacity',0.2); $('#searchlist').hide(); sugpos=0; }
		 
	      if(e.which == 13){
	       	if (str.length>2) { buscar(str); } else { $(this).effect("shake", { direction:'up', distance:3, times:3 }, 30); $(this).css('background-color','#454545');
				setTimeout("$('#searchfield').focus();",600);
	 			}
	       }
	});
	
	//roll news
	$('#roll_'+ap).animate({ marginTop: '0px'},400);
	
	$(".termsframe").overscroll({direction:'vertical',showThumbs:true,persistThumbs:true});
	
});

//roll news
var rollpos=1;

function clicbannerpos(pos) {
	
	window.clearInterval(banntimer);
	//banntimer =  window.setInterval("nextbanner()", 3000);
	movbanner(pos);
	
}

function movroll() {
	
	$('#roll_'+ap).animate({ marginTop: '35px'},100);
	setTimeout("$('#rollref').html('"+actualist[rollpos][0]+"')",140);
	setTimeout("$('#rollref').attr('href','"+actualist[rollpos][1]+"')",140);
	//setTimeout("$('#roll').css('marginTop','35px');",150);
	setTimeout("$('#roll_"+ap+"').animate({ marginTop: '0px'},300);",150);
	
}
function nextroll() {
	rollpos++;
	if (rollpos>=(actualist.length)) { rollpos=1; }
	movroll();
}
if ((ap=='') || (ap=='home')) { var rolltimer =  window.setInterval("nextroll()", 5000); }
//fechas

//video
function video_tam(id,tam) {
	
	if (id=='tambotpers') { $('#tampers').show(); tam=$('#tampersancho').val(); $('#tampersancho').focus(); } else { $('#tampers').hide();  }
	tamx=tam;
	tamy=Math.round(tam/videoprop);
	$('#embebtext').html('<iframe scrolling="no" frameborder="0" width="'+tamx+'" height="'+tamy+'"  src="http://www.humania.tv/video.php?id='+videoid+'&h='+tamy+'"></iframe>')
	$('.tambotsel').removeClass('tambotsel').addClass('tambot');
	
	$('#'+id).removeClass('tambot').addClass('tambotsel');
	
	$('#tampersancho').val(tamx);
	$('#tampersalto').val(tamy);	

}

function changetam(id) {

	if (id=='tampersancho') {
		tamx=$('#tampersancho').val();
		tamy=Math.round(tamx/videoprop);
		$('#tampersalto').val(tamy);	
	}
	if (id=='tampersalto') {
		tamy=$('#tampersalto').val();
		tamx=Math.round(tamy*videoprop);
		$('#tampersancho').val(tamx);	
	}
	
	video_tam('tambotpers',tamx)
	
}

//buscador
sugpos=0;
totalsug=3;

function changesugpos() {
	
	$('.slsel').attr('class','slnosel');
	if (sugpos>totalsug) sugpos=totalsug;
	if (sugpos<1) sugpos=1;
	$('#sug'+sugpos).attr('class','slsel');
	$('#searchfield').val($('#sug'+sugpos+' a').html());
	
}

function mostrarsugs() {
	
	/*
	ruta=rutaweb+'includes/getsugs.php?ap='+ap+'&s='+$('#searchfield').val();
	$.ajax({
		type: "GET",
		url: ruta,
		cache: false,
		success: function(html) {
			$('#searchlist').html(html);
			$('#searchlist').show();
			window.clearInterval(sugtimer);
		}
	});	
	*/
}

var sugtimer;

function mostrarsugstimer() {
	window.clearInterval(sugtimer);
	sugtimer =  window.setInterval("mostrarsugs()", 600);
}


function buscar(pals) {
	var apart=ap;
	if ((ap=='home') || (ap=='humania') || (ap=='contacto')) apart='';
	
	window.location=rutaweb+'?ap='+apart+'&s='+pals.replace(/ /gi,'+');
	
}

function cargasmasres(id,start,pals,inst) {
	
	$('#'+id+'_text').html('<p style="color:#eee">.</p><div class="loading"></div>');
	ruta='includes/maslist.php?ap='+ap+'&st='+start+'&s='+pals+'&si='+inst;
	$.ajax({
		type: "GET",
		url: ruta,
		cache: false,
		success: function(html) {
			$('#'+id).html(html);
		}
	});
	
}

function cargasmasvids(id,start) {
	
	$('#'+id+'_text').html('<p style="color:#eee">.</p><div class="loading"></div>');
	ruta='includes/masvideos.php?st='+start;
	$.ajax({
		type: "GET",
		url: ruta,
		cache: false,
		success: function(html) {
			$('#'+id).html(html);
		}
	});
	
}



//banners
var bannerpos=0;
var totbaners=0;

function clicbannerpos(pos) {
	
	window.clearInterval(banntimer);
	//banntimer =  window.setInterval("nextbanner()", 3000);
	movbanner(pos);
	
}

function movbanner(pos) {
	
	$('#bannerlist').animate({
        left: '-'+(pos*990)+'px'
    },
    800);
	$('.possel').attr('class','posunsel');
	$('#banpos'+pos).attr('class','possel');
	bannerpos=pos;	
}
function nextbanner() {
	
	bannerpos++;
	if (bannerpos<totbaners) { movbanner(bannerpos); }
	
	
}
var banntimer =  window.setInterval("nextbanner()", 6000);
//fechas

function opencal() {
	
	if($('#calendario').is(":hidden")) {
		$('#calendario').slideDown("fast");
	} else {
		$('#calendario').slideUp("fast");
	}
	
}

// effects

function fadein(obj) {
	//alert(obj);
	$('#'+obj).animate({
        opacity: '1.0'
    },
    400);
}


function desplegarfecha(id){
	
	if($('#'+id+'_list').is(":hidden")) {
	 	$('#'+id+'_list').slideDown();
		$('#'+id).attr("class",'list_fecha fechaup');
		
	
	} else {
		$('#'+id+'_list').slideUp();
		$('#'+id).attr("class",'list_fecha fechadown');
	}
	setTimeout('resizcont();',500);
}
function desplegardia(id){
	
	if($('#'+id+'_list').is(":hidden")) {
	 	$('#'+id+'_list').slideDown();
		$('#'+id).attr("class",'list_fecha fechaup');
		
	
	}
	setTimeout('resizcont();',500);
}

//filtros
var filtroactual='todo';

function filtrar_tipo(filtro) {
	
	//alert(filtro);
	
	$('#menutipos li a').attr('class', '');
	$('#bot_filt_'+filtro).attr('class', 'menusel');
	$('#menucats li a').attr('class', '');
	$(".catsel").attr('class','catnosel');
	$(".instsel").attr('class','instnosel');
	
	filtroactual=filtro;
	if (filtro=='todo') {
		$('.list_item').show();
		$('#videoframe').animate({height: bannerwidth+'px'});
	} else {
		$('#videoframe').animate({height: '0px'});
		$('.list_item').hide();
		$('.tipo_'+filtro).show();
	}
	$('#bot_cat_0').html('Filtrar por categoría'); 
	$('#bot_inst_0').html('Filtrar por institución'); 
	/*	
	if (filtro=='todo') {
		$('.tipo_noticias').animate({opacity: '1.0'},300);
		$('.tipo_eventos').animate({opacity: '1.0'},300);
		$('.tipo_videos').animate({opacity: '1.0'},300);
		$('#videoframe').animate({height: '360px'});
	} else {
		$('#videoframe').animate({height: '0px'});
		$('.tipo_noticias').animate({opacity: '0.05'},300);
		$('.tipo_eventos').animate({opacity: '0.05'},300);
		$('.tipo_videos').animate({opacity: '0.05'},300);
		$('.tipo_'+filtro).animate({opacity: '1.0'},300);
	}
	*/
		if (apid!=0) desplegardia('fecha0');
		setTimeout('resizcont();',500);
}

function abrircats() {
	$('#instlist').hide();
	if($('#catlist').is(":hidden")) {
		$('#catlist').show();
	} else {
		$('#catlist').hide();
	}
	
}
var totcats=0;
function abririnsts() {
	$('#catlist').hide();
	if($('#instlist').is(":hidden")) {
		$('#instlist').show();
	} else {
		$('#instlist').hide();
	}
	
}
var totinsts=0;

function resetfiltrocats() {
	$('.catsel').attr('class', 'catnosel');
	$('.list_cat').hide();
	if (filtroactual=='todo') {
		$('.list_item').show();
		//$('#videoframe').animate({height: bannerwidth+'px'});
	} else {
		$('.list_item').hide();
		$('.tipo_'+filtroactual).show();
	}
	$('#bot_cat_0').html('Filtrar por categoría '); 
	//filtrar_cat('cat_0_0');
	
}
function resetfiltroinsts() {
	$('.instsel').attr('class', 'instnosel');
	$('.list_cat').hide();
	if (filtroactual=='todo') {
		$('.list_item').show();
		//$('#videoframe').animate({height: bannerwidth+'px'});
	} else {
		$('.list_item').hide();
		$('.tipo_'+filtroactual).show();
	
	}
	$('#bot_inst_0').html('Filtrar por institución '); 
	
	
	//filtrar_inst('inst_xx');
	
}


function capitalize(cad){ 
    return cad.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};

function filtrar_cat(cat) {
	
	resetfiltroinsts();
	var id=cat;
	var cl=cat.split('_');
	var cat=cl[1];
	var subcat=cl[2];
	
	//alert(cat);
	
	//$('#menucats li a').attr('class', '');
	if ($('#'+id).attr('class')=='catnosel') { $('#'+id).attr('class', 'catsel'); } else { $('#'+id).attr('class', 'catnosel'); }
		if (id!='cat_0_0') $('#videoframe').animate({height: '0px'});
		//filtrar_tipo(filtroactual);
		$('.list_cat').hide();
		if (filtroactual=='todo') {
			$('.list_item_cat'+cat).show();
		} else {
			$('.list_item_cat'+cat+'.tipo_'+filtroactual).show();
		}
		//repasamos cat activadas y preparados cadena jquery comparacion
		var cad='';
		
		totcats=0;
		$(".catsel").each(function (i) {
			var id=this.id;
			if (filtroactual=='todo') {
				$(".list_item_"+id).show();
			} else {
				$(".tipo_"+filtroactual+".list_item_"+id).show();
			}
			cad+=$("#"+id).html()+' + ';
			//alert(".list_item_"+id);
			totcats++;
		});
		//alert(totcats);
		if (totcats>0) {
			cad ='<strong>'+cad.substr(0,cad.length-3)+'</strong>';
			$('#bot_cat_0').html('Filtrando por '+cad); 	
		} else { 
			if (filtroactual=='todo') {
				$('.list_item').show();
			} else {
				$('.list_item').hide();
				$('.tipo_'+filtroactual).show();
			}
			$('#bot_cat_0').html('Filtrar por categoría '); 
		}
		abrircats();
		if (apid!=0) desplegardia('fecha0');
		setTimeout('resizcont();',500);
}

function filtrar_inst(inst) {
	
	resetfiltrocats();
	var id=inst;
	//alert(cat);
	$('.instsel').attr('class', 'instnosel');
	//$('#menucats li a').attr('class', '');
	$('#'+id).attr('class', 'instsel'); 
		if (id!='inst_0_0') $('#videoframe').animate({height: '0px'});
		//filtrar_tipo(filtroactual);
		$('.list_cat').hide();
		if (filtroactual=='todo') {
			$('.list_item_inst'+inst).show();
		} else {
			$('.list_item_inst'+inst+'.tipo_'+filtroactual).show();
		}
		//repasamos cat activadas y preparados cadena jquery comparacion
		var cad='';
		
		totinsts=0;
		$(".instsel").each(function (i) {
			var id=this.id;
			if (filtroactual=='todo') {
				$(".list_item_"+id).show();
			} else {
				$(".tipo_"+filtroactual+".list_item_"+id).show();
			}
			cad+=$("#"+id).html()+' + ';
			//alert(".list_item_"+id);
			totinsts++;
		});
		//alert(totcats);
		if (totinsts>0) {
			
			cad ='<strong>'+cad.substr(0,cad.length-3)+'</strong>';
			$('#bot_inst_0').html('Filtrando por '+cad); 
			
		} else { 
				if (filtroactual=='todo') {
					$('.list_item').show();
				} else {
					$('.list_item').hide();
					$('.tipo_'+filtroactual).show();
				}
				$('#bot_inst_0').html('Filtrar por institución '); 
		}
		abririnsts();
		if (apid!=0) desplegardia('fecha0');
		setTimeout('resizcont();',500);
	
}

function filtrar_geo_cat(cat) {
	//resetfiltrogeo_insts();
	//ocultamos todos los markers
	for (i=0;i<markersArray.length;i++) {
		markersArray[i].setVisible(false);
	}
	//$('#menucats li a').attr('class', '');
	if ($('#'+cat).attr('class')=='catnosel') { $('#'+cat).attr('class', 'catsel'); } else { $('#'+cat).attr('class', 'catnosel'); }
	totcats=0;
	var cad='';
	$(".catsel").each(function (i) {
		totcats++;
		var cat=this.id;
		cad+=$("#"+cat).html()+' + ';
		for (i=0;i<markersArray.length;i++) {
			var mar=markersArray[i];
			var ht=mar.html.split('//');
			if (ht[6]==cat) {
				mar.setVisible(true);
			}
		}
	});
	if (totcats>0) {
		cad ='<strong>'+cad.substr(0,cad.length-3)+'</strong>';
		$('#bot_cat_0').html('Filtrando por '+cad); 
		
	} else { 
			$('#bot_cat_0').html('Filtrar por categoría '); 
	}
	abrircats();
	$('#bot_inst_0').html('Filtrar por institución '); 
	setTimeout("map.fitBounds(bounds); ",600);
	//hide_geo_info();
	hide_paises();
	document.getElementById('location_menu').innerHTML='País';
}

function filtrar_geo_inst(inst) {
	//resetfiltrogeo_insts();
	//ocultamos todos los markers
	for (i=0;i<markersArray.length;i++) {
		markersArray[i].setVisible(false);
	}
	$('.instsel').attr('class', 'instnosel');
	$('#'+inst).attr('class', 'instsel');
	totinsts=0;
	var cad='';
	$(".instsel").each(function (i) {
		totinsts++;
		var inst=this.id;
		cad+=$("#"+inst).html()+' + ';
		for (i=0;i<markersArray.length;i++) {
			var mar=markersArray[i];
			var ht=mar.html.split('//');
			if (ht[7]==inst) {
				mar.setVisible(true);
			}
		}
	});
	if (totinsts>0) {
		cad ='<strong>'+cad.substr(0,cad.length-3)+'</strong>';
		$('#bot_inst_0').html('Filtrando por '+cad); 
		
	} else { 
		$('#bot_inst_0').html('Filtrar por institución '); 
	}
	abririnsts();
	//hide_geo_info();
	setTimeout("map.fitBounds(bounds); ",600);
	$('.catsel').attr('class', 'catnosel');
	$('#bot_cat_0').html('Entidades colaboradoras '); 
	hide_paises();
	document.getElementById('location_menu').innerHTML='País';
	
	//resetfiltrogeo_insts();
}
function resetfiltrogeo_cats() { 
	$('.catsel').attr('class', 'catnosel');
	for (i=0;i<markersArray.length;i++) {
		markersArray[i].setVisible(true);
	}
	$('#bot_cat_0').html('Entidades colaboradoras '); 
	//filtrar_cat('cat_0_0');	
	//hide_geo_info();
	setTimeout("map.fitBounds(bounds); ",600);
	hide_paises();
	document.getElementById('location_menu').innerHTML='País';
}
function resetfiltrogeo_insts() {
	$('.instsel').attr('class', 'instnosel');
	for (i=0;i<markersArray.length;i++) {
		markersArray[i].setVisible(true);
	}
	$('#bot_inst_0').html('Filtrar por institución '); 
	//filtrar_cat('cat_0_0');
	//hide_geo_info();
	setTimeout("map.fitBounds(bounds); ",600);
	hide_paises();
	document.getElementById('location_menu').innerHTML='País';
	
}

//AJAX functions

// ajax para recargar paginas -->
function nuevoAjax() {
    var xmlhttp = false;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(E) {
            xmlhttp = false;
        }
    }

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function cargadiv(capa, url) {
	//alert(capa);
    var ajax = nuevoAjax();
    //$('iphone_website').innerHTML='';
    nocache = Math.random();
	//alert(url);
    ajax.open("GET", url + '&nocahe=' + nocache.toString(), true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            //alert(ajax.responseText);
            document.getElementById(capa).innerHTML = ajax.responseText;
        }
    }
    ajax.send(null);
}


//resize functions

function ancho() {
    var myWidth = 0,
    myHeight = 0;
    if (typeof(window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth - 15;
}

function alto() {
    var myWidth = 0,
    myHeight = 0;
    if (typeof(window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight - 2;
}

function resizcont() {
	
	//alert($('#page').height());
	//$('#logo').html($('#page').height());
	if (($('#page').height()+250)<alto()) { $('#footerpage').attr("class",'footerpage'); } else { $('#footerpage').attr("class",''); }
	$('#footerpage').show();
	
}

//window.setInterval("resizcont()", 1000);

function resiz() {

	resizcont();
	
	if (ap=='geo') {
    	var an = Math.round(((ancho() - 990) / 2) + (990 / 2) - 150);
		if (map) map.panTo(last_pos);
	    if (geo_info == 1) {

	        //document.getElementById('map').style.width=(ancho()-an-70)+'px';
	        document.getElementById('mapinfo').style.width = (an) + 'px';
	
        
	    } else {
	        //document.getElementById('mapframe').style.height = (alto() - 176) + 'px';
	    }
	    if (geo_full == 0) {
			$('#fullbot').show();
	        //document.getElementById('map').style.width=(ancho()-an-70)+'px';
	        //document.getElementById('mapinfo').style.width = (an) + 'px';
	        document.getElementById('mapframe').style.height = (280) + 'px';
			document.getElementById('map').style.height = (305) + 'px';
	    } else {
			$('#fullbot').hide();
	        document.getElementById('mapframe').style.height = (alto() - 174) + 'px';
			document.getElementById('map').style.height = (alto() - 174) + 'px';
	    }
	}

	
}

$(window).resize(function() {
  resiz();
});


//funciones mapa geo

function geo_activaorg() {
    $('#bottaborg').attr('class', 'tab taborg activa');
    $('#bottabfil').attr('class', 'tab tabfiles');
    $('#bottabcol').attr('class', 'tab tabcolab');
    $('#tab_organiza').show();
    $('#tab_files').hide();
    $('#tab_colab').hide();
    $('#tab_organiza').animate({
        opacity: '1.0'
    },
    400);
    $('#tab_files').animate({
        opacity: '0.0'
    },
    400);
    $('#tab_colab').animate({
        opacity: '0.0'
    },
    400);
	setTimeout('resizcont();',500);

}
function geo_activafil() {
    $('#bottaborg').attr('class', 'tab taborg');
    $('#bottabfil').attr('class', 'tab tabfiles activa');
    $('#bottabcol').attr('class', 'tab tabcolab');
    $('#tab_organiza').hide();
    $('#tab_files').show();
    $('#tab_colab').hide();
    $('#tab_organiza').animate({
        opacity: '0.0'
    },
    400);
    $('#tab_files').animate({
        opacity: '1.0'
    },
    400);
    $('#tab_colab').animate({
        opacity: '0.0'
    },
    400);
	setTimeout('resizcont();',500);

}
function geo_activacol() {
    $('#bottaborg').attr('class', 'tab taborg');
    $('#bottabfil').attr('class', 'tab tabfiles');
    $('#bottabcol').attr('class', 'tab tabcolab activa');
    $('#tab_organiza').hide();
    $('#tab_files').hide();
    $('#tab_colab').show();
    $('#tab_organiza').animate({
        opacity: '0.0'
    },
    400);
    $('#tab_files').animate({
        opacity: '0.0'
    },
    400);
    $('#tab_colab').animate({
        opacity: '1.0'
    },
    400);
	setTimeout('resizcont();',500);
}

var geo_info = 0;
var last_id = 0;



function loadgeoinfo(id) {
	
	$('#geo_info').css('opacity', '0.0');
	//alert(id);
	ruta=rutaweb+'includes/geo_info.php?ap='+subap+'&idc='+id;
	//alert(ruta);
	$.ajax({
			type: "GET",
			url: ruta,
			cache: false,
			success: function(html) {
				$('#geo_info').html(html);
				$('#geo_info').animate({opacity: '1.0'},400);
				if (subap=='inst') { $('#botmas').hide(); } else { $('#botmas').show(); }
			}
	});
	
}

function show_geo_info(html) {

	var ht=html.split('//');
	last_id=ht[2];
	last_url=ht[5];
    if (geo_info == 0) {
        var an = Math.round(((ancho() - 990) / 2) + (990 / 2) - 150);
        $('#mapinfo').show();
        //$('#map').animate({width: (ancho()-an-70)+'px'}, 200);
        $('#mapinfo').css('opacity', '1.0');
		$('#mapinfo').animate({
            width: an + 'px'
        },
        200);
    }
	if (geo_full == 0) {
        $('#geo_cuerpo').hide();
    } else {
        $('#geo_cuerpo').show();
    }
    geo_info = 1;
	document.getElementById('botloc').innerHTML = '<p>'+ht[1]+'</p>';
    loadgeoinfo(last_id);

}

function hide_geo_info() {
	$('#mapinfo').animate({
        width: '0px', opacity: '0.0'
    },
    200);
	$('#fullbot').show();
	geo_info=0; resiz();
	setTimeout("$('#mapinfo').hide();",400);
}

function geo_fullscreen(){
	geo_full=1;
	$('.geo_content').hide();
	$('#footer').show();
	resiz();
	brooklyn = map.getCenter();
	origZoom = map.getZoom();
	//map.fitBounds(bounds);
	initialize();
}

function show_paises() {
	$('#menu_paises').animate({
        bottom: '33px'
    },
    300);
}

function hide_paises() {
	$('#menu_paises').animate({
        bottom: '-800px'
    },
    300);
}

function select_pais(address,paisid){
	
	
	if (paisid==0) {
		
		for (i=0;i<markersArray.length;i++) {
			markersArray[i].setVisible(true);
		}
		document.getElementById('location_menu').innerHTML='País';
		setTimeout("map.fitBounds(bounds); ",600);
		
	} else { 
	
		for (i=0;i<markersArray.length;i++) {
			markersArray[i].setVisible(false);
		}
		//$('.instsel').attr('class', 'instnosel');
		//$('#'+inst).attr('class', 'instsel');

		for (i=0;i<markersArray.length;i++) {
				var mar=markersArray[i];
				var ht=mar.html.split('//');
				if (ht[8]==paisid) {
					mar.setVisible(true);
				}
		}
	
		//abririnsts();
		//hide_geo_info();
		//setTimeout("map.fitBounds(bounds); ",600);
		//$('.catsel').attr('class', 'catnosel');
		$('#bot_inst_0').html('Filtrar por institución ');
		$('#bot_cat_0').html('Entidades colaboradoras ');
	
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': address}, function(results, status) {
	      if (status == google.maps.GeocoderStatus.OK) {
	        map.setCenter(results[0].geometry.location);
			map.fitBounds(results[0].geometry.bounds);
			//map.setZoom(17);
			//document.getElementById('geo_dir').value=(results[0].formatted_address);
			//document.getElementById('geo_lat').value=(results[0].geometry.location.lat());
			//document.getElementById('geo_lng').value=(results[0].geometry.location.lng());
			geo_lat=results[0].geometry.location.lat();
			geo_lng=results[0].geometry.location.lng();
	        //marker.setPosition(results[0].geometry.location);
	        //$('#map_status').html('Localización OK');
	      } else {
	        //$('#map_status').html('<span style="color:#ff0000;">No encontrado</span>');
			//alert("Geocode was not successful for the following reason: " + status);
	      }
	    });
	}
	
	hide_paises();
	document.getElementById('location_menu').innerHTML=address;
	if (geo_info==1) {
		hide_geo_info();
		
	}

}

Number.prototype.toRad = function() {
   return this * Math.PI / 180;
}

function markers_distance(lat1,lon1,lat2,lon2) {
	var R = 6371; // km
	var dLat = (lat2-lat1).toRad();
	var dLon = (lon2-lon1).toRad(); 
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
	        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
	        Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = R * c;
	return d;
	
}

function sel_marker(num) {
	var mark=markersArray[num];
	last_pos=mark.position;
    map.panTo(mark.position);
    //show_geo_info(mark.html);
	//mark.setIcon(image2);
	//getid=mark.tittle;
	
	
}

var list_mark_cercanos=new Array()
function markers_cercanos(id,lat,lng,km) {
	//alert(markersArray.length);
	var arrstring='';
	for (i=0; i<markersArray.length;i++) {
		var d=markers_distance(lat,lng,markersArray[i].getPosition().lat(),markersArray[i].getPosition().lng());
		if ((id!=markersArray[i].tittle) && (d<=km)) { 
			list_mark_cercanos.push(markersArray[i].tittle);
			var dist=Math.round(d*1000)/1000 ;
			if (dist<1) { dist=dist*1000+' metros'; } else { var dist=Math.round(d*10)/10 ; dist=dist+' Km'; }			
			arrstring=arrstring+markersArray[i].tittle+'_'+dist+'_'+i+','; 
			}
	}
	if (getid>0) cargadiv('geo_lista_cercanos','/ajax/geo_lista_cercanos.php?subap='+subap+'/&l='+arrstring);
}

function abrir_menu_distancias() {
	$('#dist_menu').animate({
        height: '100px'
    },
    300);
	
}
function cerrar_menu_distancias() {
	$('#dist_menu').animate({
        height: '0px'
    },
    300);
	
}
function select_distancia(d) {
	
	document.getElementById('dist_sel').innerHTML=d+' Km';
	cerrar_menu_distancias();
	markers_cercanos(getid,brooklyn.lat(),brooklyn.lng(),d);
	
}

var minimap;


  function initminimap(lat,lng) {
	//alert(lat);
  if($('#minimap_canvas').is(":hidden")) {
	$('#minimap_canvas').show();
	
    var latlng = new google.maps.LatLng(lat, lng);
    var myOptions = {
      zoom: 15,
      center: latlng,
	  scrollwheel: 0,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    minimap = new google.maps.Map(document.getElementById("minimap_canvas"), myOptions);
	var marker = new google.maps.Marker({
        position: latlng, 
        map: minimap
    });
  } else {
	$('#minimap_canvas').hide();
	
  }
	
  }







