// When the DOM is loaded, initilize initPage
DOMAssistant.DOMReady(initPage);
var pulsadas = 0;
var infoX = 0;
var infoY = 0;

jQuery.preloadImages("/img/mapa/mun/b_mun.gif",
					"/img/mapa/mun/t_mun_norteamerica.gif",
					"/img/mapa/mun/t_mun_centroamerica.gif",
					"/img/mapa/mun/t_mun_sudamerica.gif",
					"/img/mapa/mun/t_mun_europa.gif",
					"/img/mapa/mun/t_mun_africa.gif",
					"/img/mapa/mun/t_mun_asia.gif",
					"/img/mapa/mun/t_mun_oceania.gif",
					"/img/mapa/nor/b_nor.gif",
					"/img/mapa/cen/b_cen.gif",
					"/img/mapa/sud/b_sud.gif",
					"/img/mapa/eur/b_eur.gif",
					"/img/mapa/afr/b_afr.gif",
					"/img/mapa/asi/b_asi.gif",
					"/img/mapa/oce/b_oce.gif");
					
function initPage() {
	pulsadas++;
	// Get a reference to the containing div with id='map'
 	var map = $$('map');
	inicializarLateral ();
	// Check if map exists
	if (map) {
		// Get all the areas of the imagemap
		var country_list = map.getElementsByTagName('AREA');
		
		var leftOffsetInicial = 0;
		
		// Loop through all areas
		for(var i=0; i<country_list.length; i++){

			$(country_list[i]).style.display = 'none';

			// Assignin an action to the mouseover event
			$(country_list[i]).addEvent('mouseover', function(e) {
				// Get the id from the hovered area
				var country_id = this.id;
				var num = jQuery (this).attr ("name");
				
				if (navigator.appName.indexOf("Explorer") != -1) {
					//Detectado explorer
					var nombre =  this.alt;
					var id_continente = this.title;
				} else {
					var array_nombre = this.alt.split ("@");
					var nombre =  array_nombre [0];
					var id_continente = array_nombre [1];				
				}

				// Extract the "country"-part of the id = the id of the list-item
				country_id = country_id.substring(country_id.indexOf('_')+1, country_id.length);
				// Set the li to "display: inline" = show it
				$$(country_id).setStyle('display', 'inline');
			
				$$('infoTitulo').innerHTML = nombre;
				$$('infoContenido').innerHTML = "Videos - "+num;
				$$('info').setStyle('display', 'inline');

				if (leftOffsetInicial == 0)	
					leftOffsetInicial = jQuery('#info').offset().left;

				jQuery('#info').css('left', ((jQuery('#wrapperMapa').offset().left) + (jQuery('#wrapperMapa').width() / 2) - (jQuery('#info').width() / 2) + infoX) + "px");
			});
			
			// Assign an action to the mouseout event
			$(country_list[i]).addEvent('mouseout', function(e) {
				
				// Get the id from the hovered area
				var country_id = this.id;
				
				// Extracti the "country"-part of the id = the id of the list-item
				country_id = country_id.substring(country_id.indexOf('_')+1, country_id.length);
				
				// Set the li to "display: none" = hide it
				$$(country_id).setStyle('display', 'none');
				$$('info').setStyle('display', 'none');

			});
			
			$(country_list[i]).addEvent('click',mapa);
		}
	}
}

