jQuery().ready(function(){
	//jQuery UI dialog and lighbox 
	//http://jqueryui.com/demos/dialog/
	$(function () {
		$("div.dialog").hide();
		
		//add a class of "dialog" to attach the popup open function to an anchor
		//also add a rel attribute to the anchor with the same value as the id 
		//of the element it should open
		$("a.dialog").click(function () {
			var myDiv = $("div#" + $(this).attr("rel"));
			$(myDiv).dialog({
				autoOpen: false,
				height: 600,
				width: (myDiv.hasClass('fixed')?860:200)
			}).dialog("open");
			return false;
		});	
	})					
						
	//Slideshow for homepage feature images
	//jQuery Cycle plugin - http://jquery.malsup.com/cycle/
	$('#slideshowContainer').cycle({ 
		fx: 'fade',
		pager: '.featureTabs',
		pagerAnchorBuilder: function (idx, slide) {
			return '.featureTabs li:eq(' + idx + ') a';
		},
		timeout: 7000,
		pauseOnPagerHover: true,
		cleartype:  1 // enable cleartype corrections 
	});
	
	
	/*-------------------------------------------------------------------- 
	 * javascript method: "pxToEm"
	 * by:
	   Scott Jehl (scott@filamentgroup.com) 
	   Maggie Wachs (maggie@filamentgroup.com)
	   http://www.filamentgroup.com
	 *
	 * Copyright (c) 2008 Filament Group
	 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
	 *
	 * Description: Extends the native Number and String objects with pxToEm method. pxToEm converts a pixel value to ems depending on inherited font size.  
	 * Article: http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/
	 * Demo: http://www.filamentgroup.com/examples/pxToEm/	 	
	 *							
	 * Options:  	 								
			scope: string or jQuery selector for font-size scoping
			reverse: Boolean, true reverses the conversion to em-px
	 * Dependencies: jQuery library						  
	 * Usage Example: myPixelValue.pxToEm(); or myPixelValue.pxToEm({'scope':'#navigation', reverse: true});
	 *
	 * Version: 2.0, 08.01.2008 
	 * Changelog:
	 *		08.02.2007 initial Version 1.0
	 *		08.01.2008 - fixed font-size calculation for IE
	--------------------------------------------------------------------*/
	
	Number.prototype.pxToEm = String.prototype.pxToEm = function(settings){
		//set defaults
		settings = jQuery.extend({
			scope: 'body',
			reverse: false
		}, settings);
		
		var pxVal = (this == '') ? 0 : parseFloat(this);
		var scopeVal;
		var getWindowWidth = function(){
			var de = document.documentElement;
			return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
		};	
		
		/* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
			For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size. 	
			When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
			to get an accurate em value. */
					
		if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
			var calcFontSize = function(){		
				return (parseFloat($('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
			};
			scopeVal = calcFontSize();
		}
		else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };
				
		var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
		return result;
	};
	
	// Equal box heights
	/*-------------------------------------------------------------------- 
	 * JQuery Plugin: "EqualHeights"
	 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
	 *
	 * Copyright (c) 2008 Filament Group
	 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
	 *
	 * Description: Compares the heights or widths of the top-level children of a provided element 
			and sets their min-height to the tallest height (or width to widest width). Sets in em units 
			by default if pxToEm() method is available.
	 * Dependencies: jQuery library, pxToEm method	(article: 
			http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
	 * Usage Example: $(element).equalHeights();
			Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
	 * Version: 2.0, 08.01.2008
	--------------------------------------------------------------------*/
	
	$.fn.equalHeights = function(px) {
		$(this).each(function(){
			var currentTallest = 0;
			$(this).children().each(function(i){
				if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
			});
			if (!px || !Number.prototype.pxToEm) {
				currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
			}
			// for ie6, set height since min-height isn't supported
			if ($.browser.msie && $.browser.version == 6.0) { 
				$(this).children().css({'height': currentTallest}); 
			}
			$(this).children().css({'min-height': currentTallest}); 
		});
		return this;
	};
    
	$(".contentBlocks").equalHeights(true);
	$(".contactBlocks").equalHeights(true);
	$(".commonPromoBlocks").equalHeights(true);
	$(".custom404Blocks").equalHeights(true);
	
	//Print 
    $('#print').click(function(){
        window.print();
    });

    //Drop down menus
    $('ul#menu').superfish ({
        delay: 0,
        speed: 0,
        autoArrows: false,
        dropShadows: false
    });

    /* Site Map */
    $('#sitemapDisplay ul.treeList li').each(function(i) {
        $(this).prepend('<span class="plusMinus"><img src="prebuilt/images/wrapper/empty-list-bullet.gif" class="listBullet" /></span>');
    });

    $('#sitemapDisplay ul.treeList li > ul').each(function(i) {

        var parentListItem = $(this).parent('li');

        var sub_ul = $(this).remove();

        parentListItem.children('span.plusMinus').wrapInner('<a href="#" />').find('img').attr('src','prebuilt/images/wrapper/list-plus.gif');

        parentListItem.find('span.plusMinus > a').click(function() {

            if (sub_ul.css("display") == "none") {
                $(this).children('img').attr('src','prebuilt/images/wrapper/list-minus.gif');
            } else {
                $(this).children('img').attr('src','prebuilt/images/wrapper/list-plus.gif');
            }

            sub_ul.toggle();

            return(false);

        });

        parentListItem.append(sub_ul);

    });

    // Hide all child > child ULs
    $('#sitemapDisplay ul.treeList li ul li ul').hide()

    $('#sitemapDisplay ul.treeList li ul.firstLevel').parents('li').each(function(i) {
        $(this).children('span.plusMinus').children('a').children('img').attr('src','prebuilt/images/wrapper/list-minus.gif');
    });


    /* Begin US Maps */
    
    // Global mouseout state
    $('area').mouseout(function(){ $('#usMap').css({backgroundPosition: "0 -948px"}); })

    // West Territory
    $('area.WestTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "-1000px 0"}); })
    // Midwest Territory
    $('area.MidwestTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "-1000px -316px"}); })
    // Northwest Territory
    $('area.NorthwestTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "0 0"}); })
    // California Territory
    $('area.CaliforniaTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "-500px 0"}); })
    // Nevada Territory
    $('area.NevadaTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "-500px 0"}); })
    // Northwest Alaska
    $('area.NorthwestAlaskaTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "0 0"}); })
    // DMU HI
    $('area.dmuHi').mouseover(function(){ $('#usMap').css({backgroundPosition: "-500px -948px"}); })
    // Southeast Territory
    $('area.SouthEast').mouseover(function(){ $('#usMap').css({backgroundPosition: "-500px -632px"}); })
    // Great Lakes Territory
    $('area.GreatLakesTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "0 -316px"}); })
    // DMU PR
    $('area.dmuPr').mouseover(function(){ $('#usMap').css({backgroundPosition: "-1000px -632px"}); })
    // Mid-Atlantic Territory
    $('area.MidAtlanticTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "-500px -316px"}); })
    // Northeast Territory
    $('area.NortheastTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "0 -632px"}); })
    // South Central Territory
    $('area.SouthCentralTerritory').mouseover(function(){ $('#usMap').css({backgroundPosition: "-1000px -632px"}); })


    if($('#usMap').hasClass('wt')) {
        $('#usMap').css({backgroundPosition: "-1000px 0"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-1000px 0"});
        })
    } else if($('#usMap').hasClass('mwt')) {
        $('#usMap').css({backgroundPosition: "-1000px -316px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-1000px -316px"});
        })
    } else if($('#usMap').hasClass('nwt')) {
        $('#usMap').css({backgroundPosition: "0 0"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "0 0"});
        })
    } else if($('#usMap').hasClass('cali')) {
        $('#usMap').css({backgroundPosition: "-500px 0"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-500px 0"});
        })
    } else if($('#usMap').hasClass('dmuhi')) {
        $('#usMap').css({backgroundPosition: "-500px -948px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-500px -948px"});
        })
    } else if($('#usMap').hasClass('dmupr')) {
        $('#usMap').css({backgroundPosition: "-1000px -632px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-1000px -632px"});
        })
    } else if($('#usMap').hasClass('set')) {
        $('#usMap').css({backgroundPosition: "-500px -632px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-500px -632px"});
        })
    } else if($('#usMap').hasClass('glt')) {
        $('#usMap').css({backgroundPosition: "0 -316px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "0 -316px"});
        })
    } else if($('#usMap').hasClass('mat')) {
        $('#usMap').css({backgroundPosition: "-500px -316px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-500px -316px"});
        })
    } else if($('#usMap').hasClass('net')) {
        $('#usMap').css({backgroundPosition: "0 -632px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "0 -632px"});
        })
    } else if($('#usMap').hasClass('sct')) {
        $('#usMap').css({backgroundPosition: "-1000px -632px"});
        $('area').mouseout(function(){
        $('#usMap').css({backgroundPosition: "-1000px -632px"});
        })
    }

    /* End US Maps */

});

