jQuery.noConflict();
jQuery(document).ready(function($){

//$("a[href='/']").attr('href', 'http://area203.com/naca');
	
	$("#main_menu ul li a").removeAttr("href").css("cursor", "pointer");
	
	var todaysDate = $(".invisibleDate").text()
	$("#dateHolder").append(todaysDate);
								
	$(document).pngFix(); 
	
	try {
		$('#home_announcements').feature({
			'mode': 'fade'
		});
	} catch (e) {}

    $("ul").each(function() {
        $(this).find("li:first").addClass('li_first');
        $(this).find("li:last").addClass('li_last');
        $(this).find("li:even").addClass('li_even');
        $(this).find("li:odd").addClass('li_odd');
    });
    
    $("tr:even").addClass('row_even');
    $("tr:odd").addClass('row_odd');
    
    $(".breadcrumb a:first").addClass('breadcrumb_first');
    $(".breadcrumb a:last").addClass('breadcrumb_last');
    
    $("#home_contact").inputclear();
    $(".round_corners").roundcorners();
    $(".peekaboo_list").peekaboolist();
    
    $('#naca_logo').click(function(){
    	window.location = '/';
    })
    $('#home_schedule li').click(function() {
		window.location = $('.complete_schedule_link').attr('href');	
	});

    
    $('#content_container:not(.no_sidebar) .content_inner').before('<img src="themes/naca/images/content_header.png" alt="" width="648" height="17" />');
    
    // Massage the Silverstrip form template for the homepage contact
    $('#home_contact div:has(label)').each(function() {
    	var my_value = $(this).find('label').text();
    	$(this).find('input').val(my_value);
    	$(this).find('textarea').val(my_value);
    });
	$('#home_contact .textarea').insertAfter($('#home_contact fieldset'));
	

	$('#home_schedule li').hover(
		function() {
			$(this).addClass('schedule_listing_selected');
		},
		function() {
			$(this).removeClass('schedule_listing_selected');
		}
	
	)
	
	$('#naca_network > ul > li strong').click(function() {
			$(this).parent().find('ul').slideToggle('fast');
			if ($(this).hasClass('naca_network_expanded')) {
				$(this).removeClass('naca_network_expanded')
			} else {
				$(this).addClass('naca_network_expanded')
			}
	})
	
	$('#header_search form').append('<a href="/" class="header_search_submit_image tir" title="Search">Go</a>');
	$('#header_search a').click(function(){ $('#header_search form').submit(); })
	
	$('#home_contact form').append('<a class="contact_send tir" title="Send" style="cursor:pointer;">Send</a>');
	// The above line is the one that was giving us trouble. Removed the href and styled it with the pointer cursor and everything seems to be working ok now.
	$('#home_contact a').click(function(){ $('#home_contact form').submit(); })
	  
    /**************************
    * SUB MENU HANDLING
    ***************************/
    
    /* Populate empty sub menu items to have equal heights */
    var max_items = 0;
    $("#main_sub_menu > ul > li > ul").each(function(i) {
    	var count = $(this).find('li').length;
    	// Get the highest number of items in a sub menu group
		if (count > max_items) {
			max_items = count;
		}
		
		// On last item
		if ((i+1) == $("#main_sub_menu > ul > li").length) {
			// Iterate through sub menu again
			$("#main_sub_menu > ul > li > ul").each(function() {
				// Find out how many empty items to add
				var items_to_add = max_items - $(this).find('li').length;
				for(x=0;x < items_to_add; x++) {
					// Add the empty items
					$(this).append('<li class="empty">&nbsp;</li>');
				}
			})
		}
    });
    
    /* Show the sub menu when a main menu item is clicked */
    $("#main_menu a").each(function() {
		// Get the menu items text
		var title = $(this).text(); 
    		
		// Find x pos of this menu item
		pos = $(this).position();
		
		$("#main_sub_menu > ul > li").each(function() {
			// Menu item text should be the same as the strong label in the sub menu
			var sub_title = $(this).find('strong:first').text();
			
   			if (title == sub_title) {
   				$(this).css('paddingLeft', pos.left - 4);
   			}
		})
		
		
    	$(this).click(function() {		
    		
    		$("#main_sub_menu > ul > li").each(function() {
    			// Menu item text should be the same as the strong label in the sub menu
    			var sub_title = $(this).find('strong:first').text();

    			// Hide all the sub menu groups
    			$(this).hide();
				    			
				// If the titles match, show the sub menu container and sub menu group
    			if (title == sub_title) {
    				$(this).show();
    				$(this).parent().parent().slideToggle('fast');
    			}
				
    		}) 
    		
    		return false;   	
    	})    	
    });
    
    /* Hide sub menu when anything but a menu item is clicked */
    $("body").bind("click", function(e) {
    	var $target = $(e.target);
    	
    	// Don't hide the menu if a menu item was clicked
    	if (!$target.is('#main_sub_menu a')) {
	    	$('#main_sub_menu').slideUp('fast');
	    }
    });
    
  $("#header").mouseleave(function() {
	$('#main_sub_menu').slideUp('fast');
});
    
    /**************************
    * END SUB MENU HANDLING
    ***************************/
}); // ready

