/* Browser check */
var IS_DOM = (document.getElementById) ? true : false;
var IS_IE = (document.all) ? true : false;
var IS_IE50 = (navigator.userAgent.indexOf("IE 5.0") != -1);
var IS_Mac = (navigator.appVersion.indexOf("Mac") != -1);

function getThis(sId)
{
	var oObject;
	oObject = false;
	
	if (IS_DOM) {
		if (document.getElementById(sId)) {
			oObject = document.getElementById(sId);
		}
	}
	
	return oObject;
}


function changeImages() 
{
  if (IS_DOM) {
    for (var i = 0; i < changeImages.arguments.length; i += 2) { 
  		document.getElementById(changeImages.arguments[i]).src = eval(changeImages.arguments[i + 1] + ".src"); 
	}
  }
}

// function used to sort by number
function numberOrder(a, b)
{
	return a - b;
}

function popChatWin()
{
	//popFile(sBaseUrl + "/shared/popup.htm",'',0,0,10,10,'none');
}

function popFile(file,windowName,top,left,width,height,scrollbars)
{
	windowAttributes = "top=" + top + ",left=" + left + ",width=" + width + ",height=" + height + ",scrollbars=" + scrollbars + ",resizable=yes";
	window.open(file,windowName,windowAttributes);
}

function Trim( inputString )
{
	var str = inputString;
	
	while ( str.charCodeAt( 0 ) == 32 )
	{
		str = str.substr( 1 );
	}
	
	while ( str.charCodeAt( str.length - 1 ) == 32 )
	{
		str = str.substr( 0, str.length - 1 );
	}
	
	return str;
}
// This function creates a new HTML element which contains only child nodes which are other HTML elements
function cleanOutWhitespace(theElement)
{
	// Create an array which will hold the needed HTML elements
	var storeHTMLElement = new Array();
	var arrayCounter = 0;
	for (i = 0; i < theElement.childNodes.length; i++)
	{
		// Store HTML elements in the array
		if (theElement.childNodes[i].nodeType == 1) {
			storeHTMLElement[arrayCounter] = theElement.childNodes[i];
			arrayCounter = arrayCounter + 1;
		}
	}
	
	// Remove all child nodes
	// Set a variable to the length, because the length changes every time a node is removed
	var elementLength = theElement.childNodes.length;
	for (i = 0; i < elementLength; i++)
	{
		theElement.removeChild(theElement.childNodes[0]);
	}
	
	// Append HTML elements back onto the parent element
	for (i = 0; i < storeHTMLElement.length; i++)
	{
		theElement.appendChild(storeHTMLElement[i]);
	}
	
	return theElement;
}

function hoverRowOn(theRow)
{
	for (i = 0; i < theRow.childNodes.length; i++)
	{
		if (theRow.childNodes[i].nodeType == 1) {
			theRow.childNodes[i].style.background = "#fff url(../../shared/images/content-design/bg-row-hover.gif) top left repeat-x";
			theRow.childNodes[i].style.cursor = "pointer";
			if (IS_IE) {
				theRow.childNodes[i].style.cursor = "hand";
			}
			
		}
	}
}

function hoverRowOff(theRow)
{
	for (i = 0; i < theRow.childNodes.length; i++)
	{
		if (theRow.childNodes[i].nodeType == 1) {
			theRow.childNodes[i].style.background = "#fff";
		}
	}
}

function isdefined(object, variable)
{
	return (typeof(eval(object)[variable]) != 'undefined');
}

/* =================================================================== */
// function to add events crossbrowser
// note: probably needs a removeEvent function at some point
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);
/* =================================================================== */
/* =================================================================== */
// various link functionality - popups, external, onstate
var anchors = {
	a : Object,
	doPopups : function() {
		if (!document.getElementsByTagName) return false;
		var links = document.getElementsByTagName("a");
		for (var i=0; i < links.length; i++) {
			var anchor = links[i];
			if (anchor.getAttribute('href') && anchor.rel.match('external')) {
				anchor.target = '_blank';
				//cssjs('add',anchor,'external');
				YAHOO.util.Dom.addClass(anchor,'external');
			}
		    if (anchor.className.match("movie")) {
				anchor.onclick = function() {
		        	window.open(this.href, "newwin","width=480, height=268, toolbar=no");
		        	return false;
				}
		    }
			
		}
	}
}
//addEvent(window,'load',anchors.doPopups);
YAHOO.util.Event.onDOMReady(anchors.doPopups);

// init search
(function() {
	function initSearch() {
		var buttons = [
			{ input: "searchField", button: "searchButton" },
			{ input: "searchField2", button: "searchButton2" },
			{ input: "inlineSearchField", button: "inlineSearchButton" },
			{ input: "focusSearchField", button: "focusSearchButton" }
		];
		
		for (var i = 0, j = buttons.length; i < j; i++) {
			var input = document.getElementById(buttons[i].input);
			var button = document.getElementById(buttons[i].button);
			
			if (input && button) {
				YAHOO.util.Event.on(input, "keypress", handleSubmit);
				YAHOO.util.Event.on(button, "click", handleClick, input);
			}
		}
	}
	function handleClick(e, searchInput) {
		YAHOO.util.Event.preventDefault(e);
		redirect(searchInput.value);
	}
	function handleSubmit(e) {
		if (e.keyCode == 13) {
			YAHOO.util.Event.preventDefault(e);
			redirect(this.value);
		}
	}
	function redirect(searchTerm) {
		if (searchTerm != 'search name/number' && searchTerm != 'search products') {
			window.location = sBaseUrl + "/catalog/search-results.aspx?term=" + escape(searchTerm);
		}
	}
	YAHOO.util.Event.on(window, "load", initSearch);
})();


