var g_cache_whole_list = false;
var g_max_cache_entries = 1000;
var g_query_prev = null;
var oParsedResponseGlobal = null;

var g_staged = (document.getElementById("sp_staged") ? document.getElementById("sp_staged").value : 0);

function init_cache_mode()
{
	var acDSGetLineCount = new YAHOO.util.ScriptNodeDataSource( (g_staged ? "http://content.atomz.com/autocomplete/sp10/04/1e/8d-stage/" : "http://content.atomz.com/autocomplete/sp10/04/1e/8d/"));
		
	acDSGetLineCount.responseSchema = {
		resultsList: "ResultSet.Result",
		fields: ["line_count"]
	};

	// Called asynchronusly!
	acDSGetLineCount.handleResponse = function(oRequest, oRawResponse, oCallback, oCaller, tId) {
		var line_count = oRawResponse;
		g_cache_whole_list = (line_count != null && line_count <= g_max_cache_entries);

		if (g_cache_whole_list)
			HD.Nav.init_cache_whole_list_mode();	
	}
	// Trigger asynchronus request / response.
	acDSGetLineCount.sendRequest("?get_line_count=1", null);
}

// Data source
var acDataSource = new YAHOO.util.ScriptNodeDataSource( (g_staged ? "http://content.atomz.com/autocomplete/sp10/04/1e/8d-stage/" : "http://content.atomz.com/autocomplete/sp10/04/1e/8d/"));
acDataSource.asyncMode="ignoreStaleResponses";
acDataSource.maxCacheEntries = g_max_cache_entries;
acDataSource.responseSchema = {
	resultsList: "ResultSet.Result",
	fields: ["phrase"]
};

function validate_query_and_submit(text) {
	if (text.value=="SEARCH") {
		text.value="*";
	}

	window.location='http://search.harley-davidson.com?q=' + text.value;
	
 	return false;
}

//TODO: remove all below after netmining is done (8/10/10) ~ 4 months from now

if (typeof HD == "undefined" || !HD) {
	  var HD = {};
	} 
if (typeof HD.util == "undefined" || !HD.util) {
  HD.util = {};
}

/**
 * @namespace HD.util.Cookie
 */
HD.util.Cookie = { 
  
	/**
	 * This method returns a cookie value
	 * 
	 * @method get
	 * @param  {String} identifier cookie name
	 * @param  {Object or Function} (optional) options contains a boolean raw and a converter function
	 * @return {String} the value
	 */
	get : function (identifier, options){
		
		return YAHOO.util.Cookie.get(identifier, options);
	}
};
  
HD.util.Localization = { 
		  
	/**
	 * Possible cookie names for locale
	 */	
	localeCookieNames : ['cwpLocCountryLanguage'],
	
	/**
	 * Possible url param names for locale
	 */	
	localeParamNames : ['locale'],
	
	/**
	 * This method returns a locale from a path 
	 * 
	 * @method getLocaleSegment
	 * @param  {String} path the path to be segmented
	 * @return {String} the locale
	 */
	getLocaleSegment : function (path){
		
		// Break the path into segments
		var locale = "";
		var segments = HD.util.Url.segmentURL(path);
		
		// Try to match a locale example: en_US
		if(segments.length > 1){
			if (segments[1].match(/[a-z][a-z]_[a-z][a-z]/i)){
				locale = segments[1];
			}
		}
		return locale;
	},
	
	/**
	 * This method returns a locale from a cookie 
	 * 
	 * @method getLocaleSegment
	 * @param  {String} path the path to be segmented
	 * @return {String} the locale
	 */
	getLocaleCookie : function (){
		
		// Loop through the cookies for an existing match
		var locale = "";
		for (var i=0; i<this.localeCookieNames.length; i++){
			var cookie = HD.util.Cookie.get(this.localeCookieNames[i]);
			if (cookie){
				locale = cookie;
				break;
			}
		}
		return locale;
	},
	
	/**
	 * This method returns a locale from a url 
	 * 
	 * @method getLocaleParam
	 * @return {String} the locale
	 */
	getLocaleParam : function (){
		
		// Loop through the params for an existing match
		var locale = "";
		for (var i=0; i<this.localeParamNames.length; i++){
			var cookie = HD.util.Url.getRequestParam(this.localeParamNames[i]);
			if (cookie){
				locale = cookie;
				break;
			}
		}

		return locale;
	},
	
	/**
	 * This method returns a locale from the current page 
	 * 
	 * @method getLocale
	 * @return {String} the locale
	 */
	getLocale : function(){
		var locale = this.getLocaleSegment(location.pathname) 
		|| this.getLocaleCookie() 
		|| this.getLocaleParam() || '';
		
		return locale;
	}
};

HD.util.Url = { 
		  
	/**
	 * This method checks the current url to see if it contains a match to the 
	 * passed in name. 
	 * 
	 * @method getRequestParam
	 * @param  {String} name  the url that we are parsing
	 * @return {String}       the match
	 */
	getRequestParam : function (name) {
		if (!name) {return;}
		name = name.replace(/\[/g,"\[").replace(/\]/g,"\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(window.location.href);
		var res = results == null ? null : results[1];
		return !!res && res.match(/^(true|1)$/) ? true :
			(!!res && res.match(/^(false|0)$/) ? false : 
			(!!res && res.match(/^(null|undefined)$/) ? null : res));
	},
	
	/**
	 * This method breaks a url into segments delimited by slashes 
	 * 
	 * @method segmentURL
	 * @param  {String} url the url to be segmented
	 * @return {Array}  the segments
	 */
	segmentURL : function (url){
		var segments = new Array;
		if (url){
			segments = url.split('/');
		}
		return segments;
	}
};
	
YAHOO.util.Event.onDOMReady(function () {
	var dletectedLocale = HD.util.Localization.getLocale();
	if(dletectedLocale == 'en_US' || dletectedLocale == 'de_DE') {
		var nm_proto = (('https:' == document.location.protocol) ? 'https://': 'http://');
		YAHOO.util.Get.script(nm_proto + 'com-harley.netmng.com/');
	}
});


