// JavaScript Document	
//VARIABLES GENERALES
var map_lat_region = "";//"-32.88881315761994";
var map_long_region = "";//"-68.83895874023438";
var map;
var get_fiestas_mapa = config['controller']+"/front_get_fiestas_mapa.php";
var get_fiestas_alertas = config['controller']+"/front_get_fiestas_alertas.php";
var get_fiestas_videos = config['controller']+"/front_get_videos_perid.php";
var intval = "";
//var array_fiestas = new Array();
var array_mapa_fie_id = new Array();
var array_mapa_fie_nombre = new Array();
var array_mapa_fie_comentario = new Array();
var array_mapa_fie_imagen_perfil = new Array();
var	array_mapa_map_long = new Array();
var array_mapa_map_lat = new Array();
var	array_mapa_fie_tipo = new Array();
var array_mapa_per_nombre = new Array();
var array_mapa_per_apellido = new Array();
var	array_mapa_reg_id = new Array();
var array_mapa_provincia = new Array();
var array_mapa_pais = new Array();
var array_mapa_votos = new Array();
var array_mapa_fans = new Array();
var array_mapa_fecha = new Array();
var array_mapa_twitter = new Array();
var array_mapa_facebook = new Array();

var array_alert_fie_id = new Array();
var array_alert_fie_nombre = new Array();
var array_alert_fie_imagen_perfil = new Array();
var	array_alert_map_long = new Array();
var array_alert_map_lat = new Array();
var	array_alert_fie_tipo = new Array();
var array_alert_per_nombre = new Array();
var	array_alert_reg_id = new Array();
var array_alert_provincia = new Array();

var xml_fiestas_mapa = "";

var videos = new Array();
var video_count = 0;
var video_cant	= 0;

