//var host = document.location.protocol + '//' + document.location.hostName;
var host = document.location.protocol + '//www.boarders.com.ar';
var path_images_secciones = './images/secciones/';
var img_seccion = {
	'home': path_images_secciones.concat('seccion_home.jpg'), 
	'novedades': path_images_secciones.concat('seccion_novedades.jpg'),
	'notas': path_images_secciones.concat('seccion_notas.jpg'),
	'eventos': path_images_secciones.concat('seccion_eventos.jpg'),
	'montanias': path_images_secciones.concat('seccion_montanias.jpg'),
	'riders': path_images_secciones.concat('seccion_riders.jpg'),
	'fotos': path_images_secciones.concat('seccion_fotos.jpg'),
	'videos': path_images_secciones.concat('seccion_videos.jpg'),
	'escuelas': path_images_secciones.concat('seccion_escuelas.jpg'), 
	'links': path_images_secciones.concat('seccion_links.jpg'),
	'contacto': path_images_secciones.concat('seccion_contacto.jpg'),
	'reporter': path_images_secciones.concat('seccion_reporter.jpg'),
	'pistas': path_images_secciones.concat('seccion_pistas.jpg'),
	'garage': path_images_secciones.concat('seccion_garage.jpg'),
	'hospedajes': path_images_secciones.concat('seccion_hospedajes.jpg'),
	'viajes': path_images_secciones.concat('seccion_viajes.jpg'),
	'snowparks': path_images_secciones.concat('seccion_snowparks.jpg')
};
var default_seccion = 'novedades';

$(document).ready(function(){
	$("#home").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		procesarContenido('novedades');
	});
	$("#notas").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		procesarContenido('notas');
	});
	$("#eventos").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		procesarContenido('eventos');
	});
	$("#montanias").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		procesarContenido('montanias');
	});
	$("#riders").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		procesarContenido('riders');
	});	
	$("#fotosMenu").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		procesarContenido('fotos');
	});
	$("#videosMenu").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		procesarContenido('videos');
	});
	$("#escuelas").click(function(){
		desactivarLinks();
		activarLink($(this).attr("id"));
		$("#titulo_contenido").html('ESCUELAS');
		$("#contenido").load('./escuelas/escuelas.html');
		$("#texto_central").load('./texto_secciones/texto_escuelas.html');
		mostrarImagenCentral(img_seccion['escuelas']);
	});
	$("#quienes_somos").click(function(){
		procesarQuienesSomos();
	});
	
	$("#fotos").click(function(){
		desactivarLinks();
		procesarContenido('fotos');
	});
	$("#videos").click(function(){
		desactivarLinks();
		procesarContenido('videos');
	});
	$("#links").click(function(){
		desactivarLinks();
		$("#titulo_contenido").html('LINKS');
		$("#contenido").load('./links.html');
		$("#texto_central").load('./texto_secciones/texto_links.html');
		mostrarImagenCentral(img_seccion['links']);
	});
	$("#contacto").click(function(){
		desactivarLinks();
		procesarContacto();
	});
	/*
	$("#suscripcion").click(function(){
		procesarContenido('suscripcion');
	});
	*/
	$("#enviar_suscripcion").click(function(){
		procesarSuscripcion();
	});
		
	$("#reporter").click(function(){
		desactivarLinks();
		procesarContenido('reporter');
	});
	$("#pistas").click(function(){
		desactivarLinks();
		procesarContenido('pistas');
	});
	$("#garage").click(function(){
		desactivarLinks();
		procesarContenido('garage');
	});
	$("#hospedajes").click(function(){
		desactivarLinks();
		$("#titulo_contenido").html('HOSPEDAJES');
		$("#contenido").load('./hospedajes/hospedajes.html');
		$("#texto_central").load('./texto_secciones/texto_hospedajes.html');
		mostrarImagenCentral(img_seccion['hospedajes']);
	});
	$("#viajes").click(function(){
		desactivarLinks();
		procesarContenido('viajes');
	});
	$("#snowparks").click(function(){
		desactivarLinks();
		procesarContenido('snowparks');
	});
	
	mostrarFechaHora();
	ajustarPublicidad();
	ajustarTextoContenedorImagen();
	
	var seccion_inicial = recuperarSeccionSolicitada();
	
	switch( seccion_inicial )
	{
		case 'escuelas':
			$("#texto_central").load('./texto_secciones/texto_escuelas.html');
			$("#titulo_contenido").html('ESCUELAS');
			$("#contenido").load('./escuelas/escuelas.html');
			mostrarImagenCentral(img_seccion['escuelas']);
			break;
		case 'links':
			$("#titulo_contenido").html('LINKS');
			$("#contenido").load('./links.html');
			$("#texto_central").load('./texto_secciones/texto_links.html');
			mostrarImagenCentral(img_seccion['links']);
			break;
		case 'contacto':
			procesarContacto();
			break;
		case 'hospedajes':
			$("#titulo_contenido").html('HOSPEDAJES');
			$("#contenido").load('./hospedajes/hospedajes.html');
			$("#texto_central").load('./texto_secciones/texto_hospedajes.html');
			mostrarImagenCentral(img_seccion['hospedajes']);
			break;
		default:
			procesarContenido(seccion_inicial);
			break;
	}	
});

