var textFields = {};
var scriptLoaders = [];

if('undefined' == typeof $) {
	window.$ = function(sElement) {
		return document.getElementById(sElement);
	};
}

var elementHasClass = function(oElem, sSearchClass) {
	var sClass = oElem.className;
	var bHasClass = false;
	
	if(sClass != null) {
		var aClasses = sClass.split(new RegExp("/\s+/"));
		
		for(var i=0; i < aClasses.length; i++) {
			sClass = aClasses[i];
			
			if(sClass == sSearchClass) {
				bHasClass = true;
				break;
			}
		}
	}
	
	return bHasClass;
};

var getElementsByClassValue = function(oParent, sSearchClass, aMatches) {
	var oChild = oParent.firstChild;
	
	if(aMatches == null) {
		aMatches = [];
	}
	
	var stack = [];
	stack[0] = oChild;
	var iPos = 0;
	
	while(stack[0] != null) {
		oChild = stack[iPos];
		
		if(oChild == null) {
			stack[--iPos] = stack[iPos].nextSibling;
		} else{
			if(oChild.nodeType == 1) {
				if(window.elementHasClass(oChild, sSearchClass)) {
					aMatches = aMatches.concat([oChild]);
				}
			}
			
			if(++iPos < 1024) {
				stack[iPos] = oChild.firstChild;
				//aMatches = window.getElementsByClassValue(oChild, sSearchClass, aMatches);
			}
		}
	}
	
	return aMatches;
};

var scriptLoader = function() {
	var aNoScriptElems = window.getElementsByClassValue(document, 'noscript');
	for(var i=0; i < aNoScriptElems.length; i++) {
		aNoScriptElems[i].style.display = 'none';
	}
	
	var aSubMenus = window.getElementsByClassValue($('mainNav'), 'dropdown_menu');
	
	for(var i=0; i < aSubMenus.length; i++) {
		var oSubMenu = aSubMenus[i];
		oSubMenu.style.display = "none";
		var sSubMenuID = oSubMenu.getAttribute("id");
		var oSubMenuButton = $(sSubMenuID + 'Button');
		
		oSubMenuButton.onmouseout = function() {
			MM_swapImgRestore();
			
			var sAnchorID = this.getAttribute('id');
			var sSubMenuID = sAnchorID.match(new RegExp('(.+?)Button'))[1];
			$(sSubMenuID).style.display = 'none';
		};
		
		oSubMenuButton.onmouseover = function() {
			var oImage = null;
			var oChild = this.firstChild;
			
			while(oChild != null && oChild.nodeType != 1 && oChild.nodeName != 'img') {
				oChild = oChild.nextSibling;
			}
			
			oImage = oChild;
			
			if(oImage != null) {
				var sImageID = oImage.getAttribute('id');
				var sImageURI = oImage.getAttribute('src');
				var aImageURIComponents =
					sImageURI.match(new RegExp('^(.+?)\.([^.]+)$'));
				var sFilename = aImageURIComponents[1];
				var sFileExt = aImageURIComponents[2];
				
				MM_swapImage(sImageID, '', sFilename + '_over' + '.' + sFileExt, 1);
				
				var sAnchorID = this.getAttribute('id');
				var sSubMenuID = sAnchorID.match(new RegExp('(.+?)Button'))[1];
				$(sSubMenuID).style.display = 'block';
			}
		};
		
		oSubMenu.onmouseover = function() {
			this.style.display = 'block';
		};
		
		oSubMenu.onmouseout = function() {
			this.style.display = 'none';
		};
	}
	
	return;
	
	// Transform all labels with class "insideField" (for text input fields only)
	// to dynamic text that appears in the field and goes away when the user
	// accesses the field.
	
	// Find all elements with the class "insideField"
	var elems = window.getElementsByClassValue(document, 'insideField');
	
	// Iterate through each element
	for(var i=0; i < elems.length; i++) {
		var elem = elems[i];
		
		// For each element, get the element name
		var name = elem.nodeName.toLowerCase();
		
		// If the element is a "label" element, get the value of the "for"
		// attribute
		if(name == 'label') {
			var label = elem;
			
			// Get the label's text
			var labelText = label.firstChild.nodeValue;
			
			// Check for the existence of a "for" attribute
			var forAttribute = label.getAttributeNode('for');
			if(forAttribute != null) {
				// Get the value of the "for" attribute.
				var forValue = forAttribute.nodeValue;
				
				// Get the element referenced by the value of the "for" attribute
				elem = document.getElementById(forValue);
				
				// Get the element name
				name = elem.nodeName.toLowerCase();
				
				// If the element is an "input" element, check for the existence of a
				// "type" attribute
				if(name == 'input') {
					var textField = elem;
					var textFieldID = forValue;
					
					// Check for the existence of a "type" attribute
					var typeAttribute = textField.getAttributeNode('type');
					if(typeAttribute != null) {
						// Get the value of the "type" attribute"
						var fieldType = typeAttribute.nodeValue.toLowerCase();
						
						// If the value of the "type" attribute is "text" or "password",
						// associate the field ID with the label text
						if(fieldType == 'text' || fieldType == 'password') {
							// Save the field ID, the label text, and the field type in an
							// array
							window.textFields[textFieldID] = {};
							window.textFields[textFieldID]['label'] = labelText;
							window.textFields[textFieldID]['type'] = fieldType;
							
							// If the type is "password", change it temporarily to "text"
							if(fieldType == 'password') {
								textField.type = 'text';
							}
							
							label.parentNode.removeChild(label);
							
							// Set the field's value to the label text
							textField.value = labelText;
							
							// Attach events to the field so that the text disappears when the
							// field gets "focus" and reappears when the field loses "focus"
							textField.onfocus = function() {
								var textFieldID = this.getAttribute('id');
								var input = window.textFields[textFieldID]['input'];
								
								if(input == null || input == '') {
									this.value = '';
								}
								
								this.type = window.textFields[textFieldID]['type'];
							};
							
							textField.onblur = function() {
								var textFieldID = this.getAttribute('id');
								var labelText = '';
								
								if(this.value != null && this.value != '') {
									window.textFields[textFieldID]['input'] = this.value;
									labelText = this.value;
								} else {
									window.textFields[textFieldID]['input'] = '';
									this.type = 'text';
									labelText = window.textFields[textFieldID]['label'];
								}
								
								this.value = labelText;
							};
						}
					}
				}
			}
		}
	}
};

window.scriptLoaders = window.scriptLoaders.concat([scriptLoader]);

window.onload = function() {
	for(var i=0; i < window.scriptLoaders.length; i++) {
		window.scriptLoaders[i]();
	}
};
