(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 notSafariCheck() {
		var notIe = (BrowserDetect.browser != 'Safari') ? true : false;
		return notIe;
	}
	window['NICE']['notSafariCheck'] = notSafariCheck;
	
	function notIpadCheck() {
		var notIpad = (BrowserDetect.OS != 'iPad') ? true : false;
		return notIpad;
	}
	window['NICE']['notIpadCheck'] = notIpadCheck;
	
	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
			var sel = selDiv.split('-')[0];
			if (sel != 'alpha' && sel != 'region') {	// Custom condition for OpenSRS
				$('#' + 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 = {};
		var leadOpt = null;
		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;
				if (o.readAttribute('selected') == true) {
					leadOpt = o.innerHTML;
				}
			});
			if (leadOpt == null) 
				leadOpt = optsVals[0].string;
			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(leadOpt);
			});
			$$('#' + 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();
				if ($(this).attr('selected') == true) {
					leadOpt = $(this).html();
				}
			});
			if (leadOpt == null) 
				leadOpt = optsVals[0].string;
			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(leadOpt);
			$('#' + 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 id="alpha-'+ optsVals[i].value +'" href="#'+ optsVals[i].value +';"><span>' + optsVals[i].string + '</span></a></li>');
			});
			
			
			if (selectId == 'alpha') {	// Custom condition for OpenSRS
				$('#' + selWrap + ' div.selClone a.selOpt span').html('Alpha');
			} else if (selectId == 'region') {	// Custom condition for OpenSRS
				$('#' + selWrap + ' div.selClone a.selOpt span').html('Region');
			}
			
			/*$('#' + 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;
})();

function textInputTextColour(id) {
	var attrValue = $('#' + id).attr('value');
	$('#' + id).focus(function() {
		if ($(this).attr('value') == attrValue) {
			$(this).attr('value', '');
			$(this).css('color', '#000');
		}
	});
	$('#' + id).blur(function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value', attrValue);
			$(this).css('color', '#8c7f7f');
		}
	});
}

function searchButtonHover(button) {
	$('#' + button).hover(function() {
		$(this).attr('src', 'images/button_quickSearch_on.gif');
	}, function() {
		$(this).attr('src', 'images/button_quickSearch_off.gif');
	});
}

var HideShow = {
	toggle: function(id) {
		$('#toggle-' + id).toggle(function() {
			if (ie7) 
				$('#hs-' + id).show();
			else 
				$('#hs-' + id).slideDown(500);
			$(this).attr('class', 'hsToggle hstOpen');
			return false;
		}, function() {
			if (ie7) 
				$('#hs-' + id).hide();
			else 
				$('#hs-' + id).slideUp(500);
			$(this).attr('class', 'hsToggle hstClosed');
			return false;
		});
	},
	init: function(id) {
		this.toggle(id);
	}
};

var RsSelect = {
	currSection: null,
	transitioning: false,
	markCurrent: function() {
		$('form.filterSelector fieldset ul li.button a').css('color', '#d83');
		$('#rsBut-' + RsSelect.currSection).css('color', '#000');
	},
	setButtons: function() {
		$('form.filterSelector fieldset ul li.button a').click(function() {
			var nextSect = $(this).attr('id').split('-')[1];
			if (nextSect != RsSelect.currSection) {
				if (!RsSelect.transitioning) {
					RsSelect.transitioning = true;
					$('#stage-' + RsSelect.currSection).fadeOut(250, function() {
						RsSelect.currSection = nextSect;
						RsSelect.markCurrent();
						$('#stage-' + RsSelect.currSection).fadeIn(250, function() {
							RsSelect.transitioning = false;
						});
					});
				}
			}
			return false;
		});
	},
	init: function(set) {
		this.currSection = set;
		this.markCurrent();
		this.setButtons();
	}
};



var NicePoint = {
	mOutTimer: 0,
	currSection : $('ul#ssnUl li.current'),
	getPositionArrow: function(button) {
	    
	    if($(button).attr('href')){
	        li = $(button).parent("li");
	        widthOffset = $(li).width()/2;
		    return  $(li).position().left + widthOffset;
	    }else if($('ul#ssnUl li.current').size()!=0){
	        
	        return this.getPositionArrow($('ul#ssnUl li.current a'));
        }else{
	       return -10;
	    }
	},
	movePointer: function(loc) {
		$('#pointer').animate({
		'left': loc + 'px'
		}, 300);
	},
	getInitial: function(){
	  
	    if($('ul#ssnUl li.current').size()===0){
	        return null;
	    }else{
	        return $('ul#ssnUl li.current a');
	    }
	},
	mOut: function() {
		this.movePointer(NicePoint.getPositionArrow(NicePoint.currSection));
	},
	init: function() {
	   
	//	this.currSection = current;
		$('#pointer').css({
			'left': NicePoint.getPositionArrow(NicePoint.getInitial()) + 'px'
		});
		$('ul#ssnUl li a').hover(function() {
		    
			clearTimeout(NicePoint.mOutTimer);
			
			NicePoint.movePointer(NicePoint.getPositionArrow($(this)));
			}, function() {
				NicePoint.mOutTimer = setTimeout('NicePoint.mOut();', 500);
		});
	}
};

/*
//new and improved wiht fade in and out
 if($('#tagCarouselHome').length>0){
        var current_home_banner=0;
        function moveTo(pos){
             $('#rotator-space').animate({
                 left: pos
               }, 500);
        }
        function cyclehome(){
            current_home_banner+=1;
            if(current_home_banner>2)
                current_home_banner=0;
            moveTo(-746 * current_home_banner);
        }
        $('li.hp-menu a').click(function(){
            clearInterval(timer);
            var id = $(this).parent("li").attr("id").split("-")[1];
            var pos = (-746 * parseInt(id));
            moveTo(pos);
        });
   
       var timer = setInterval( cyclehome, 5000);
      }
*/

if($('#tagCarouselHome').length>0){
       var current_home_banner=0;
       var stop_cycle=false;
       function fadeTo(item)
       {
           $(".tcContent").fadeOut();
           $(".tcContent:eq("+item+")").fadeIn();
           
       }
       
       $('li.hp-menu a').click(function(){
           clearInterval(timer);
           var id = $(this).parent("li").attr("id").split("-")[1];
          
           fadeTo(id);
       });
        function cyclehome(){
               current_home_banner+=1;
               if(current_home_banner>2){
                   current_home_banner=0;
                   stop_cycle=true;
               }
                   
               fadeTo(current_home_banner);
               if(stop_cycle){
                 clearInterval(timer);
               }
           }
      var timer = setInterval( cyclehome, 7500);
     }


      
    