$(window).resize(function(){
	ajustarPublicidad();
});

function recuperarSeccionSolicitada()
{
	var seccion_solicitada = '';

	if( 0 < document.location.search.length && document.location.search.indexOf('=') )
	{
		var consulta = document.location.search.substring(document.location.search.indexOf('?') + 1);
		consulta = consulta.toLowerCase();
		
		var campos = consulta.split('=');
		
		if( 2 == campos.length && esSeccionValida(campos[1]))
		{
			seccion_solicitada = campos[1];
		}
	}
	
	if( 0 == seccion_solicitada.length )
		seccion_solicitada = default_seccion;
		
	return seccion_solicitada;
}

function esSeccionValida(seccion)
{
	var valorRetorno = false;
	
	switch( seccion )
	{
		case 'novedades':
		case 'notas':
		case 'eventos':
		case 'montanias':
		case 'riders':
		case 'fotos':
		case 'videos':
		case 'escuelas':
		case 'reporter':
		case 'pistas':
		case 'viajes':
		case 'snowparks':
		case 'links':
		case 'contacto':
		case 'garage':
		case 'hospedajes':
			valorRetorno = true;
			break;
		default:
			break;
	}
	
	return valorRetorno;	
}


function mostrarFechaHora() 
{
	window.setTimeout( mostrarFechaHora, 1000 );
	t = new Date();
	$("#reloj").html(t.toLocaleString());
}

function ajustarTextoContenedorImagen()
{
	var ancho_texto = 0;
	ancho_texto = $("div#imageContent").width() - $("div#imagen_central").width() - 10;
	$("div#texto_central").css({width: ancho_texto});
}

function ajustarPublicidad()
{
	var ancho = 0;
	var ancho_contenedor = 0;

	if( $.browser.msie )
		ancho_contenedor = $("div#content").width()-1;
	else if( $.browser.mozilla)
		ancho_contenedor = $("div#content").width()-342;
	else if( $.browser.opera )
		ancho_contenedor = $("div#content").width()-342;
	else if( $.browser.safari )
		ancho_contenedor = $("div#content").width()-342;
	else
		ancho_contenedor = $("div#content").width()-342;
	
	ancho = ancho_contenedor/2;
	
	$.each($(".publicidad_central"), function() {
		$(this).attr({width: ancho, border: "1px solid blue"});
	});
}

function mostrarImagenCentral(link)
{
	$("#imagen_central img").fadeOut("slow", function(){$("#imagen_central img").attr({"src": link})});
	$("#imagen_central img").fadeIn("slow");
}

function procesarSuscripcion()
{
	var nombre = $("#txt_nombre").val();
	var mail = $("#txt_mail").val();
	var existe_error_nombre = false;
	var existe_error_mail = false;
	var pagina_mensaje = 'suscripcion_ok.html';
	
	if( "" == nombre || 0 == nombre.length )
		existe_error_nombre = true;
		
	if( !esMailValido(mail) )
		existe_error_mail = true;
		
	if( existe_error_nombre && existe_error_mail )
		pagina_mensaje = 'suscripcion_error.html';
	else if( existe_error_nombre )
		pagina_mensaje = 'suscripcion_error_nombre.html';
	else if( existe_error_mail )
		pagina_mensaje = 'suscripcion_error_mail.html';

	if( existe_error_nombre || existe_error_mail )
		JT_show('./suscripcion/' + pagina_mensaje, 'titulo_suscripcion' , 'Atención!');
	else
		enviarFormularioSuscripcion(nombre, mail);
}

