function hideTopMenu() {
	jQuery("div.top_menu_sub").slideUp(100);
}

function hideMainMenu() {
	jQuery("div.menu_sub").slideUp(100);
	jQuery("img.menu_arrow").attr("src", "http://img.sparknotes.com/images/icon_arrow_off.gif");
	unHideTopAd();
}

jQuery(document).ready(function() {
	jQuery("#BlogModuleContainer").tabs(); /* Blog Module Tabs */
	jQuery("#BlogModuleCenterContainer").tabs({ selected: Math.floor(Math.random()*3) }); /* Home page Module Tabs */
	jQuery("#AllBlogsContainer").tabs(); /* All Blogs Module Tabs */
	jQuery("#BlogModuleContainer").tabs(); /* Blog Module Tabs */
	jQuery("#FaqModuleContainer").tabs(); /* FAQs Module Tabs */
	jQuery("#QuizModuleContainer").tabs(); /* Quiz Module Tabs */
	jQuery("#CollegeAdvisorContainer").tabs();
	jQuery("#TestPrepModuleContainer").tabs();
	fixAds();
	
	jQuery(":not(li.menu_head,li.menu_head *,div.menu_sub,div.menu_sub *)").hover(
		function() {
			hideMainMenu();
		}
	);
	jQuery(":not(div #topNav,div #topNav *)").hover(
		function() {
			hideTopMenu();
		}
	);

	jQuery("img.menu_arrow").hover(
		function() {
			jQuery(this).attr("src", "http://img.sparknotes.com/images/icon_arrow_on.gif");
		},
		function() {
		    selectedSubmenu = jQuery(this).parent().parent().find("div.menu_sub");
			if(selectedSubmenu.is(":hidden")) {
		        jQuery(this).attr("src", "http://img.sparknotes.com/images/icon_arrow_off.gif");
			}
		}
	);

	jQuery("a.topNavTab").hover(
		function() {			
			jQuery(this).find("img.top_menu_arrow").attr("src", "http://img.sparknotes.com/images/icon_arrow_black.gif");
		},
		function() {
			jQuery(this).find("img.top_menu_arrow").attr("src", "http://img.sparknotes.com/images/icon_arrow_white.gif");
		}
	);

	jQuery("img.menu_arrow").click(
		function() {
			selectedSubmenu = jQuery(this).parent().parent().find("div.menu_sub");
			if(selectedSubmenu.is(":hidden")) {
				selectedSubmenu.slideDown(100);
				jQuery(this).attr("src", "http://img.sparknotes.com/images/icon_arrow_on.gif");
			} else {
				selectedSubmenu.slideUp(100);					
				jQuery(this).attr("src", "http://img.sparknotes.com/images/icon_arrow_off.gif");

			}
			jQuery("div.menu_sub").not(selectedSubmenu).slideUp(100);
			jQuery("img.menu_arrow").not(jQuery(this)).attr("src", "http://img.sparknotes.com/images/icon_arrow_off.gif");
			hideTopMenu();
			hideTopAd(); //so flash wont stomp on menu
			return false;
		}		
	);

	jQuery("li.top_menu_head").click(
		function() {
			selectedSubmenu = jQuery(this).find("div.top_menu_sub");
			if(selectedSubmenu.is(":hidden")) {
				selectedSubmenu.slideDown(100);
			} else {
				selectedSubmenu.slideUp(100);					
			}
			jQuery("div.top_menu_sub").not(selectedSubmenu).slideUp(100);				
			hideMainMenu();
			return true;
		}		
	);

	jQuery("input.search_field").focus(function () {
		jQuery("div.searchbg").css({ "background" : "url(http://img.sparknotes.com/images/search_box_on.gif) no-repeat 0 0" });
   	});
	
	jQuery("input.search_field").blur(function () {
	    if(jQuery("input.search_field").attr("value") == "") {
			jQuery("div.searchbg").css({ "background" : "url(http://img.sparknotes.com/images/search_box_off.gif) no-repeat 0 0" });
		}			
	});
	
	jQuery("input.search_field2").focus(function () {
    	if(jQuery("input.search_field2").attr("value") == "Search SparkLife") {
			jQuery("input.search_field2").attr("value", "");
		}			
   	});
	
	jQuery("input.search_field2").blur(function () {
	    if(jQuery("input.search_field2").attr("value") == "") {
			jQuery("input.search_field2").attr("value", "Search SparkLife");
		}			
	});
});

function hideTopAd()
{
	jQuery("div.adTarget").css({'z-index' : '0'});
}

function unHideTopAd()
{
	//jQuery("div.adTarget").css({'z-index' : '1000'}); we don't need to be nice to them cuz they aren't nice to us. dont give back z-index
}

function fixAds()
{
	//adds a class to rude ad units so we can easily grab them
	var containers = jQuery("#topAd div");
	for(i=0;i<containers.length;i++)
	{
		target = jQuery(containers[i]);
		if(target.css('z-index') > 6000)
		{
			target.addClass("adTarget");
		}
	}
	hideTopAd(); //revoke z-index right away.
}

