/**
* FIX IE6 IMAGE FLICKER PROBLEM
*
*/
try {
    document.execCommand('BackgroundImageCache', false, true);
} catch (e) { }

var WHA = {

    init: function() {
        /* IE6 fix for the lit state of the primary navigation. */
        var selectedNode = $('div.navigation.primary ul > li > a.rtsSelected');
        
        if($(selectedNode).size() >= 1) {
			$(selectedNode).parent('li').addClass('rtsSelected');
        }

        this.textsize();
        this.rightFeatures();
        this.photo3col();
        //this.pageTeasers();
        this.zebraTables();
        this.thisIsHep();
    },

    textsize: function() {

        var __me = this;

        var textsize = $('ul.toolbar li.textsize');
        $('a.' + title, textsize).addClass(title + '-active');

        $('a', textsize).click(function() {
            var classname = $(this).attr('class').replace('bg ', '');

            $('a', textsize).removeClass('small-active medium-active large-active');
            $(this).addClass(classname + '-active');

            setActiveStyleSheet(classname);

            /* Ensure that the dynamic features at the bottom get resized to allow the
            text to flow properly. */
            __me.pageTeasersResize();

            return false;
        });

    },

    /* Add a keyline (class) to the top-most graphic feature in the sidebar. */
    rightFeatures: function() {
        $('#sidebar div.graphic-feature:first img').addClass('keyline keyline-top');
    },

    /* Assign a b&w photo to any 'tall' three column page. */
    photo3col: function() {
//        if ($('div.three-column')) {

//            /* Determine the height of the 'body' and 'navigation' areas - 
//            columns 1 and 2 respectively. */
//            var bodyHeight = $('#body').height();
//            var navHeight = $('#navigation').height();

//            /* There needs to be at least 350 pixels of 'dead' space in the 
//            left-hand portion of the screen in order to fit in the photo. */
//            if ((bodyHeight - navHeight) > 350) {
//                var rand = Math.floor(Math.random() * 4);
//                $('div.three-column').append('<div class="bwphoto bwphoto-' + (rand + 1) + '" />');
//            }

//        }
    },

    /* Small page teasers that are located at the bottom of all primary 
    landing pages. */
    pageTeasers: function() {

        /* Determine the number of panes/rows that need to be accounted for. */
        var num_teasers = $('#bottom div.teaser-navigation div.teaser').size();
        var num_panes = parseInt(num_teasers / 4);
        num_panes = (num_teasers % 4 == 0) ? num_panes : num_panes + 1;

        for (var x = 0; x < num_panes; x++) {

            var start = x * 4;
            var end = (x + 1) * 4;

            $('#bottom div.teaser').filter(function(i) {
                return (i >= start && i < end);
            }).wrapAll('<div class="pane keyline" />');
        }

        /* Remove the keyline from the last row/pane. */
        $('#bottom div.pane:last').removeClass('keyline');

        /* Every last teaser in the row (pane) should have a class of 'last' 
        applied to it. */
        $('#bottom div.pane').each(function(i) {
            $('div.teaser:last div.item', this).addClass('last');
        });

        this.pageTeasersResize();
    },

    /* Go through and resize each page teaser so that their heights are 
    identical throughout. */
    pageTeasersResize: function() {

        var max_teaser_height = 0;
        var max_hdr_height = 0;

        $('#bottom div.pane div.teaser').each(function(i) {
            /* Reset the current height. */
            $('div.item h2', this).height('auto');
            max_hdr_height = ($('div.item h2', this).height() > max_hdr_height) ? $('div.item h2', this).height() : max_hdr_height;
        }).each(function(j) {
            $('div.item h2', this).height(max_hdr_height + 'px');
        });

        $('#bottom div.pane div.teaser').each(function(i) {
            /* Reset the current height. */
            $('div.item', this).height('auto');
            max_teaser_height = ($(this).height() > max_teaser_height) ? $(this).height() : max_teaser_height;
        }).each(function(j) {
            $('div.item', this).height(max_teaser_height + 'px');
        });

    },

    /* Makes all Sitefinity grids into 'Zebra' tables. */
    zebraTables: function() {
        $('#content table.sf_libraryGrid tbody tr:even').addClass('zebra');
    },

    /* Ensures 'this is hepatitis...' is bolded properly wherever it occurs in the
    navigation. */
    thisIsHep: function() {
        var str = 'This is hepatitis...';

        $('#navigation li a:contains(' + str + '), div.breadcrumb span:contains(' + str + '), #body h1:contains(' + str + ')').each(function() {
            var html = $(this).html();
            $(this).html(html.replace('hepatitis...', '<strong>hepatitis...</strong>'));
        });
    }

}

/**
* Form Helper
*
* Example usage:
* FormHelper.textSelector('.search input[type="text"]'); // argument is a CSS selector
*/
var FormHelper = {

	textSelector: function(elem) {
		
		if(!$(elem)) { return false; }
		
		var fields = $(elem);
		var fields_start_val = new Array();
		
		$(fields).each(function(i) {
			fields_start_val.push($(this).val());

			$(this).focus(function() {
				$(this).select();
			});

			$(this).focusout(function() {
				if(jQuery.trim($(this).val()) == '') {
					$(this).val(fields_start_val[i]);
				}
			});
		});

		$(fields).focus(function() {
			$(this).select();
		});
		
	}

}