$(function(){

// Translate Moves / Flag Assignments
$('#nav-li-translate').appendTo($('#nav-const > ul'));
$('#translate_me').remove();
$('#nav-li-translate ul li a').each(function () {
	$(this).css('background', 'url(/images/flags/' + $(this).closest('li').attr('class') + '.png) no-repeat 0 8px transparent');
});

// Randomization of Header Images
var HISTORY_PAGE = $('body').hasClass('history');
var WASATCH_WIRE = $('.sc').find('div.wasatch_wire_item').length;

function addImages() {
	if(headerWidth > browserWidth) {
		return;
	} else {
		if(HISTORY_PAGE) {
			rand = 1 + Math.floor(Math.random()*10);
			$('#header_images .images').append('<li><img src="/images/header/history/side/' + rand + '.jpg" /></li>');
		} else {
			rand = 1 + Math.floor(Math.random()*55);
			$('#header_images .images').append('<li><img src="/images/header/side-header/' + rand + '.jpg" /></li>');	
		}
		$('#header_images .images li:last img').load(function () {
			headerWidth += $(this).width();
			addImages();
		});
	}
}

if(!WASATCH_WIRE) {
	$('#header_images .images li').remove();
	var rand = 0;
	 if(HISTORY_PAGE) {
		// history
		rand = 1 + Math.floor(Math.random()*5);
		$('#header_images .images').append('<li><img src="/images/header/history/' + rand + '.jpg" /></li>');
	} else if($('body').attr('id') == 'about-us' || $('body').attr('id') == 'admissions' || $('body').attr('id') == 'learning' || $('body').attr('id') == 'exploring' || $('body').attr('id') == 'living' || $('body').attr('id') == 'giving') {
		rand = 1 + Math.floor(Math.random()*16);
		$('#header_images .images').append('<li><img src="/images/header/' + $('body').attr('id') + '/' + rand + '.jpg" /></li>');
	} else {
		rand = 1 + Math.floor(Math.random()*28);
		$('#header_images .images').append('<li><img src="/images/header/common/' + rand + '.jpg" /></li>');
	}
		
	
	var browserWidth = $(window).width();
	var headerWidth = 0;
	$('#header_images .images li img').each(function () {
		headerWidth += $(this).width();
	});
	
	addImages();
}

// Attachment Viewer fixes
$('.attachment-viewer')
	.closest('.module')
	.removeClass('module');
	
// Forms fixes for wide fields and fieldsets
$('form fieldset div > label[for]').each(function () {
    if($(this).height() > 35) {
        $(this).closest('div').addClass('wide')
    }
});

// Remove h2.section if there isn't an h2.section
if(!$('h2.section a').text().length) {
	$('h2.section').remove();
}

//Accordion Joiner (DOES NOT WORK)
//$('.accordion:not(:first)').each(function () {
//    $('.accordion:first > :last').after($(this).html())
//    $(this).remove()
//});
//$('.accordion').accordion();

// default

$("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-form").addClass("none"); $("#gallery-form 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");
	
});
