//READY --------------------------------------------------------->
$().ready(function(){
	//$("a").click(function(){
		mostrar_tapa_inicial();	 
	//});
});
// -------------------------------------------------------------->

//MOSTRAR Y QUITAR TAPA DE FONDO -------------------------------->
function mostrar_tapa(ver_texto_cargando){
	
	/*var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
		
	$("#tapa_ventana").css("height",yScroll);	
	$("#tapa_ventana").css("width",xScroll);
	$("#tapa_ventana").show();
	*/
	if (ver_texto_cargando=="1"){
		$("#texto_tapa").css("display","inline");	
	} else {
		$("#texto_tapa").css("display","none");			
	}
	
	var win_h = $(document).height();
	var win_w = $(window).width();
	$("#tapa_ventana").css("height",win_h);	
	$("#tapa_ventana").css("width",win_w);
	$("#tapa_ventana").show();
}
function quitar_tapa(){
	$("#tapa_ventana").hide();
}

function mostrar_tapa_inicial(){
	/*var win_h = $(document).height();
	var win_w = $(window).width();
	$("#tapa_ventana_inicial").css("height",""+win_h+"");	
	$("#tapa_ventana_inicial").css("width",""+win_w+"");*/
	$("#tapa_ventana_inicial").show();
}

function quitar_tapa_inicial(){
	$("#tapa_ventana_inicial").hide();
}
// -------------------------------------------------------------->
//MOSTRAR Y QUITAR LOADING -------------------------------------->
function mostrar_cargador(){
	mostrar_tapa();
	//$("#loading").fadeIn("slow");
	$("#loading").show();
}

function ocultar_cargador(){
	//$("#loading").fadeOut("slow");
	$("#loading").hide();
	quitar_tapa();
}
// -------------------------------------------------------------->	
//MOSTRAR VENTANA DE ALERTA ------------------------------------->
function centrar_ventana(div){
	var win_h = $(document).height();
	var win_w = $(window).width();
	var frm_h = $(""+div+"").css("height");//var frm_h = document.getElementById('formularioReg').height;//
	var frm_w = $(""+div+"").css("width");
	frm_h = frm_h.split("px");
	frm_w = frm_w.split("px");
	//alert(frm_w[0]+"x"+frm_h[0]);
	
	var top = (parseInt(win_h)/2)-(parseInt(frm_h)/2);
	if (frm_w != "auto")
		var left = (parseInt(win_w)/2)-(parseInt(frm_w[0])/2);
	else
		var left = (parseInt(win_w)/2);
	if (top < 0)
		top = 0;
	//$(""+div+"").css("top",top);	
	if (left < 0)
		left = 0;
	$(""+div+"").css("left",left);	
}

function mostrar_alerta(detalle, sin_tapa){
	
	//Enviar el scroll hacia arriba
	scrollTo(0,0); 

	//centrar_ventana("#mensajesAlertas");
	$("#reproductor_videos").hide();
	if (sin_tapa == 0)
		mostrar_tapa();
	$("#comentario").html(detalle);
	//$("#alerta").fadeIn("slow");
	$("#mensajesAlertas").show();
	
}
function ocultar_alerta(sin_tapa){
	//$("#alerta").fadeOut("slow");
	$("#mensajesAlertas").hide();
	
	if ($("#formularioContactos").css("display") != "block"){
		quitar_tapa();
		$("#reproductor_videos").show();
	}
	
	if (glb_url == "" || glb_url == "undefined"){
		
	} else {
		document.location = glb_url;
		if (glb_url.indexOf("#") != -1)
			location.reload();
	}
}

function cerrar_ventana(){
	if ($("#formularioLogin").css("display") == "block"){
		$("#formularioLogin").hide();
		$("#formularioLogin").html("");
		if ($("#slider1").css("display") == "block")
			if(!history.back())document.location = config['dominio']+"/"+config['idioma_url'];
				//location.reload();
	}
	if ($("#formularioRegistro").css("display") == "block"){
		$("#formularioRegistro").hide();
		$("#formularioRegistro").html("");
	}
	if ($("#reproductor_videos").css("display") == "none")
		$("#reproductor_videos").show();
	quitar_tapa();
}

function cerrar_ventana_gral(contenedor){
	$(""+contenedor+"").hide();
	if ($("#reproductor_videos").css("display") == "none")
		$("#reproductor_videos").show();
	quitar_tapa();
}

