﻿$(document).ready(function () {
	// header image scroll
	function mycarousel_initCallback(carousel) {
		$('#RightCarouselArrow').bind('click', function () {
			carousel.next();
			return false;
		});

		$('#LeftCarouselArrow').bind('click', function () {
			carousel.prev();
			return false;
		});
	};

	/*
	var startPoint = $("form").attr("class").split(" ").pop();

	if (startPoint.indexOf("object-") != -1) {
	startPoint = startPoint.replace("object-", "") - 12;
	}
	else {
	startPoint = 1;
	}
	*/

	var $SubjectAreaCarouselList = $('ul#SubjectAreasCarousel');

	var jcOptions = { 
		scroll: 1,
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap: 'both'
	}

	var $courseAreaIDInput = $('input.CourseAreaIDInput');
	
	if ($courseAreaIDInput.size() > 0) {
		var $listItems = $SubjectAreaCarouselList.children('li');
		var $StartItems = $listItems.filter('.' + $courseAreaIDInput.val());
		if ($StartItems.size() > 0) {
			var startItemIndex = $listItems.index($StartItems.first());
			if (startItemIndex != -1)
				jcOptions.start = startItemIndex + 1;
		}
	}

	$('ul#SubjectAreasCarousel').jcarousel(jcOptions);

	// course index scrollpane

	//$('.scrollpane').jScrollPane({ verticalDragMinHeight: 23, verticalDragMaxHeight: 23});

	// blog posts list

	$('.blogNav #BlogPostListBox ul#BlogPostsList').listScroll();

	// form example

	$('.header-container .search-container input.searchTextBox').example('Search for a course');

	// multicols

	$('div.twoCols').simpleColumns();

	//date-location-dropdown

	$("li.date-location-dropdown").each(function () {

		$(this).children("label").hide();
		$(this).children("select").hide();

		$(this).append("<label>Course Location</label>");

		var uniqueLocations = [];

		$(this).children("select").first().each(function () {
			$(this).children("option").each(function () {
				if ($(this).val() != "Select...") {
					uniqueLocations.push($(this).val().split(" - ").slice(-1)[0]);
				}
			});
		});

		var locationDropDown = $("<select class=\"ddl\" />");
		$(this).append($(locationDropDown));

		var dateLi = $("<li class=\"date-dropdown\" />");
		$(dateLi).insertAfter($(this));
		$(dateLi).append("<label>Course Date</label>")

		var dateDropDown = $("<select class=\"ddl\" />");
		$(dateLi).append($(dateDropDown));


		if ($(this).find("select").first().val() == "TBC") {
			$(locationDropDown).append("<option value=\"TBC\" selected=\"selected\">TBC</option>");
			$(locationDropDown).attr('disabled', 'disabled');

			$(dateDropDown).append("<option value=\"TBC\" selected=\"selected\">TBC</option>");
			$(dateDropDown).attr('disabled', 'disabled');
		}
		else {

			$(locationDropDown).append("<option value=\"Select...\" selected=\"selected\">Select...</option>");
			$(uniqueLocations.unique().sort()).each(function () {
				$(locationDropDown).append("<option value=\"" + this + "\">" + this + "</option>");
			});

			$(dateDropDown).attr('disabled', 'disabled');
			$(dateDropDown).append("<option value=\"Select...\" selected=\"selected\">Select...</option>");

			var parent = $(this);

			$(locationDropDown).change(function (e) {
				if ($(this).val() != "Select...") {

					var uniqueDates = [];

					//alert(uniqueDates.unique().sort(dmyOrdA).length);

					$(parent).children("select").first().each(function () {
						$(this).children("option").each(function () {
							if ($(this).val().indexOf($(locationDropDown).val()) != "-1") {
								uniqueDates.push($(this).val());
							}
						});
					});

					//alert(uniqueDates.unique().sort(dmyOrdA).length);
					//$(dateDropDown).detach();

					$(dateDropDown).empty();
					$(dateDropDown).remove();

					$(dateDropDown).append("<option value=\"Select...\" selected=\"selected\">Select...</option>");

					$(uniqueDates.unique().sort(dmyOrdA)).each(function () {
						$(dateDropDown).append("<option value=\"" + this + "\">" + formatDate(this.split(" - ").slice(-3)[0]) + "</option>");
					});


					$(dateDropDown).removeAttr('disabled');

					$(dateDropDown).change(function (e) {
						if ($(this).val() != "Select...") {
							$(parent).children("select").first().children("option").removeAttr("selected")
							$(parent).children("select").first().children("option[value='" + $(this).val() + "']").first().attr("selected", true);
							$(parent).children("select").first().val($(this).val());
						}
					});

					$(dateLi).append($(dateDropDown));
				}
				else {
					$(dateDropDown).attr('disabled', 'disabled');
				}
			});
		}
	});

	var term = $("input.searchTextBox").val();

	$('form.template-14 ul.index-list li div.description').highlight(term);

});

Array.prototype.unique =
  function() {
      var a = [];
      var l = this.length;
      for (var i = 0; i < l; i++) {
          for (var j = i + 1; j < l; j++) {
              // If this[i] is found later in the array
              if (this[i] === this[j])
                  j = ++i;
          }
          a.push(this[i]);
      }
      return a;
  };

var dateRE = /^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/;

function dmyOrdA(a, b) {
    a = a.split(" - ").slice(-3)[0].replace(dateRE, "$3$2$1");
    b = b.split(" - ").slice(-3)[0].replace(dateRE, "$3$2$1");
    if (a > b) return 1;
    if (a < b) return -1;
    return 0;
}

var monthNames = new Array("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sept",
"Oct", "Nov", "Dec");

function formatDate(date) {
    var dt = new Date(FormatUkDate(date));
    return (dt.getDate() + " " + monthNames[dt.getMonth()] + " " + dt.getFullYear());
}

function FormatUkDate(dateStr) {
    dateStr = dateStr.split("/");
    return new Date(dateStr[2], dateStr[1] - 1, dateStr[0]);
}