/* =Text Resize Functionality
--------------------------------------------------------------------------------------*/
function increaseFontSize() {
    document.getElementById('content').style.fontSize = '116%';
    var elementId = 'page-content';
    var elm = document.getElementById(elementId);
};
function decreaseFontSize() {
    document.getElementById('content').style.fontSize = '85%';
    var elementId = 'page-content';
    var elm = document.getElementById(elementId);
};
function resetFontSize() {
    document.getElementById('content').style.fontSize = '100%';
    var elementId = 'page-content';
    var elm = document.getElementById(elementId);
};

function contactUsRedirect(url, pageId) {
	window.location.href=url+'&id='+pageId+'#USMAPANCHOR';
}

function popsize(url,w,h) {
 window.open(url,'','scrollbars=yes,resizable=yes,width='+w+',height='+h);
}

$(document).ready(function() {
	$.tablesorter.defaults.cssAsc = "headerSortUp";
	$.tablesorter.defaults.cssDesc = "headerSortDown";
	$.tablesorter.defaults.sortList = [[1,0]];
	$("#resultsTable").tablesorter({widthFixed: true, widgets: ['zebra']})
					  .tablesorterPager({container: $("#pageControl"), size: 5, seperator: " of ",positionFixed: false,pageDisplayPrefix: "Page "})
					  .tablesorterPager({container: $("#pageControl2"), size: 5, seperator: " of ",positionFixed: false,pageDisplayPrefix: "Page "});


	/*Highlights the current issue in the menu*/
	$("#ArchiveContainer li.article.current").parent().parent().addClass("highlight"); /*Removal of this fixes dissapearing 2008 in IE*/
	
	/*Removes <hr /> from <li /> if it is the last one in an <ul />*/
	$(".Archive ul.issues hr:last-child").remove();
	
	/*Removes <hr /> from teh bottom of the highlighted area in the menu*/
	//$("li.highlight").parent().parent().next().remove();
	
	$("#ArchiveContainer ul.issues, #ArchiveContainer ul.articles").addClass("hidden");
	
	/*Adds the "+" or "-" link*/
	$("#ArchiveContainer ul").prev().before("<a href='#' class='switch'></a>");
	
	 /*Makes the current issue visible in menu*/
	$(".current").parent().removeClass("hidden").parent().parent().removeClass("hidden");
	$(".current > ul").removeClass("hidden");
	
	/* open all switches from current to root */
	$(".current").parents().children("a.switch").toggleClass("open");
	$(".current").children("a.switch").toggleClass("open");
	
	/*Toggles switch to "+" or "-" and visibility of menu when clicked.*/
	$("#ArchiveContainer a.switch").click(function() {
		//$(this).siblings("ul").slideToggle("fast").parent().siblings().children("ul").slideUp("fast");
		//$(this).toggleClass("open").parent().siblings().children("a.switch").removeClass("open");
		$(this).siblings("ul").slideToggle("fast");
		$(this).toggleClass("open");
		return false;
	})

});



