$(document).ready(function() {
    InitHTMLElements();
    InitPageTools();
});


InitHTMLElements = function() {
    // inject caps for content container top/bottom drop shadows
    $('.pp-ContentContainer').prepend('<div class="pp-ContainerCapTop"></div>').append('<div class="hack-clearBoth"></div><div class="pp-ContainerCapBottom"></div>');

    // add "first" class to first instances of <h2> elements
    $('.pp-Content1 h1:first').addClass('first');
    $('.pp-Section, .pp-TabContent').find('h2:first').addClass('first');

    // insert rounded corner on left nav
    $('.pp-NavPrimary li a:first').append('<div class="pp-RoundedCorner"></div>');
    $('.pp-NavPrimary li:last').addClass('last');


    InitHome();
    InitTabSwitchers();
    InitForms();


    // lists
    $('ul.pp-List-3col').after('<div class="hack-clearBoth"></div>').find('li:nth-child(3n+1)').addClass('first');

    // init buttons
    $('.pp-Button').button();
}

InitHome = function() {
    // content switchers
    $('.pp-Home .pp-ContentSwitcher select').change(function(i) {
        if (this.selectedIndex > 0) {
            $(this).siblings('div').removeClass('selected');
            $(this).siblings('div:eq(' + (this.selectedIndex - 1) + ')').addClass('selected');
        }
    });

    // pre-select branch, if chosen
    var selectedBranch = $('#userBranch').val();
    if (selectedBranch) {
        $('.pp-Home .pp-ContentSwitcher select').val(selectedBranch);
        $('.pp-Home .pp-ContentSwitcher select').trigger('change');
    }
}

InitTabSwitchers = function() {
    $('.pp-TabSwitcher ul.pp-TabNav li:first').addClass('first');
    $('.pp-TabSwitcher ul.pp-TabNav li:last').addClass('last');
    $('.pp-TabSwitcher ul.pp-TabNav li a').prepend('<span></span>');
}

InitForms = function() {
    $('table.form tr').each(function() {
        if ($(this).children('td').length == 2) {
            if ($('.pp-Home').length > 0) {
                $(this).children('td:first').css('width', '38%');
                $(this).children('td:last').css('width', '62%');
            }

            $(this).children('td:contains("*")').each(function() {
                $(this).addClass('pp-Label');
                $(this).html($(this).html().replace('*', ''));
            });
        }
    });

    $('table.form input[type=submit]').each(function() {
        $(this).after('<a href="javascript: void(0);" onclick="$(\'#' + $(this).attr('id') + '\').trigger(\'click\');" class="pp-Button">' + $(this).val() + '</a>').hide();

        if ($.browser.msie)
            $(this).css('position', 'absolute').show();
    });
}

InitPageTools = function() {
    var s = '<div class="pp-TextResizer">\n'
		+ '<span>Text Size:</span> '
		+ '<a class="styleswitch larger" rel="larger" href="javascript:void(0);">A</a>'
		+ '<a class="styleswitch default" rel="default" href="javascript:void(0);">A</a>'
		+ '<a class="styleswitch smaller" rel="smaller" href="javascript:void(0);">A</a>'
		+ '</div>\n';

    $('.pp-Header').append(s);
    StyleSwitcher.Init();
}



/*
style sheet switcher for font size, requires jquery.cookie.js,
*/

var StyleSwitcher = {
    Init: function() {
        $('.styleswitch').click(function() {
            StyleSwitcher.Switch(this.getAttribute("rel"));
            return false;
        });
        var c = $.cookie('style');
        StyleSwitcher.Switch((c == null ? "default" : c));
    },

    Switch: function(styleName) {
        $('link[rel*=style][title]').each(function(i) {
            this.disabled = true;
            if (this.getAttribute('title') == styleName) this.disabled = false;
        });

        $.cookie('style', styleName, { expires: 365, path: '/' });
        $('a.styleswitch').removeClass("selected");
        $('a.styleswitch.' + styleName).addClass("selected");
    }
}

$.fn.button = function() {
    return this.each(function() {
        var el = $(this);
        el.append('<span></span>');
    });
}

$.fn.stripHTML = function() {
    var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
    this.each(function() {
        $(this).html(
			$(this).html().replace(regexp, "")
		);
    });
    return $(this);
}