function mapa (e) {

				$$('info').setStyle('display', 'none');
				// Get the id from the hovered area
				var country_id = this.id;
				var num = jQuery (this).attr ("name");
				
				if (navigator.appName.indexOf("Explorer") != -1) {
					//Detectado explorer
					var nombre =  this.alt;
					var id_continente = this.title;
				} else {
					var array_nombre = this.alt.split ("@");
					var nombre =  array_nombre [0];
					var id_continente = array_nombre [1];				
				}

				var cont_id= jQuery("#cont_id").attr ("value");
				if (!cont_id) jQuery("#cont_id").attr ("value",id_continente);
				
				// Extract the "country"-part of the id = the id of the list-item
				country_id = country_id.substring(country_id.indexOf('_')+1, country_id.length);

				if(/^([0-9])*$/.test(country_id.substring(country_id.length-1, country_id.length))) {
					country_id = country_id.substring(country_id+1, country_id.length-1);
				}

				if (pulsadas==2)
					if (idiomaActual=="es")
						document.location.replace ("/es/lista-videos/pais/"+id_continente+"/tipo/"+tipo_destino);
					else
						document.location.replace ("/en/video-list/country/"+id_continente+"/type/"+tipo_destino);

				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="+0+"&tipo_destino="+tipo_destino+"&continente_id="+id_continente,
					success:actualizar_pie,
					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_menu&tipo_destino="+tipo_destino+"&continente_id="+id_continente,
					success:recargar_menu,
					timeout: 40000,
					error: problemas
				});

				DOMAssistant.AJAX.post("/continente.php?continente=" + country_id + "&tipo_destino=" + tipo_destino,
					function(data){
						jQuery("#textoDondeInfo").hide();
						var count_pre = country_id.substring(0, 3);

						$$('map').innerHTML = data;

						switch(count_pre) {
							case 'nor':
								infoX = 230;
								$$('info').setStyle('top', '610px');
							break;
							case 'cen':
								infoX = -370;
								$$('info').setStyle('top', '530px');
							break;
							case 'sud':
								infoX = -200;
								$$('info').setStyle('top', '440px');
							break;
							case 'eur':
								infoX = -250;
								$$('info').setStyle('top', '550px');
							break;
							case 'afr':
								infoX = -190;
								$$('info').setStyle('top', '585px');
							break;
							case 'asi':
								infoX = 225;
								$$('info').setStyle('top', '585px');
							break;
							case 'oce':
								infoX = -210;
								$$('info').setStyle('top', '610px');
							break;
						}

						initPage();
						
						var scripts = document.getElementById('map').getElementsByTagName('script');   
						for (var i = 0; i < scripts.length; i++) {   
							eval(scripts[i].text);   
						}
						
						jQuery("#textoDondeInfo").html(trad_continente);
						jQuery("#textoDondeInfo").show();
					});

				DOMAssistant.preventDefault(e);

}

function inicializarLateral (){

	jQuery("a[id^='listado&']").click (menu_mapa);

}

function recargar_menu (datos){

	jQuery("#menu_categ_ul").html(datos);

}

function menu_mapa (e){

				jQuery("#dondeInfo").css ("top","-570px");

				$$('info').setStyle('display', 'none');
				// Get the id from the hovered area
				var array_id = this.id.split ("&");
				var country_id = array_id[1];

				var array_nombre = this.name.split ("@");
				var nombre =  array_nombre [0];
				var num = jQuery (this).attr ("name");
				var id_continente = array_nombre [1];

				var cont_id= jQuery("#cont_id").attr ("value");
				if (!cont_id) jQuery("#cont_id").attr ("value",id_continente);
				
				// Extract the "country"-part of the id = the id of the list-item
				country_id = country_id.substring(country_id.indexOf('_')+1, country_id.length);

				if(/^([0-9])*$/.test(country_id.substring(country_id.length-1, country_id.length))) {
					country_id = country_id.substring(country_id+1, country_id.length-1);
				}
				
				if (pulsadas==2)
					document.location.replace ("/listado_videos.php?paises_id="+id_continente+"&tipo_destino="+tipo_destino);

				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="+0+"&tipo_destino="+tipo_destino+"&continente_id="+id_continente,
					success:actualizar_pie,
					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_menu&tipo_destino="+tipo_destino+"&continente_id="+id_continente,
					success:recargar_menu,
					timeout: 40000,
					error: problemas
				});

				DOMAssistant.AJAX.post("/continente.php?continente=" + country_id+"&tipo_destino="+tipo_destino,
					function(data){
						jQuery("#textoDondeInfo").hide();
						$$('map').innerHTML = data;
						var count_pre = country_id.substring(0, 3);
						
						switch(count_pre) {
							case 'nor':
								infoX = 230;
								$$('info').setStyle('top', '610px');
							break;
							case 'cen':
								infoX = -370;
								$$('info').setStyle('top', '530px');
							break;
							case 'sud':
								infoX = -200;
								$$('info').setStyle('top', '440px');
							break;
							case 'eur':
								infoX = -250;
								$$('info').setStyle('top', '550px');
							break;
							case 'afr':
								infoX = -190;
								$$('info').setStyle('top', '585px');
							break;
							case 'asi':
								infoX = 225;
								$$('info').setStyle('top', '585px');
							break;
							case 'oce':
								infoX = -210;
								$$('info').setStyle('top', '610px');
							break;
						}
						initPage();
						jQuery("#textoDondeInfo").html(trad_continente);
						jQuery("#textoDondeInfo").show();
					});

				DOMAssistant.preventDefault(e);
}

