$(document).ready(function()
{
    Font.OriginalSize = parseInt($('html').css('font-size'));
    Font.CurrentSize = Cookie.Get('font-size');
    if (Font.CurrentSize)
    {
        Font.SetSize();
    }
    else
    {
        Font.CurrentSize = 0;
        setPageHeight();
    }
    
    // Reset Font Size
    $(".resetFont").click(function()
    {
        Font.SetSize(0);
    });

    // Increase Font Size
    $(".increaseFont").click(function()
    {
        Font.Increase();
        return false;
    });
    
    // Decrease Font Size
    $(".decreaseFont").click(function()
    {
        Font.Decrease();
        return false;
    });
    
    // Show/Hide Nav
    // TOP Menu button
    $('.menu:first a.glass-btn').click(function()
    {
        $('#elevator').removeClass('down-low');
        $('#nav').show();
        return false;
    });
    
    // BOTTOM Menu Button
    $('.menu:last a.glass-btn').click(function()
    {
        $('#nav').show();
        $('#elevator').addClass('down-low');
        // stick the bottom
        $('html, body').animate({scrollTop: $(document).height()},1);
        return false;
    });

    // Select Menu Item
    $('#nav td a').click(function()
    {
        //$('#nav').hide();
    });

    $('a#close-nav').click(function()
    {
        $('#nav').hide();
        return false;
    });
    
    // Opens photo browser
    $('.story-photo a').click(function()
    {
        $('#photo-viewer').show();
    });
    
    // Closes photo browser
    $('#nix').click(function()
    {
        $('#photo-viewer').hide();
    });
    
    // Photo viewer
    $('#slideshow').cycle(
    { 
        fx: 'fade',
        speed: 1,
        pager: '#pager',
        prev:   '#prev a.glass-btn', 
        next:   '#next a.glass-btn', 
        timeout: 0,
        after : onAfter
    });
    
});


function onAfter(curr, next, opts)
{
    currentSlide = $("#pager a.activeSlide").html();
    if (!currentSlide) currentSlide = "1";
    $('#photo-count').html(currentSlide + ' of ' + opts.slideCount);
}

function setPageHeight()
{
    // reset wrapper height to auto
    $('#wrap').height('auto');
    // show nav menu briefly and grab document height while it's open
    $('#nav').show();
    var ht = $(document).height();
    // hide nav menu
    $('#nav').hide();
    // set height
    $('#wrap').height(ht);
}

// FONT functions
function Font() {}

Font.SetSize = function()
{
    var pxSize = Font.OriginalSize * Font.Sizes[Font.CurrentSize];
    var newSize = pxSize + 'px';
    $('html').css('font-size', newSize);
    
    if (Font.CurrentSize)
    {
        Cookie.Set('font-size',Font.CurrentSize,30);
    }
    else
    {
        Cookie.Delete('font-size');
    }
    setPageHeight();
}

Font.Increase = function()
{
    Font.CurrentSize++;
    if (Font.CurrentSize > 4) Font.CurrentSize = 4;
    Font.SetSize();
}

Font.Decrease = function()
{
    Font.CurrentSize--;
    if (Font.CurrentSize < -4) Font.CurrentSize = -4;
    Font.SetSize();
}

Font.Sizes = 
{
    '-4':   .4,
    '-3':   .5,
    '-2':   .6,
    '-1':   .8,
    '0':    1,
    '1':    1.2,
    '2':    1.5,
    '3':    1.9,
    '4':    2.4
}

// COOKIE functions
function Cookie(){}

Cookie.Set = function(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
    
Cookie.Get = function(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
    
Cookie.Delete = function(name)
{
    Cookie.Set(name,"",-1);
}
