$(document).ready(function(){
$("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");

$("ul.news li:only-child").addClass("only-child");


$("#q").focus(function() { $(this).attr("value",""); }); // remove input value when the user clicks on the search input
	
	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();
		}
	});
	
	$('#content a[href^="mailto:"]').addClass("email");// Auto Class email links
	
	/* Add alt class to gallery thumbnails - Not being used yet */
	$(".gallery-thumbnails ul li:odd").addClass("alt");
	
	$('.file-toggle dl.file-list dd').css({'display' : 'none'});
        $('.file-toggle dl.file-list dt').addClass('enabled');
        $('.file-toggle dl.file-list dd').addClass('enabled');
        $('.file-toggle dl.file-list dt').click(function () {
            if ($(this).next().is(':visible')) {
                $(this).removeClass('collapse');
            } else {
                $(this).addClass('collapse');
            }
            $(this).next().toggle();
            SI_clearFooter();
        });
       
    $('.file-toggle dl.file-group').prepend('<dt class="toggleCtrl"><a href="#" id="showallfiles" class="button" onclick="return false;">Show All</a> <a href="#" id="hideallfiles" class="button" 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();
        });
	function wrapStuff() {
		$('table.styled th').wrapInner('<div class="thwrap" />');
		$('.sc .module h2, #portal .pc .module h2, .layout-n1-p1-s0-c #SecondaryZone .module h2').wrapInner('<div class="h2wrap" />');
		$('blockquote p').wrapInner('<div class="quotebg" />');
		$('.button, .calendar-list .paging a, .news-paging a, a.showall, a.hideall, .module-footer a').wrapInner('<span class="buttonbg" />');
		$('blockquote').wrapInner('<span class="bqwrap" />');
		}
	
	$('.tab-row ul li a').click(function() {
	$('.tab-row ul li a.selected').parent().removeClass('active')
	$(this).parent().addClass("active");
});

/* 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(wrapStuff);
		}
	} else {
		wrapStuff();	
	}
});
