$(document).ready(function(){
	
	/* ======= Navigation (Show/Hide) ===================== */
	
	//$('.toggles[id!="'+$('#nav a:first').attr('rel')+'"]').css({display:'none'})
	/*$('.toggles').css({display:'none'});
	
	$('#nav a, a#benefits, a[rel*="introducing"], a[rel="checklist"]').click(function(event){
		event.preventDefault();
		
		// Toggle Content
		var thisAnchor = $(this)
		$('#content .toggles').css({display:'none'})
		$('.toggles[id="'+thisAnchor.attr('rel')+'"]').css({display:'block'})
		
		// Active state
		$('#nav a').removeClass()
		$(this).addClass('active')
		
		// Bookmark Set
		window.location.href = getBookmark()[0]+'#!'+$(this).attr('rel')
		
	})
	
	// Bookmark Get
	if ( hasBookmark() ) {
		$('a[rel="'+getBookmark()[1]+'"]').click()
	} else {
		$('#nav a:first').click()
	}
	
	function getBookmark(){
		return window.location.href.split('#!')
	}
	function hasBookmark(){
		if ( getBookmark().length > 1 ) {
			return true
		} else {
			return false
		}
	}*/
	
	/* ======= Drop Down Nav ===================== */
	
	$('#nav li').hoverIntent({
		over:function(){
			$(this).find('#nav-t2').fadeIn('fast');
		},
		out:function(){
			$(this).find('#nav-t2').fadeOut('fast');
		},
		timeout:500
	});
	
	// Prevent Drop Down Navs From Going to URL - URL is a backup for no JS
	
	$('#nav li a').click(function(event){
		if ( $(this).parent().find('#nav-t2').size() != 0 ) {
			$(this).find('#nav-t2').fadeIn('fast');
			event.preventDefault();
		}
	});
	
	// Caption Creation from .articleimg alt parameter
	
	$('.articleimg').each(function(){
		if ( $(this).attr('alt') != '' ) {
			$(this).after('<span class="caption">'+$(this).attr('alt')+'</span>');
			$(this).next().css({width:$(this).outerWidth()-20});
		}
	});
	
	/* ===== Banner Rotator ==================== */
	
	/* pseudo code:
	* turn off all but first logo
	* create JS array based on anchors in the div
	* set current index to 0
	* start a timer which will fade out any visible logos and fade in the next logo based on current index ++
		* but if current index equals array length - 1, reset current index to 0
	*/
	
	if ( $('#banner').size() != 0 ) {
	
	var aBanners = new Array();
	
		$('#banner').append('<div id="dots"></div>');
		
		$('#banner a').each(function(){
			aBanners.push($(this));
			$('#dots').prepend('<div class="hit"><div class="dot" rel="'+(aBanners.length-1)+'"></div></div>')
			$(this).css({opacity:0})
		})
		//aBanners.reverse()
		
		//$('#banner a:first').css({opacity:'1',display:'block'})
		
		/* === Banner Navigation ==== */
		
		// Generate Dots
		
		/*$('#banner').append('<div id="dots"></div>');
		
		$('#banner a').each(function(){
			$('#dots').prepend('<div class="hit"><div class="dot" rel="'+$(this).find('img').attr('src')+'"></div></div>')
		})*/
		
		//$('#dots .dot:last').attr('id', 'dot-active')
		
		$('#dots .hit')
			.mouseenter(function(){
				//$(this).find('.dot').css({backgroundColor:'#a9d2e6'})
			})
			.mouseleave(function(){
				//$(this).find('.dot').css({backgroundColor:''})
			})
			
		// Banner Rotation (After dots are generated)
		
		var nCurRotation = 0;
		var nPrevRotation;
		
		function startRotation(){
			// Instant Start
			rotatorInterval()
			// Timer
			myRotator = setInterval(rotatorInterval, 5000)
		}
		
		function rotatorInterval(){
			
			if ( aBanners.length == 1 ) { return false; }
			
			nPrevRotation = nCurRotation == 0 ? 0 : nCurRotation-1;
			
			if ( nCurRotation >= aBanners.length ) {
				//$('body').append('<br />Reset index')
				nCurRotation = 0;
				nPrevRotation = aBanners.length-1;
			}
			
			aBanners[nPrevRotation].animate({opacity:0}, 300, function(){
				aBanners[nPrevRotation].css('display','none');
				aBanners[nCurRotation].css('display','block');
				//$('body').append('<br />Image: '+aBanners[nCurRotation].attr('href')+' index:'+nCurRotation)
				/*aBanners[nPrevRotation].click(function(event){event.preventDefault()});
				$('#banner').click(function(event){
					window.location.href = aBanners[nCurRotation].attr('href');
					event.preventDefault();
				})*/
				aBanners[nCurRotation].animate({opacity:1}, 300, function(){
					// Set Active Dot
					$('#dots .dot').attr('id', '');
					$('#dots .dot[rel="'+nCurRotation+'"]').attr('id', 'dot-active');
					// Increment index
					nCurRotation++;
				})
				
			})
		}
		
		function stopRotation(){
			clearInterval(myRotator);
		}
		
		startRotation();
		
		// Hover Pause
		
		$('#banner').hoverIntent({
			over:function(){
				stopRotation();
			},
			out:function(){
				if ( startRotationOnMouseOut ) {
					startRotation();
				}
			},
			timeout:500
		})
		
		// Manual Nav
		
		var startRotationOnMouseOut = true;
		
		/*$('.dot').click(function(){
			startRotationOnMouseOut = false;
			stopRotation();
			var nPrevRotation = nCurRotation == 0 ? 0 : nCurRotation-1;
			if ( nCurRotation >= aBanners.length ) { // This is redundant code - see above
				//$('body').append('<br />Reset index')
				nCurRotation = 0;
				nPrevRotation = aBanners.length-1;
			}
			aBanners[nPrevRotation].animate({opacity:0}, 300, function(){
				nCurRotation = Number($(this).attr('rel'));
				aBanners[Number($(this).attr('rel'))].animate({opacity:1}, 300);
			});
		})*/
		
	} // IF banner exists
	
	/* ======= Home Col Height ===================== */
	
	if ( $('#home-col1').height() < $('#home-col2').height() ) {
		$('#home-col1').css('height',$('#home-col2').height())
	}
	
	if ( $('#home-col1').height() > $('#home-col2').height() ) {
		$('#home-col2').css('height',$('#home-col1').height())
	}
	
	/* ======= Current Events - Pop up window ====== */
	
	$('#home-col2 a').click(function(event){
		event.preventDefault();
		window.open($(this).attr('href'), "Details", "scrollbars=1, width=400, height=500, no, yes, no, screenX=75,screenY=75");
	});
	
	/* ======= Drawers ===================== */
	
	/*$('.drawers .drawer, .drawers .drawer-bot').css({display:'none'})
		
		$('.drawers div[class!="drawer"]')
			.each(function(){
				myToggle($(this))
			})
			
			
		function myToggle(target){
			target
				.toggle(function(){
					$('.drawers div[class*="-open"]').each(function(){
						$(this).click()
					})
					$(this).attr('class', $(this).attr('class')+'-open')
					if ( $(this).next().next().attr('class') == 'drawer-bot' ) {
						$(this).next().next().css({display:'block'})
					}
					$(this).next().slideDown('fast')
				},
				function(){
					$(this).next().slideUp('fast', function(){
						if ( $(this).next().attr('class') == 'drawer-bot' ) {
							$(this).next().css({display:'none'})
						}
						$(this).prev().attr('class', $(this).prev().attr('class').replace('-open', ''))
					})
				})
		}*/
		
	/* ======= Drawers 2 ===================== */
	
	$('.drawer-shelf').css({display:'none'})
		
		$('.drawer-handle')
			.each(function(){
				myToggle($(this))
			})
			
		$('div[rel="init-open"]').click()
			
			
		function myToggle(target){
			target
				.toggle(function(){
					$('div[class*="-open"]').each(function(){
						$(this).click()
					})
					$(this).attr('class', $(this).attr('class')+'-open')
					$(this).next().slideDown('fast')
				},
				function(){
					$(this).next().slideUp('fast', function(){
						$(this).prev().attr('class', $(this).prev().attr('class').replace('-open', ''))
					})
				})
		}
	
});

/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);
