/*
American Museum of Natural History
Richard Gilder Graduate School
Site-wide JavaScript include
Created: 29 Aug 2007 by Pat Collins <pat@walltowall.com>
Wall-to-Wall Studios, Inc.
*/

$(function(){
	$("#home-promo-flash").wrap('<div id="home-promo-flash-wrap"></div>');
	$("#home-content").append('<div class="clear"></div>');
	$("#footer").prepend('<div id="footer-left"></div><div id="footer-right"></div>');
	$(".offsite").each(function(){this.setAttribute("target", "_blank")});
	$("#interior #container").append('<div class="clear"></div>').after('<div class="clear"></div>');
	
	$("#flashMessage").each(function(){
		setTimeout(function(){
			$("#flashMessage").fadeOut("slow");
		}, 6000);
	});
	
	var totalMenus = $("#menu").children("li").children("a").length;
	
	textBoxLabelFromValue($("#ProfessorName"));
	
	$("#menu").children("li").children("a").hover(function(){
		// link mouseover
		var id = $(this).parent().get(0).id;
		if(!($(this).parent().get(0).className.indexOf('first') > -1 && $(this).get(0).className.indexOf('current') > -1)){
			$(this).add($(this).parent().get(0)).addClass("hover");
			if($.browser.msie) {
				$("select").css("visibility", "hidden");
			}
			$('#'+id+'_submenu').show();
		}
	},function(){
		// link mouseout
		var id = $(this).parent().get(0).id;
		if(!($(this).parent().get(0).className.indexOf('first') > -1 && $(this).get(0).className.indexOf('current') > -1)){
			$(this).add($(this).parent().get(0)).removeClass("hover");
			if($.browser.msie) {
				$("select").css("visibility", "visible");
			}
			$('#'+id+'_submenu').hide();
		}
	}).end().each(function(menuIndex){
		// alert(index);
		var id = this.id;
		// taking the id of the links, finding the divs, and attaching behavior.
		$('#'+id+'_submenu').hover(function(){
			$('#'+id).add($('#'+id).children().get(0)).addClass("hover");
			if($.browser.msie) {
				$("select").css("visibility", "hidden");
			}
			$(this).show();
		},function(){
			$('#'+id).add($('#'+id).children().get(0)).removeClass("hover");
			if($.browser.msie) {
				$("select").css("visibility", "visible");
			}
			$(this).hide();
		}).mousemove(function(){
			$('#'+id).add($('#'+id).children().get(0)).addClass("hover");
			if($.browser.msie) {
				$("select").css("visibility", "hidden");
			}
			$(this).show();
		}).each(function(){
			// set the absolute left position for each dropdown submenu.
			var id = this.id.split('_submenu')[0];
			$(this).css("left", $('#'+id).children("a").position().left+'px');
			if(menuIndex == totalMenus-1) {
				var offset = 236 - $("#"+id).outerWidth() + 1;
				$(this).css("left", ($('#'+id).children("a").position().left - offset)+'px');
			}
		});
	});
	
	var hC = $("#header_callout");
	var hCchildrenCount = hC.children("li").length;
	var hCInterval;
	var hCI_i = 0;
	hC.each(function(){
		hC.children("li").hide().end().hide();
		hC.children("li:eq("+hCI_i+")").show().end().show();
		hCInterval = setInterval(function(){
			hCI_i++;
			if(hCI_i == hCchildrenCount) {
				hCI_i=0;
			}
			hC.children("li").hide().end().hide();
			hC.children("li:eq("+hCI_i+")").show().end().fadeIn(1000);
		}, 10000);
	});
	
	$(".checkbox_div").each(function(){
		var id = '#'+this.id.split("_div")[0];
		$(id).click(function(){
			$(id+'_div').slideToggle();
		});
	});
	
	$("#professors_table tr").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	}).children("td.name").click(function(){
		location.href = $(this).children("a").attr("href");
	});
	
	$("#search_back_link").click(function(){
		history.go(-1);
		return false;
	});
	
	$("a").filter(function(){
		if(typeof($(this).attr("href")) != 'undefined') {
			return $(this).attr("href").substring(0,1) == '#';
		}
		return false;
	}).click(function(){
		// console.log($(this).attr("href"));
		var name = $(this).attr("href").split('#')[1];
		var h5 = $("a[@name="+name+"]").next("h5");
		h5.addClass("fade");
		setTimeout(function(){
			h5.removeClass("fade");
		}, 2500);
		// console.log(name);
		return true;
	});
	
	
	// $(".modules").append('<div class="clear"></div>').after('<div class="clear"></div>');
	
	// $("select.alt").children("option").each(function(){
	// 	$(this).wrap('<div class="option-alt"></div>').parent('div.option-alt').each(function(){
	// 		if(typeof($(this).children("option").attr("id")) != 'undefined') {
	// 			$(this).attr("id", $(this).children("option").attr("id"));
	// 		}
	// 		if(typeof($(this).children("option").attr("class")) != 'undefined') {
	// 			$(this).attr("class", $(this).attr("class") + ' ' + $(this).children("option").attr("id"));
	// 		}
	// 		if(typeof($(this).children("option").attr("style")) != 'undefined') {
	// 			$(this).attr("style", $(this).children("option").attr("style"));
	// 		}
	// 		if(typeof($(this).children("option").attr("label")) != 'undefined') {
	// 			$(this).attr("label", $(this).children("option").attr("label"));
	// 		}
	// 		// console.log($(this).children("option").attr("selected"));
	// 		if( typeof($(this).children("option").attr("selected")) != 'undefined' ) {
	// 			var selected = $(this).children("option").attr("selected") ? true : false;
	// 			console.log(selected);
	// 		}
	// 		if( $(this).children("option").attr("selected") ) {
	// 			$(this).addClass("selected");
	// 		}
	// 		if(typeof($(this).children("option").attr("value")) != 'undefined') {
	// 			$(this).attr("title", $(this).children("option").attr("value"));		// VALUE goes into TITLE
	// 		}
	// 		if(typeof($(this).children("option").text()) != '') {
	// 			$(this).text( $(this).children("option").text() );
	// 		}
	// 	}).end().remove();
	// }).end().wrap('<div class="select-alt"></div>').parent('div.select-alt').each(function(){
	// 	$(this).html( $(this).children("select.alt").html() );
	// }).end().remove();
});

function highlightCurrentSection(section) {
	var link = "#menu_"+section;
	$(link).children("a").addClass("current");
}

function textBoxLabelFromValue(jQueryNode) {
	jQueryNode.each(function(){
		if($(this).get(0).nodeName == "INPUT" && ($(this).attr("type") == "text") && $(this).val() != ""){
			var startText = $(this).val();
			$(this).focus(function(){
				$(this).addClass("focus");
				if($(this).val() == startText){
					$(this).val("");
				}
			}).blur(function(){
				if($(this).val() == ""){
					$(this).removeClass("focus");
					$(this).val(startText);
				}
			}).val(startText).attr("title", startText);
		}
		else {
			return false;
		}
	});
}