function mostrar_confirmacion(detalle, sin_tapa){
	
	//Enviar el scroll hacia arriba
	scrollTo(0,0); 

	centrar_ventana("#confirmacion");
	
	if (sin_tapa == 0)
		mostrar_tapa();
	
	$("#confirmacion_mensaje").html(detalle);
	//$("#alerta").fadeIn("slow");
	$("#confirmacion").show();
	
}
function ocultar_confirmacion(sin_tapa){
	//$("#alerta").fadeOut("slow");
	$("#confirmacion").hide();
	if (sin_tapa == 0)
		quitar_tapa();
}

function obtener_nombre_pais(id_pais){
	var nombre_pais = "";
	switch (id_pais){
		case "1":
			nombre_pais = "Argentina";
			break;
		case "2":
			nombre_pais = "Brasil";
			break;
		case "3":
			nombre_pais = "Colombia";
			break;
		case "4":
			nombre_pais = "Mexico";
			break;
		case "5":
			nombre_pais = "Chile";
			break;
		case "6":
			nombre_pais = "Peru";
			break;
		case "7":
			nombre_pais = "Panama";
			break;
		case "8":
			nombre_pais = "Ecuador";
			break;
		case "9":
			nombre_pais = "Venezuela";
			break;
		case "31":
			nombre_pais = "Puerto Rico";
			break;
	}
	return nombre_pais;
}

function obtener_codigo_pais(id_pais){
	var codigo_pais = "";
	switch (id_pais){
		case "1":
			codigo_pais = "es-AR";
			break;
		case "2":
			codigo_pais = "pt-BR";
			break;
		case "3":
			codigo_pais = "es-CO";
			break;
		case "4":
			codigo_pais = "es-MX";
			break;
		case "5":
			codigo_pais = "es-CL";
			break;
		case "6":
			codigo_pais = "es-PE";
			break;
		case "7":
			codigo_pais = "es-PA";
			break;
		case "8":
			codigo_pais = "es-EC";
			break;
		case "9":
			codigo_pais = "es-VE";
			break;
		case "31":
			codigo_pais = "es-PR";
			break;
	}
	return codigo_pais;
}

function obtener_id_pais(codigo_pais){
	var id_pais = "";
	switch (codigo_pais){
		case "AR":
			id_pais = 1;
			break;
		case "BR":
			id_pais = 2;
			break;
		case "CO":
			id_pais = 3;
			break;
		case "MX":
			id_pais = 4;
			break;
		case "CL":
			id_pais = 5;
			break;
		case "PE":
			id_pais = 6;
			break;
		case "PA":
			id_pais = 7;
			break;
		case "EC":
			id_pais = 8;
			break;
		case "VE":
			id_pais = 9;
			break;
		case "PR":
			id_pais = 31;
			break;
	}
	return id_pais;
}