// ON READY ------------------------------------------------------//
$().ready(function (){		
	
	var coordenadas_gm = obtener_coordenadas_gm(lng_pais);
	coordenadas_gm = coordenadas_gm.split(",");
	map_lat_region = coordenadas_gm[0];
	map_long_region = coordenadas_gm[1];
	
	$("#btn_direccion_home").click(function (){
		mostrar_tapa();
		$("#formularioIngresarDireccion").css("display","inline");
		carga_pagina("formularioIngresarDireccion.php","#formularioIngresarDireccion",'get',0,"")	
		window.scrollTo(0,0);
		window.clearInterval(intval);
	})


	$("#calle").focus(function(){this.value="";})
	$("#altura").focus(function(){this.value="";})
	$("#provincia").focus(function(){this.value="";})
	$("#pais").focus(function(){this.value="";})

	/*if ($.browser.msie) {
		var mapa_height = parseInt($(window).height()) - 380;
		var mapa_width = parseInt($(window).width()) - 150;
	} else {
		var mapa_height = parseInt($(window).height()) - 20;
		var mapa_width = parseInt($(window).width()) - 20;
	}*/
	if ($("#conten_mapa").css("display")=="block"){
		$("#conten_mapa").css("height","389px");
		$("#conten_mapa").css("width","769px");
	
		// el navegador es compatible?
		if (GBrowserIsCompatible()) {
			
			map = new GMap2(document.getElementById("conten_mapa"));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(map_lat_region, map_long_region),5);
			geocoder = new GClientGeocoder();
			 
			

			
			
			//DEFINO EL ICONO DE FIESTA REAL
			iconoMarcaReal = new GIcon(G_DEFAULT_ICON);
			iconoMarcaReal.image = config['ruta_iconos']+config['icono_usuario'];			
			tamanoIconoReal = new GSize(config['icono_user_width'],config['icono_user_heigth']);
			iconoMarcaReal.iconSize = tamanoIconoReal;
			//iconoMarcaReal.shadow = config['ruta_iconos']+config['icono_usuario_sombra'];
			//tamanoSombraUser = new GSize(config['icono_user_sombra_width'],config['icono_user_sombra_heigth']);
			//iconoMarcaReal.shadowSize = tamanoSombraUser;
			iconoMarcaReal.iconAnchor = new GPoint(config['icono_user_anchor_x'], config['icono_user_anchor_y']);
			//Defino la posicion del globo (x,y)
			iconoMarcaReal.infoWindowAnchor = new GPoint(18,5);
			//defino el area de click
			iconoMarcaReal.imageMap = [15,42,9,42,5,40,2,38,0,36,2,34,4,33,9,33,12,2,16,0,21,0,25,2,27,3,22,35,26,36,29,38,30,41,28,42,15,42];
			
			
			//DEFINO EL ICONO DE FIESTA VIRTUAL
			iconoMarcaVirtual = new GIcon(G_DEFAULT_ICON);
			iconoMarcaVirtual.image = config['ruta_iconos']+config['icono_local_a'];
			tamanoIconoVirtual = new GSize(config['icono_local_a_width'],config['icono_local_a_heigth']);
			iconoMarcaVirtual.iconSize = tamanoIconoVirtual;
			//iconoMarcaVirtual.shadow = config['ruta_iconos']+config['icono_local_a_sombra'];
			//tamanoSombraLocal_l = new GSize(config['icono_local_a_sombra_width'],config['icono_local_a_sombra_heigth']);
			//iconoMarcaVirtual.shadowSize = tamanoSombraLocal_l;
			iconoMarcaVirtual.iconAnchor = new GPoint(config['icono_local_a_anchor_x'], config['icono_local_a_anchor_y']);
			//Defino la posicion del globo (x,y)
			iconoMarcaVirtual.infoWindowAnchor = new GPoint(18,5);
			//defino el area de click
			iconoMarcaVirtual.imageMap = [15,42,9,42,5,40,2,38,0,36,2,34,4,33,9,33,12,2,16,0,21,0,25,2,27,3,22,35,26,36,29,38,30,41,28,42,15,42];
			GEvent.addListener(map, "click", function() {
				window.clearInterval(intval);
			});
			GEvent.addListener(map, "dragend", function() {
				window.clearInterval(intval);
			});
			agregar_fiestas_mapa();
		} else {
			alert("Browser no compatible");
		}
	}
	
	setTimeout("agregar_alertas()",1000);
	setTimeout("armar_lista_videos('')",1000);
								   
	//INTERVALOS DE BUSQUEDA
	intval=setInterval("recargar_fiestas_mapa()",7000);
	setInterval("agregar_alertas()",300000);

});
// ---------------------------------------------------------------//
// CREAR MARCA EN EL MAPA ----------------------------------------//
function createMarker_real(point,html) {
	var marker = new GMarker(point,{icon:iconoMarcaReal});
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	  window.clearInterval(intval);
	});
	return marker;
}
function createMarker_virtual(point,html) {
	var marker = new GMarker(point,{icon:iconoMarcaVirtual});
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	  window.clearInterval(intval);
	});
	return marker;
}
// ---------------------------------------------------------------//
// BUSCO Y AGREGO LAS FIESTAS AL MAPA ----------------------------//
function agregar_fiestas_mapa() {
	var n_random	= 0;
	var reg_ids		="";
	var contador 	= 0;
	$.ajax({
		async:false,
		url: get_fiestas_mapa,
		dataType: "xml",
		/*type: "POST",
		data: {pai_id:config['id_pais']}, */
		success:function envia_datos(xml){
			
			//CARGO EL XML EN UNA VARIABLE PARA DESPUES PODER USARLO EN LA RECARGA DEL MAPA 
			xml_fiestas_mapa = xml;
			
			$('fiestas',xml).each(function(i) {
				var total_fiestas = $(this).attr('total');
				if (total_fiestas >= 20)
					total_fiestas = 20;
				n_random = Math.round(Math.random()*(total_fiestas - 1));
			});
			
			$('fiesta',xml).each(function(j) {
				array_mapa_fie_id[contador]				= $(this).attr('id');
				array_mapa_fie_nombre[contador] 		= $(this).find("nombre").text();
				array_mapa_fie_comentario[contador] 	= $(this).find("comentario").text();
				array_mapa_fie_imagen_perfil[contador]  = $(this).find("imagen_perfil").text();
				array_mapa_map_long[contador] 			= $(this).find("map_long").text();
				array_mapa_map_lat[contador] 			= $(this).find("map_lat").text();
				array_mapa_fie_tipo[contador]			= $(this).find("tipo").text();
				array_mapa_votos[contador]				= $(this).find("votos").text();
				array_mapa_fans[contador]				= $(this).find("fans").text();
				array_mapa_fecha[contador]				= $(this).find("fecha").text();
				array_mapa_twitter[contador]			= $(this).find("user_twitter").text();
				array_mapa_facebook[contador]			= $(this).find("id_facebook").text();
				if (reg_ids == "")
					reg_ids += $(this).attr("per_id");
				else
					reg_ids += ","+$(this).attr("per_id");
				contador = contador+1;
			});
			
		},
		error:function manejo_error(error){
			//mostrar_alerta("Error al cargar la informaci&oacute;n.",0);
		}
	});
	
	//OBTENEMOS LOS DATOS DE LAS PERSONAS
	//reg_ids = "1408863,1408748,1407035,1407035,1407035,1407035,1407035,1407035,1407035,1407035,1407035";
	if (reg_ids != ""){
		$.ajax({	
			async:false,
			url: config['controller_reg'],
			dataType: "xml",
			type: "POST",
			data: {action:"usuarios.get_usuarios",acoid:config['acoid'],regids:reg_ids},
			success:function envia_datos(xml){
				contador = 0;
				$('usuario',xml).each(function(j) {
					array_mapa_per_nombre[contador] 	= $(this).attr('nombre');
					array_mapa_per_apellido[contador] 	= $(this).attr('apellido');
					array_mapa_reg_id[contador] 		= $(this).attr('regId');
					array_mapa_provincia[contador] 		= $(this).attr('estado');
					array_mapa_pais[contador] 			= $(this).attr('paisNombre');
					
					contador = contador+1;
				})
			},
			error:function manejo_error(error){
				//mostrar_alerta("Error al cargar la informaci&oacute;n.",0);
			}
		});
	}
	
	//CREO LOS PUNTOS EN EL MAPA
	var html = "";
	//PROPIEDADES DE LA FOTO
	var propiedades_imagen="&w=80&h=80";
	
	for (i=0; i < array_mapa_fie_id.length; i++){
		
		if (array_mapa_per_nombre[i] != undefined){
			
			var point = new GLatLng(array_mapa_map_lat[i], array_mapa_map_long[i]);
			
			//alert(config['tod']+array_mapa_fie_imagen_perfil[i]);
			//alert(array_mapa_fie_tipo[i]);
			if (array_mapa_fie_tipo[i]==0){
				html = "";
				html += "	<div class='modal-mapa'>";
				html += "		<span class='bar-virtual'></span>";
				html += "		<p class='txt-blue'>";
				html += "			<img src='"+config['tod']+array_mapa_fie_imagen_perfil[i]+propiedades_imagen+"' alt='party'/>";
				html += "			<span class='n-fiesta'>";
				html += "				<u>";
				html += "					<a href='"+config['dominio']+"/"+config['idioma_url']+"/"+array_mapa_fie_nombre[i]+"' title='"+array_mapa_fie_nombre[i]+"'>";
				html += "						<strong><u>"+array_mapa_fie_nombre[i]+"</u></strong>";
				html += "					</a>";
				html += "				</u>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='n-fiesta'>";
				html += "				<strong>"+array_mapa_per_nombre[i]+" "+array_mapa_per_apellido[i]+"</strong>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='ubicacion'>"+array_mapa_provincia[i]+" "+array_mapa_pais[i]+"</span>";
				html += "			"+array_mapa_fecha[i]+"";
				html += "		</p>";
				html += "			<span class='iconos_face_twit'>";
				if(array_mapa_twitter[i]!=""){
					html += "			<a href='http://www.twitter.com/"+array_mapa_twitter[i]+"' target='blank'>";
					html += "				<img src='../images/twitter_chico.jpg' alt='twitter' style='margin:2px;' title='Ir a Twitter...'/>";
					html += "			</a>";
				}
				if(array_mapa_facebook[i]!=""){
					html += "			<a href='http://www.facebook.com/profile.php?id="+array_mapa_facebook[i]+"' target='blank'>";
					html += "				<img src='../images/facebook_chico.jpg' alt='facebook' style='margin:2px;' title='Ir al perf&iacute;l de Facebook...'/>";
					html += "			</a>";
				}
				html += "			</span>";
				html += "			<ul>";
				html += "				<li class='left'>Fans: "+array_mapa_fans[i]+"</li>";
				html += "				<li class='right'>Votos: "+array_mapa_votos[i]+"</li>";
				html += "			</ul>";
				html += "	</div>";
				var marker = createMarker_virtual(point,html);
			} else {
				html = "";
				html += "	<div class='modal-mapa'>";
				html += "		<span class='bar-virtual-red'></span>";
				html += "		<p class='txt-red'>";
				html += "			<img src='"+config['tod']+array_mapa_fie_imagen_perfil[i]+propiedades_imagen+"' alt='party'/>";
				html += "			<span class='n-fiesta-red'>";
				html += "				<u>";
				html += "					<strong><u>"+array_mapa_fie_nombre[i]+"</u></strong>";
				html += "				</u>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='n-fiesta'>";
				html += "				<strong>"+array_mapa_per_nombre[i]+" "+array_mapa_per_apellido[i]+"</strong>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='ubicacion'>"+array_mapa_provincia[i]+" "+array_mapa_pais[i]+"</span>";
				html += "			"+array_mapa_fecha[i]+"";
				html += "		</p>";
				html += "			<span class='iconos_face_twit'>";
				if(array_mapa_twitter[i]!=""){
					html += "		<a href='http://www.twitter.com/"+array_mapa_twitter[i]+"' target='blank'>";
					html += "			<img src='../images/twitter_chico.jpg' alt='twitter' style='margin:2px;' title='Ir a Twitter...'/>";
					html += "		</a>";
				}
				if(array_mapa_facebook[i]!=""){
					html += "		<a href='http://www.facebook.com/profile.php?id="+array_mapa_facebook[i]+"' target='blank'>";
					html += "			<img src='../images/facebook_chico.jpg' alt='facebook' style='margin:2px;' title='Ir al perf&iacute;l de Facebook...'/>";
					html += "		</a>";
				}
				html += "			</span>";
				html += "	</div>";
				var marker = createMarker_real(point,html);
			}	
			
			
			
			map.addOverlay(marker);
			
			if(i==n_random){
				marker.openInfoWindowHtml(html);
				map.setCenter(new GLatLng(array_mapa_map_lat[i], array_mapa_map_long[i]),5);
			}
			
		}
	}
	
}
// ---------------------------------------------------------------//
// BUSCO LAS FIESTAS RECIENTES AGREGADAS -------------------------//
function agregar_alertas() {
	var temp = "";
	var reg_ids = "";
	$("#btn_direccion_home").show();
	//OBTENEMOS LOS DATOS DE LAS FIESTAS
	$.ajax({
		async:false,
		url: get_fiestas_alertas,
		dataType: "xml",
		type: "GET",
		/*data: {pai_id:config['id_pais']}, */
		success:function envia_datos(xml){
			
			var contador = 0;
			
			$("fiesta",xml).each(function(j) {
										  
				if (contador < 3) {
					array_alert_fie_id[j] 			= $(this).attr('id');
					array_alert_fie_nombre[j] 		= $(this).find("nombre").text();
					array_alert_fie_imagen_perfil[j] = $(this).find("imagen_perfil").text();
					array_alert_map_long[j] 			= $(this).find("map_long").text();
					array_alert_map_lat[j] 			= $(this).find("map_lat").text();
					array_alert_fie_tipo[j] 			= $(this).find("tipo").text();
					if (reg_ids == "")
						reg_ids += $(this).attr("per_id");
					else
						reg_ids += ","+$(this).attr("per_id");
					contador = contador+1;
				}
			})			
		},
		error:function manejo_error(error){
			//mostrar_alerta("Error al cargar la informaci&oacute;n.",0);
		}
	});
	
	//OBTENEMOS LOS DATOS DE LAS PERSONAS
	//reg_ids = "1408863,1408748,1407035";
	//alert(reg_ids);
	if (reg_ids != ""){
		$.ajax({	
			async:false,
			url: config['controller_reg'],
			dataType: "xml",
			type: "POST",
			data: {action:"usuarios.get_usuarios",acoid:config['acoid'],regids:reg_ids},
			success:function envia_datos(xml){
				$('usuario',xml).each(function(j) {
					array_alert_per_nombre[j] 	= $(this).attr('nombre');
					array_alert_reg_id[j] 		= $(this).attr('regId');
					array_alert_provincia[j] 	= $(this).attr('estado');
				})
				
			},
			error:function manejo_error(error){
				//mostrar_alerta("Error al cargar la informaci&oacute;n.",0);
			}
		});
	}
	
	//ARMAMOS EL LISTADO DE ALERTAS
$("#listado_alertas").html("");
	
temp += "<ul>";
//PROPIEDADES DE LA FOTO
var propiedades_imagen="&w=20&h=20";

	for (var i=0; i < array_alert_fie_id.length; i++){
		
		if (array_alert_per_nombre[i] != undefined){
		
		temp += "<li><div style='float:left; padding-right:5px;'><img src='"+config['tod']+array_alert_fie_imagen_perfil[i]+propiedades_imagen+"' alt='party'/></div><div> "+array_alert_per_nombre[i]+" de "+array_alert_provincia[i]+" cre&oacute; la fiesta <a href='"+config['dominio']+"/"+config['idioma_url']+"/"+array_alert_fie_nombre[i]+"' title='"+array_alert_fie_nombre[i]+"'>\""+

array_alert_fie_nombre[i].substring(0,10)

+"...\"</a></div></li>";
		
		}
	}
	temp += "</ul>";
	$("#listado_alertas").html(temp);
}
// ---------------------------------------------------------------//
// RECARGO LAS FIESTAS AL MAPA -----------------------------------//
function recargar_fiestas_mapa() {	

	var n_random	= 0;
	
	$('fiestas',xml_fiestas_mapa).each(function(i) {
		var total_fiestas = $(this).attr('total');
		if (total_fiestas >= 20)
			total_fiestas = 20;
		n_random = Math.round(Math.random()*(total_fiestas - 1));
	})
	
	//CREO LOS PUNTOS EN EL MAPA
	var html = "";
	//PROPIEDADES DE LA FOTO
	var propiedades_imagen="&w=80&h=80";
	
	//Limpiamos el mapa
	if (map != undefined)
		map.clearOverlays(); 
	
	for (i=0; i < array_mapa_fie_id.length; i++){
		
		if (array_mapa_per_nombre[i] != undefined){
			
			var point = new GLatLng(array_mapa_map_lat[i], array_mapa_map_long[i]);
			if (array_mapa_fie_tipo[i] == 0){
				html = "";
				html += "	<div class='modal-mapa'>";
				html += "		<span class='bar-virtual'></span>";
				html += "		<p class='txt-blue'>";
				html += "			<img src='"+config['tod']+array_mapa_fie_imagen_perfil[i]+propiedades_imagen+"' alt='party'/>";
				html += "			<span class='n-fiesta'>";
				html += "				<u>";
				html += "					<a href='"+config['dominio']+"/"+config['idioma_url']+"/"+array_mapa_fie_nombre[i]+"' title='"+array_mapa_fie_nombre[i]+"'>";
				html += "						<strong>"+array_mapa_fie_nombre[i].substring(0,20)+"</strong>";
				html += "					</a>";
				html += "				</u>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='n-fiesta'>";
				html += "				<strong>"+array_mapa_per_nombre[i]+" "+array_mapa_per_apellido[i]+"</strong>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='ubicacion'>"+array_mapa_provincia[i]+" "+array_mapa_pais[i]+"</span>";
				html += "			"+array_mapa_fie_comentario[i]+"";
				html += "		</p>";
				html += "			<span class='iconos_face_twit'>";
				if(array_mapa_twitter[i]!=""){
					html += "		<a href='http://www.twitter.com/"+array_mapa_twitter[i]+"' target='blank'>";
					html += "			<img src='../images/twitter_chico.jpg' alt='twitter' style='margin:2px;' title='Ir a Twitter...'/>";
					html += "		</a>";
				}
				if(array_mapa_facebook[i]!=""){
					html += "		<a href='http://www.facebook.com/profile.php?id="+array_mapa_facebook[i]+"' target='blank'>";
					html += "			<img src='../images/facebook_chico.jpg' alt='facebook' style='margin:2px;' title='Ir al perf&iacute;l de Facebook...'/>";
					html += "		</a>";
				}
				html += "			</span>";
				html += "		<ul>";
				html += "			<li class='left'>Fans: "+array_mapa_fans[i]+"</li>";
				html += "			<li class='right'>Votos: "+array_mapa_votos[i]+"</li>";
				html += "		</ul>";
				html += "	</div>";
				var marker = createMarker_virtual(point,html);
			} else {
				html = "";
				html += "	<div class='modal-mapa'>";
				html += "		<span class='bar-virtual-red'></span>";
				html += "		<p class='txt-red'>";
				html += "			<img src='"+config['tod']+array_mapa_fie_imagen_perfil[i]+propiedades_imagen+"' alt='party'/>";
				html += "			<span class='n-fiesta-red'>";
				html += "				<u>";
				html += "					<strong>"+array_mapa_fie_nombre[i].substring(0,20)+"</strong>";
				html += "				</u>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='n-fiesta'>";
				html += "				<strong>"+array_mapa_per_nombre[i]+" "+array_mapa_per_apellido[i]+"</strong>";
				html += "			</span>";
				html += "			<br />";
				html += "			<span class='ubicacion'>"+array_mapa_provincia[i]+" "+array_mapa_pais[i]+"</span>";
				html += "			"+array_mapa_fie_comentario[i]+"";
				html += "		</p>";
				html += "			<span class='iconos_face_twit'>";
				if(array_mapa_twitter[i]!=""){
					html += "		<a href='http://www.twitter.com/"+array_mapa_twitter[i]+"' target='blank'>";
					html += "			<img src='../images/twitter_chico.jpg' alt='twitter' style='margin:2px;' title='Ir a Twitter...'/>";
					html += "		</a>";
				}
				if(array_mapa_facebook[i]!=""){
					html += "		<a href='http://www.facebook.com/profile.php?id="+array_mapa_facebook[i]+"' target='blank'>";
					html += "			<img src='../images/facebook_chico.jpg' alt='facebook' style='margin:2px;' title='Ir al perf&iacute;l de Facebook...'/>";
					html += "		</a>";
				}
				html += "			</span>";
				html += "	</div>";
				var marker = createMarker_real(point,html);
			}	
			
			map.addOverlay(marker);
			
			if(i==n_random){
				marker.openInfoWindowHtml(html);
				map.setCenter(new GLatLng(array_mapa_map_lat[i], array_mapa_map_long[i]),5);
			}
		}
	}
}
// ---------------------------------------------------------------//

