
/* 	when user visits between downtime and uptime (AZ time), they will
	be redirected the maintenance page.
*/
var downtime = new Date("April 15, 2006 6:00:00"); // server goes down
var uptime = new Date("August 16, 2007 8:00:00"); // server back up
	
	
// extract domain from URL to get correct path to "images" dir and preload rollover images
var url = window.location.href;
var domain = url.substring(0, url.indexOf("/", 7));// grab everything from "http://" until the first slash
if(domain == "http://community.pinggolf.com")
{	domain = "http://www.pinggolf.com";
}
var globalImageDirectory = domain + "/images/";
//domain += url.substring(domain.length, domain.length+4); // UK needs to add language directory, ie. "/_uk"
var globalNavDirectory = domain + "/images/nav/";


// stores ids for all expandable navigation so you can control it dynamically
var expandableNav = new Array()
expandableNav["drivers"] = ['subDrivers','swtDrivers'];
expandableNav["fairways"] = ['subFairways','swtFairways'];
expandableNav["hybrids"] = ['subHyb','swtHyb'];
expandableNav["irons"] = ['subIrn','swtIrn'];
expandableNav["wedges"] = ['subWdg','swtWdg'];
expandableNav["putters"] = ['subPut','swtPut'];
expandableNav["bags"] = ['subBag','swtBag'];
expandableNav["accessories"] = ['subAcc','swtAcc'];
expandableNav["headwear"] = ['subAcc','swtAcc']; // same as accessories
expandableNav["womens"] = ['subWmn','swtWmn'];
expandableNav["juniors"] = ['subJr','swtJr'];
expandableNav["tour"] = ['subTour','swtTour'];
expandableNav["fitting"] = ['subFitting','swtFitting'];


// colors of each section
var sectionColors = new Array()
sectionColors["drivers"] = ['red'];
sectionColors["fairways"] = ['electron'];
sectionColors["hybrids"] = ['orange'];
sectionColors["irons"] = ['green'];
sectionColors["wedges"] = ['teal'];
sectionColors["putters"] = ['yellow'];
sectionColors["bags"] = ['purple'];
sectionColors["accessories"] = ['yellow'];
sectionColors["womens"] = ['purple'];
sectionColors["juniors"] = ['teal'];
sectionColors["tour"] = ['orange'];
sectionColors["fitting"] = ['electron'];

function getSectionColor(section)
{	return sectionColors[section];
}

/* preload images in main navigation */
var suffix = new Array('', '_f2');

function preLoad()
{	if(document.images)
  	{	var argLen = arguments.length;
   		for(var i = 0; i < argLen; i++)
		{	var arg = arguments[i];
			var sufLen = suffix.length;
			for(j = 0; j < sufLen; j++)
			{	var suf = suffix[j]
				self[arg + '_' + suf] = new Image();
				self[arg + '_' + suf].src = globalNavDirectory + arg + suf + '.gif';
				window.status="loading " + globalNavDirectory + arg + suf + '.gif';
}	}	}	}

/* rollovers */
function rollOver(iName, iState)
{  if(document.images && self[iName + '_' + iState])
    document.images[iName].src = self[iName + '_' + iState].src;
}

function onLoad()
{	preLoad('navDrivers','navFairways','navHybrids','navIrons','navWedges','navPutters','navBags','navAccessories','navWomens','navJuniors','navWrx','navTour','navFitting','globalFit','globalLocator','globalDemo','globalCustServ','behindScenes','timeline');
	noOp();
}

/* automatically preload images when page loads */
window.onLoad = onLoad();

// returns the anchor string at the end of the URL (ie. if the URL is "grips.html#woods", it returns "woods"
function getAnchor()
{	href=window.location.href;
	var index = href.indexOf("#", 0);
	if(index == -1)
	{	//alert("no anchor");
		return null;
	}
	else
	{	//alert("anchor: " + href.substring(index+1,href.length));
		return href.substring(index+1,href.length);	
}	}


function openNav(nav)
{	closeAllNav();
	if(nav != null && expandableNav[nav] != null)
	{	openClose(expandableNav[nav][0],expandableNav[nav][1])
		contentID = document.getElementById("content"); // find <div id="content">
		contentID.className = getSectionColor(nav); // change its color to match the appropriate section
}	}


