$(document).ready(function () {
	preload();
	// Nav RO's
	$(function(){
		try{
			$("img.mo,input.mo").mouseover(function(){
				if(!this.src.match(/-over/)){
					var dot = (this.src.lastIndexOf('.'));
					this.src = this.src.substr(0, dot)+'-over.'+this.src.substr((dot+1));
				}
			})
			.mouseout(function(){
				if(this.src.match(/-over/)) this.src = this.src.replace("-over","");
			});
			$("img.om").mouseover(function(){
				if(this.src.match(/-over/)){
				this.src = this.src.replace("-over","");
				}
			})
			.mouseout(function(){
				var dot = (this.src.lastIndexOf('.'));
				if(!this.src.match(/-over/)) this.src = this.src.substr(0, dot)+'-over.'+this.src.substr((dot+1));
			});
			//firefox retains over state when back button is pressed
		if($.browser.mozilla){
				$("img.mo,input.mo").click(function(){
					this.src = this.src.replace("-over","");
				});
				$("img.om").click(function(){
					this.src = this.src.replace("-over","");
				});
		}
		}catch(err){
			alert('caught jquery '+err);
		}
	});

	// Dropdown Menus
	$('#nav a').mouseenter(function() {			
		menuId = $(this).parent().attr("id");
		navRO(menuId);
	}).mouseleave(function() {
		$(this).removeClass('a_over');
		$(this).next().removeClass('dropdown_active');
	});
	$('#nav .dropdown').mouseenter(function() {
		menuId = $(this).parent().attr("id");
		navRO(menuId);
	}).mouseleave(function() {
		$(this).removeClass('dropdown_active');
		$(this).prev().removeClass('a_over');
		$(this).prev().children().attr("src", function() {
					this.src = this.src.replace("-over","");
		});		
	});
	
});

var base = "images/nav/";

function preload() {
	var the_images = new Array('home-over.gif','about-over.gif','tutoring-over.gif','winter-over.gif','testprep-over.gif','summer-over.gif','news-over.gif');
	preload_images(the_images);
}

function preload_images(the_images_array) {
	for(loop = 0; loop < the_images_array.length; loop++)  { var an_image = new Image(); an_image.src = base + the_images_array[loop]; } 
}

function navRO(menuId) {
	var current_a = $("#" + menuId + " a");
	var current_img = $("#" + menuId + " a img");
	current_a.addClass('a_over'); 
	current_img.attr("src", function() {
		if(!this.src.match(/-over/)){
			var dot = (this.src.lastIndexOf('.'));
			this.src = this.src.substr(0, dot)+'-over.'+this.src.substr((dot+1));
		}			
	});

	var $dropdown = $("#"+ menuId +"_drop");
	if ($dropdown.length) {
		if (!($dropdown.hasClass('dropdown_active'))) {
			$dropdown.addClass('dropdown_active');
		}
	}
};

