/*
Funcionalidad principal Javascript

18/09/2011 0:12:53
// áéúíó
*/



(function($){

var timeoutID;
var flag_events_scroll_for_menu=true;

// Definición del comportamiento del rebote
$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.7; // Valor que controla la velocidad del rebote
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};


$(document).ready(function(){


// Pone la primera opción del menú como selected
$('div.mainmenu ul li:first-child').addClass('selected');


// Gestiona el uso del menú
$('div.mainmenu ul li a').click(function(){
	flag_events_scroll_for_menu=false;
	anchor=$(this).attr('href').substr(1);
	$.scrollTo('a[name="'+anchor+'"]', {duration:3000, easing:'elasout', onAfter:function(){flag_events_scroll_for_menu=true;} } );
	
	$('div.mainmenu ul li').removeClass('selected');
	$(this).parent().addClass('selected');
	$(this).animate({
		'margin-top': '0px'
	}, 200, function(){
		$(this).animate({
			'margin-top':'26px'
		},100, function(){
			$(this).animate({
				'margin-top':'16px'
				},50, function(){
					$(this).animate({
						'margin-top':'26px'
					},40);
			});
		});
	});
	return false;
});



// Define el scrollTo
$('div.boton_arriba a').click(function(){
	flag_events_scroll_for_menu=false;
	$.scrollTo('a[name="home"]', {duration:4000, easing:'elasout', onAfter:function(){flag_events_scroll_for_menu=true;}} );
	return false;
});



// La ventana de enlaces
$('div#enlaces').click(function(){
	$(this).removeClass('enlaces_muestra');
});

$('div.enlaces a').click(function(){
	$('div#enlaces').toggleClass('enlaces_muestra');
});




// Inicializa los slider de fotos
// INFO: http://flex.madebymufffin.com/


$('#flex-mainimages').flexslider({
	animation: "fade",
	slideshowSpeed: 7000,
	animationDuration: 600,         //Set the speed of animations, in milliseconds
	slideshow: false,
	directionNav: true,            //Create navigation for previous/next navigation? (true/false)
	controlNav: true                //Create navigation for paging control of each clide? (true/false)
});


$('#flex-event').flexslider({
	animation: "slide",
	slideshow: false,
	directionNav: true,             //Create navigation for previous/next navigation? (true/false)
	controlNav: false               //Create navigation for paging control of each clide? (true/false)
});


$('#flex-junke').flexslider({
	animation: "slide",
	slideshow: false,
	directionNav: true,             //Create navigation for previous/next navigation? (true/false)
	controlNav: false               //Create navigation for paging control of each clide? (true/false)
});

$('#flex-accio').flexslider({
	animation: "slide",
	slideshow: false,
	directionNav: true,             //Create navigation for previous/next navigation? (true/false)
	controlNav: false               //Create navigation for paging control of each clide? (true/false)
});

$('#flex-otros').flexslider({
	animation: "slide",
	slideshow: false,
	directionNav: true,             //Create navigation for previous/next navigation? (true/false)
	controlNav: false               //Create navigation for paging control of each clide? (true/false)
});







// El menú de experiencia
$('ul#exp_botones li').mouseenter(function(){
	if ($(this).attr('class') != 'selected') {
		$(this).children('a').animate({
			top: '142',
			}, 300, function() {
			}
		);
	};
});

$('ul#exp_botones li').mouseleave(function(){
	if ($(this).attr('class') != 'selected') {
		$(this).children('a').animate({
			top: '0',
			}, 500, function() {
			}
		);
	};
});



$('ul#exp_botones li a').click(function(){
	selectExp($(this));
});

$('ul#exp_botones li').click(function(){
	selectExp($(this).children('a'));
});

function selectExp(o) {
	if (o.parent().attr('class') != 'selected') {
		$('div.exp_sectiones-blank').hide();
		
		o.parent().parent().children('li').each(function(){
			// Se pone el fondo original a cada uno
			c = $(this).css('background-position').split(' ');
			$(this).css('background-position', c[0]+' -284px');
			if ($(this).attr('class') == 'selected') {
				$(this).children('a').animate({
					top: '0',
					}, 500, function() {
					}
				);
			};
		});
		o.parent().parent().children('li').removeClass('selected');
		o.parent().addClass('selected');
		
		section_id = o.parent().children('span').html();
		/*
		$('div.exp_section').fadeOut('fast',function(){
			$('div#'+section_id).fadeIn();
		});
		*/
			
		$('div.exp_section').hide();
		$('div#'+section_id).show();
	}
}




var lastMenuSelected='';

// Controla el scroll para hacer aparecer los edificions
$(window).scroll(function(){
	// La aparición de los edificios
	var posicion            = $(this).scrollTop();
	var alturatotal         = $(document).height() - $(this).height();
	var distanciahastaabajo = alturatotal - posicion;
	//$('#debug').html('Datos:<br/>Posición: ' + posicion + '<br/>Altura total: ' + alturatotal + '<br/>Distancia hasta abajo: ' + distanciahastaabajo);
	if (distanciahastaabajo < 505) { // Altura del footer
		var posskline=distanciahastaabajo;
		$('div#footer div.skyline').css('top', posskline);
	}
	
	
	// Controla el cambio del menú según hacemos scroll
	if (flag_events_scroll_for_menu) {
		var posHom = $('a[name="home"]').offset().top + 100;
		var posQue = $('a[name="part_quehacemos"]').offset().top;
		var posExp = $('a[name="part_experiencia"]').offset().top - 200;
		var posCli = $('a[name="part_clientes"]').offset().top - 200;
		var posCon = $('a[name="footer"]').offset().top - 900;
		// $('#debug').html('Posición: ' + posicion + '<br/>' + posHom + ', ' + posQue + ', ' + posExp + ', ' + posCli + ', ' + posCon);
		
		var done=false;
		
		if ( (!done) && (lastMenuSelected!='posCon') && (posicion > posCon)  ){
			$('div.mainmenu ul li').removeClass('selected');
			$('div.mainmenu ul li a[href="#footer"]').parent().addClass('selected');
			lastMenuSelected='posCon';
			done=true;
			// d=$('#debug2').html(); $('#debug2').html(d+'[posCon]');
		}
		
		if ( (!done) && (lastMenuSelected!='posCli') && (posicion > posCli) && (posicion < posCon) ){
			$('div.mainmenu ul li').removeClass('selected');
			$('div.mainmenu ul li a[href="#part_clientes"]').parent().addClass('selected');
			lastMenuSelected='posCli';
			done=true;
			// d=$('#debug2').html(); $('#debug2').html(d+'[posCli]');
		}
		
		
		if ( (!done) && (lastMenuSelected!='posExp') && (posicion > posExp) && (posicion < posCli) ){
			$('div.mainmenu ul li').removeClass('selected');
			$('div.mainmenu ul li a[href="#part_experiencia"]').parent().addClass('selected');
			lastMenuSelected='posExp';
			done=true;
			// d=$('#debug2').html(); $('#debug2').html(d+'[posExp]');
		}
		
		
		
		/*
		if ( (!done) && (lastMenuSelected!='posQue') && (posicion >= posQue) && (posicion < posExp)){
			$('div.mainmenu ul li').removeClass('selected');
			$('div.mainmenu ul li a[href="#part_quehacemos"]').parent().addClass('selected');
			lastMenuSelected='posQue';
			done=true;
		}
		*/
		
		
		
		if ( (!done) && (lastMenuSelected!='posHom') && (posicion < posExp) ){
			$('div.mainmenu ul li').removeClass('selected');
			$('div.mainmenu ul li a[href="#home"]').parent().addClass('selected');
			lastMenuSelected='posHom';
			done=true;
			// d=$('#debug2').html(); $('#debug2').html(d+'[posHom]');
		}

	}


});




// Quita las seccions dos segundos más tarde, porque si no, no cargan bien las galerías
timeoutID = window.setTimeout(f_erase_exp_secciones, 2500);





});






function f_erase_exp_secciones() {
	//$('div.exp_section').addClass('fuera');
	$('div.exp_section').hide();
	$('div.exp_sectiones-blank').show();
	$('div.exp_sectiones-blank').show();
	window.clearTimeout(timeoutID);
}


})(jQuery);


