$(window).load(function() {
	
	// site url variable
	var site_url = $('#pattern').attr('class');
	
	// Easily selectable test within forms
	$('input, textarea').click(function(){ $(this).select(); });
	
	// Remove border from the last link within a subnav
	$('#nav li ul li:last-child a').addClass('last');
	
	// Nav dropdown
	$('#nav li').hover(
		function() {
			$(this).children('ul').stop(true, true).slideDown('fast');
			$(this).children('ul').children('li').children('ul').hide();
			$('#nav li ul li ul li ul').hide();
	},
		function(){
			$(this).children('ul').stop(true, true).hide();
	});
	
	// add arrows to parts with children
    $('#nav li ul li').each(function() {
    	$(this).has('ul').children('a').append(' <img src="'+site_url+'public/images/nav_arrow.png" />');
	});

	// add arrows to parts with children subnav
    $('#subnav li').each(function() {
    	$(this).has('ul').children('a').append(' <img src="'+site_url+'public/images/nav_arrow.png" />');
	});	

	// add arrows to parts with children subnav
    $('#subnav li.current').each(function() {
    	$(this).has('ul').children('a').children('img').addClass('hide');
    	$(this).has('ul').children('a').append(' <img src="'+site_url+'public/images/down_arrow.png" style="margin-top:6px" />');
    	
	});	
		
	// sidebar length logic
	$('.node').each(function(){ 
		var content_length = $('#content').height();
   		var sidebar_length = $('#sidebar').height();
		if (sidebar_length > content_length) {
			$(this).addClass('hide');
		}
	});
	
	// position the last three nav links differently
	var nav_btns = $('#nav > li').length;
	var nav_btns_half = nav_btns / 2 + 1;
	$('#nav > li').children('ul').slice(nav_btns_half, nav_btns).css({ 'right' : '0px' });
	
	// opacity hover to all images located in links
	if ( $.browser.msie ) {
 	
 	} else { 	
		$('a img').hover(
		function(){
			$(this).stop(true, true).animate({ opacity : .7 }, 250);
		}, 
		function(){
			$(this).stop(true, true).animate({ opacity : 1 }, 250);
		});
 	}
	
	// altternate rows
	$("tr:nth-child(odd)").addClass("odd");
	
	// other fields 
	$('select').change(function() {
		var select_value = $(this).val();
		if (select_value == 'Other') {
			$(this).parent().parent().next('tr').show();
		}
	});
	
	$('a.event').click(function(){
		var event_url = $(this).attr('href');
		$('body').append('<div class="overlay"></div>');
		$('body').append('<div class="event_information"></div>');
		$('.overlay').animate({ opacity : .8 }).fadeIn();
		
		$.ajax({
		  url: event_url,
		  success: function(msg){
		    $('.event_information').html(msg);
		  }
		});
		
		$('.event_information').fadeIn();
		var event_info = $('.event_information').height();
				
		$('a#close').live('click', function() { 
			$('.event_information').fadeOut().remove();
			$('.overlay').fadeOut().remove();
			return false;
		});
		
		return false;
	});
	
});