function esMailValido(valor)
{
	var reg = /(^[a-zA-Z0-9._-]{1,30})@([a-zA-Z0-9.-]{1,30}$)/;
	if( reg.test(valor) ) 
		return true;
	else 
		return false;
}

function enviarFormularioSuscripcion(nombre, email)
{
	$.ajax({
		type: "post", 
		url: host + "/procesar_suscripcion.php",
		data: "nombre="+nombre+"&email="+email,
		beforeSend: function(){
			;
		},
		success: function(msg){
			JT_show('./suscripcion/suscripcion_ok.html', 'titulo_suscripcion' , 'Suscripcion');
		},
		error: function(msg){
			JT_show('./suscripcion/suscripcion_error_fatal.html', 'titulo_suscripcion' , 'Atención!');
		},
		complete: function(){
			;
		}
	});
}

function limpiarFormularioSuscripcion()
{
	$("#txt_nombre").val('');
	$("#txt_mail").val('');
}

function procesarContenido(seccion)
{
	$.ajax({
		type: "post", 
		url: host + "/contenido.php",
		data: "accion=" + seccion,
		beforeSend: function(){
			$("#contenido").css({"background-color": "#cdcdcd"});
			$("#contenido").css({"background": "url(./images/fondo_contenido_small.jpg) repeat center center"});
			$("#contenido").html("<center><img src=\"./images/loading_nieve.gif\"></center>");
		},
		success: function(msg){
				mostrarImagenCentral(img_seccion[seccion]);
				$("#contenido").html(msg);
				if( seccion.toUpperCase() == 'MONTANIAS' )
				{
					$("#titulo_contenido").html('MONTA&Ntilde;AS');
					$("#texto_central").load('./texto_secciones/texto_montanias.html');
				}
				else
				{
					$("#titulo_contenido").html(seccion.toUpperCase());
					$("#texto_central").load('./texto_secciones/texto_' + seccion + '.html');
				}
		},
		error: function(msg){
			$("#contenido").html("Ha ocurrido un error");
			$("#contenido").html(msg);
		},
		complete: function(){
			$("#imageContent").css({"text-align": "center"});
			$("#contenido").css({"background": "none"});
			$("#contenido").css({"background-color": "#cdcdcd"});
			$("#contenido").css({"background": "url(./images/fondo_contenido_small.jpg) repeat center center"});
		}
	});
}

function procesarContacto()
{
	$.ajax({
		type: "post", 
		url: host + "/contacto.html",
		data: "", //"accion=" + seccion,
		beforeSend: function(){
			$("#contenido").css({"background-color": "#cdcdcd"});
			$("#contenido").css({"background": "url(./images/fondo_contenido_small.jpg) repeat center center"});
			$("#contenido").html("<center><img src=\"./images/loading_nieve.gif\"></center>");
		},
		success: function(msg){
			mostrarImagenCentral(img_seccion['contacto']);
			$("#texto_central").load('./texto_secciones/texto_contacto.html');
			$("#titulo_contenido").html('CONTACTO');
			$("#contenido").html(msg);
		},
		error: function(msg){
			$("#contenido").html("Ha ocurrido un error");
			$("#contenido").html(msg);
		},
		complete: function(){
			$("#contenido").css({"background": "none"});
			$("#contenido").css({"background-color": "#cdcdcd"});
			$("#contenido").css({"background": "url(./images/fondo_contenido_small.jpg) repeat center center"});
		}
	});
}

function activarLink(id)
{
	identificador = '#' + id;
	$(identificador).css({color: "rgb(255,0,255)"});
}

function desactivarLinks()
{
	$("#globalNav a").css({"color": "#fcd04e"});
}

function procesarQuienesSomos()
{
	Shadowbox.open({type: 'iframe', content: './quienes_somos.html', height: '400', width: '420', title: '&iquest;Quienes Somos?'});
}