/* =================================================================== */
// use this to automatically clear a text box of it's default value
// if nothing is typed in the box, the script will put the default value back
// useful on sites where search boxes don't have a seperate label
// you can use it for more than one box per page using the txtBoxes array
txtBoxClear = {
	txtBoxes : ['searchField', 'searchField2', 'inlineSearchField', 'focusSearchField'],
	init: function() {
		for (i=0;i<txtBoxClear.txtBoxes.length;i++) {
			var oCurrentTxtBox = document.getElementById(txtBoxClear.txtBoxes[i]);
			if (!oCurrentTxtBox) { continue; }
			oCurrentTxtBox.defaultVal = oCurrentTxtBox.defaultValue;
			txtBoxClear.clearBox(oCurrentTxtBox);
		}
	},
	clearBox : function(txtBox) {
		txtBox.onfocus = function() {
			if (txtBox.value == txtBox.defaultVal) { txtBox.value = ''; } 
		};
		txtBox.onblur = function() {
			if (txtBox.value == '') { txtBox.value = txtBox.defaultVal; }
		};
	}
};
YAHOO.util.Event.on(window, "load", txtBoxClear.init);
/* =================================================================== */



/********************************************************
	Golf billboard functionality
********************************************************/
var golf = {
	config : {
		image01:	'img-golf-collection-01.jpg',
		image02:	'img-golf-collection-02.jpg',
		image03:	'img-golf-collection-03.jpg',
		urlRoot:	'',
		curState:	0,
		
		image:	'',
		
		links: {
			pass: '',
			back: '',
			details: '',
			shop: ''
		}
	},
	
	init : function () {
		golf.config.image = YAHOO.util.Dom.getChildren(YAHOO.util.Dom.getElementsByClassName('productListContent')[0])[1];
		var alt = golf.config.image.alt;
		var src = golf.config.image.src;
		golf.config.urlRoot = src.slice(0,src.lastIndexOf('/')+1);
		
		if (alt.match('Golf')) {
			golf.config.links.pass = document.getElementById('player-pass');
			golf.config.links.back = document.getElementById('back');
			golf.config.links.details = document.getElementById('details');
			golf.config.links.shop = document.getElementById('shop');
			
			golf.setInitialImage();
			golf.initializeLinks(golf.config.links.pass);
			
			YAHOO.util.Event.on(golf.config.links.pass, "click", function (e) {
					YAHOO.util.Event.preventDefault(e);
					golf.playerPass();
			});
			YAHOO.util.Event.on(golf.config.links.back, "click", function (e) {
					YAHOO.util.Event.preventDefault(e);
					golf.back();
			});
			YAHOO.util.Event.on(golf.config.links.details, "click", function (e) {
					YAHOO.util.Event.preventDefault(e);
					golf.details();
			});
		}
	},
	
	setInitialImage : function () {
		golf.config.image.src = golf.config.urlRoot+golf.config.image01;
		golf.config.curState = 0;
	},
	
	initializeLinks : function (firstLink) {
		YAHOO.util.Dom.addClass(golf.config.links.back, 'hide');
		YAHOO.util.Dom.addClass(golf.config.links.details, 'hide');
		YAHOO.util.Dom.addClass(golf.config.links.shop, 'hide');
		YAHOO.util.Dom.removeClass(firstLink, 'hide');
	},
	
	playerPass : function () {
		golf.config.image.src = golf.config.urlRoot+golf.config.image02;
		YAHOO.util.Dom.addClass(golf.config.links.pass, 'hide');
		YAHOO.util.Dom.removeClass(golf.config.links.back, 'hide');
		YAHOO.util.Dom.removeClass(golf.config.links.details, 'hide');
		YAHOO.util.Dom.removeClass(golf.config.links.shop, 'hide');
		
		golf.config.curState = 1;
	},
	
	details : function () {
		golf.config.image.src = golf.config.urlRoot+golf.config.image03;
		YAHOO.util.Dom.addClass(golf.config.links.pass, 'hide');
		YAHOO.util.Dom.removeClass(golf.config.links.back, 'hide');
		YAHOO.util.Dom.addClass(golf.config.links.details, 'hide');
		YAHOO.util.Dom.addClass(golf.config.links.shop, 'hide');
		
		golf.config.curState = 2;
	},
	
	back : function () {
		var state = golf.config.curState;
		
		if (state == 2) {
			golf.playerPass();
		}else{
			golf.setInitialImage();
			golf.initializeLinks(golf.config.links.pass);
		}
	}
}
//YAHOO.util.Event.onDOMReady(golf.init);


/********************************************************
	BIOM Retailer Hide/Show
********************************************************/
function divSwitch() {
	var  contentDiv = document.getElementById('retailers');
	var  buttonDiv = document.getElementById('hideShow');

	  if (contentDiv.style.display == "" ) {
		 contentDiv.style.display = "block";
		 buttonDiv.className = 'showBtn';
		 
	    } else {
		 contentDiv.style.display = "";
		 buttonDiv.className = 'hideBtn';
	}
}

/********************************************************
	Contest Expand/Collapse
********************************************************/
function divExpand() {
	var  contentDiv = document.getElementById('resortDetails');
	var  buttonDiv = document.getElementById('hideShow');

	  if (contentDiv.style.display == "" ) {
		 contentDiv.style.display = "block";
		 buttonDiv.innerHTML = "close facility details";
	    } else {
		 contentDiv.style.display = "";
		 buttonDiv.innerHTML = "view facility details";
	}
}


