// JavaScript Document

 var timer;
 var speed;

$(document).ready(function(){
  	speed = 600; // increase this number to slow the menu down

	var hideall = function(){
		clearTimeout(timer);
	 	timer =setTimeout(function(){
				$("ul.mainnav_sub").hide();
				$("ul.mainnav_sub li div").hide();
		  }, speed);
	}


	$("ul.mainnav_sub li div").hide();

	// set width to widest elements
	 $('ul.mainnav_sub').each(function(){
			var maxwidth = 0;
			$(this).find('li a').each(function(){
				if($(this).width() > maxwidth) maxwidth = $(this).width();
			});
			$(this).find('li').width(maxwidth+5);
			$(this).find('li').each(function(){
		  		  $(this).find('div').css( {'left':$(this).width()+3} );
			});
	  });


	$("ul.mainnav_sub").hide();

   // hide nav when typing into search bar under neath dropdown
   // or any other form element
   $(":input").focus(function(){ hideall(); });
   $(":input").keydown(function(){ hideall(); });

   // set up popup links
   $("a.popuplink").each(function(){
      var divid = "#"+$(this).attr("id")+"_div";

      // show onclick
      $(this).click(function(){
         // move div
         offset = $(this).offset({ scroll: false });
         offset['top'] += $(this).height();
   	   offset['left'] -= 1;
    	   $(divid).css(offset);

         $(divid).show();
         return false; // disable link
      });

      $("#"+$(this).attr("id")+"_close").click(function(){
          $(divid).hide();
          return false; //disable link
      } );
   });

	// preload images
   $('#mainnav a img').each(function(){
         preloadImage = new Image;
         preloadImage.src = this.src.replace('.gif','_over.gif');
	});

	// move menus up
	 $('#mainnav a img').each(function(){

		  offset = $(this).offset({ scroll: false });
 		  offset['top'] += $(this).height();
 		  offset['left'] += 15;

 		  $("#"+$(this).parent().attr("id")+"_div").css(offset)

	  });

	// top level
   $('#mainnav a').hover(
						function(){
						 	clearTimeout(timer);
							$("ul.mainnav_sub").hide();
							$("ul.mainnav_sub li div").hide();

							$(this).find('img').each(function(){
                       // move menus again
               		  offset = $(this).offset({ scroll: false });
                		  offset['top'] += $(this).height();
                		  offset['left'] += 15;

                		  $("#"+$(this).parent().attr("id")+"_div").css(offset)

								if(this.src.indexOf("_over") == -1)
									this.src = this.src.replace('.gif','_over.gif');
							});

						 	$("#"+$(this).attr("id")+"_menu").show();
						},
						function(){
							$(this).find('img').each(function(){
								this.src = this.src.replace('_over.gif','.gif');
							});

            		 	hideall();
						}
					);


	// second level
   $('ul.mainnav_sub li').hover(
						function(){
						 	$(this).addClass("over");
							clearTimeout(timer);
				 			$("ul.mainnav_sub li div").hide();
							$(this).find('div').show();
						},
						function(){
            		 	$(this).removeClass("over");
            		 	hideall();
						}
					);
					
	// third level
	$("ul.mainnav_sub li div").hover(function(){clearTimeout(timer);},hideall	);
					
});