//*** LISTADO DE VIDEOS ******************************************//
//--- CARGO EL SERVICIO ------------------------------------------//
function armar_lista_videos(usuario){
	if (usuario != ""){
		$.ajax({
			type: "post",
			async: false,
			url: get_fiestas_videos,
			data: {per_id:usuario},
			dataType: "xml",
			success: function(xml){
				$('stage',xml).each(function(i) {
					var code = $(this).attr("code");
					if(code!=0){
						alert("error");
					}
				})
				$('video',xml).each(function(i) {
					var id = $(this).attr("id");
					var url = $(this).find("url").text();
					var id_video = $(this).find("id_url").text();
					videos[i]=id_video;
				});
				video_cant=videos.length;
				init_video(videos[0]);
			}
		})
	} else {
		$.ajax({
			async: false,
			url: "/xml/playlist_defecto.xml",
			dataType: "xml",
			success: function(xml){
				$('stage',xml).each(function(j) {
					var code = $(this).attr("code");
					if(code!=0){
						alert("error");
					}
				});
				$('video',xml).each(function(i) {
					var id = $(this).attr("id");
					var url = $(this).find("url").text();
					var id_video = $(this).find("id_url").text();
					videos[i]=id_video;
					//alert(videos[i]);
				});
				video_cant=videos.length;
				init_video(videos[0]);
			},
			error: function(error){
				//alert(error);
			}
		})
	}
	
}
//---------------------------------//
//--- CONTROLES COMUNES -----------//
function play() {
  if (ytplayer) {
	ytplayer.playVideo();
  }
}

