﻿/// <reference path="libraries.js" />
/*
***************************************************************************************
* JAVASCRIPT ENHANCEMENT
*
* Author: valerie.hanesse @ readingroom.com
* Created: 2011.02.09
* Uptated: 2011.02.15
*
***************************************************************************************
*/

/***** ~~~~~ things that have to be done immediately ~~~~~ *****/
document.getElementsByTagName('html')[0].className = 'js';
/***** ~~~~~ things that are done when the document is ready ~~~~~ *****/
rr(document).ready(function () {
	accordions();
	features();
	var currentDate = new Date();
	var month = currentDate.getMonth() + 1;
	var day = currentDate.getDate();
	var year = currentDate.getFullYear();
	rr('.datepicker').datepicker({ dateFormat: "dd/mm/yy", minDate: currentDate });
	configureDropDowns();
	forms();
	resize();
	corners();

	// expand first item in topics accordion
	rr(".accordion-title").first().children().first().trigger("click");

	// add "last" css class for last section in bottom menu
	rr("#Explore").find(".split").last().addClass("last");
});

function configureDropDowns() {
	var allOptionRegions = "<option value='-1'>Regions</option>";
	var allOptionSubjects = "<option value='-1'>Subjects</option>";

	var subjectAreasControl = rr('#selectSubjectAreas');
	var regionsControl = rr('#selectRegions');
	var subjectsControl = rr('#selectSubjects');

	// disable regions
	regionsControl.selectmenu({ style: 'dropdown' });
	regionsControl.selectmenu('disable');

	// disable subjects
	subjectsControl.selectmenu({ style: 'dropdown' });
	subjectsControl.selectmenu('disable');

	subjectAreasControl.bind("change", function () {
		var subjectAreasValue = subjectAreasControl.val();
		if (subjectAreasValue == "-1" || subjectAreasValue == "14410") {
			regionsControl.html(allOptionRegions);
			regionsControl.selectmenu('disable');

			subjectsControl.html(allOptionSubjects);
			subjectsControl.selectmenu('disable');
		}
		else {
			rr.ajax({
				type: "POST",
				url: "Utils/CoursesService.asmx/GetRegions",
				data: '{ subjectAreaId: ' + subjectAreasValue +' }',
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (html) {
					regionsControl.html(html);
					regionsControl.selectmenu({ style: 'dropdown' });
					regionsControl.selectmenu('enable');
				}
			});

			rr.ajax({
				type: "POST",
				url: "Utils/CoursesService.asmx/GetSubjects",
				data: '{ subjectAreaId: ' + subjectAreasValue + ' }',
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (html) {
					subjectsControl.html(html);
					subjectsControl.selectmenu({ style: 'dropdown' });
					subjectsControl.selectmenu('enable');
				}
			});	
		}
	});
	
	rr.ajax({
		type: "POST",
		url: "Utils/CoursesService.asmx/GetSubjectAreas",
		data: {},
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (html) {
			subjectAreasControl.html(html);
			subjectAreasControl.selectmenu({ style: 'dropdown' });
		}
	});
}
