// requires Glow, plus LZC library loaded as window.lzc_lib
// Also requires certain hidden fields (see below near top) which MUST be defined on the home page.

// was all in global space, now inside closure (2008-09-10 MET).

(function(){ // wrap contents in a closure (called immediately)

	var lib = window.lzc_lib, // library for this site
		browse = null; // whole of code contained in this object

	glow.ready(function() {	
		if (browse && browse.init) browse.init() 
	});

	/* this script fills the topic and subject lists and relies on the output from a QE call using main_topics.cfg */
	browse = {
		data:script_output.RECORD,
		numrecords:script_output.RECORD.length,
		level:"Primary",
		subject:"",
		topic:"",
		topicsAllText:"All topics",
		subjectsAllText:"All subjects",
		subjectFieldId:"clipsBrowseSubject",
		hiddenSubjectFieldId:"browse-subject",
		topicFieldId:"clipsBrowseTopic",
		hiddenTopicFieldId:"browse-topic",
		init:function(){
			glow.dom.get("#school_level").val(browse.level); // this field is submitted as val_1_1
			glow.dom.get("#browse-level").val(browse.level);
			glow.dom.get("#browse-subject").val(browse.subject);
			glow.dom.get("#browse-topic").val(browse.topic);
			glow.dom.get("#primaryButton").addClass("active");
			browse.initLevelLinks();
			browse.getSubjects();
		},
		initLevelLinks:function(){
			var levelLinks = glow.dom.get(".selectLevel a");
			levelLinks.each( function (i) { 
				glow.events.addListener(
					levelLinks[i],
					'click',
					function (e) {
						//remove active class from all links and reapply to current link                  
						glow.dom.get(".selectLevel div").removeClass("active");                   
						glow.dom.get(this).parent().addClass("active");
						glow.dom.get("#browse-level,#school_level").val(glow.dom.get(levelLinks[i]).attr("title"));
						browse.clearSelections();
						browse.getSubjects();
						e.preventDefault();
					}
				);

			});
		},
		clearSelections:function(){
			browse.subject = "";
			browse.topic = "";
			glow.dom.get("#browse-subject").val("");
			glow.dom.get("#browse-topic").val("");
			glow.events.removeListener(browse.subjectListener);
			glow.events.removeListener(browse.topicListener);
		},
		getSubjects:function(){
			browse.subjects = new Array();
			browse.level = glow.dom.get("#browse-level").val();
			//go through all records and retrieve subjects
			for (var i = 0; i < browse.numrecords; i++){
				//if it's the right level, get the subjects and add to the subjects array
				if( script_output.RECORD[i].SUBJECT_NAME == "" || script_output.RECORD[i].SUBJECT_NAME == undefined ) continue;
				if( browse.level.toLowerCase() == script_output.RECORD[i].SCHOOL_LEVEL_NAME.toLowerCase() ){
					browse.subjects.push(script_output.RECORD[i].SUBJECT_NAME);
				}
			}
			//extract the unique subjects, sort them, add an "All" value to the start of the array
			browse.subjects = lib.unique(browse.subjects);
			browse.subjects.sort();
			browse.subjects.unshift(browse.subjectsAllText);
			//add the option tags to the subject field
			browse.populateSubjectField();
			//now find all the topics for this level/subject
			browse.getTopics();
		},
		getTopics:function(){
			//clear existing topics array
			browse.topics = new Array();
			//get subject from the hidden form field
			browse.subject = glow.dom.get("#browse-subject").val();
			for (var i = 0; i < browse.numrecords; i++){
				//filter out topic names which are undefined
				if( script_output.RECORD[i].TOPIC == undefined || script_output.RECORD[i].TOPIC == "" ) continue;
				if( ( (browse.subject.toLowerCase() == script_output.RECORD[i].SUBJECT_NAME.toLowerCase()) ||  (browse.subject.toLowerCase() == "")) && (browse.level.toLowerCase() == script_output.RECORD[i].SCHOOL_LEVEL_NAME.toLowerCase()) ){
					//build an array of topic names - either all, or those relating to a particular subject
					browse.topics.push(script_output.RECORD[i].TOPIC);
				}
			}
			//extract the unique topics, sort them, add an "All" value to the start of the array
			browse.topics = lib.unique(browse.topics);
			browse.topics.sort();
			browse.topics.unshift(browse.topicsAllText);
			//add the option tags to the topic field
			browse.populateTopicsField();
		},
		populateSubjectField:function(){
			if(!document.getElementById(browse.subjectFieldId)) return false;	
			var selectEl = document.getElementById(browse.subjectFieldId);
			selectEl.innerHTML = "";
			var sel = "";
			for( var i=0; i<browse.subjects.length; i++ ){
				sel = false;
				if(i==0){
					value = "";
				} else {
					value = browse.subjects[i];
				}
				label = browse.subjects[i];
				if(i==0) sel = true;
				var opt = browse.addOptionEl(selectEl,value,label,sel);
			}
			browse.subjectListener = glow.events.addListener(
					selectEl,
					'change',
					function () { 
						browse.changeSubject();
						browse.getTopics();
					}
			);
		},
		changeSubject:function(){
			// Find out which option the user has selected
			browse.subject = "";
			browse.topic = "";
			glow.dom.get("#browse-topic").val("");
			var firstOpt = lib.getFirstChildElement(document.getElementById(browse.subjectFieldId));
			var opt = firstOpt;
			do{
				if (opt.selected && ( browse.subject != '' )){
					// If this is not the first selection, deselect it to prevent multiple selections
					opt.selected = false;
				} else if (opt.selected) {
					browse.subject = opt.value;
				}
			}
			while (opt = opt.nextSibling);
			if(browse.subject == browse.subjectsAllText) browse.subject = "";
			glow.dom.get("#browse-subject").val(browse.subject);
			glow.events.removeListener(browse.topicListener);
		},
		populateTopicsField:function(){
			if(!document.getElementById(browse.topicFieldId)) return false;
			var selectEl = document.getElementById(browse.topicFieldId);
			selectEl.innerHTML = "";
			var sel = "";
			for( var i=0; i<browse.topics.length; i++ ){
				sel = false;
				if(i==0){
					value = "";
				} else {
					value = browse.topics[i];
				}
				label = browse.topics[i];
				if(i==0) sel = true;
				var opt = browse.addOptionEl(selectEl,value,label,sel);
			}
			browse.topicListener = glow.events.addListener(
					selectEl,
					'change',
					function () { 
						browse.changeTopic();
					}
			);
		},
		changeTopic:function(){
			// Find out which option the user has selected
			browse.topic = "";
			var opt = lib.getFirstChildElement(document.getElementById(browse.topicFieldId));
			do{
				if (opt.selected && browse.topic != ""){
					// If this is not the first selection, deselect it to prevent multiple selections
					opt.selected = false;
				} else if (opt.selected) {
					browse.topic = opt.value;
				}
			}
			while (opt = opt.nextSibling);
			if(browse.topic == browse.topicsAllText) browse.topic = "";
			glow.dom.get("#browse-topic").val(browse.topic);
		},
		//function for adding a single option element to a select element
		addOptionEl:function(selectEl,value,label,sel){
			var optionEl = document.createElement("option");
			optionEl.setAttribute("value",value);
			if(sel==true)optionEl.setAttribute("selected","selected");
			optionEl.innerHTML = label;
			selectEl.appendChild(optionEl);
			return optionEl;
		}
	}

})(); // execute immediately

// end of script
