// trackin debug flag

var cmdebug = getValue(location.search,"cmdebug") ;

// cookie related
	var days2keepCookie = 365;
	var cookieName = "nikebasketballHomeLocation";
	var currentCookie = getCookie( cookieName );
	
	function setCookie( name, value, path, domain, secure ) {
		var today = new Date();
		var expires = new Date();
		if (days2keepCookie==null || days2keepCookie==0) days2keepCookie=1;
		expires.setTime( today.getTime() + 3600000 * 24 * days2keepCookie);
		var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		document.cookie = curCookie;
	}
	
	
	// name - name of the desired cookie
	// * return string containing value of specified cookie or null if cookie does not exist
	function getCookie(name) {
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return "";
		} else
		begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) end = dc.length;
		return unescape(dc.substring(begin + prefix.length, end));
	}
	
	// if the cooke has been set, redirect
	function redirectToRegion() {
	
		if (currentCookie != null && currentCookie != "") {
			if ( currentCookie.indexOf("?") > -1 ) {
				currentCookie = currentCookie + "&ref=bball_global_redirect";
			} else {
				currentCookie = currentCookie + "?ref=bball_global_redirect";
			}
			document.location.href = currentCookie;
		}
	}

// track first then redirct to another URL, setting cookie if needed
function trackAndGoToUrl( link, actionTag, referral, cookieOk ) {
	Tracker.track( actionTag );
	
	if (referral == null || referral == "")
		var referral = defaultHost;

	if (referral == null || referral == "")
		var referral = window.location.hostname;

	if( referral && referral != null && referral == "global_home" ) {
		referral = "bball_global_home" ;
	}
   
	//alert( "link: " + link + "\nactionTag: " + actionTag + "\nreferal: " + referral + "\ncookieOk: " + cookieOk );

	if (link != null && link != "") {
		if ( cookieOk != null && cookieOk == "true" ) {
			setCookie(cookieName,link,"/");
		}
		  	  
		if ( link.indexOf("?") > -1 ) {
			link = link + "&ref=" + referral;
		} else {
			link = link + "?ref=" + referral;
		}

		// If in Debug mode, display the link
		if( cmdebug && cmdebug !=null && cmdebug== "yes" ) {
			link += "&cmdebug=yes" ;
			alert("in debug mode link= "+ link) ;
		}

		// defined in common.js
		gotoOtherSite( link )
		
   	} else {
		// System.out.println("The javascript:goToUrl() function was called with either a null or empty url");
	}
}


// pass it the queryString and the variable name
// it will return the value of that parameter or it does not exist, null
function getValue ( queryString, parameterName ) {
        if ( queryString.charAt( 0 ) == "?" ) queryString = queryString.substr(1);
        var query_array = queryString.split("&");
        for (var i=0; i< query_array.length; i++ ) {
                query_array[ i ] = query_array[ i ].split("=");
                if ( query_array[ i ][ 0 ] == parameterName ) {
                        return query_array[ i ][ 1 ];
                        break;
                }
        }
        return null;
}

//this functionality is exclusive for USA nikebasketball baller of the week
//create properties for baller of the week pop-up window 
var newWindow;

function bow_open ( quality ) {
	
	if ( quality == "lo" ) {
		newWindow = window.open("family/bow/bow_lo.html", "lo", "height=200, width=240");
		
	} else {
		newWindow = window.open("family/bow/bow_hi.html", "hi", "height=260, width=320");

	}
}

//open window from flash
function openNewWindow( URLtoOpen, windowName, windowFeatures ) {
	newWindow = window.open(URLtoOpen, windowName, windowFeatures); 
}

var surveyWindow;

//survey pop up window
function open_survey () {

	var fixWidth = 8;
	var fixHeight = 28;	
	var screenHeight = screen.availHeight-fixHeight;
	var screenWidth = screen.availWidth-fixWidth;

	if ( screenWidth < 900 || screenHeight < 650 ) {
		//popWindow = window.open( url, "pop_window", "height="+height+", width="+width+", resizable, scrollbars" );
		surveyWindow = window.open( "http://www.surveymonkey.com/s.asp?u=935661377463", "pop_window", "height=350, width=300, resizable, scrollbars" );
	} else {
		surveyWindow = window.open( "http://www.surveymonkey.com/s.asp?u=935661377463", "pop_window", "height=650, width=780, resizable, scrollbars" );
		
	}
}

function setHash( newHash, newTitle )
{
	var agent = navigator.userAgent.toLowerCase();
	if ( ( agent.indexOf("macintosh") < 0 && agent.indexOf("mac_") < 0 ) &&
		navigator.appName.toLowerCase().indexOf( "microsoft" ) >= 0 )
	{
		parent.window.setFlag(true);
		// alert("setHash() newHash: " + newHash + " qloc: " + qloc );
		if ( newHash != null )
		{
			parent.window.location.hash = newHash;
		}
		else
		{
			parent.window.location.hash = unescape( qloc );
		}
		
		if ( newTitle != null )
		{
			title = newTitle;
		}
		else if(queryVars.title && queryVars.title != null && queryVars.title != "" && unescape(queryVars.title) != "_root")
		{
			title = unescape(queryVars.title);
		}
		else
		{
			title = "Nike Basketball";
		}
		parent.document.title = title;
	}
	
	
}
	
function checkHash() {
if ( parent.window )
{
	if ( parent.window.location.hash != hashLoc )
	{
		if ( parent.window.getFlag() )
		{
			// alert("checkHash() getFlag is true: hashLoc is " + hashLoc);
			hashLoc = parent.window.location.hash;
			// alert("set hashLoc to: " + parent.window.location);
		}
		else
		{
			// alert("utils reload parent.window.getFlag() " + parent.window.getFlag() + " TYPEOF " + typeof(parent.window.getFlag()) + " parent.window.location.hash " + parent.window.location.hash + " hashLoc " + hashLoc);
			parent.window.location.reload( false );
		}
	
	}
	parent.window.setFlag(false);
	}
}

/* ------------------------------------------------------------
   Used for getting the users browser
   ------------------------------------------------------------ */

// browser detection
var _appName = navigator.appName;
var _appVersion = navigator.appVersion;
var _userAgent = navigator.userAgent.toLowerCase();

function getBrowser() {
      
      var browser = '';
      
      if ((_appName.indexOf('Microsoft') != -1) && (_userAgent.indexOf('mac') == -1)) {
		  browser = 'IE';
	  // We're treating MAC IE as a special case anymore
	  } else if ((_appName.indexOf('Microsoft') != -1) && (_userAgent.indexOf('mac') != -1)) {		
	      browser = 'Other';
	  } else if (_userAgent.indexOf('safari') != -1) {
		  browser = 'Safari';
	  } else if (_userAgent.indexOf('firefox') != -1) {
		  browser = 'Firefox';
	  } else {
		  browser = 'Other';
	  }
	  return browser;
}

