$(window).resize(function() {
    repositi();
});

$(document).ready(function(){

    randomImages();
    repositi()
    random = setInterval('randomImages()',10000);

    $('nav li').click(function(){
        idn = $(this).attr('rel');
        
        clearInterval(random);
       
        if(idn == "empresa"){
            $('#cred').hide();
            $('section #stage').animate({ left: "0" }, 300);
            $('section').animate({ width: '611px' }, 300);
            $('section div').scrollTo('article#' + idn,300);
        } else if (idn == "software"){
           $('#cred').hide();
           $('section #stage').animate({ left: "0" }, 300);
           $('section').animate({ width: '611px' }, 300);
           $('section div').scrollTo('article#' + idn,300);
        } else if (idn == "clientes"){
            $('#cred').hide();
            $('section #stage').animate({ left: "0" }, 300);
            $('section').animate({ width: '611px' }, 300);
            $('section div').scrollTo('article#' + idn,300);
        } else if (idn == "home"){
            randomImages();
            random = setInterval('randomImages()',10000);
            $('#cred').show();
            $('section #stage').animate({ left: "611" }, 500);
            $('section').animate({ width: '0px' }, 300);
        }
        
    });
    $('#up').click(function(){
        $('section #stage').animate({ left: "611" }, 500);
        $('section').animate({ width: "0" }, 500);
        $('nav').delay(500).animate({ height: "110" }, 500);
        randomImages();
        random = setInterval('randomImages()',10000);
    });
    $('#home').hover(function(){
        $('nav').animate({ height: "499" }, 500);
    });

    $('#bgswitch a').click(function(){
        if(id != $(this).attr('ref')) {
            id = $(this).attr('ref');
            title = $(this).attr('rel');

            $('section #stage').animate({ left: "611" }, 500);
            $('section').animate({ width: "0" }, 500);


            setBg(id, title);
        }
    });

});

var id = 3;
var maximgs = 3;

function randomImages(){
    idx = Math.floor(Math.random()*9);
    if(idx == id || idx == 0) {
        randomImages();
    } else {
        id = idx;
        setBg(id, "");
    }
}

function setBg(id, title){
    $('#cred').show().html( title )
    $('#bgs img').fadeOut('fast');
    $('#bgs img#' + id).fadeIn('slow');
    $('#bgswitch a').removeClass('active');
    $('#bgswitch a[ref=' + id + ']').addClass('active');
}

function repositi() {
     if($(window).height() <= 530) {

        } else {
            height = $(window).height() - 530;
            height = height / 2;
            $('body').css('paddingTop',height);
        }
}
