﻿$(document).ready(function() {

/*
	Progressive Enhancement: Navigation Hovers
		This adds hoverIntent to the navigation dropdown menus.
*/

	// Remove CSS fallback
	$("#navigation li ul.dropdown").css("display","none");

	// Add hoverIntent
	
	if ($.browser.msie && $.browser.version <= 7) {
	
		$("#navigation li").hover(function() {
			$("ul.dropdown", this).css("display","block");
		}, function() {
			$("ul.dropdown", this).css("display","none");
		});
	
	} else { // real browsers
	
		function navDropdown() {
			$("a.nav-item", this).addClass("dropped");
			$("ul.dropdown", this).css("display","block");
		}
		function navDropup() {
			$("a.nav-item", this).removeClass("dropped");
			$("ul.dropdown", this).css("display","none");
		}
		var hoverIntentConfig = {
			over:		navDropdown,
			timeout:	100,
			out: 		navDropup
		}
		$("#navigation li").hoverIntent(hoverIntentConfig);
	
	}


/*
	Progressive Enhancement for IE 8: Content Lighting
		Automagically makes content lighting with images for IE8.
		IE 7 and below do not get any lighting effects. (For now?)
*/
	if ($(".interior .content").length) {

		if ($.browser.msie && $.browser.version > 7) {
	
		var glowCapWidth		= 832;
		var glowCapHeight		= 159;
	
		var contentDiv			= $(".interior .content");
		var glowDivWidth		= contentDiv.outerWidth();
		var glowDivHeight		= contentDiv.outerHeight();
	
			$(".interior").css("position","relative");


			contentDiv.css({
				"position"	: "relative",
				"zIndex"	: "1"
			}).unwrap().after('<div class="interior-content-glow" />');

			glowDivHeight		= glowDivHeight - glowCapHeight;
			contentDivPosition	= contentDiv.position();
			glowDivPositionLeft	= contentDivPosition.left + ((glowDivWidth - glowCapWidth) / 2);
			glowDivPositionTop	= (glowCapHeight / 2);
			
			$(".interior-content-glow").css({
				"width" : glowCapWidth + "px",
				"height" : glowDivHeight + "px",
				"left" : glowDivPositionLeft + "px",
				"top" : glowDivPositionTop + "px"
			})
			.prepend('<div class="interior-content-glow-topcap" />')
			.append('<div class="interior-content-glow-bottomcap" />');
		
		}
	}

/*
	Progressive Enhancement: Table Striping
		Dev note: If possible tables should be striped server-side!
*/
	$("table tr:not(thead tr, #homepage tr):odd").addClass("table-alternate-bg");

/*
	Homepage - Slideshow
*/

	if ($(".homepage-slideshow ul").length) {

		$(".homepage-slideshow ul")
			.after('<p class="slidenav">')
			.cycle({ 
				fx:		'scrollHorz', 
				speed:	'fast', 
				timeout: 5000, 
				pager:	'.slidenav' 
			});

		var slidernavWidth = 0;
		$('.slidenav a').each(function() {
			slidernavWidth += $(this).outerWidth(true);
		});
		var slidernavHalfWidth = -(slidernavWidth / 2);
		$('.slidenav').css({'width' : slidernavWidth , 'margin-left' : slidernavHalfWidth});
		
	}

/*
	Homepage - Gang Forecast flyout
*/

	$("#gang-forecast-flyout")
		.hide()
		.css({
			"width"		: "500px",
			"height"	: "auto"
		});
		
	var gangFlyoutOffset = (-($("#gang-forecast-flyout").outerHeight() + 15) + "px");
	$("#gang-forecast-flyout")
		.css({
			"position"	: "absolute",
			"top"		: gangFlyoutOffset,
			"right"		: "15px"
		});
	
	$(".gang-forecast-flyout-anchor").hover(
		function() {
			$("#gang-forecast-flyout").show();
		},
		function() {
			$("#gang-forecast-flyout").hide();
		})
		.click( function(e) { e.preventDefault() });

/*
	Forms: Multipurpose clear focus
*/
	var clearMePrevious = '';
	$('.clearMeFocus').focus(function() {
		if($(this).val()==$(this).attr('title')) {
			clearMePrevious = $(this).val();
			$(this).val('');
		}
	}).blur(function() {
		if($(this).val()=='') {
			$(this).val(clearMePrevious);
		}
	});



});
