var hasFlash = false;
try {
var flashobject = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
if(flashobject) hasFlash = true;
}catch(e){
if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) hasFlash = true;
}

$(document).ready(function(){

//toggle body background
$("body").prepend('<img id="bg" alt="" />');
$(function() {   
var theWindow = $(window),
	$bg = $("img#bg"),
	aspectRatio = 1.5;
										
function resizeBg() {
if ((theWindow.width()/theWindow.height()) < aspectRatio) {
	$bg.removeClass('bgwidth bgheight').addClass('bgheight');
} else {
	$bg.removeClass('bgwidth bgheight').addClass('bgwidth');
}
}
theWindow.resize(function() {
resizeBg();
}).trigger("resize");
});
$("img#bg").randomClass(['bg1','bg2','bg3','bg4','bg5','bg6','bg7','bg8']);
$("img#bg").attr("src","/images/common/"+$("img#bg").attr("class")+".jpg");

$("#container").css('background-image','none');
$("body").append('<div id="lens" />');

$("#weather-icon span.temp").each(function(){
	$(this).text($(this).text().slice(0,$(this).text().indexOf("F")+1));
});
$("#weather-icon span").css("visibility", "visible");
$("#masthead #headline").css("width",$("#masthead #headline").width()+12);
$("#masthead #headline").css("left",Math.ceil((980-($("#masthead #headline").width()))/2)+6);

//toggle body background
$("body").prepend('<div id="tour-link"><a href="/admission/welcome/virtual-tour-of-campus/index.aspx" class="button">Virtual Tour of Campus &raquo;</a></div><div id="shrinky"><a href="#prev" id="shrinky-prev">&laquo;Prev</a><a href="#toggle" id="shrinky-toggle">Toggle</a><a href="#next" id="shrinky-next">Next&raquo;</a><img id="previmg" /><img id="nextimg" /></div>');
if (hasFlash) {
} else {
	$("dl#bigbutton a, #tour-link a").attr("href","/gallery/index.aspx");
	$("dl#bigbutton a span, #tour-link a").html("Photo Galleries of Campus &raquo;");
}

$('#shrinky-toggle').click(function() {
  $('#container, #footer-container, #lens, #tour-link').fadeToggle('fast');
  $('#shrinky').toggleClass('shrinky-active');
  return false;
});
var currentBG = $("img#bg").attr("class").split(/\s/);
$("img#previmg").addClass(currentBG[currentBG.length -1]);
$("img#previmg").cycleClass(["bg8","bg7","bg6","bg5","bg4","bg3","bg2","bg1"]);
$("img#previmg").attr("src","/images/common/"+$("img#previmg").attr("class")+".jpg");
$("img#nextimg").addClass(currentBG[currentBG.length -1]);
$("img#nextimg").cycleClass(["bg1","bg2","bg3","bg4","bg5","bg6","bg7","bg8"]);
$("img#nextimg").attr("src","/images/common/"+$("img#nextimg").attr("class")+".jpg");

$('#shrinky-prev').click(function() {
	$("img#bg").cycleClass(["bg8","bg7","bg6","bg5","bg4","bg3","bg2","bg1"]);
	var currentBG = $("img#bg").attr("class").split(/\s/);
	$("img#bg").attr("src","/images/common/"+currentBG[currentBG.length -1]+".jpg");
	return false;
});
$('#shrinky-next').click(function() {
	$("img#bg").cycleClass(["bg1","bg2","bg3","bg4","bg5","bg6","bg7","bg8"]);
	var currentBG = $("img#bg").attr("class").split(/\s/);
	$("img#bg").attr("src","/images/common/"+currentBG[currentBG.length -1]+".jpg");
	return false;
});
$('#shrinky-prev, #shrinky-next').click(function() {
	$("img#previmg").cycleClass(["bg1","bg2","bg3","bg4","bg5","bg6","bg7","bg8"]);
	$("img#previmg").attr("src","/images/common/"+$("img#previmg").attr("class")+".jpg");
	$("img#nextimg").cycleClass(["bg2","bg3","bg4","bg5","bg6","bg7","bg8","bg1"]);
	$("img#nextimg").attr("src","/images/common/"+$("img#nextimg").attr("class")+".jpg");
	return false;
});

$("body.layout-n1-p1-s0-c .float-container").addClass("sc");

//$(".news .paging a, .calendar .paging a").addClass("button");
//$(".sc .gallery .scroll .paging a, .module .controls .paging a, body#home .paging a").removeClass("button");
$(".faq a.showall, .faq a.hideall").addClass("button disabled");

$("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 < 1) { $(".gallery-teaser form, .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");
	
	// Reverse lightbox caption/description	
	$("div.thumb").each(function(){
	var title = $(this).find("a").attr("title");
	var alt = $(this).find("img").attr("alt");
	$(this).find("a").attr("title", alt);
	$(this).find("img").attr("alt", title);
	});

$("#footer a.silverpoint").attr("target","_blank");

});
