$(document).ready(function(){
	setup_nav();
});

var current_section;  //this is the section that is currenty open
var slide_time = 200;
var timeout;
var tab_section; //this is the tab that the mouse is over
var tab_object; //this is the same as tab_section but it is the object


function setup_nav() {
	
	current_section = current_page;
	
	if(typeof subnav[current_page] != 'undefined') {
		//we've got subtabs
		fill_subnav(current_page);
		$('#subnav').show();
		default_open = true;
	}
	tab_events();
}


function tab_events() {
	
	//first event is when the mouse enters a tab
	$('.mo').mouseover(function() {

		if(tab_section && tab_section != current_page && tab_section != current_section) {
			if($(tab_object).attr('src').match('-over.jpg')) {
				var src = $(tab_object).attr('src').replace('-over.jpg', '.jpg');
				$(tab_object).attr('src', src);
				clearTimeout(timeout);
			}
		}

		//the tab we just entered
		tab_section = $(this).attr('id').split('.')[1];
		tab_object = $(this);
		
		//yea this is a hack....
		if(tab_section == 'home') tab_section = '/';

		//no need to do anything if the section is already open
		if(tab_section != current_section) { 
			//swap the image
			if(!$(this).attr('src').match('-over.jpg')) {
				var src = $(this).attr('src').replace('.jpg', '-over.jpg');
				$(this).attr('src', src);
			}
			
			//if this section has a subnav
			if(typeof subnav[tab_section] != 'undefined') {
				//swap the links in
				fill_subnav(tab_section);
				toggle_nav();
				current_section = tab_section;
			}
			else {
				current_section = false;
				$('#subnav').slideUp(slide_time);
			}
		}
	});
	
	$('.mo').mouseout(function(e) {
		//we've left a tab
		//we need to know where the mouse went so we know what to do
		var old_tab = $(this);
		var new_target = e.relatedTarget;
		var old_tab_section = $(old_tab).attr('id').split('.')[1];
		
		switch(where_did_i_go(e)) {
			case 'new_tab':
				
				clearTimeout(timeout);
				
				var new_target_section = $(new_target).attr('id').split('.')[1];
				
				//we're on another tab
				//if the old tab was not the default, we need to change back the image
				if(old_tab_section != current_page) {
					if($(old_tab).attr('src').match('-over.jpg')) {
						var src = $(old_tab).attr('src').replace('-over.jpg', '.jpg');
						$(old_tab).attr('src', src);
					}
				}
				
				break;
				
			case 'page':

				if(typeof subnav[old_tab_section] != 'undefined') {
					timeout = setTimeout('reset_nav()', 2000);
				}
				else {
					reset_nav();
				}
				break;
		}
	});
	
	$('#subnav').mouseover(function() {
		clearTimeout(timeout);
	});
	
	//we moved into the subnav
	//we dont need to do anything until the mouse moves off of it
	$('#subnav').mouseout(function(e) {
		
		if(where_did_i_go(e) == 'page') {
			//we moved from the subnav to somewhere on the page (other than a tab)
			//so set a timeout to reset the subnav
			timeout = setTimeout('reset_nav()', 2000);
		}
		else if (where_did_i_go(e) == 'new_tab') {
			//we're on a new tab, so we got to swap the subnav and tab images
			//we don't need to do anything if the new tab is the last one we came from
			var new_tab = e.relatedTarget;
			var new_tab_section = $(new_tab).attr('id').split('.')[1];
			
			//this is a hack!
			if(new_tab_section == 'home') new_tab_section = '/';

			if(new_tab_section != tab_section) {
				//default doesnt need to swap
				if(current_section != current_page) {
					//swap the image
					if($(tab_object).attr('src').match('_over.gif')) {
						var src = $(tab_object).attr('src').replace('_over.gif', '.gif');
						$(tab_object).attr('src', src);
					}
				}
			}
		}		
	});
}

function reset_nav() {
	//yea this is a hack....
	if(tab_section == 'home') tab_section = '/';
	if(typeof subnav[current_page] != 'undefined') {
		//we've got to show the subnav
		//if it isn't already open to the right one\
		if(tab_section != current_page) {
			if($(tab_object).size() && $(tab_object).attr('src').match('-over.jpg')) {
				var src = $(tab_object).attr('src').replace('-over.jpg', '.jpg');
				$(tab_object).attr('src', src);
			}

			
			$('#subnav').slideUp(slide_time, function() {
				fill_subnav(current_page);
				$('#subnav').slideDown(slide_time);
				current_section = current_page;
			});
		}
	}
	else {
		if(tab_section != current_page) {
			if($(tab_object).attr('src').match('-over.jpg')) {
				var src = $(tab_object).attr('src').replace('-over.jpg', '.jpg');
				$(tab_object).attr('src', src);
			}
			current_section = false;
			$('#subnav').slideUp(slide_time);
		}
	}
}


function fill_subnav(section) {
	$('#subnav').empty();
	for(var i in subnav[section]) {
		if(subnav[section][i]['url_code'] == current_subpage) {
			$('#subnav').append('<span>' + subnav[section][i]['title'] + '</span>');
		}
		else {
			$('#subnav').append('<a class="navlink" href="/' + section + '/' + subnav[section][i]['url_code'] + '">' + subnav[section][i]['title'] + '</a>&nbsp;&nbsp;');
		}
	}
}