function obtener_coordenadas_gm(pais){
	var coordenadas_gm = "";
	switch (pais){
		case "AR":
			coordenadas_gm = "-35.376,-65.167";
			break;
		case "BR":
			coordenadas_gm = "-10.773,-53.089";
			break;
		case "CL":
			coordenadas_gm = "-37.852,-71.373";
			break;
		case "CO":
			coordenadas_gm = "3.901,-73.073";
			break;
		case "EC":
			coordenadas_gm = "-1.427,-78.767";
			break;
		case "MX":
			coordenadas_gm = "23.95,-102.533";
			break;
		case "PA":
			coordenadas_gm = "8.507,-80.102";
			break;
		case "PE":
			coordenadas_gm = "-9.163,-74.375";
			break;
		case "PR":
			coordenadas_gm = "18.221,-66.462";
			break;
		case "VE":
			coordenadas_gm = "7.122,-66.169";
			break;
	}
	return coordenadas_gm;
}
// -------------------------------------------------------------->
//OBTENER ID DEL USUARIO Y DEL PAIS DE LA FIESTA ---------------->
function cargar_session(data){
	var xml = data;
	var per_id = 0;
	var pai_id = "";
	var fie_id = "";
	var fie_tipo = "";
	//Obtengo los datos de la fiesta
	$(xml).find("fiesta").each(function(){
		fie_id=$(this).attr('id');
		per_id=$(this).attr('per_id');
		pai_id=$(this).find("id_pais").text();
		fie_tipo=$(this).find("tipo").text();
	});
	var lng_gral = obtener_codigo_pais(pai_id);
	if (fie_tipo == 0){
		$.ajax({
			async:false,
			type: "POST",
			dataType: "html",
			contentType: "application/x-www-form-urlencoded",
			url: "../script/site/app_codes/service/carga_session_fiesta.php",
			data: "per_id="+per_id+"&lng_gral="+lng_gral+"&fie_id="+fie_id,
			success:function (data){
				//alert("yuuuu");
			},
			error: function(xhr,msg,excep) {
				if(excep==undefined)excep="";
				error_ajax= msg+ " status " + xhr.status + ": <br>" + excep;
				alert(error_ajax);
			}
		});
	} else {
		document.location = "/"+config['idioma_url'];
	}
}
function obtener_usuario_pais_por_fiesta(fiesta){
	$.ajax({
		async:false,
		type: "POST",
		dataType: "xml",
		contentType: "application/x-www-form-urlencoded",
		url: "../script/site/app_codes/service/frontend/front_get_fiestas_fie_nombre.php",
		data: "fie_nombre="+fiesta,
		success:function (data){
			cargar_session(data);
		},
		error: function(xhr,msg,excep) {
			if(excep==undefined)excep="";
			error_ajax= msg+ " status " + xhr.status + ": <br>" + excep;
			alert(error_ajax);
		}
	}); 
}
// -------------------------------------------------------------->
//OBTENER IDIOMA-PAIS DE LA PREHOME ----------------------------->
function obtener_idioma_pais(lng_pais){
	//$("#lng_pais").val(lng_pais);
	var pais = lng_pais.split("-")
	$("#frm_lng_pais").attr("action",dominio+"/"+pais[1]);
	$("#frm_lng_pais").submit();
}
// -------------------------------------------------------------->
//OBTENER ID DE REGISTRACION ------------------------------------>
function get_registro(reg_id){
	//alert($.browser.msie+", "+$.browser.version);
	if ($.browser.msie && $.browser.version == "6.0"){
		$.ajax({
			async:false,
			type: "POST",
			dataType: "html",
			contentType: "application/x-www-form-urlencoded",
			url: "/script/site/app_codes/service/carga_session_regId.php",
			data: "reg_id="+reg_id+"&usr_nombre=&usr_apellido=&usr_mail=",
			success:function (data){
				if (glb_url == "")
					document.location = "/"+config['idioma_url'];
				else
					document.location = glb_url;
			},
			error: function(xhr,msg,excep) {
				mostrar_alerta("Error al loguearte",0);
			}
		})
	} else {
		$.ajax({	
			async:false,
			url: config['controller_reg'],
			dataType: "xml",
			type: "post",
			data: {action:"usuarios.get_usuarios",acoid:config['acoid'],regids:reg_id},
			success:function (xml){
				//alert(xml);
				$('usuario',xml).each(function(j) {
					var nombre_usuario 	 	= $(this).attr('nombre');
					var apellido_usuario 	= $(this).attr('apellido');
					var mail_usuario 		= $(this).attr('email');
					var id_pais 			= $(this).attr('idPais');
					var codigo_pais = obtener_codigo_pais(id_pais);
					if (codigo_pais != "")
						codigo_pais = codigo_pais.split("-");
					
					$.ajax({
						async:false,
						type: "POST",
						dataType: "html",
						contentType: "application/x-www-form-urlencoded",
						url: "/script/site/app_codes/service/carga_session_regId.php",
						data: "reg_id="+reg_id+"&usr_nombre="+nombre_usuario+"&usr_apellido="+apellido_usuario+"&usr_mail="+mail_usuario,
						success:function (data){
							if (codigo_pais[1] != config['idioma_url']){
								document.location = "/"+codigo_pais[1];
							} else {
								if (glb_url == "")
									document.location = "/"+config['idioma_url'];
								else
									document.location = glb_url;
							}
						},
						error: function(xhr,msg,excep) {
							mostrar_alerta("Error al loguearte",0);
						}
					})
				})
			},
			error:function (error){
				mostrar_alerta("Error al loguearte",0);
			}
		});
	}
}
// -------------------------------------------------------------->
//CHEQUEAR NOMBRE FIESTA SIN ESPACIOS NI CARACTERES RAROS ------->
function chequear_nombre_fiesta(fie_nombre){
	var RegExPattern = /^[a-zA-Z0-9]+$/;
	if (fie_nombre.match(RegExPattern) != null)
		return 0;
	else
		return 1;	
}
// -------------------------------------------------------------->
//CERRAR SESSION ------------------------------------------------>
function cerrar_sesion(){
	$.ajax({
		async:false,
		type: "GET",
		dataType: "html",
		contentType: "application/x-www-form-urlencoded",
		url: "/script/site/app_codes/service/cerrar_session.php",
		//data: "reg_id="+reg_id,
		success:function (data){
			document.location = "/"+config['idioma_url'];
		},
		error: function(xhr,msg,excep) {
			if(excep==undefined)excep="";
			error_ajax= msg+ " status " + xhr.status + ": <br>" + excep;
			mostrar_alerta("Error al cerrar cesion", 0);
		}
	}); 
}
// -------------------------------------------------------------->
//OBTENER MES EN NUMERO DE TEXTO EN INGLES ---------------------->
function obtener_mes_de_nombre(mes) {
	var mes_numero = ""
	switch(mes) {
		case 'Jan':
		  mes_numero = "01";
		  break;
		case 'Feb':
		  mes_numero = "02";
		  break;
		case 'Mar':
		  mes_numero = "03";
		  break;
		case 'Apr':
		  mes_numero = "04";
		  break;
		case 'May':
		  mes_numero = "05";
		  break;
	   	case 'Jun':
		  mes_numero = "06";
		  break;
	   	case 'Jul':
		  mes_numero = "07";
		  break;
	  	case 'Aug':
		  mes_numero = "08";
		  break;
	  	case 'Sep':
		  mes_numero = "09";
		  break;
	  	case 'Oct':
		  mes_numero = "10";
		  break;
	  	case 'Nov':
		  mes_numero = "11";
		  break;
	  	case 'Dec':
		  mes_numero = "12";
		  break;
	  }
  return mes_numero;
}
// -------------------------------------------------------------->
//FUNCINES COOKIES ---------------------------------------------->
function setCookie(name, value){
	 //If name is the empty string, it places a ; at the beginning
	 //of document.cookie, causing clearCookies() to malfunction.
	 var ahora=new Date();
	 var manyana=new Date(ahora.getTime()+1000*60*60*24) 
	 document.cookie = name + '=' + value + "; expires = "+manyana;
	// alert(name + '=' + value + "; expires = "+manyana);
}
function getCookie(name)
{
	 //Without this, it will return the first value 
	 //in document.cookie when name is the empty string.
	 if(name == '')
		return('');
	 
	 name_index = document.cookie.indexOf(name + '=');
	 
	 if(name_index == -1)
		return('');
	 
	 cookie_value =  document.cookie.substr(name_index + name.length + 1, 
											document.cookie.length);
	 
	 //All cookie name-value pairs end with a semi-colon, except the last one.
	 end_of_cookie = cookie_value.indexOf(';');
	 if(end_of_cookie != -1)
		cookie_value = cookie_value.substr(0, end_of_cookie);

	 //Restores all the blank spaces.
	 space = cookie_value.indexOf('+');
	 while(space != -1)
		  { 
		  cookie_value = cookie_value.substr(0, space) + ' ' + 
		  cookie_value.substr(space + 1, cookie_value.length);
						 
		  space = cookie_value.indexOf('+');
		  }

	 return(cookie_value);
}
function clearCookie(name)
 {                  
 expires = new Date();
 expires.setYear(expires.getYear() - 1);

 document.cookie = name + '=null' + '; expires=' + expires; 		 
 }       
function clearCookies()
 {
 Cookies = document.cookie;
 Cookie = Cookies;
 expires = new Date();
 expires.setYear(expires.getYear() - 1);

 while(Cookie.length > 0)
	  {
	  //All cookie name-value pairs end with a semi-colon, except the last one.
	  Cookie = Cookies.substr(0, Cookies.indexOf(';'));
	  Cookies = Cookies.substr(Cookies.indexOf(';') + 1, Cookies.length);

	  if(Cookie != '')
		 document.cookie = Cookie + '; expires=' + expires;
	  else
		 document.cookie = Cookies + '; expires=' + expires;			  			  	  
	  }		 		 
 }
// -------------------------------------------------------------->
//REGISTRAR USUARIO FACEBOOK ------------------------------------>
function registro_facebook(id_facebook){
	//alert(id_facebook);
	$.ajax({
		async:false,
		type: "POST",
		dataType: "xml",
		contentType: "application/x-www-form-urlencoded",
		url: config['controller']+"/front_set_id_facebook_perid.php",
		data: "per_id="+glb_user_id+"&id_facebook="+id_facebook,
		success:function (data){
		
		},
		error: function(xhr,msg,excep) {
			
		}
	}); 
}
// -------------------------------------------------------------->
