$(document).ready(function() {
    resizeKen();
    window.setInterval('incrementCounter()', 4000);
    window.setInterval('rotateStories()', 10000);
});
    
function resizeKen() {
    if (parseInt(window.innerWidth) > 0 && parseInt(window.innerHeight) > 0) { // Firefox
        var dims = determineDimensions(window.innerWidth, window.innerHeight);
    }
    else if (parseInt(document.body.clientWidth) > 0 && parseInt(document.body.clientHeight) > 0) { // IE
        var dims = determineDimensions(document.body.clientWidth, document.body.clientHeight);
    }
    
    $('#ken').css('width', dims[0] + 'px');
    $('#ken').css('height', dims[1] + 'px');
}

var fullW = 928;
var fullH = 842;
var aspect = fullW / fullH;

function determineDimensions(clientWidth, clientHeight) {
    // By default, Ken is as tall as the page
    var kenH = clientHeight;
    var kenW = kenH * aspect;
    
    // however, Ken shouldn't be wider than 50% of the page
    if (kenW > (.5 * clientWidth)) {
        kenW = .5 * clientWidth;
        kenH = kenW / aspect;
    }
    
    return [kenW, kenH];
}

function incrementCounter() {
    count = count + Math.floor(Math.random() * 51);
    
    $('#digits').fadeOut('fast', function() {
        $('#digits').text(addCommas(count));
        $('#digits').fadeIn('fast');
    });
}

function addCommas(nStr) {
   nStr += '';
   x = nStr.split('.');
   x1 = x[0];
   x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1)) {
       x1 = x1.replace(rgx, '$1' + ',' + '$2');
   }
   return x1 + x2;
}

function blk() {
    $('#blk-button').attr('onclick', '').blur().addClass('loading');
    $.post("metoo.php", {}, function(data){
        $('#blk-button').removeClass('loading').addClass('noted');
    });
}

var storyIndex = 0;

function rotateStories() {
    $('#story').fadeOut('slow', function() {
        storyIndex++;
        if (storyIndex == stories.length)
            storyIndex = 0;
        $('#story blockquote').html(stories[storyIndex].story);
        $('#story .cite').html(stories[storyIndex].cite);
        $('#story').fadeIn('slow');
    });
}