//Arreglar bug en contacto.js
var url_sitio = $('#titulo a').attr('href');
var ie = $.browser['msie'];
var webkit = $.browser['webkit'];
var body = $('body');
var sitio = $('#sitio');
var header = $('#header');
var titulo = $('#titulo');
var pantalla = $('#pantalla');
var footer = $('#footer');
var menu = $('#menu');
var secciones = menu.children();
var fondo = $('#fondo');
var perfiles = $('#perfiles');
var creditos = $('#creditos');
var mp3 = $('#mp3');
var timeoutFondo = '';
var fondoImg = '';

var lenguaje = $('#lenguaje').val();

if(!ie) fondoImg = fondo.children();

$(function(){
	if(ie)
		body.addClass('msie ie'+$.browser['version'].replace('.0',''));
		
	else if(webkit) body.addClass('webkit');
	//Acomodando todo lo necesario para la correcta aplicación de jQuery
	if(ie) fondoImg = fondo.children();
	var srcImg = fondoImg.attr('src');
	
	fondoImg.remove();
	fondoImg = $('<img/>',{ src:srcImg
				 			,load:function(){
								if(!ie) iniciar();
							}
						});
	fondo.append(fondoImg);
	if(ie){
		iniciar();
	}
});

function iniciar(){
	body.show();
	sitio.show();
	fxFondo();
	//fondoImg.show(); header.show(); footer.show(); footer.find('.inf').show(); pantalla.show(); seleccionarSeccion($('.seccion-sel')); //Sin animacion
	fondoImg.hide().fadeIn(3000,function(){ fxSitio(); }); //Con animacion	
}

function fxFondo(){
	clearTimeout(timeoutFondo);
	var ventana = $(window);
	if(ventana.width() > ventana.height()){
		if(ie) fondoImg.height(fondoImg.height()*ventana.width()/fondoImg.width());
		fondoImg.width(ventana.width());
		var top = -(fondoImg.height() - ventana.height())*.5;
		var left = -(fondoImg.width() - ventana.width())*.5;
		fondo.height(ventana.height());
		fondoImg.css({ marginTop:top, marginLeft:left });
	}
	
	timeoutFondo = setTimeout(function(){ fondoImg.width(ventana.width()); },100);
}

function fxSitio(){
	var altura = footer.height();
	var estirar = 10;
	header.fadeIn(1000);
	footer.css({ height:0 })
		  .animate({ height:altura+estirar },1500)
		  .animate({ height:'-='+estirar },500,function(){
				footer.find('.inf').fadeIn(1000);
				pantalla.fadeIn(500,function(){
					seleccionarSeccion($('.seccion-sel'));
				});
		  });
}

function fxVentana(cual,efectos,tiempo,funcion){
	if(typeof(tiempo) == 'undefined') tiempo = 1000;
	if(cual && efectos)
		cual.animate(efectos,tiempo,funcion);
	/*if(typeof(efectos) == 'undefined') efectos = new Object();
	var ventana = $(window);
	
	var arr = pantalla.height() - footer.height() -  cual.height();
	
	arr = arr*.5 ;
	efectos.top = arr;*/
	return true
}

function seleccionarSeccion(cual){
	var seccion = cual.children().attr('href');
	var sel = menu.find('.seccion-sel');
	sel.removeClass('seccion-sel').stop().fadeTo(500,.5);
	cual.addClass('seccion-sel');
	cual.stop().fadeTo(500,1);
	if(!$('[id^=sec-'+seccion+']').length){
		var dir = 'spip.php?lang='+lenguaje+'&page=sec-'+seccion;
		$.post(dir,function(data){
			pantalla.append(data);
			accionSeccion(seccion);
		});
	} else
		accionSeccion(seccion);
}

function accionSeccion(seccion){
	//UNIFICAR FUNCION DE VALIDACION (asi no se requiere integrar un js //Crear funcion en la que se valide leyendo el dom del form
	var funcion = '';
	
	if(seccion == 'inicio')
		funcion = function(){ if(ie) $('#ultimo-video').slideDown(500); }
	else if(seccion == 'multimedia'){
		funcion = function(){
			var img_req = $('#imagen'+$('#request_imagen').val()+' .gbtn','#sec-multimedia #imagenes');
			var vid_req = $('#video'+$('#request_video').val()+' .gbtn','#sec-multimedia #videos');
			if(img_req.length) img_req.click();
			else if(vid_req.length) vid_req.click();
		}
	} else if(seccion == 'libro-de-visitas')
		funcLibroDeVisitas('#sec-libro-de-visitas');
	else if(seccion == 'contacto')
		funcContacto('#sec-contacto');
	
	$('.ventana').stop().animate({ opacity:0, top:'+=20' },500,function(){
		$('.ventana').hide();
		$('[id^=sec-'+seccion+']').show();
		fxVentana($('[id^=sec-'+seccion+']'),{ opacity:1, top:0 },1000,funcion);
	});	
}

fondo.click(function(){
	return false;
});

secciones.hover(function(){
	if(!$(this).hasClass('seccion-sel')) $(this).css({ opacity:.5 }).stop().fadeTo(500,1);									  
},function(){
	if(!$(this).hasClass('seccion-sel'))
		$(this).stop().fadeTo(500,.5);
});

secciones.click(function(){
	if(!$(this).hasClass('seccion-sel')) seleccionarSeccion($(this));
	return false;
});

perfiles.children().hover(function(){
	$(this).find('img').hide().stop().fadeTo(500,1);									
},function(){
	$(this).find('img').stop().fadeTo(500,0);									
});

$(window).resize(function(){
	fxFondo();
	fxVentana(pantalla.children(':visible'));
});