﻿// Javascript file for plans

$(document).ready(function ()
{
	handleSearchHighlighting();

	expandRelevantDivs();

	setHrefsForPopupAnchors();

	$("a.helpPopupLink").fancybox({
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': 600,
		'speedOut': 200,
		'overlayShow': false,
		'autoDimensions': true,
		'autoScale': true,
		'showCloseButton': true
	});
})

// --------------------------------------------------------------------------------
// If searchtext is in the querystring, highlight occurances
// --------------------------------------------------------------------------------
function handleSearchHighlighting()
{
	if (isInSearchMode())
	{
		var strSearchText = $("#hidSearchText").val();

		var searchTermArray = strSearchText.split(",");
		for (var i = 0; i < searchTermArray.length; i++)
		{
			if ($.trim(searchTermArray[i]).length == 0)
				continue;

			$('p').highlight(searchTermArray[i]);
			$(':header').highlight(searchTermArray[i]);
		}
	}
}

function expandRelevantDivs()
{
	if (isInSearchMode())
		expandOnlyDivsThatContainSearchText();
	else
		SlideToggle() 
}

function isInSearchMode()
{
	var strSearchText = $("#hidSearchText").val();
	if (typeof (strSearchText) != "undefined" && strSearchText != "")
		return true;

	return false;
}

// --------------------------------------------------------------------------------
// Toggles the display of availability issues for a plan
// --------------------------------------------------------------------------------
function toggleAvailabilityIssues(planID)
{
   $("#divAvailabilityIssue_" + planID).slideToggle();
}

// --------------------------------------------------------------------------------
// Toggles the plan panels between open and closed
// --------------------------------------------------------------------------------
function SlideToggle() 
{
    //hide the all of the elements with class plan_body
	$(".item-body").hide();
	
	//toggle the componenet with class plan_body
	$(".item-openclose").click(function() 
	{
		$(this).parents(".list-item").children(".item-body").slideToggle(250);
	});
	
	$(".item-openclose:first").parents(".list-item").children(".item-body").slideToggle(250);
}

function expandOnlyDivsThatContainSearchText()
{
	//hide the all of the elements with class plan_body
	$(".item-body").hide();

	//toggle the componenet with class plan_body
	$(".item-openclose").click(function ()
	{
		$(this).parents(".list-item").children(".item-body").slideToggle(250);
	});

	// Expand all divs that contain the search text somewhere within
	$("div.list-item").find('.highlight').parents(".list-item").children(".item-body").slideToggle(250);
}

// --------------------------------------------------------------------------------
// Performs an HTTP GET request based on the search criteria selected by the user
// --------------------------------------------------------------------------------
function performHttpGetBasedOnPlansSearchCriteria() {
	
	if ($("#YearOptions").attr("selectedIndex") == 0 || $("#TermOptions").attr("selectedIndex") == 0)
		return;

	var subject = $("#Subject").val();
	var planTypeName = $("#PlanTypeName").val();
	var yearName = $("#YearOptions").val();
	var termName = $("#TermOptions :selected").text();

	var url = formatForUrl("/" + subject + "/" + planTypeName + "/" + yearName + "/" + termName);
		
	// TODO: May need to make this a bit more sophisticated
	window.location.replace("http://" + location.host + url);
}

// --------------------------------------------------------------------------------
// Formats a string for a URL parameter
// --------------------------------------------------------------------------------
function formatForUrl(string) {
	var returnString = string.replace(/ /gi, "_").replace(/,/gi, "_").replace(/!/gi, "_").replace(/:/gi, "_").replace(/-/gi, "_").replace(/&/gi, "And");

	returnString = returnString.replace(/___/gi, "_").replace(/__/gi, "_");

	return encodeURI(returnString);
}

// --------------------------------------------------------------------------------
// Sets the hrefs for any help popup links on the page 
// (the href is set to a nodeid initially in case JavaScript is disabled)
// --------------------------------------------------------------------------------
function setHrefsForPopupAnchors() {
	
	var helpPopupLinks = $('a.helpPopupLink');

	for (var i = 0; i < helpPopupLinks.length; i++) {
			helpPopupLinks[i].href = "#helppopup";
	}
}