function autoExpandNav(nav)
{	/*	if there's an anchor on the URL (ie. ../grips.html#woods), open the navigation
		indicated by the anchor.
		Otherwise, open the navigation for the section indicated by the page name.
		ie. http://www.pinggolf.com/fitting_index.html opens the fitting navigation
	*/
	nav = nav == null ? getAnchor() : nav; // if nav is not supplied, check for an anchor
	if(nav == null)
	{	url=window.location.href;
		var lastSlash = url.lastIndexOf("/", url.length-1);
		var uScore = url.indexOf("_", lastSlash);
		if(uScore > 0)
		{	nav = url.substring(lastSlash+1,uScore);
	}	}
	openNav(nav);
}


/* open and close the <div>s in the main expandableNav */
function openClose(id, id2) 
{	var subID = document.getElementById(id);
	var swtID = document.getElementById(id2);

	// toggle visibility
	if (subID.style.display == "block")
	{	subID.style.display = "none";
		swtID.className = "swt clearIt plus";
	}
	else 
	{	subID.style.display = "block";
		swtID.className = "swt clearIt minus";
} 	}


function closeAllNav()
{	for(nav in expandableNav)
	{	var subID = document.getElementById(expandableNav[nav][0]);
		var swtID = document.getElementById(expandableNav[nav][1]);
		if(subID != null)
		{	subID.style.display = "none";
			swtID.className = "swt clearIt plus";
}	}	}



/* POPUP WINDOWS FOR SPECIAL FEATURES */

function popup(u,n,w,h,l,t,s,r)
{	/*
	u=url
	n=name of window
	w= width
	h=height
	l=distance from left side
	t=distance from top
	s=scrollbars? boolean
	r=resizable? boolean
	*/
	var attributes="directories=no,toolbar=no,status=yes,menubar=no";
	attributes += ",width=" + w;
	attributes += ",height=" + h;
	attributes += ",left=" + l;
	attributes += ",top=" + t;
	var scroll = (s>0) ? "yes" : "no" ;
	if(s>1)
	{	scroll = "auto";
	}
	var resize= (r>0) ? "yes" : "no" ;
	attributes += ",scrollbars=" + scroll;
	attributes += ",resizable=" + resize;
	window.open(u,n,attributes);
}


// dedicated popup shortcuts:
function community(page)
{	// use the page parameter to take care of special cases by tweaking the URL
	
	if(checkCommunityAvailability())
	{	window.location.href = "global_maintenance.html";
	}
	else
	{	if(page=="knowledge")
		{	page="knowledge_search";
		}
		url="http://community.pinggolf.com/community/index.cfm?page=" + page;
		popup(url, 'community', 640, 450, 20, 20, 1, 1);
	}
}

function timeline()
{	url="http://www.pinggolf.com/flash/timeline/loader.html";
	popup(url, 'timeline', 816, 467, 20, 20, 0, 0);
}

function innovation()
{	url="http://www.pinggolf.com/flash/innovation/index.html";
	popup(url, 'innovation', 800, 460, 20, 20, 0, 0);
}

function checkCommunityAvailability()
{	var azOffset = -7; // Arizona's GMT offset is -7 hours
	var rightNow = new Date(); // time on user's computer
	var myOffset = checkTimeZone(); // user's GMT offset
	rightNow.setHours(rightNow.getHours() + (azOffset - myOffset)); // adjust user's time to AZ time
	return(rightNow > downtime && rightNow < uptime);
}

function checkTimeZone()
{	var rightNow = new Date();
   	var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
  	var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
	var temp = date1.toGMTString();
	var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var temp = date2.toGMTString();
	var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
	return(hoursDiffStdTime);
	 /*
		var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
		if (hoursDiffDaylightTime == hoursDiffStdTime) 
		{	alert("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is NOT observed here.");
		}
		else
		{	alert("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is observed here.");
		}
   */
}


function noOp()
{	window.status = "PING: Play Your Best";
}

/* makes sure the default phrase "search site" is not submitted */
function validateSiteSearch(fields)
{	return (fields.elements['sp-q'].value != "search site");
}





/* 	THESE FUNCTIONS CONTROL THE DYNAMIC HIDING & REVEALING
	OF <DIVS>,  IMAGES AND OTHER OBJECTS */

/* loadImage replaces the source of image */
function loadImage(imageName, newImage)
{	if(document.images)
  	{	document.images[imageName].src = newImage;
}   }

 
/* selectImage allows a user to choose an image from a select box for display.*/
function selectImage(pickList, imageObj, dir)
{	if(document.images)
  	{	var pic = pickList.options[pickList.selectedIndex].value;
		document.images[imageObj].src = dir + pic;
}   }


