//Useful Array Shuffle functionality

Array.prototype.shuffle = function() {
	var i = this.length;
	var j;
	var temp;
	if (i == 0) {
		return;
	}
	while(--i) {
		j = Math.floor(Math.random() * (i + 1));
		temp = this[i];
		this[i] = this[j];
		this[j] = temp;
	}
};


var swappedItemContents;

$(document).ready(function() {
	var subFeatureItems = jQuery.makeArray($(".subFeatureItem").remove());
	var allProgramsHTML = '<img src="/eng/common/images/bannersSubfeatures/allPrograms.png" alt="Explore All Programs" width="209" height="179" /><p><a href="#">Explore All Programs</a></p>';
	
	
	
	//---- Nivo Slider ----
	
	var numSlides = $("#slider img").length;
	var randStartSlide = Math.floor(Math.random() * numSlides);
	
	//bind to custom slider added to Nivo Slider source. Only way to use Cufon with the captions and not create graphics.
	//should degrade gracefully if Nivo is upgraded to simply not use Cufon with the captions. Change to Nivo was suggested to the developer.
	$("#slider").bind("nivo:textReady", function() {
		Cufon.refresh('.nivo-caption');
	});
	
	$("#slider").nivoSlider({
		effect: 'fade',
		animSpeed: 600,
		pauseTime: 6000,
		startSlide: randStartSlide,
		directionNav: true,
		directionNavHide: false,
		controlNav: false,
		keyboardNav: true,
		pauseOnHover: true,
		manualAdvance: false,
		captionOpacity: 1
	});
	
	//---- End Nivo Slider ----
	
	
	
	//---- Sub Feature Randomizer/Fader ----
	
	subFeatureItems.shuffle();
	$(".showingAll").remove();
	
	for (var sfi = 0; sfi < subFeatureItems.length; sfi++) {
		if (sfi < 4) {
			if (sfi == 2) {
				swappedItemContents = $(subFeatureItems[sfi]).children().detach();
				$(subFeatureItems[sfi]).html(allProgramsHTML);
				$(subFeatureItems[sfi]).addClass("exploreAll");
			}
			$(".subFeatures").append(subFeatureItems[sfi]);
		}
		else if (sfi == 4) {
			$(".subFeatures").append('<div class="showingAll"></div>');
			$(".showingAll").append(subFeatureItems[sfi]);
		}
		else {
			$(".showingAll").append(subFeatureItems[sfi]);
		}
		
		//add no margin classes
		if (sfi % 4 == 0) {
			$(subFeatureItems[sfi]).addClass("beginningOfRow");
		}
	}
		
	$(".subFeatureItem:lt(4)").each(function(i) {
		$(this).css("visibility", "visible");
		$(this).delay(150 * i).animate({opacity: 1}, 400);
	});
	
	$(".exploreAll").live("click", function() {
		$(".showingAll").slideDown(600, function() {
			$(".closePrograms").show();
			$(".exploreAll").animate({opacity: 0}, 300, function() {
				$(this).children().remove();		
				$(this).append(swappedItemContents);
				$(this).animate({opacity: 1}, 300);
				$(this).removeClass("exploreAll");
			});
		});
		
		$(".subFeatureItem:gt(3)").each(function(i) {
			$(this).css("visibility", "visible");
			$(this).delay(50 * i).animate({opacity: 1}, 400);
		});
		
		return false;
	});
	$(".closePrograms").click(function() {
		$(this).hide();
		$(".showingAll").slideUp(600, function() {
			$(".subFeatureItem:eq(2)").animate({opacity: 0}, 300, function() {
				swappedItemContents = $(this).children().remove();
				$(this).html(allProgramsHTML);
				$(this).addClass("exploreAll");
				$(this).animate({opacity: 1}, 300);
			});
		});
		$(jQuery.makeArray($(".subFeatureItem:gt(3)")).reverse()).each(function(j) {
			$(this).delay(50 * j).animate({opacity: 0}, 400);
		});
	
		return false;	
	});
	
	//---- End Sub Feature Randomizer/Fader ----
	
		
	
	//---- Interior Navigation Highlight/Collapse ----
	
	function processURLPieces(arrPieces) {
		if (arrPieces) {
			if (arrPieces[0] == "") {
				//eliminate empty string from urls starting with a '/'
				arrPieces.shift();
			}
			else if (arrPieces[0] == "http:" || arrPieces[0] == "https:") {
				//Remove http:, '', and www.bu.edu from the list of URL Pieces
				for (var foo = 0; foo < 3; foo++) {
					arrPieces.shift();
				}
			}
			
			if (arrPieces[arrPieces.length - 1].indexOf("index.shtml") !== -1 || arrPieces[arrPieces.length - 1] == "" || arrPieces[arrPieces.length - 1].indexOf("#") !== -1) {
				//Remove index.shtml if it has been accidentally inserted and empty entries when a filename is not provided as well as #additions
				arrPieces.pop();
			}
		}
	}
	
	var match = false;
	var urlPieces = document.location.href.split("/");
	processURLPieces(urlPieces);
	
	function activeAtor(curUL) {		
		curUL.children("li").each(function() {
			var comparisonURLPieces = $(this).children("a").attr("href").split("/");
			processURLPieces(comparisonURLPieces);
/* 			console.log("----"); */
/* 			console.log("Working on li: " + $(this).html()); */
			
			//directory match test
			var shorterURLLength = comparisonURLPieces.length < urlPieces.length ? comparisonURLPieces.length : 0;
			if (shorterURLLength === 0) {
/* 				console.log("no valid comparison url within constraints, pending special allowed circumstances."); */
			}
			if (comparisonURLPieces.length == urlPieces.length) {
/* 				console.log("Special circumstances: could be a directory that links to a sub level or an exact match candidate"); */
				var firstChildLinkPieces = $($(this).children("ul").children("li")[0]).children("a").attr("href");
				if (firstChildLinkPieces) {
					firstChildLinkPieces = firstChildLinkPieces.split("/");
					processURLPieces(firstChildLinkPieces);
					if (comparisonURLPieces.length == firstChildLinkPieces.length) {
						for (var zap = 0; zap < comparisonURLPieces.length; zap++) {
							if (comparisonURLPieces[zap] != firstChildLinkPieces[zap]) {
/* 								console.log("invalid circumstance, first child must match parent if their links exist in the same directory level, not proceeding."); */
								break;
							}
							else if (zap == comparisonURLPieces.length - 1 && comparisonURLPieces[zap] == firstChildLinkPieces[zap]) {
/* 								console.log("Special circumstances confirmed, override and process without final URL piece"); */
								comparisonURLPieces.pop();
								shorterURLLength = comparisonURLPieces.length;
							}
						}
					}
				}
			}
			for (var bar = 0; bar < shorterURLLength; bar++) {
/* 				console.log("urlPieces[" + bar + "]: " + urlPieces[bar] + ", comparisonURLPieces[" + bar + "]: " + comparisonURLPieces[bar]); */
				if (urlPieces[bar] != comparisonURLPieces[bar]) {
/* 					console.log("no directory match"); */
					
					break;
				}
				else if (bar == shorterURLLength - 1 && urlPieces[bar] == comparisonURLPieces[bar]) {
					//if the parsed URL Pieces have a filename in them, we're dealing with a specific file
					if (urlPieces[urlPieces.length - 1].indexOf(".shtml") !== -1 ) {
						//if the length of the comparison url pieces is one less than the actual url we're dealing with the immediate parent
						//we don't want to highlight that as well as the page, e.g.: /bio/ and /bio/no.shtml, we don't want to highlight the /bio/ entry
						if (urlPieces.length - comparisonURLPieces.length !== 1) {
							$(this).children("a").addClass("active");
						}
					}
					else {
						$(this).children("a").addClass("active");
					}
					$(this).siblings("li").children("ul").hide();
/* 					console.log("matched a directory"); */
/* 					console.log("Hiding: " + $(this).siblings("li").children("ul").html()); */
					match = true;
				}
			}
			
			
			//match for exact filename and hide siblings/grandchildren from view
			if (document.location.href.indexOf($(this).children("a").attr("href")) > -1 && comparisonURLPieces.length == urlPieces.length && $(this).children("a").attr("href") != "#") {
				$(this).children("a").addClass("active");
/* 				console.log("Exact page match: " + $(this).children("a").html()); */
/* 				console.log("Hiding: " + $(this).siblings("li").children("ul").html()); */
/* 				console.log("Hiding: " + $("ul", $(this).children("ul").children("li")).html()); */
				$(this).siblings("li").children("ul").hide();
				$("ul", $(this).children("ul").children("li")).hide();
				match = true;
/* 				console.log("matched an exact page"); */
			}
		});
		
		if (curUL.children("li").children("ul").length) {
			curUL.children("li").children("ul").each(function() {
				activeAtor($(this));
			});
		}
	}
	
	activeAtor($(".subNav > ul"));
	
	//if there is no match, collapse the grandchildren uls.
	if (!match) {
/* 		console.log("collapsing grandchildren ULs because of no match."); */
		$(".subNav > ul > li > ul").hide();
	}
	
	//---- End Interior Navigation Highlight/Collapse ----
	
	
	
	//---- Main Navigation Highlight ----
	
	var mainNavLIs = $(".mainNav > ul > li");
	var mainNavHREFs = $(mainNavLIs).children("a");
	for (var liCount = 0; liCount < mainNavLIs.length; liCount++) {
		var hrefPieces = mainNavHREFs[liCount].href.split("/");
		//when the link is only "#" JavaScript reports the URL of the current page ending in "#" causing an erroneous match
		//also being excessively thorough and checking for paths that should not necessarily exist that would also end in # and possibly trigger a match
		if (hrefPieces[hrefPieces.length - 1] == "#" || (hrefPieces[hrefPieces.length - 1].length > 1 && hrefPieces[hrefPieces.length - 1].indexOf("#") == hrefPieces[hrefPieces.length - 1].length - 1)) {
			continue;
		}
		processURLPieces(hrefPieces);
		for (var baz = 0; baz < hrefPieces.length; baz++) {
			if (urlPieces[baz] != hrefPieces[baz]) {
				break;
			}
			else if (baz == hrefPieces.length - 1 && urlPieces[baz] == hrefPieces[baz]) {
				$(mainNavLIs[liCount]).addClass("active");
			}
		}
	}
	
	Cufon.refresh(".mainNav li.active > a");
	Cufon.refresh(".mainNav li.active > a span");
	
	//---- End Main Navigation Highlight ----
	
	
	
	//---- Interior Expand/Collapse ----
	
	$(".programSlideToggle").click(function() {
		if ($(this).text() == "Keep Reading") {
			$(this).text("Close").addClass("expanded");
		}
		else {
			$(this).text("Keep Reading").removeClass("expanded");
		}
		$(".extraInfo").slideToggle(600);
	});
	
	if ($(".programSlideToggle").length > 0) {
		if (document.location.href.slice(document.location.href.indexOf("#") + 1) == "expand") {
			$(".programSlideToggle").click();
		}
	}
	
	//---- End Interior Expand/Collapse


//---- Content Toggle ----

	$(".collapsible_trigger").click(function(){
		$(this).toggleClass("active").next(".collapsible_toggle").slideToggle();
	});	

//---- End Content Toggle
	
	//---- ?? ----
	
	$("input").checkBox();

	//---- End ?? ----
});
