$(document).ready(function () {
    // set body font size
    $('body').css('font-size', getCookie('zoom'));

    $('#header-zoom a').click(function() {
        var zoom = $('body').css('font-size');
        
        if (zoom == '10px') {
            zoom = '75%'; // 12px
        }
        else if (zoom == '12px') {
            zoom = '87.5%'; // 14px
        } else {
            zoom = '62.5%'; // 10px
        }

        $('body').css('font-size', zoom);

        setCookie('zoom', zoom);
    });

    $('#header-nav li.horiz').mouseover(function() {
        $(this).find('ul.vert').show();
        $(this).find('a.horiz').addClass('horiz-hover');
    });

    $('#header-nav li.horiz').mouseleave(function() {
        $(this).find('a.horiz').removeClass('horiz-hover');
        $(this).find('ul.vert').hide();
    });

    $('input.hint, textarea.hint').focus(function() {
        if ($(this).val() == $(this).attr('title'))
            $(this).val('');
    });

    $('input.hint, textarea.hint').blur(function() {
        if ($(this).val() == '')
            $(this).val($(this).attr('title'));
    });

    $('#header-banners').cycle({
        containerResize: 0,
        fx: 'fade',
        random: true,
        slideExpr: '.header-banner-outer',
        timeout: 5000
    });

    $('.slide-show .slide-show-body-inner').cycle({
        containerResize: 0,
        fx: 'fade',
        timeout: 5000,
        pager: '.slide-show-pager',
        next: '.slide-show-next',
        prev: '.slide-show-prev',
        pause: 1,
        before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
            var captionElement = $(currSlideElement).parents('.slide-show').find('.slide-show-caption');
            var imgElement = null;

            if (nextSlideElement.tagName.toLowerCase() == 'img')
                imgElement = $(nextSlideElement);
            else
                imgElement = $(nextSlideElement).find('img');

            captionElement.html(imgElement.attr('alt'));
        }
    });

    $('.tweets-hidden').hide();

    $('a.see-all-tweets').click(function () {
        var tweetsHidden = $('.tweets-hidden');

        if (tweetsHidden.is(':hidden')) {
            tweetsHidden.slideDown('fast');
        } else {
            tweetsHidden.slideUp('fast');
        }
    });

    $('.tales-hidden').hide();

    $('a.see-all-tales').click(function () {
        var talesHidden = $('.tales-hidden');

        if (talesHidden.is(':hidden')) {
            talesHidden.slideDown('fast');
        } else {
            talesHidden.slideUp('fast');
        }
    });
});

function setCookie(name, value, expiry, path, domain, secure) {
    var docCookie = name + "=" + escape(value) +
        ((expiry) ? "; expires=" + expiry.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");

    document.cookie = docCookie;

    return true;
}

function getCookie(name) {
    var value = null;
    var docCookie = document.cookie;
    var start = docCookie.indexOf(name + "=");
    var finish = -1;

    if (start >= 0) {
        start = start + name.length + 1;
        finish = docCookie.indexOf(";", start);

        if (finish < 0)
            finish = docCookie.length;

        value = unescape(docCookie.substring(start, finish));
    }

    return value;
}

