// JavaScript Document
$(document).ready(function(){
	
	$('header').fadeIn(1500);
	$('img.b').css({"opacity": "0.25"}, 0);
	$('img.t').css({"opacity": "0"}, 0);
    $('.info').css({ "top": "180px", "opacity": "0" }, 0);
    $('.large .info').css({ "top": "180px", "opacity": "0" }, 0);
	
	
	
	
	
	/* MENU HEADER*/
	$('a.m_trab').hover(function(){
		$(this).animate({backgroundPosition:'0px -60px'},{queue:false,duration:200});
	}, function(){
		$(this).animate({backgroundPosition:'0px 0px'},{queue:false,duration:200});
	});
	$('a.m_perf').hover(function(){
		$(this).animate({backgroundPosition:'-126px -60px'},{queue:false,duration:200});
	}, function(){
		$(this).animate({backgroundPosition:'-126px 0px'},{queue:false,duration:200});
	});
	$('a.m_cont').hover(function(){
		$(this).animate({backgroundPosition:'-227px -60px'},{queue:false,duration:200});
	}, function(){
		$(this).animate({backgroundPosition:'-227px 0px'},{queue:false,duration:200});
	});
	
	
	/* MENU HOME*/
	$('a.h_trab').hover(function(){
		$(this).animate({backgroundPosition:'0px -70px'},{queue:false,duration:200});
	}, function(){
		$(this).animate({backgroundPosition:'0px 0px'},{queue:false,duration:200});
	});
	$('a.h_perf').hover(function(){
		$(this).animate({backgroundPosition:'-294px -70px'},{queue:false,duration:200});
	}, function(){
		$(this).animate({backgroundPosition:'-294px 0px'},{queue:false,duration:200});
	});
	$('a.h_cont').hover(function(){
		$(this).animate({backgroundPosition:'-528px -70px'},{queue:false,duration:200});
	}, function(){
		$(this).animate({backgroundPosition:'-528px 0px'},{queue:false,duration:200});
	});
	
	/* Botones formulario */
	$('#bt_submit').hover(function(){
		$(this).animate({backgroundPosition: '0px -30px'},{queue:false, duration:100});
	}, function(){
		$(this).animate({backgroundPosition: '0px 0px'},{queue:false, duration: 200});
	});
	$('#bt_submit').click(function(){
		$('#respuesta').html('<span style=\"color: #F00\"> Enviando ... </span>');
	});
	
	
	/* references */
	$('.item').hover(function(){
    $('img.b',this).stop().animate({"opacity":"0"}, "fast");
    $('img.t',this).stop().animate({"opacity":"1"}, "fast");
    $('.info',this).css({"top":"180px", "width":"320px"});
    $('.info',this).stop().animate({"top":"220px", "opacity":"1"},{queue:false,duration:200});
	}, function() {
    $('img.b',this).stop().animate({"opacity":"0.25"}, "slow");
    $('img.t',this).stop().animate({"opacity":"0"}, "slow");
    $('.info',this).stop().animate({"width":"0px", "opacity":"0"},{queue:false,duration:200});
	});
	
	$('.large').hover(function(){
		$('.info',this).css({"width":"640px"});
	});
	
	
	var $window = $(window);
	var $homeBG = $('#home');
	var $menuHomeBG = $('#menuHome');
	var $workBG = $('#trabajos');
	var $aboutBG = $('#perfil');
	var $contactBG = $('#contacto');
	var bodyBg = $("body");
	var windowHeight = $window.height(); //get the height of the window
	
	
	$('#home, #menuHome, #trabajos, #perfil, #contacto').bind('inview', function(event, visible){
		if (visible == true) {
			$(this).addClass("inview");
		} else {
			$(this).removeClass("inview");
	 	}
	});
	
	
	
	
	function newPos(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	function newPosBottom(x, maxPos, pos, maxTop, inertia){
		return x + "% " + (((maxPos - pos) * inertia)+maxTop)  + "px";
	}
	
	//function to be called whenever the window is scrolled or resized
	function Move(){ 
		var pos = $window.scrollTop(); //position of the scrollbar
		windowHeight = $window.height();
		var bodyHeight = $('body').height();
		if($homeBG.hasClass("inview")){
			$homeBG.animate({'backgroundPosition': newPos(50, 0, pos, 0, 0.3)},{queue:false,duration:300});
		} else {
        }
		if($menuHomeBG.hasClass("inview")){
			$('#logo h1').animate({backgroundPosition: '0px -32px'}, {queue:false, duration:200});
			$('header').animate({backgroundPosition: '0px -70px'}, {queue:false, duration:200});
			$('#menu').animate({top:'-65px'},{queue:false, duration:200});
		}else{
			$homeBG.animate({'backgroundPosition': newPos(50, 0, pos, 0, 0.3)},{queue:false,duration:300}); // OJO con esta linea y la de arriba
			$('#logo h1').animate({backgroundPosition: '0px 0px'}, {queue:false, duration:200});
			$('header').animate({backgroundPosition: '0px 0px'}, {queue:false, duration:200});
			$('#menu').animate({top:'5px'},{queue:false, duration:400});
		}
		if($workBG.hasClass("inview")){
			bodyBg.animate({'backgroundPosition': newPos(50, 0, pos, 0, -0.6)},{queue:false,duration:300});
		}else{
		}
		if($contactBG.hasClass("inview")){
			$('footer').animate({bottom: '-44px'},{queue:false, duration:300});
		}else{
			$('footer').animate({bottom: '0px'},{queue:false, duration:300});
		}
	}
	
	
	
	
	$window.resize(function(){ //if the user resizes the window...
		Move(); //move the background images in relation to the movement of the scrollbar
		// RepositionNav(); //reposition the navigation list so it remains vertically central
	});		
	$window.bind('scroll', function(){ //when the user is scrolling...
		Move(); //move the background images in relation to the movement of the scrollbar
	});
	
	
	// Formulario
	$('#contactar').html5form({
		action: 'contacto.php',
        responseDiv: '#respuesta',
		messages: 'es',
		colorOn: '#FFF',
		colorOff: '#FFF',
		labels: 'hide',
		allBrowsers: true,
		emailMessage: 'Dirección de correo inválida'
    });
	
	
});



/* SCROLL */
$(function(){
    $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});
