$(function () {

    // font size adjustment
    window.jTextSizeAnchors = $('#text-size a');
    jTextSizeAnchors.each(function () {
        var jThis = $(this),
            sStyleSheet = jThis.attr('rel');
        jThis.bind('click', function () {
            switchStyleSheet(sStyleSheet);
            $.cookie('stylePref', sStyleSheet);
            return false;
        });

    });

    var stylePref = $.cookie('stylePref');
    if (stylePref) switchStyleSheet(stylePref);

    // hover
    $('#navigation>ul>li').each(function () {
        var jThis = $(this);
        jThis.hover(
            function () { jThis.addClass('hover'); },
            function () { jThis.removeClass('hover'); }
        );
    });

    var jLogo = $('#logo').bind('click', function () {
        window.location.href = jLogo.find('a').attr('href');
    });


    var jNotifyExternal = $('#notifyExternal');

    // locate external links and apply a hover event to them to notify the user that this link leads elsewhere
    var jAnchors = $('#content').find('a').each(function () {
        var jThis = $(this);
        if( jThis.size() > 0 && jThis.attr('href') ) {
            if (jThis.attr('href').indexOf('http') != -1) {
                // if a title already exists, preserve it
                var sOrigTitle = jThis.attr('title') || '',
                    sNewTitle = 'This link leaves the King County Housing Authority website';
                if( sOrigTitle != '' ) {
                    sNewTitle = "[ " + sNewTitle + " ]   " + sOrigTitle;
                }

                jThis.attr('title', sNewTitle);
            }
        }
    });


});

function switchStyleSheet(stylesheet) {
    jTextSizeAnchors
        .removeClass('active')
        .filter(function() {return $(this).attr('rel') == stylesheet;} )
        .addClass('active');

    $('link[@rel*=style][title]').each(function () {
        var jStylesheet = $(this),
            disabled = (jStylesheet.attr('title') == stylesheet) ? false : true;
        jStylesheet.attr('disabled', disabled);
    });
}