/* 	designed to work with selectImage, preloadOptions loops 
	through options in a select box	and preloads the images */
function preloadOptions(pickList, dir)
{	if(document.images)
  	{	for(var i=0; i<pickList.options.length; i++)
    	{	var pic = pickList.options[i].value;
			self[i] = new Image();
			self[i].src = dir + pic;
			// alert(dir + pic + " has been pre-loaded");
			window.status = "loading " + pic + "...";
	}	}   
	noOp();
}


function getSelectValue(selectObj)
{	return selectObj.options[selectObj.selectedIndex].value
}


/* 	divPicker lets user selectively display a <div> from a select box */
function divPicker(pickList)
{	if(pickList.options[pickList.selectedIndex].value != "")
	{	for(var i=0; i<pickList.options.length; i++)
		{	var id = document.getElementById(pickList.options[i].value);
			if(id)
			{	id.style.display = pickList.selectedIndex == i ? "block" : "none";
}	}	}	}


/* 	toggleDiv toggles the visibility of a <div> */
function toggleDiv(id)
{	var obj = document.getElementById(id);
	obj.style.display = obj.style.display == "none" ? "block" : "none";
}		


/* 	selectImageDiv lets user selectively display an image
and a matching <div> from a select box.

imageArray is an associative array image names
imageObj is the (name) label of the image whose src will be changed
dir is the directory where the images can be found
pickList is the select object in the form
 */

function selectImageDiv(pickList, dir, imageArray, imageObj)
{	// first, close the open div so you don't have two open at the same time
	for(var i=0; i<pickList.options.length; i++)
	{	var id = document.getElementById(pickList.options[i].value);
		id.style.display = "none";
	}
	// call divPicker to display selected div
	divPicker(pickList);
	
	// the selected value is also an index into the image array
	var imageSrc = imageArray[pickList.options[pickList.selectedIndex].value];
	
	// replace the image with the new source
	document.images[imageObj].src = dir + imageSrc;
}		



function selectDiv(div, divArray)
{	// display selected div, hide others in divArray
	//alert("divArray has " + divArray.length + " elements");
	for(var i=0; i<divArray.length; i++)
	{	var id = document.getElementById(divArray[i]);
		if(id != null)
		{	if(divArray[i] == div)
			{	id.style.display = "block";
			}
			else
			{	id.style.display = "none";
}	}	}	}


function showDiv(div)
{	// shows a div (not sure if it works for blocks)
	var id = document.getElementById(div);
	id.style.display = "inline";
}


function hideDiv(div)
{	// hides a div
	var id = document.getElementById(div);
	id.style.display = "none";
}


function changeTab(tabID, tabArray)
/*
used in conjunction with the "tabs.css" file, changeTab() toggles the class name of
two list elements that look like tabs so appear to be selected or unselected.
you would also call selectDiv() to display different content depending on which
tab was selected.
*/
{	
	//alert(document.getElementById(tabID).className);
	if(document.getElementById(tabID).className == "current")
	{	return(0); // exit if they clicked the active tab
	}
	
	for(var i=0; i<tabArray.length; i++)
	{	var tab=document.getElementById(tabArray[i]);
		if(tab.className == "current")
			tab.className = "";
		else if(tabArray[i] == tabID)
			tab.className = "current";
}	}


function getCookie(name)
{	var cname = name + "=";               
	var dc = document.cookie;             
    if (dc.length > 0) 
	{	begin = dc.indexOf(cname);       
        if (begin != -1) 
		{	begin += cname.length;       
        	end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
    }   }
	return null;
}


function setCookie(name, value, expires) 
{	document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}


function deleteCookie(name) 
{	document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" +  ";	path=/";
}


function convertDate(date)
{	// takes date object and converts to yyyymmdd string
	y=date.getYear();
	m=('0'+(date.getMonth()+1));		// force months to be numbered 1-12
	d=('0'+date.getDate());
	return(y+m.substring(m.length-2,m.length)+d.substring(d.length-2,d.length));
} 

// set cookie with last visit
var today = new Date();
today.setTime(today.getTime());
var xpdate = new Date();
xpdate.setTime(xpdate.getTime() + (1000 * 60 * 60 * 24 * 365)); // expires in one year
setCookie('last_visit',today,xpdate);


