function preloadImages(imgs) {
  for(var i = 0; i<imgs.length; i++)
  {
    $('<img>').attr('src', imgs[i]);
  }
}
function fullSize() {
    /*//var imageRatio = 0.5;
    var $img = $('#background img');
    //var imageRatio = (img.width())/(img.height());
    var imageRatio = ($img.width())/($img.height());
    */var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    var docHeight = $(document).height();/*
    var windowScale = windowWidth / windowHeight;
    var targetWidth = windowHeight * imageRatio;
    var targetWidthFull = windowWidth;
    var leftPos = - (targetWidth - windowWidth) / 2;
    var leftPosFull = 0;
       
    $('#background-pattern').css('height', windowHeight);
    if (windowScale <= imageRatio) {
        $('#background img').attr('width', targetWidth);
        $('#background').css('left', leftPos);
    } else {
        $('#background img').attr('width', targetWidthFull);
        $('#background').css('left', leftPosFull);
    }*/

    if (docHeight == windowHeight) {
        $('#footer').css('position', 'fixed');
    } else {
        $('#footer').css('position', 'fixed');
    }
}

function changeBackground(id) {
    $('#background img').fadeOut(200, function() {
        var newBG = bgs[id];
        $('#background img').attr('src', newBG);
        setTimeout('fullSize()', 10);
        $('#background img').fadeIn(500);
    });
}
function getPrevBackgroundID(bgs) {
    var currentBG = $('#background img').attr('src');
    var bgIndex = bgs.indexOf(currentBG);
    var bgsLength = bgs.length;
    var newIndex = bgIndex-1;
    if (bgIndex > 0) {
        return newIndex;
    } else {
        return bgsLength-1;
    }
}
function getNextBackgroundID(bgs) {
    var currentBG = $('#background img').attr('src');
    var bgIndex = bgs.indexOf(currentBG);
    var bgsLength = bgs.length;
    var newIndex = bgIndex+1;
    if (bgIndex < (bgsLength-1)) {
        return newIndex;
    } else {
        return 0;
    }
}

Array.prototype.max = function() {
    var max = this[0];
    var len = this.length;
    for (var i = 1; i < len; i++) {
        if (this[i] > max) {
            max = this[i];
        }
    }
    return max;
};

function timer(section) {
    var len = section.length;
    var rands = [];

    for (i=0; i<len; i++) {
        rands.push(Math.floor(Math.random()*12));
    }

    var counter = 0;

    var interval = setTimeout(function() {
       timerFunc();
    }, 5);
    function timerFunc() {
        var letters = section.split('');
        for (j=0; j < len; j++) {
            if (counter < rands[j]) {
                letters[j] = Math.floor(Math.random()*9);
            }
        }
        $('h1#subtitle').text(letters.join(''));
        counter++;

        if (counter > rands.max()) {
            clearInterval(interval);
        } else {
            setTimeout(function() {
                timerFunc();
            }, 12*counter);
        }
    }
}

$(window).load(function() {
    fullSize();
});

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

$(document).ready(function() {
    fullSize();
    $('#sub-menu a').click(function() {
        
        $('#footer').css('position', 'fixed');
        $('#sub-menu li.active').removeClass('active');
        $(this).parent().addClass('active');
        var section = $(this).parent().attr('id');
        var link = $(this).attr('href');
        //var bg = "images/bg/" + section + ".jpg";
        //$('#background img').attr("src", bg);
        //timer(section);
        $.get(link, { embed: "1" }, function(data){
            $('#background img').fadeOut(200, function() {
                //var width = $('#background img').width();
                //alert(width);
            });
            $('#data').slideUp(400, function() {
                timer(section);
                $(this).empty().append(data).fadeIn(500);
                bgID = Math.floor(Math.random()*bgs.length);
                var bg = bgs[bgID];
                $('#background img').attr("src", bg);
                //setTimeout('fullSize();', 10);
                fullSize();
                $('#background img').fadeIn(500);
            });
            var width = $('#background img').width();
            if (width === 0) {
                fullSize();
            }
        });
        
        //fullSize();
        //timer(section);
        return false;
    });
    $('#bg-show').click(function() {
        if ($(this).hasClass('background')) {
            $(this).removeClass('background').addClass('content').text('Show Content');
            $('#page').fadeOut(500);
            $('#bg-controls').fadeIn(500);
        } else {
            $(this).removeClass('content').addClass('background').text('Show Background');
            $('#page').fadeIn(500);	
            $('#bg-controls').fadeOut(500);
        }
        fullSize();
    });
    $('#bg-prev').click(function() {
        newBG = getPrevBackgroundID(bgs);
        changeBackground(newBG);
    });
    $('#bg-next').click(function() {
        newBG = getNextBackgroundID(bgs);
        changeBackground(newBG);
    });
});