jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function() {
						   
	$.preloadImages("/images/nav_home_over.png","/images/nav_about_over.png","/images/nav_services_over.png","/images/nav_events_over.png","/images/nav_news_over.png");

	$("#navBGMap area").each(function() {
							  
		$(this).hover(
			function() {
				var id = $(this).attr("id");

				$("#" + id + "Link").fadeIn(400);
			},
			
			function() { return; }
		);
	});

	$("#nav a").each(function() {
							  
		$(this).hover(
			function() { return; },
			
			function() { 
				$(this).fadeOut(400);
			}
		);
	});
	
	
	initCustomSelects();
});

function initCustomSelects() {

	$("select.changeMe").css('display','none');
	$("select.changeMe").each(function(){
		
		var curSel = $(this);
		// get the CSS width from the original select box
		var gddWidth = $(curSel).css('width');
		var gddWidthL = gddWidth.slice(0,-2);
		var gddWidth2 = gddWidthL - 28;
		var gddWidth3 = gddWidthL - 16;
		var gddWidth2 = gddWidthL - 28;
		var gddWidth3 = gddWidthL - 16;

	// build the new div structure
		var gddTop = '<div id="custom_' + curSel.attr("id") + '" style="width:' + gddWidthL + 'px" class="selectME" tabindex="0">';
		//get the default selected option
		var whatSelected = $(curSel).children('option:selected').text();
		//write the default
		var gddFirst = '<div class="first"><span class="selectME gselected" style="width:'+ gddWidthL +  'px;">'+ whatSelected +'</span><span id="arrowImg"></span><div class="clears"></div></div><ul class="selectME">';
		// create a new array of div options from the original's options
		var addItems = new Array();      
		$(curSel).children('option').each( function() {           
			var text = $(this).text();  
			var selVal = $(this).attr('value'); 
//			var before =  '<li style="width:' + gddWidthL + 'px;"><a href="#" rel="' + selVal + '" tabindex="0"  style="width:' + gddWidth3 + 'px;">';
			var before =  '<li ><a href="#" rel="' + selVal + '" tabindex="0">';
			var after = '</a></li>';           
			addItems.push(before + text + after);
		});
		//hide the default from the list of options 
		var removeFirst = addItems.shift();
		// create the end of the div selectbox and close everything off
		var gddBottom ='</ul></div>'
		//write everything after each selectbox
		var GDD = gddTop + gddFirst + addItems.join('') + gddBottom;
		$(curSel).after(GDD);
		//this var selects the div select box directly after each of the origials
		var nGDD = $(curSel).next('div.selectME');
		
		
		$(nGDD).find('li:first').addClass("first");
		
		$(nGDD).find('li:last').addClass('last');
		//handle the on click functions - push results back to old text box
		$(nGDD).click( function(e) {
			var myTarA = $(e.target).attr('rel');
			var myTarT = $(e.target).text();
			var myTar = $(e.target);
			 
			 //if closed, then open
			if( $(nGDD).find('li').css('display') == 'none') {
				//this next line closes any other selectboxes that might be open
				$('div.selectME').find('li').css('display','none');
				$(nGDD).find('li').css('display','block');
				
				//if user clicks off of the div select box, then shut the whole thing down
				$(document.window || 'body').click( function(f) {
					var myTar2 = $(f.target);
					if (myTar2 !== nGDD) {
						$(nGDD).find('li').css('display','none');
					}
				});
				return false;

			} else {      
				if (myTarA == null){
					$(nGDD).find('li').css('display','none');
					return false;
				} else {
					//set the value of the old select box
					$(curSel).val(myTarA);
					//set the text of the new one
					 $(nGDD).find('span.gselected').text(myTarT);
					 $(nGDD).find('li').css('display','none');
					 return false;
				}
			}

		//handle the tab index functions
		}).focus( function(e) {
			$(nGDD).addClass("activeSelect");
			
			$(nGDD).find('li:first').addClass('currentDD');
			$(nGDD).find('li:last').addClass('lastDD');
			
			function checkKey(e){
				//on keypress handle functions
				function moveDown() {
					var current = $(nGDD).find('.currentDD:first');
					var next = $(nGDD).find('.currentDD').next();
					if ($(current).is('.lastDD')){
					return false;
					} else {
						$(next).addClass('currentDD');
						$(current).removeClass('currentDD');
					}
				}
				
				function moveUp() {
					var current = $(nGDD).find('.currentDD:first');
					var prev = $(nGDD).find('.currentDD').prev();
					if ($(current).is('.first')){
					return false;
					} else {
						$(prev).addClass('currentDD');
						$(current).removeClass('currentDD');
					}
				}
				var curText = $(nGDD).find('.currentDD:first').text();
				var curVal = $(nGDD).find('.currentDD:first a').attr('rel');
				
				switch (e.keyCode) {
					case 40:
						$(curSel).val(curVal);
						$(nGDD).find('span.gselected').text(curText);
						moveDown();
						return false;
						break;
					case 38:
						$(curSel).val(curVal);
						$(nGDD).find('span.gselected').text(curText);
						moveUp();
						return false;
						break;
					case 13:
						$(nGDD).find('li').css('display','none');
				}     
			}
			
			$(document).keydown(checkKey);
			
		}).blur( function() {
			$(document).unbind('keydown');
			$(nGDD).removeClass("activeSelect");

		});
		
		newWidth = 0;
		$(nGDD).find("li").each(function() {
			if($(this).innerWidth() > newWidth) {
				newWidth = $(this).innerWidth();	
			}
		});

//		alert("width: " + newWidth);
		newWidth = (newWidth >= 248) ? newWidth : 248;
		
		//alert("width: " + newWidth);
		$(nGDD).find("li").css("width",newWidth + "px");
		$(nGDD).find("a").css("width",(newWidth - 16) + "px");
		
		//////////////////////////
		//
		// IE does not like this - it reports back newWidth as NAN.  Goddammit...
		//
		////////////////////////////
		
//		alert("width: " + newWidth);
//		$(nGDD).find("a").css("width","30px");
		
	});
}
