﻿var videos_per_page = 6;
var regExp = new RegExp(videoTagRegEx, "g");

function FilterItems(filterId) {
	//Deselect all other dropdowns
	var stickyFilters = "", sel;

	// keep year and type filters sticky for all filter selections except year
	if (filterId != "year") {
		stickyFilters = ", #type, #year";
	}

	$("#videoSelectionOptions :input:not(#" + filterId + stickyFilters + ") option:first-child").attr("selected", "selected");

	// TODO: Refreshing a page with an active filter doesn't work and crashes
	// if the filterId is "session".  Here is a work-around for the crash.
	if(filterId == "session") {
		filterId = "type";
		sel = filterId;
	} else {
		//Read the current selection from the dropdown
		sel = $("#" + filterId).val().replace(regExp, "-");
	}

	if (filterId == "type") {
		if (sel.toLowerCase() == "session") {
			ShowSessionSubFilters();
			sel = "Breakout";
		}
		else {
			HideSessionSubFilters();
		}
	}
	else if (filterId == "year") {
		sel = sel.replace("20", "");
		HideSessionSubFilters();
	}

	var filterClass;

	// the "sessioncode" selectlist maps to the "session--" class in the thumbnail <li> markup
	if (filterId == "sessioncode") {
	  filterClass = "session";
	}
	else {
	  filterClass = filterId;
	}

	// check if we're also filtering by year
	var yearSel = $("#year").val().replace(/<%= VideoContent.TagRegex %>/g, "-");

	var yearSelClass = "";
	if ($("#year")[0].selectedIndex != 0 && yearSel.length > 0) {
	  yearSelClass = ".year--" + yearSel.replace("20", "");
	}

	// hide all thumbnails
	$("#VideoThumbnailsSection ul li").hide();

	// hide pager
	$(".qc_pager").hide();
	var selectedThumbnails;

	if ($("#" + filterId)[0].selectedIndex != 0 && sel.length > 0) {

	  //Figure out which item was selected and filter on that
	  var selClass = filterClass.toLowerCase() + "--" + sel;

	  if (yearSelClass != "") {
		selectedThumbnails = $("#VideoThumbnailsSection ul li").filter("." + selClass).filter(yearSelClass);
	  }
	  else {
		selectedThumbnails = $("#VideoThumbnailsSection ul li").filter("." + selClass);
	  }

	  //Set the page hash to enable deep linking
	  window.location.hash = selClass;
	}
	else {
	  if (yearSelClass != "") {

		selectedThumbnails = $("#VideoThumbnailsSection ul li").filter(yearSelClass);
	  }
	  else {
		selectedThumbnails = $("#VideoThumbnailsSection ul li");
	  }


	  // session filter has been reset
	  if (filterId != "year" && filterId != "type") {
		  var sessionSelClass = ".type--Breakout";
		  selectedThumbnails = selectedThumbnails.filter(sessionSelClass);
	  }
	  else {
		  HideSessionSubFilters();
	  }

	}

	selectedThumbnails.show();
	selectedThumbnails.quickpaginate({ perpage: videos_per_page });
}

function RebuildSelect(id, names) {
	var el = $(id);
	var firstOption = $(id + " option:first-child").detach();
	el.empty();
	el.append(firstOption);
	for(var i in names)
		el.append("<option>" + names[i] + "</option>");
}

function ShowSessionSubFilters() {
	var year = $("#year").val().replace(/<%= VideoContent.TagRegex %>/g, "-").replace("20", "");
	RebuildSelect("#sessioncode", sessionCodeMap[year]);
	$("#sessioncodelist").show();
	RebuildSelect("#level", levelMap[year]);
	$("#levellist").show();
	RebuildSelect("#speaker", speakerMap[year]);
	$("#speakerlist").show();
	RebuildSelect("#track", trackMap[year]);
	$("#tracklist").show();
	RebuildSelect("#competency", competencyMap[year]);
	$("#competencylist").show();
	RebuildSelect("#product", productMap[year]);
	$("#productlist").show();
}

function HideSessionSubFilters() {
	$("#sessioncodelist").hide();
	$("#levellist").hide();
	$("#speakerlist").hide();
	$("#tracklist").hide();
	$("#competencylist").hide();
	$("#productlist").hide();
}