/**
 * @author agerard
 */
 
    var name = "#floatMenu";
	var name2 = "#mainMenu";
    var menuYloc = null;
	var offset = 0;
	var onPg = false;
	var anchr = false;
	//var home = false;
	
	$(document).ready(function(){
		$("ul.topList_no").each(function(i){
			$(this).removeClass("topList_no");
			$(this).addClass("topList");
			
			// work on this...
			$(this).children().children().filter("a[@class=top]").each(function(e){
				$(this).css({
				"text-decoration" : "none",
				"cursor" : "inherit"
				}).click(function(e) {
				e.preventDefault();
				this.blur();
				return false;
				});
			});
		});

		// apply menu arrow styles		
		$("img[@src=images/arrow.gif]").addClass("arrow");
		$(".menuList > li > .subMenu").find("span").addClass("arrowhere");
				
		// hide everything that should be hidden
		$(".detailWin").hide();
		$(".topList > li > ul").hide();
		$(".topList > li ").filter("li[@class!=top]").hide();
		
		$(".home").find(".menuList > li > .subMenu").show();
		
		$(".noScrTopMenu").hide();
		//alert('content: ' + $("#content").height() + ' menu: ' + $("#mainMenu").height()/3);
		// can't figure out why this layout thinks the menu is 3 times as long as it really is....
		if($("#content").height() > 700 && $("#content").height() > $("#mainMenu").height()/3) $("#floatMenu").show();

		// if a menu item has children, add a plus		
		// with list style,
		$(".menuList > li ").not($(".subMenu")).css({"list-style-image": "url(images/blank.gif)","left":"10px"});
		$(".subMenu").parent(".menuList > li").not($(".subMenu")).css({ 
			"list-style-image": "url(images/plus.gif)","left":"10px"});
		
		// or image
		//alert($(".subMenu").parent(".menuList > li > a").children().not($(".subMenu")).attr("title"));
		//$(".subMenu").parent(".menuList > li > a").children().not($(".subMenu")).append("<img src='images/closed.gif' alt='' />");


		// for all page links, add click function to chg class and italicize if clicked
		$(".subMenu > li > a[href*=#]").click(function(e){
			//alert($(this).attr("name"));
			$(this).addClass("here").parent("li").addClass("here").parent().parent().find("a:first").addClass("here");
			$(this).blur().parent().siblings("li").removeClass("here").find("a").removeClass("here");		
		});	

		// check thru each menu link and	
		$("a").each(function(i){
			// set up menu behavior
			setMenu($(this),chkPg($(this)));
			
			//and add class/style for menu item(s) matching page
			//alert(chkPg($(this)));
			if(chkPg($(this))){
				var tag = $(this)
				// alert('one:' + $(this).parent().attr("class"));
				if(tag.parent().attr("class") == "subMenu"){
					tag.addClass("here").parent().siblings("li").removeClass("here").find("a").removeClass("here");
					tag.parent().parent().parent().find("a:first").addClass("here");	
				}
				
				if(tag.parent().attr("class") == "menuList"){
					//alert('got here main');
					tag.parent().addClass("here").end().addClass("here");
					}
				if(tag.attr("class") == "here")tag.click(function(e) {
					e.preventDefault();
					this.blur();
					});
			}

		});

		//$(".subMenu > a").not(".here").parent("li").hide("fast"); // alternate - less commotion?

		// show subMenu and italicize parent on click (IF not already open!)
 		 $(".menuList > li > a").filter(":has(~ ul)").click(function(e)
		 {
		    if($(this).siblings().css("display")=="block"){
			$(this).parent().css({"list-style-image": "url(images/plus.gif)","left":"10px"}).end().siblings().hide("fast");
			this.blur();
			return false;
			}
		   if($(this).siblings().css("display")=="none"){			
				var anchrElemPar = $(this).parent();
				xpand($(this).parent());			
				anchrElemPar.find(".subMenu").find("li").addClass("subMenu");
				$(this).blur();
			return false;
			}
	   	  });

		// show  all topmenu items on hover
 		 $("div").filter("[@class=one]").hover(function(e)
		 {
 		   if ($.browser.msie)$(this).find("ul > li").show("normal");
		   if (!$.browser.msie)$(this).find("ul > li").slideDown("slow");
		   //$(this).find("ul > li.top").blur();
	  		
		 }, function(){	
		   		if ($.browser.msie)$(this).find("ul > li").filter("[@class!=top]").hide("normal");
				if (!$.browser.msie)$(this).find("ul > li").filter("[@class!=top]").slideUp("normal");
			}).stop();
		 
		
		// for scrolling 'top' link
        $(window).scroll(function () {
			menuYloc =  $(name2).height();
			if($(document).scrollTop() < menuYloc) offset = 0+"px";
			if($(document).scrollTop()/menuYloc > 1) {
				offset = $(document).scrollTop()-$(name2).height()+"px";
				}
	            $(name).animate({top:offset, opacity: 1},{duration:730,queue:false});
	     });			
			
	});

	
//FUNCTIONS
	
	function setMenu(tag, bool){
		// if this is href for page we're on, disable and italicize
		if (bool){
			// and show/expand submenu (if any)
			if(tag.parent().parent().is(".subMenu")){	
			//alert('got here 1');			
				var anchrElemPar = tag.parent().parent().parent();
				xpand(tag.parent().parent().parent());				
					
				// if we're on this anchor	
				if(anchr == true){
					//alert('got here 2');
					//alert(anchrElemPar.find(".subMenu").find("li").attr("class"));
					tag.addClass("here");
					tag.click(function(e) {
					e.preventDefault();
					this.blur();
					});
				return false;
				}
				//italicize the link and make sure siblings aren't.'
				//alert('got here 3');	
				anchrElemPar.find(".subMenu").find("li").addClass("here").find("a").addClass("here").end().siblings().removeClass("here").find("a").removeClass("here");
			}				
			
			return false;
			}
			//alert('got here 4');	
			tag.removeClass("here");
		}
		
	function chkPg(tag){
		onPg = false;
		if(tag.attr("href")!= null && tag.attr("href")== document.URL.slice(document.URL.lastIndexOf("/")+1 ,document.URL.length)){
			onPg = true;
			//alert(onPg);
			anchr = false;
			if(tag.attr("href").indexOf("#")> -1)anchr = true;
			// alert(anchr);
		}
//make sure there aren't duplicate filenames in subdirectories since this only looks at filename
		if(tag.attr("href")!= null && tag.attr("href").slice(tag.attr("href").lastIndexOf("/")+1 ,tag.attr("href").length)== document.URL.slice(document.URL.lastIndexOf("/")+1 ,document.URL.length)){
			onPg = true;
			//alert(onPg);
			anchr = false;
			if(tag.attr("href").indexOf("#")> -1)anchr = true;
			// alert(anchr);
		}
		return onPg;
	}
	
	function chgMenuStyle(tag){
		alert('three:' + tag.attr("class"));
		tag.addClass("here").find("a").addClass("here");
		tag.siblings().removeClass("here").find("a").removeClass("here");
		return;
	}
	
	
	function xpand(anchrElemPar){
		anchrElemPar.find(".subMenu").show("slow").parent().css({ 
			"list-style-image": "url(images/minus.gif)",
			"left":"10px" // and remove style from submenu list items for IE!
			}).find(".subMenu").find("li").css({
					"list-style-type": "none",
					"text-decoration": "none"
					//"list-style-image": "none"				
			});
		if ($.browser.msie)anchrElemPar.find(".subMenu").find("li").css("list-style-image", "none");
		return anchrElemPar;
	}


	