/**
 * @author mraichelson
 */
// this creates a boolean variable we can test against to see if the browser is safari mobile on iphone
	var is_iphone=false;
	var agent=navigator.userAgent.toLowerCase();
	if(agent.indexOf('iphone')!=-1){
		is_iphone=true;
	}

// code applied to the page via jQuery when the DOM is ready.
jQuery(document).ready(function(){
	// add a class to the first and last LI in any OL or UL
	jQuery('ul,ol').each(function(){
		jQuery('li:first',this).addClass('first');
		jQuery('li:last',this).addClass('last');
	});

	// split "twocol" lists into two columns
	jQuery('ul.twocol,ol.twocol').each(function(){
		jQuery('li:nth-child(odd)',this).css({width:'45%',float:'left',clear:'both'});
		jQuery('li:nth-child(even)',this).css({width:'45%',float:'right'});
	});

	// create tabbed content areas.
	jQuery('div.tabbed>ul').tabs();
	jQuery('div.tabbed ul.tabs li a').focus(function(){
		this.blur();
	});

	// add a class to event calendar list items on mouseover
	if(!is_iphone){ // disable this behavior on iphone
		jQuery('ul#eventlist li').hover(
			function(){ // mouseover
				jQuery(this).addClass('over');
			},
			function(){ // mouseout
				jQuery(this).removeClass('over');
			}
		);
		jQuery('ul#eventlist li').each(function(){
			jQuery(this).click(function(){
				location.href=jQuery('a',this).attr('href');
			});
		});
	}

	// add a class to the last div.module inside a .modules container
	jQuery('div.modules div.module:last').addClass('last');

	// top navigation and dropdown menus
	if(!is_iphone){ // disable this behavior on iphone
		jQuery('#navigation li[id^="nav"]').hover(
			function(){ //mouseover actions
				jQuery(this).addClass('open');
				if(!jQuery('a.active',this)[0]){ // only apply this if the .active item is NOT within the currently hovered menu
					jQuery('#navigation a.active').css('border-bottom','1px solid #B4B1A0').css('height','37px');
				}
			},
			function(){ // mouseout actions
				jQuery(this).removeClass('open');
				jQuery('#navigation a.active').css('border-bottom','none').css('height','38px');
			}
		);
	}
	// allow top menus to function when tabbing through links
	jQuery('#navigation li[id^="nav"] a').focus(function(){
		jQuery(this).parents('li[id^="nav"]').addClass('open');
	});
	jQuery('#navigation li[id^="nav"] a').blur(function(){
		jQuery(this).parents('li[id^="nav"]').removeClass('open');
	});

	// highlight parent items of left nav
	jQuery('ul#left-nav a.active').parents('ul').parents('li').addClass('with-sub').find('a:first').addClass('active-sub');
	
	// allow top menus to function when tabbing through links
	jQuery('#left-nav li[id^="nav"] a').focus(function(){
		jQuery(this).parents('li[id^="nav"]').addClass('open');
	});
	jQuery('#left-nav li[id^="nav"] a').blur(function(){
		jQuery(this).parents('li[id^="nav"]').removeClass('open');
	});


	// attach extra class to BODY of pages on iphone for some CSS adjustments
	if(is_iphone){ jQuery('body').addClass('iphone'); }

	// prevent blank search form submission
	jQuery('#global-search form').submit(function(){
		if(jQuery('#q',this).val()===''){
			return false;
		}
	});
});