// library of functions for learning zone clips site
// was all in global space, now exports to a single global object (2008-09-10 MET).

window.lzc_lib = (function() {

	var lib = null; // object to be returned

/* not currently used:

*/

// FUNCTIONS NOT EXPORTED (referenced here only)

	function browsecontains(a, e) {
		for(j=0;j<a.length;j++)if(a[j]==e)return true;
		return false;
	}

// EXPORTED FUNCTIONS:

	lib = {	// the library functions

		// Add an event in a browser-independent way
		addEvent: function (objNode, strEvent, fnHandler)
		{
			if (objNode.attachEvent)
			{
				objNode['e' + strEvent + fnHandler] = fnHandler;
				objNode[strEvent + fnHandler] = function() { objNode['e' + strEvent + fnHandler](window.event); }
				objNode.attachEvent('on' + strEvent, objNode[strEvent + fnHandler]);
			}
			else
			{
				objNode.addEventListener(strEvent, fnHandler, false);
			}
		},
		
		// Add a class to an element
		addClass: function(objElement, strClass)
		{
			objElement.className += objElement.className ? ' ' + strClass : strClass;
		},

		// Build query string for QueryEngine
		buildQuery: function(arrUserInput, arrFields, arrOperators, arrBooleans, intStartField)
		{
			var intFieldCount = arrFields.length;
			var strQueryString = '';

			// Build the querystring for queryengine
			for (var i = 0; i < intFieldCount; i++)
			{
				var strFields = '&attrib_' + (intStartField + i + 1) + '=' + arrFields[i];
				var strOperators = '&oper_' + (intStartField + i + 1) + '=' + arrOperators[i];
				var strValues = '';
				
				if ((arrOperators[i] == 're') && (arrUserInput[i] != '.'))
				{
					strValues = '&val_' + (intStartField + i + 1) + '_1' + '=\\b' + escape(arrUserInput[i]) + '\\b';
				}
				else
				{
					if ((arrOperators[i] != 're') && (arrUserInput[i].indexOf('|') > 0))
					{
						// This is not a regular expression, but it's a multiple match, so assign multiple values to val field
						var arrMatches = arrUserInput[i].split('|');
						for (var j = 0; j < arrMatches.length; j++)
						{
							strValues += '&val_' + (intStartField + i + 1) + '_1' + '=' + escape(arrMatches[j]);
						}
					}
					else
					{
						strValues = '&val_' + (intStartField + i + 1) + '_1' + '=' + escape(arrUserInput[i]);
					}
				}
				
				// We need one less boolean operator than number of parameters
				if (i != (intFieldCount - 1))
				{
					strBooleans = '&bool_' + (intStartField + i + 1) + '=' + arrBooleans[i];
				}
				else
				{
					strBooleans = '';
				}

				strQueryString += strFields + strOperators + strValues + strBooleans;
			}

			return strQueryString;
		},

		// Fetch the first child element in a browser-independent way
		getFirstChildElement: function(objNode)
		{
			var objFirstChild = objNode.firstChild;

			while (objFirstChild.nodeType != 1)
			{
				objFirstChild = objFirstChild.nextSibling;
			}

			return objFirstChild;
		},

		// Get user input from text box
		getData: function(strNode)
		{
			return document.getElementById(strNode).value;
		},

		// Hide input field and insert placeholder text (if empty)
		hideField: function(strFieldId, strPlaceholderText)
		{
			if ((document.getElementById(strFieldId).value == '') || (document.getElementById(strFieldId).value == strPlaceholderText))
			{
				// Grey out terms field
				document.getElementById(strFieldId).style.color = '#b0b0b0';

				// Insert placeholder text
				document.getElementById(strFieldId).value = strPlaceholderText;
			}
		},

		// Convert a folder name to a level name
		levelName: function(strFolderName)
		{
			switch(strFolderName.substr(0, 1))
			{
				case 'p':
					return 'Primary';
				case 's':
					return 'Secondary';
				case 'f':
					return 'College';
			} 
			return 'Unknown';
		},

		// Display input field and clear placeholder text
		// field can be HTML object or id of same.
		prepareFieldForInput: function(fld)
		{
			var o_fld = (typeof fld == 'string') ? document.getElementById(fld)	: fld;

			o_fld.style.color = '#000';
			if (o_fld.value == o_fld.defaultValue) o_fld.value = '';
		},

		// Grey out input field and insert placeholder text (if empty)
		restoreFieldToDefault: function(fld)
		{
			var o_fld = (typeof fld == 'string') ? document.getElementById(fld)	: fld;

			if (o_fld.value == '') 
				o_fld.value = o_fld.defaultValue;

			if (o_fld.value == o_fld.defaultValue)
				o_fld.style.color = '#b0b0b0';
		},

		// Remove a class from an element
		removeClass: function(objElement, strClass)
		{
			var replace = objElement.className.match(' ' + strClass) ? ' ' + strClass : strClass;
			objElement.className = objElement.className.replace(replace, '');
		},

		// Display input field and clear placeholder text
		showField: function(strFieldId, strPlaceholderText)
		{
			// Display field fully
			document.getElementById(strFieldId).style.color = '#000';

			// Remove placeholder text if present
			if (document.getElementById(strFieldId).value == strPlaceholderText) document.getElementById(strFieldId).value = '';
		},

		// Convert a folder name to a subject name
		subjectName: function(strFolderName)
		{
			switch(strFolderName.substr(2, 4))
			{
				case 'artd':
					return 'Art and Design';
				case 'biol':
					return 'Biology';
				case 'busi':
					return 'Business Studies';
				case 'chem':
					return 'Chemistry';
				case 'citz':
					return 'Citizenship';
				case 'clas':
					return 'Classical Studies';
				case 'desi':
					return 'Design and Technology';
				case 'econ':
					return 'Economics';
				case 'engi':
					return 'Engineering';
				case 'engl':
					return 'English';
				case 'geog':
					return 'Geography';
				case 'govp':
					return 'Government and Politics';
				case 'hsoc':
					return 'Health and Social Care';
				case 'hist':
					return 'History';
				case 'huma':
					return 'Humanities';
				case 'ict*':
					return 'ICT';
				case 'it**':
					return 'IT';
				case 'lang':
					return 'Languages';
				case 'law*':
					return 'Law';
				case 'leis':
					return 'Leisure and Tourism';
				case 'lite':
					return 'Literacy';
				case 'manu':
					return 'Manufacturing';
				case 'math':
					return 'Maths';
				case 'musi':
					return 'Music';
				case 'nume':
					return 'Numeracy';
				case 'pe**':
					return 'PE';
				case 'pshe':
					return 'PHSE';
				case 'phys':
					return 'Physics';
				case 'psyc':
					return 'Psychology';
				case 'reli':
					return 'Religious Education';
				case 'scie':
					return 'Science';
				case 'arts':
					return 'The Arts';
				case 'vces':
					return 'VCE science/applied science';
				case 'wels':
					return 'Welsh';
				case 'trto':
					return 'Travel and Tourism';
				case 'sktu':
					return 'Skills and Tutorial';
				case 'bufe': 
					return 'Business';
			} 
			return 'Unknown';
		},

		unique: function(a) {
			tmp = new Array(0);
			for(i=0;i<a.length;i++){
				if(!browsecontains(tmp, a[i])){
					tmp.length+=1;
					tmp[tmp.length-1]=a[i];
				}
			}
			return tmp;
		},

		dummy_not_called: null // so we can every other value with ","
	};

	return lib;

})(); // call the function immediately

// end of script
