$(document).ready(function(){
	$('li:first-child').addClass('first');
	$('li:last-child').addClass('last');
	
	$("div.dropdown").parent().append("<span></span>");
	
	$('#main_menu > ul > li').hover(
		function(){
			$('#main_menu > ul > li').removeClass('hover');
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	
	$('#header h1').click(function() {
		window.location = 'index.php';
	});
	
	var divIndex = 1000;
	$('div').each(function(){
		
		$(this).css('zIndex', divIndex--);
	
	});
	
	$('#search #search_submit').attr('value', '');
	
	$("#main_menu > ul > li > a").each(function(){
		if ($(this).attr('href') == '#') {
			$(this).attr('href', 'javascript:;');
		}
	});
	
	$("#main_menu > ul > li").click(function() { //When trigger is clicked...
		var w = $(this).find("div.dropdown").outerWidth();
		var p = $(this).position();
		var ow = $("#container").outerWidth();
		
		if (p.left + w > ow) {
	 		var np = ((p.left + w) - ow) * -1;
	 		
	 		$(this).find("div.dropdown").css("left", np);
		}
		

		$(this).addClass('hover');
		
		
		//Following events are applied to the subnav itself (moving subnav up and down)  
		$(this).find("div.dropdown").slideDown('fast').show(); //Drop down the subnav on click  
		
		
		$(this).hover(function() {  
		}, function(){  
			$(this).find("div.dropdown").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up, with delay 
			$(this).removeClass('hover');
		});
		  
	});  

});
