var wN = 0;

$(document).ready(function(){
	$('#top').css('height',($(window).height()/2)-$('#header').height());
	$('#menu').css('margin-left',($(window).width()/2)-180);
	$('#content').css('padding-top',$(window).height()-$('#top').height());
	
	$('#fNombre').val('');
	$('#fEmail').val('');
	$('#fMensaje').val('');
	
	
	$("#works").each(function(i, area){
        var $area = $(this);
        $("img", $area).each(function(j, img) {
            var $img = $(this), x = Math.round($img.position().top),
                w = $img.outerHeight(), bot = x + w,
                top = x - w;
            $(window).scroll(function(e) {
                var sx = $(this).scrollTop(), opac = .2;
                if( sx <= bot && sx >= top ) {
                    if( sx >= top && sx <= x ) {
						meh = 1;
						
                    } else {
						meh = 0.2;
                    }
					opac = meh
                }
                $img.animate({opacity:opac},{queue:false, duration:500});
            }).scroll();
        });
    });
	$('.workImg').mouseenter(function(){
		$(this).prepend($('#linkImg'));
		var msg = "Visitar "+$(this).parent().children('.title').text();
		$('#linkImg').html(msg);
		$('#linkImg').fadeIn('fast');
	}).mouseleave(function(){
		$('#linkImg').fadeOut('fast');
	});
	
});

$(window).scroll(function(){
	var scrollTop = $(window).scrollTop();
	var top = $('#top').height();
	var works = $('#works').offset().top;
	var friends = $('#friends').offset().top;
	if(scrollTop > top){
		$('#header').css('position','fixed');
		$('#header').css('top','0');
		playHeadAnim(0);
	} else {
		$('#header').css('position','absolute');
		$('#header').css('top',$('#top').height());
		playHeadAnim(($(window).width()/2)-180);
	}
	if(scrollTop > (works - 120) && scrollTop < (friends - 320))
	{
		$('#worksMsg').fadeIn("slow");		
	}else{
		$('#worksMsg').fadeOut("slow");		
	}
});

function playHeadAnim(op)
{
	$('#menu').animate({"margin-left":op}, {queue:false, duration:500});
}

function switchSec(sec)
{
	if(sec == "works")
	{
		var wt = (wN<1||wN>9?[sec="#"+sec,wN=0]:sec=".w"+wN);
		wN +=1;
	}else{
		sec = "#"+sec;
		wN = 0;
	}
	var destination = $(sec).offset().top;
	$("html:not(:animated),body:not(:animated)").animate({
	scrollTop: destination-110
	}, 500);
}

