jQuery(document).ready(function($){
	
	/* get the page_id_class_num, which is the important thing for the whole thing to work. */
	var page_id_class = ""; 
	var page_id_class_index = -1;
	var page_id_class_num = "";
	var nav_index = -1;
	var current_page_li_found = false;
	$('#pages li').each(function(){
		if($(this).hasClass('current_page_item') ||
		   $(this).hasClass('current_page_ancestor') ){
		   		current_page_li_found = true;
		   		page_id_class = $(this).attr('class');
		   		//alert(page_id_class);
				
				page_id_class_index = page_id_class.search(/page-item-/i); // <body> will have a class of page-id-3, for example.  li has page-item-3...
				if(page_id_class_index != -1){ // if the page-id exists
					var page_id_class_array = page_id_class.split(' ');
					for(var i = 0; i < page_id_class_array.length; i += 1){
						if(page_id_class_array[i].substr(0, 10) === 'page-item-'){
							page_id_class = page_id_class_array[i];
							
							//here it is
							page_id_class_num = page_id_class_array[i].substring(10);
						}
					}
				}
				else {
					return;
				}
		}
	});
	if(!current_page_li_found){
		page_id_class_num = 3; //home page is the default
	}
	
	if($('body').hasClass('category-testimonials')){ //if we are looking at a testimonials post, or the testimonials category page
		page_id_class_num = 7;
	}
	else if($('body').hasClass('category-practice-areas')){
		page_id_class_num = 11;
	}
	else if($('body').hasClass('category-faqs')){
		page_id_class_num = 15;
	}
	//alert(page_id_class);
	
	//$('.default').removeClass('default'); //commented out when I removed .default from header.php
	$('img#nav-' + page_id_class_num).addClass('default');
	
	var $current_li = $('li.page-item-' + page_id_class_num);
	if( !( $current_li.hasClass('current_page_item') ) ){
		$current_li.addClass('current_page_item');
	}
	
	/***** javascript is working, so reset css accordingly *****/ //alert('javascript up and running');
	$('#nav li a').css({
		'textDecoration': 'none'
	});
		
	/***** stored values *****/
	var cpi_color = $('#nav li.current_page_item a').css('color'); //alert(cpi_color);
	//var cpi_bg = $('#nav li.current_page_item a').css('background'); //alert(cpi_bg);
	var new_selected;
	var old_selected;
	
	/* *********************** section nav *********************** */
	//alert( $('#section_list li').length );
	
	//the maximum height of the bg image is 600px.  so -600 is the max for bg_height.
	var bg_height = -617; //was -608
	var section_list_bg_y = bg_height + $('#section_list').innerHeight(); //default;
	//alert(section_list_bg_y);
	
	var page_index = $('#section_list li').index($('#section_list li.current_page_item'));
	//alert('page_index ' + page_index);
	
	var page_multiple = ($('#section_list li').length - page_index) - 1;
	//alert(page_multiple);
	//alert($('#section_list li').innerHeight());
	
	section_list_bg_y -= ( page_multiple * $('#section_list li').innerHeight() );
	//alert(section_list_bg_y);
	
	var normal_position = "40px " + section_list_bg_y + "px";
	$('#section_list').css('backgroundPosition', normal_position);
	
	var new_page_index, //index within the jquery object arrary of the li hovered over
	    new_page_multiple, //how many li are after the new_page_index
	    new_section_list_bg_y, //new y-axis position of the background image
	    new_position; 
	
	/***** events *****/
	var setMouseOverEventHandlers = function(){
		setTimeout(function(){ //delayed by 300 ms because safari was triggering spurious events on page load.
			var cpi_a_height = $('#nav li.current_page_item a').height();
			$('#nav li a').hover(
				function(){ //over
					if(!$(this.parentNode).hasClass('current_page_item')){ //if we are NOT simply hovering over this page's nav button
						//alert($(this.parentNode).get(0).tagName);
						
						$('#nav li.current_page_item a').css({
							'border': 'none',
							'height': (cpi_a_height + 3) + 'px'
						}); 
						
						$(this).css({
							'borderBottom': '3px solid #f5f2dd',
							'height': cpi_a_height + 'px'
						});
						
						page_class = $(this.parentNode).get(0).className; //alert(page_class);
						//alert(page_class.slice(20,22));
						switch(parseFloat(page_class.slice(20,22))){ //break the last class down to the final number (note: kinda fragile!)
							case 3:
								new_selected = $('img#nav-3').show().css('z-index','1001');
								break;
							case 7:
								new_selected = $('img#nav-7').show().css('z-index','1001');
								break;
							case 11:
								new_selected = $('img#nav-11').show().css('z-index','1001');
								break;
							case 15:
								new_selected = $('img#nav-15').show().css('z-index','1001');
								break;
							case 18:
								new_selected = $('img#nav-18').show().css('z-index','1001');
								break;
							case 20:
								new_selected = $('img#nav-20').show().css('z-index','1001');
								break;
							default:
								new_selected = $('img#nav-3').show().css('z-index','1001');
						}
						old_selected = new_selected;
					}
				},
				function(){ //out
					if(!$(this.parentNode).hasClass('current_page_item')){
						
						//alert(old_selected.attr('id'));
						old_selected.hide().css('z-index','0');
						
						$(this).css({
							'border': 'none',
							'height': (cpi_a_height + 3) + 'px'
						});
						
						$('#nav li.current_page_item a').css({
							'borderBottom': '3px solid #f5f2dd',
							'height': cpi_a_height + 'px'
						});
					}
				}
			);
		}, 300); //300 ms delay
		clearTimeout();
		
		$('#section_list li').hover(
			function(){ //over
				new_page_index = $('#section_list li').index($(this));
				//alert(new_page_index);
				
				new_page_multiple = ($('#section_list li').length - new_page_index) - 1;
				new_section_list_bg_y = (bg_height + $('#section_list').innerHeight()) - ( new_page_multiple * $('#section_list li').innerHeight() );
				new_position = "40px " + new_section_list_bg_y + "px";
				//alert(new_position);
				
				$('#section_list').css('backgroundPosition', new_position);
			},
			function(){ //out
				$('#section_list').css('backgroundPosition', normal_position);
			}
		);
	}
	
	if (navigator.userAgent.indexOf('iPhone') != -1 ||
	    navigator.userAgent.indexOf('iPod') != -1 ||
	    navigator.userAgent.indexOf('iPad') != -1 ) { 
	   
		/* mobile safari user -- don't set the mouse over event handlers on these devices. */
		//alert('This is an iPhone or an iPod.');
		
	}
	else {
		setMouseOverEventHandlers();
	}
});