$(function(){

	// Nav-main dropdown buttons
	$('#nav-main ul li ul').each(function () {
	    $(this).find('li').slice(0,2).addClass('nav_highlight');
	    $(this).find('ul li').unwrap();
	    $(this).find('li.nav_highlight').wrapAll('<ul class="nav_highlight_buttons"></ul>');
	    $(this).children('li').wrapAll('<ul class="other_nav"></ul>');
		/*
		for(var i = 0; i < $('.other li').length; i+=3) {
			$('.other li').slice(i,i+3).wrapAll('<ul class="other_nav"></ul>');
		}
		$('.other .other_nav:eq(1)').unwrap();
	    */
		$(this).find('ul').unwrap();
	});	
	$(".sc blockquote").append('<span class="scbqbot"></span>');
	$('body.landing .pc .lead-in:eq(0)').closest('p').css('margin-top','1.5em');
	
	
	// Why did I do this? What was Alex thinking!?
	// Scroll allllllll the way down to the bottom or
	// search the function name. It fixes AJAX stuff!
	function setSubNav() {
		// PC Nav-Sub Modifications
		if(!$('body.landing').length) {
			var navItems = $('#primary-content #nav-sub ul li').length / 2;
			$('#primary-content #nav-sub ul')
				.addClass('nav_left')
				.after('<ul class="nav_right"></ul>')
				.find('li')
					.slice(-1 * navItems)
					.appendTo('.nav_right');
		} else { // landing	
			var navItems = $('#primary-content #nav-sub ul li').length / 3;
			$('#primary-content #nav-sub ul')
				.addClass('nav_left')
				.after('<ul class="nav_mid"></ul><ul class="nav_right"></ul>')
					.find('li')
						.slice(Math.ceil(navItems))
						.appendTo('.nav_mid');
			$('#primary-content #nav-sub ul.nav_mid li')
				.slice(-1 * navItems)
				.appendTo('.nav_right');
		}
	
		$('#primary-content #nav-sub ul li')
			.removeClass('first-child last-child')
			.parent() // ul
				.find(':first-child')
					.addClass('first-child')
		.end()
			.parent()
				.find(':last-child')
					.addClass('last-child');
		$('#primary-content #nav-sub ul li.first-child.last-child').toggleClass('first-child last-child only-child');
		$('#nav-sub > ul').height(Math.max($('#nav-sub ul.nav_left').height(), $('#nav-sub ul.nav_mid').height(), $('#nav-sub ul.nav_right').height()));
	}
	
	$('#primary-content .nav_right').css('height', $('#primary-content .nav_left').css('height'));
	$('#tertiary-content').css('padding-top', $('#nav-wrapper').outerHeight());
	$('.landing .sc').css('padding-top', $('body.landing.layout-n0-p1-s1 #nav-wrapper').outerHeight() + $('.block6').outerHeight() + 30);
	
	$("table.styled tr:nth-child(odd)").addClass("alt");  // Add alt class to alternating rows on styled tables
	$(".table-container td a").parent().addClass("event"); // Add event class to month view 
	$(".table-container td h4").parent().parent().parent().addClass("event"); // Add event class to month view
	if($("#gallery-form #select-gallery").children().length <= 2) { $(".gallery-teaser form").addClass("none"); $(".gallery select").addClass("none"); }
	if($(".gallery-attachment dl.image-list dt").length > 1){$(".gallery-attachment .gallery-directional").removeClass("none");}
	
	$(".gallery-attachment dl.image-list dt:first").addClass("active");
	$(".gallery-attachment dl.image-list dd:first").addClass("active");
	$(".gallery-attachment dl.image-list dt:not(.active)").addClass("none");
	$(".gallery-attachment dl.image-list dd:not(.active)").addClass("none");
	
	var changeButtons = function(){
		// Change display state of buttons
		// If not first child or last child: display next and prev
		if( !$(".gallery-attachment dl.image-list dt:first").hasClass("active") && !$(".gallery-attachment dl.image-list dt:last").hasClass("active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").removeClass("disabled");
		} 
		// If first child: display next, disable prev
		if( $(".gallery-attachment dl.image-list dt:first").is(".active") ){
			$(".gallery-next a").removeClass("disabled");
			$(".gallery-previous a").addClass("disabled");
		} 
		// If last child: display prev, disable next
		if( $(".gallery-attachment dl.image-list dt:last").is(".active") ){
			$(".gallery-previous a").removeClass("disabled");
			$(".gallery-next a").addClass("disabled");
		}
		
		SI_clearFooter();
	}
	
	var updateImages = function(){
		$(".gallery-attachment dl.image-list dt.active").addClass("none");
		$(".gallery-attachment dl.image-list dd.active").addClass("none");
		$(".gallery-attachment dl.image-list dt.active").removeClass("active");
		$(".gallery-attachment dl.image-list dd.active").removeClass("active");
	}
	
	$(".gallery-next a").click(function(){	
		if(!$(this).hasClass("disabled")){
			var nextImg = $(".gallery-attachment dl.image-list dd.active");
			updateImages();
			nextImg.next().removeClass("none");
			nextImg.next().next().removeClass("none");
			nextImg.next().addClass("active");
			nextImg.next().next().addClass("active");
			changeButtons();
		}
	});
	$(".gallery-previous a").click(function(){		
		if(!$(this).hasClass("disabled")){
			var prevImg = $(".gallery-attachment dl dt.active");
			updateImages();
			prevImg.prev().addClass("active");
			prevImg.prev().prev().addClass("active");
			prevImg.prev().removeClass("none");
			prevImg.prev().prev().removeClass("none");
			changeButtons();
		}
	});
	
	/* Add alt class to gallery thumbnails - Not being used yet */
	$(".gallery-thumbnails ul li:odd").addClass("alt");
	
	$('.file-toggle dl.file-group').prepend('<dt class="toggleCtrl"><a href="#" id="showallfiles" onclick="return false;">Show All</a> | <a href="#" id="hideallfiles" onclick="return false;">Hide All</a></dt>');
        $('a#showallfiles').click(function () {
            $('.file-toggle dl.file-list dt').addClass('collapse');
            $('.file-toggle dl.file-list dd').css({'display' : 'block'});
            SI_clearFooter();
        });
        $('a#hideallfiles').click(function () {
            $('.file-toggle dl.file-list dt').removeClass('collapse');
            $('.file-toggle dl.file-list dd').css({'display' : 'none'});
            SI_clearFooter();
        });
	
	/* Required by the Microsoft Ajax Framework:*/
	if ((typeof Sys !== "undefined") && (typeof Sys.Application !== "undefined")) {
		Sys.Application.notifyScriptLoaded();
		if (Sys.WebForms){
			Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(setSubNav);
		}
	} else {
		setSubNav();	
	}
});

