function isNumeric(sText) {
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}

	return IsNumber;
}

function cambio_idioma() {
	var cadena = jQuery(this).attr ("id");
	var vector = cadena.split ("_");
	var idioma = vector[1];
	var tokens = location.href.split("/");

	if (((tokens[4] == "magazin") || (tokens[4] == "magazine"))
		&& (tokens[5]!=null)
		&& (!isNumeric(tokens[5]))) {
		
		jQuery.ajax ({
			async:true,
			type: "POST",
			dataType: "html",
			contentType: "application/x-www-form-urlencoded;charset=iso-8859-15",
			url: "/funciones/eventos.php",
			data: "opcion=traduce_titulo&titulo="+tokens[5]+"&idioma="+idioma+"&tipo="+tokens[4],
			success: function(data) {
				tokens[5] = data;
			},
			timeout: 40000,
			error: problemas
		});
	}

	jQuery.ajax ({
		async:true,
		type: "POST",
		dataType: "html",
		contentType: "application/x-www-form-urlencoded;charset=iso-8859-15",
		url:"/funciones/eventos.php",
		data:"opcion=cambiar_idioma&idioma="+idioma,
		success: function() {

			var traduccionUrl = "";

			if (tokens[3] != 'undefined') {
				if (idioma != "es") {
					tokens[3] = "en";
					if (tokens[4] != 'undefined') {
						switch (tokens[4]) {
							case "noticias": tokens[4] = "news"; break;
							case "videos-del-mundo": tokens[4] = "videos-of-the-world"; break;
							case "magazin": tokens[4] = "magazine"; break;
							case "eventos": tokens[4] = "events"; break;
							case "oficinas-de-turismo": tokens[4] = "tourism-board"; break;
							case "suscripcion": tokens[4] = "subscription"; break;
							case "contacto": tokens[4] = "contact"; break;
							case "lista-videos": tokens[4] = "video-list"; break;
							case "mapa": tokens[4] = "map"; break;
						}
						
						switch (tokens[5]) {
							case "continente": tokens[5] = "continent"; break;
							case "pais": tokens[5] = "country"; break;
							case "categoria": tokens[5] = "category"; break;
							case "destinos": tokens[5] = "destinations"; break;
							case "hoteles": tokens[5] = "hotels"; break;
							case "viajes": tokens[5] = "travels"; break;
						}
						
						if (tokens[7] == "tipo") tokens[7] = "type";
					}
				} else {
					tokens[3] = "es";
					if (tokens[4] != 'undefined') {
					
						switch (tokens[4]) {
							case "news": tokens[4] = "noticias"; break;
							case "videos-of-the-world": tokens[4] = "videos-del-mundo"; break;
							case "magazine": tokens[4] = "magazin"; break;
							case "events": tokens[4] = "eventos"; break;
							case "tourism-board": tokens[4] = "oficinas-de-turismo"; break;
							case "subscription": tokens[4] = "suscripcion"; break;
							case "contact": tokens[4] = "contacto"; break;
							case "video-list": tokens[4] = "lista-videos"; break;
							case "map": tokens[4] = "mapa"; break;
						}

						switch (tokens[5]) {
							case "continent": tokens[5] = "continente"; break;
							case "country": tokens[5] = "pais"; break;
							case "category": tokens[5] = "categoria"; break;
							case "destinations": tokens[5] = "destinos"; break;
							case "hotels": tokens[5] = "hoteles"; break;
							case "travels": tokens[5] = "viajes"; break;
						}
						
						if (tokens[7] == "type") tokens[7] = "tipo";
					}
				}
			}

			for (var i=0; i<tokens.length; i++) {
				traduccionUrl += tokens[i];
				if (tokens.length > (i+1)) traduccionUrl += "/";
			}

			location.replace(traduccionUrl);
		},
		timeout: 40000,
		error: problemas
	});

}

function problemas (){
	alert ("Error con el ajax");
}

var x= jQuery(document);

x.ready (inicializarEventos);

function inicializarEventos (){

		jQuery("img[id^='idioma_']").click (cambio_idioma).mouseover (function (){jQuery(this).css ("cursor","pointer");});	
}

function cambiar_magazine (id_magazine){
	
	jQuery.ajax ({
			async:true,
			type: "POST",
			dataType: "html",
			contentType: "application/x-www-form-urlencoded;charset=iso-8859-15",
			url:"/magazine_detalles.php",
			data:"&id_magazine="+id_magazine,
			success:actualizarMagazine,
			timeout: 40000,
			error: problemas
		});

}

function actualizarMagazine (datos){
	
	jQuery("#magazine_detalles").html (datos);
	
}

function recargar_pie (pag,tipo_destino,continente_id){
	
	var cad="";

	if(tipo_destino) cad+="&tipo_destino="+tipo_destino;
	if (continente_id) cad+="&continente_id="+continente_id;

	jQuery("#pie_videos_destacados").html('<div align="center"><img src="/img/loading.gif"/></div>');

	jQuery.ajax ({
			async:true,
			type: "POST",
			dataType: "html",
			contentType: "application/x-www-form-urlencoded;charset=iso-8859-15",
			url:"/pie_videos.php",
			data:"&pag_video="+pag+cad,
			success:actualizar_pie,
			timeout: 40000,
			error: problemas
		});
}

function actualizar_pie (datos){
	jQuery("#pie_videos_destacados").html(datos);
}

jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

jQuery.preloadImages("/img/pro_six_0a.gif", "/img/pro_six_0b.gif", "/img/pro_six_1a.gif", "/img/pro_six_1b.gif", "/img/pro_six_2a.gif", "/img/pro_six_2b.gif");