function where_did_i_go(event) {
	var new_target = event.relatedTarget;
	
	if($(new_target).attr('id').match('navimage')) {
		//we moved onto a tab.. 
		// var target_section = $(new_target).attr('id').split('.')[1];
		return 'new_tab';
	}
	else if($(new_target).attr('id') == 'subnav') {
		//moved onto the subnav
		return 'subnav';
	}
	else {
		return 'page';
	}
}

function toggle_nav() {
	$('#subnav').slideUp(slide_time, function() {
		$('#subnav').slideDown(slide_time);
	});
	
}

// function toggle_tab(section) {
// 	if(!$(section).attr('src').match('_over.gif')) {
// 		var src = $(section).attr('src').replace('.gif', '_over.gif');
// 		$(section).attr('src', src);
// 	}
// 	else {
// 		var src = $(section).attr('src').replace('_over.gif', '.gif');
// 		$(section).attr('src', src);
// 	}
// }

// function setup_nav1() {
// 	/*
// 		1.  If the current section has subtabs, show the subnav
// 		2.  Add the mouseover events for the tabs
// 			-show/hide rollover image
// 			-show/hide subnav
// 	*/
// 	
// 	//handle the default...
// 	current_section = current_page;
// 	if(typeof subnav[current_page] != 'undefined') {
// 		//we've got subtabs
// 		fill_subnav(current_page);
// 		$('#subnav').show();
// 	}
// 	
// 	$('#subnav').mouseover(function() {
// 		clearTimeout(timeout);
// 	});
// 	
// 	$('.mo').hover(
// 		function() {
// 			clearTimeout(timeout);
// 			if(!$(this).attr('src').match('_over.gif')) {
// 				var src = $(this).attr('src').replace('.gif', '_over.gif')
// 				$(this).attr('src', src);
// 			}
// 			
// 			var section = $(this).attr('id').split('.')[1];
// 			if(typeof subnav[section] != 'undefined') {
// 				if(section != current_section) {
// 					$('#subnav').slideUp(slide_time, function() {
// 						fill_subnav(section);
// 					});
// 					$('#subnav').slideDown(slide_time);
// 				}
// 			}
// 			else {
// 				$('#subnav').slideUp(slide_time);
// 			}
// 			current_section = section;
// 
// 		},
// 		function(e) {			
// 			//so we left one of the tabs.. where did we go?
// 			var new_target = e.relatedTarget;
// 			original_tab = this;
// 			
// 			if($(new_target).attr('id').match('navimage')) {
// 				//we moved onto another nav tab so we should turn the previous tab to inactive
// 				//if it's not the tab for the current page (that always stays active)
// 				if(current_section != current_page) {
// 					if($(this).attr('src').match('_over.gif')) {
// 						var src = $(this).attr('src').replace('_over.gif', '.gif');
// 						$(this).attr('src', src);
// 					}
// 				}
// 				if($(new_target).attr('id').split('.')[1] == current_page) {
// 					$(new_target).trigger('mouseover');
// 				}
// 			}
// 			else if($(new_target).attr('id') == 'subnav') {
// 				//we moved into the subnav bar
// 				clearTimeout(timeout);
// 				$('#subnav').mouseout(function(e1) {
// 					//we've moved into the subnav, and then left it..
// 					var new_target_from_navbar = e1.relatedTarget;
// 					
// 					//if we haven't moved onto a link
// 					if(!$(new_target_from_navbar).hasClass('navlink')) {
// 						//and we're not back on the subnav bar
// 						if($(new_target_from_navbar).attr('id') != 'subnav') {
// 							//if we've moved onto a tab
// 							if($(new_target_from_navbar).attr('id').match('navimage')) {
// 								//make sure it isn't the one we just came from
// 								if($(original_tab).attr('id') != $(new_target_from_navbar).attr('id')) {
// 									//turn the old image to off
// 									if($(original_tab).attr('src').match('_over.gif')) {
// 										var src = $(original_tab).attr('src').replace('_over.gif', '.gif');
// 										$(original_tab).attr('src', src);
// 									}
// 								}
// 							}
// 							else {
// 								//we've moved onto the page so reset the nav bar
// 								//if original tab isn't the default
// 								if($(original_tab).attr('id').split('.')[1] != current_page) {
// 										timeout = setTimeout("if($(original_tab).attr('src').match('_over.gif')) { var src = $(original_tab).attr('src').replace('_over.gif', '.gif'); $(original_tab).attr('src', src); } $('#subnav').slideUp(slide_time, function() { reset_nav(); });", 2000);
// 									// if($(original_tab).attr('src').match('_over.gif')) {
// 									// 									var src = $(original_tab).attr('src').replace('_over.gif', '.gif');
// 									// 									$(original_tab).attr('src', src);
// 									// 								}
// 									// 								$('#subnav').slideUp(slide_time, function() {
// 									// 									reset_nav();
// 									// 								});
// 								}
// 							}
// 						}
// 					}
// 				});
// 			}
// 			else {
// 				//we moved somewhere on the page so reset everything
// 				//if the original tab isn't the default
// 				if($(original_tab).attr('id').split('.')[1] != current_page) {
// 					if($(original_tab).attr('src').match('_over.gif')) {
// 						var src = $(original_tab).attr('src').replace('_over.gif', '.gif');
// 						$(original_tab).attr('src', src);
// 					}
// 					$('#subnav').slideUp(slide_time, function() {
// 						reset_nav();
// 					});
// 				}
// 			}
// 		}
// 	);
// }
