(function() {
	window['NICE'] = {};
	
	/*
		The following notIeCheck(), ie7Check() and ie6Check() 
		functions require the "BrowserDetect" object function 
		in order to work. It can be downloaded from 
		http://www.quirksmode.org/js/detect.html.
	*/
	
	function notIphoneCheck() {
		var notIphone = (BrowserDetect.OS != 'iPhone/iPod') ? true : false;
		return notIphone;
	}
	window['NICE']['notIphoneCheck'] = notIphoneCheck;
	
	function notIeCheck() {
		var notIe = (BrowserDetect.browser != 'Explorer') ? true : false;
		return notIe;
	}
	window['NICE']['notIeCheck'] = notIeCheck;
	
	function ie8Check() {
		var isIe8 = ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version >= 8) && (BrowserDetect.version < 9)) ? true : false;
		return isIe8;
	}
	window['NICE']['ie8Check'] = ie8Check;
	
	function ie7Check() {
		var isIe7 = ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version >= 7) && (BrowserDetect.version < 8)) ? true : false;
		return isIe7;
	}
	window['NICE']['ie7Check'] = ie7Check;
	
	function ie6Check() {
		var isIe6 = ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 7)) ? true : false;
		return isIe6;
	}
	window['NICE']['ie6Check'] = ie6Check;
	
	function macSafariCheck() {
		var macSafari = ((BrowserDetect.browser == 'Safari') && (BrowserDetect.OS == 'Mac')) ? true : false;
		return macSafari;
	}
	window['NICE']['macSafariCheck'] = macSafariCheck;
	
	function rootPath(ind) {
		/*
			The value of ind should be whatever the class is for the <body> tag 
			on the index.html page.
		*/
		var rp = null;
		if (window.Prototype) // Checking for Prototype library
			rp = ($$('body')[0].hasClassName(ind) == true) ? '' : '../';
		else if (window.jQuery) // Checking for jQuery library
			rp = ($('body').hasClass(ind) == true) ? '' : '../';
		return rp;
	}
	window['NICE']['rootPath'] = rootPath;
	
	function buttonHovers(img) {
		if (window.Prototype) { // Checking for Prototype library
			$$('img').each(function(g) {
				if (g.readAttribute('src').split('_off')[0] == img) {
					var file = g.readAttribute('src').split('_off');
					g.observe('mouseover', function() {
						this.writeAttribute({
							src: file[0] + '_on' + file[1]
						})
					});
					g.observe('mouseout', function() {
						this.writeAttribute({
							src: file[0] + '_off' + file[1]
						})
					});
				}
			});
		} else if (window.jQuery) { // Checking for jQuery library
			jQuery.each($('img'), function() {
				if ($(this).attr('src').split('_off')[0] == img) {
					var file = $(this).attr('src').split('_off');
					$(this).hover(function() {
						$(this).attr('src', file[0] + '_on' + file[1]);
					}, function() {
						$(this).attr('src', file[0] + '_off' + file[1]);
					});
				}
			});
		}
	}
	// window['NICE']['butHovDirect'] = buttonHovers;
	function setButtonHovers(buttons) {
		// if (ie6 == false) {
			if (window.Prototype) { // Checking for Prototype library
				buttons.each(function(b) {
					buttonHovers(b);
				});
			} else if (window.jQuery) { // Checking for jQuery library
				jQuery.each(buttons, function() {
					buttonHovers(this);
				});
			}
		// }
	}
	window['NICE']['buttonHovers'] = setButtonHovers;
	
	function altTableRows(trPath, colour) {
		if (window.Prototype) { // Checking for Prototype library
			$$(trPath).each(function(tr, i) {
				if (0 != i % 2) {
					tr.childElements().each(function(td) {
						td.setStyle('background-color: ' + colour + ';');
					});
				}
			});
		} else if (window.jQuery) { // Checking for jQuery library
			$(trPath + ':odd td').css({
				'background-color': colour
			});
		}
	}
	window['NICE']['altTableRows'] = altTableRows;
	
	function blurNiceSelect(selDiv) {
		if (window.Prototype) { // Checking for Prototype library
			setTimeout('$$("#' + selDiv + ' div.selClone ul").each(function(ul) {ul.hide();});', 200);
			setTimeout('$$("#' + selDiv + ' div.selClone a.selOpt").each(function(a) {a.toggleClassName("soOpen");});', 200);
		} else if (window.jQuery) { // Checking for jQuery library
			/*setTimeout('$("#' + selDiv + ' div.selClone ul").hide();', 200);
			setTimeout('$("#' + selDiv + ' div.selClone a.selOpt").removeClass("soOpen");', 200);*/
			$('#' + selDiv + ' div.selClone ul').hide();
			$('#' + selDiv + ' div.selClone a.selOpt').removeClass('soOpen');
		}
	}
	window['NICE']['blurNiceSelect'] = blurNiceSelect;
	
	function openCloseNiceSelect(selDiv) {
		if (window.Prototype) { // Checking for Prototype library
			$$('#' + selDiv + ' div.selClone ul').each(function(ul) {
				ul.toggle();
			});
			$$('#' + selDiv + ' div.selClone a.selOpt').each(function(a) {
				a.toggleClassName('soOpen');
			});
		} else if (window.jQuery) { // Checking for jQuery library
			$('#' + selDiv + ' div.selClone ul').toggle();
			$('#' + selDiv + ' div.selClone a.selOpt').toggleClass('soOpen');
		}
	}
	window['NICE']['openCloseNiceSelect'] = openCloseNiceSelect;
	
	function valUpdate(selDiv, pos, val, string) {
		if (window.Prototype) { // Checking for Prototype library
			$$('#' + selDiv + ' div.selClone a.selOpt span').each(function(s) {
				s.update(string);
			});
			$$('#' + selDiv + ' div.selClone ul').each(function(ul) {
				ul.writeAttribute('class', val);
			});
			$$('#' + selDiv.split('-')[0] + ' option').each(function(o, i) {
				if (o.readAttribute('value') == val) 
					o.writeAttribute('selected', 'selected');
			});
		} else if (window.jQuery) { // Checking for jQuery library
			$('#' + selDiv + ' div.selClone a.selOpt span').html(string);
			$('#' + selDiv + ' div.selClone ul').attr('class', val);
			jQuery.each($('#' + selDiv.split('-')[0] + ' option'), function(i) {
				if ($(this).attr('value') == val) 
					$(this).attr('selected', 'selected');
			});
		}
		openCloseNiceSelect(selDiv);
	}
	window['NICE']['valUpdate'] = valUpdate;
	
	function niceSelect(selectId) {
		var optsVals = {};
		if (window.Prototype) { // Checking for Prototype library
			$$('#' + selectId + ' option').each(function(o, i) {
				optsVals[i] = [];
				optsVals[i].value = o.readAttribute('value');
				optsVals[i].string = o.innerHTML;
			});
			var selWrapId = $(selectId).readAttribute('id') + '-wrap';
			$(selectId).wrap('div', {
				id: selWrapId,
				'class': 'niceSelectWrap'
			});
			$(selectId).hide();
			$(selWrapId).insert('<div class="selClone"><a href="" class="selOpt"><span></span></a><ul class="" style="display: none;"></ul></div>');
			/*
				The above .insert() function creates the following XHTML mark-up 
				inside the $(selWrapId) div...
				
				<div class="selClone">
					<a href="" class="selOpText"><span></span></a>
					<ul>
					</ul>
				</div>
			*/
			$$('#' + selWrap + ' div.selClone').each(function(d) {
				d.observe('mouseout', function(d) {
					if ($('#' + selWrap + ' div.selClone a.selOpt').hasClass('soOpen')) 
						openCloseNiceSelect(selWrap);
				});
			});
			$$('#' + selWrapId + ' div.selClone a.selOpt').each(function(a) {
				a.writeAttribute('href', 'javascript:NICE.openCloseNiceSelect("' + selWrapId + '");').down().insert(optsVals[0].string);
			});
			$$('#' + selWrapId + ' div.selClone ul').each(function(ul) {
				ul.writeAttribute('class', optsVals[0].value);
			});
			$$('#' + selectId + ' option').each(function(o, i) {
				$$('#' + selWrapId + ' div.selClone ul')[0].insert('<li class="' + optsVals[i].value + '"><a href="javascript:NICE.valUpdate(\'' + selWrapId + '\', ' + i + ', \'' + optsVals[i].value + '\', \'' + optsVals[i].string + '\');"><span>' + optsVals[i].string + '</span></a></li>');
			});
		} else if (window.jQuery) { // Checking for jQuery library
			var $sel = $('#' + selectId);
			var $opts = $('#' + selectId + ' option');
			jQuery.each($opts, function(i) {
				optsVals[i] = [];
				optsVals[i].value = $(this).attr('value');
				optsVals[i].string = $(this).html();
			});
			var selWrap = $sel.attr('id') + '-wrap';
			$sel.wrap('<div></div>').parent().attr('id', selWrap).attr('class', 'niceSelectWrap');
			$sel.hide();
			var $selWrap = $('#' + selWrap);
			$selWrap.append('<div class="selClone"><a href="" class="selOpt"><span></span></a><ul class="" style="display: none;"></ul></div>');
			/*
				The above .insert() function creates the following XHTML mark-up 
				inside the $selWrap div...
				
				<div class="selClone">
					<a href="" class="selOpText"><span></span></a>
					<ul>
					</ul>
				</div>
			*/
			$('#' + selWrap + ' div.selClone').mouseleave(function() {
				if ($('#' + selWrap + ' div.selClone a.selOpt').hasClass('soOpen')) 
					openCloseNiceSelect(selWrap);
			});
			$('#' + selWrap + ' div.selClone a.selOpt').attr({
				'href': 'javascript:NICE.openCloseNiceSelect("' + selWrap + '");'
			}).children('span').html(optsVals[0].string);
			$('#' + selWrap + ' div.selClone ul').attr('class', optsVals[0].value);
			jQuery.each($opts, function(i) {
				$('#' + selWrap + ' div.selClone ul').append('<li class="' + optsVals[i].value + '"><a href="javascript:NICE.valUpdate(\'' + selWrap + '\', ' + i + ', \'' + optsVals[i].value + '\', \'' + optsVals[i].string + '\');"><span>' + optsVals[i].string + '</span></a></li>');
			});
			
			
			
			
			/*$('#' + selWrap + ' div.selClone a.selOpt ul').blur(function() {
				//window.alert($(this).siblings('ul').focus());
				if ($(this).siblings('ul').focus()) {
					$(this).removeClass('soOpen');
					$(this).siblings('ul').hide();
				}
			});*/
			
			
		}
	}
	window['NICE']['niceSelect'] = niceSelect;
	
	function loadFuncs(funcs) {
		if (window.Prototype) { // Checking for Prototype library
			funcs.each(function(f) {
				document.observe("dom:loaded", f);
			});
		}
	}
	window['NICE']['loadFuncs'] = loadFuncs;
})();