function pause() {
  if (ytplayer) {
	ytplayer.pauseVideo();
  }
}

function next() {
	if(video_count<video_cant-1){
		video_count=video_count+1;
		init_video(videos[video_count]);
	}else{
		video_count=0;
		init_video(videos[0]);
	}	
}

function back() {
	if((video_count)>0){
		video_count=video_count-1;
		init_video(videos[video_count]);
	}else{
		video_count=video_cant-1;
		init_video(videos[video_count]);
	}
}
//--- EVENTOS ---------------------//
//---------------------------------//

function onytplayerStateChange(newState) {
	//REGRESA EL ESTADO EN EL QUE SE ENCUENTRA EJ(CARGANDO, EMPEZÓ, TERMINÓ, PAUSÓ, ETC).
	//Los valores posibles son:
	////no iniciado (-1), 
	//finalizado (0), 
	//en reproducción (1), 
	//pausado (2), 
	//almacenamiento en búfer (3),
	//en cola de vídeos (5). 
	switch(newState){
		case -1:break;
		case 0:
			//CUANDO TERMINA EL VIDEO
			next();
		break;
		case 1:	break;
		case 2:	break;
		case 3:	break;
		case 4:	break;
		case 5:
			//CUANDO TERMINA DE CARGAR EL REPRODUCTOR LE DA PLAY
			//play();
		break;
	}
}
function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}
//---------------------------------//
//--- INICIALIZACION --------------//
function init_video(id_video){
	//inicializador: Crea el nuevo div donde se visualizara el video.
	$("#reproductor_videos").html("&nbsp;");
	$("#reproductor_videos").html("<div id='ytapiplayer'>You need Flash player 8+ and JavaScript enabled to view this video.</div>");
	var params = new Object();
	var atts = new Object();
	params = { allowScriptAccess: "always" };
	atts = { id: "myytplayer" };
	swfobject.embedSWF("http://www.youtube.com/v/"+id_video+"&enablejsapi=1&playerapiid=ytplayer","ytapiplayer", "180", "150", "8", null, null, params, atts);
}
//---------------------------------//
//*** LISTADO DE VIDEOS ******************************************//

// -----------------------------------------------------//
// CALCULA DIRECCION PARA MAPA -------------------------//
function calcula_direccion(address){

	$("#calle").val()
	$("#altura").focus(function(){this.value="";})
	$("#provincia").focus(function(){this.value="";})
	$("#pais").focus(function(){this.value="";})
	address = $("#calle").val()+' '+$("#altura").val()+', '+$("#provincia").val()+', '+$("#pais").val();
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					mostrar_alerta("La direcci&oacute;n '"+ address + "' no ha sido encotrada!",0);
				} else {
					map.setCenter(point, 13);
					/*var marker = new GMarker(point,{draggable:true,icon:iconoMarcaVirtual});
					map.addOverlay(marker);
					marker.openInfoWindowHtml(address);
					*/
				}
			}
		);
	}
}