/**
 * @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.
$(document).ready(function(){
	// add a class to the first and last LI in any OL or UL
	$('ul,ol').each(function(){
		$('li:first',this).addClass('first');
		$('li:last',this).addClass('last');
	});

	// split "twocol" lists into two columns
	$('ul.twocol,ol.twocol').each(function(){
		$('li:nth-child(odd)',this).css({width:'45%',float:'left',clear:'both'});
		$('li:nth-child(even)',this).css({width:'45%',float:'right'});
	});

	// create tabbed content areas.
	$('div.tabbed>ul').tabs();
	$('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
		$('ul#eventlist li').hover(
			function(){ // mouseover
				$(this).addClass('over');
			},
			function(){ // mouseout
				$(this).removeClass('over');
			}
		);
		$('ul#eventlist li').each(function(){
			$(this).click(function(){
				location.href=$('a',this).attr('href');
			});
		});
	}

	// add a class to the last div.module inside a .modules container
	$('div.modules div.module:last').addClass('last');

	// top navigation and dropdown menus
	if(!is_iphone){ // disable this behavior on iphone
		$('#navigation li[id^="nav"]').hover(
			function(){ //mouseover actions
				$(this).addClass('open');
				if(!$('a.active',this)[0]){ // only apply this if the .active item is NOT within the currently hovered menu
					$('#navigation a.active').css('border-bottom','1px solid #B4B1A0').css('height','37px');
				}
			},
			function(){ // mouseout actions
				$(this).removeClass('open');
				$('#navigation a.active').css('border-bottom','none').css('height','38px');
			}
		);
	}
	// allow top menus to function when tabbing through links
	$('#navigation li[id^="nav"] a').focus(function(){
		$(this).parents('li[id^="nav"]').addClass('open');
	});
	$('#navigation li[id^="nav"] a').blur(function(){
		$(this).parents('li[id^="nav"]').removeClass('open');
	});

	// highlight parent items of left nav
	$('ul#left-nav a.active').parents('ul').parents('li').addClass('with-sub').find('a:first').addClass('active-sub');

	// attach extra class to BODY of pages on iphone for some CSS adjustments
	if(is_iphone){ $('body').addClass('iphone'); }

	// prevent blank search form submission
	$('#global-search form').submit(function(){
		if($('#q',this).val()===''){
			return false;
		}
	});
});