if (!window.wxtools) wxtools = {};

/**
 * wxtools.RecentSearch
 *  a class that can render search results div dom object that can be placed directly into a page. For configuration, the class takes an array of hashs with certain optional parameter, like:
 * [ //The following config order determines the order in which the results types are displayed, e.g. the userPreference results display first for the following example 
 *	{id:"up",url:"/outlook/health/allergies/travel/", show:5}, //userPreference cookie(must if to display the data), the destination url(optional), and the number of results to display(optional)
 *	{id:"mp",url:"/outlook/health/allergies/travel/"}  //myPrefes cookie, the destination url
 *      {id:"wg",url:"/outlook/health/allergies/travel/"}  //wxgoldcookie not yet supported
 *      {id:"loc",url:"/outlook/health/allergies/travel/"}  //loc cookie not yet supported
 * ]
 * The class supports 3 methods for doing this:
 *   1. createResults - creates a DOM element which gets inserted into the element the page needs to display the results in.
 *   2. hide - hides the created DOM element.
 *   3. Public accessor functions that will make copy of the data that's been requested : getCopyUP27Data(), getCopyMyPrefsData() , getCopyWxGoldData() 
 *       The data returned has a structure of an array of hashs. To access the data, use something like:
 *	var data = obj.getCopyUP27Data();
 *	for (var i = 0; i < data.length; i++) {	
 *		locId = data[i].id;
 *		locationPresentationName = data[i].presName;
 *	}
 */ 

wxtools.RecentSearch = function(config) {
	with ({			
		_id:Math.floor(10000 * Math.random()),
		_config:config,
		//Recent Search Example
		//USGA0028:1:Atlanta, GA^12345:4:Schenectady,NY(12345)
		_up27:[],_up27URL:null,_up27NumOfResultsToDisplay:5,
		
		//MyPrefs Example
		//12345^4^Schenectady,+NY+(12345)^|*|*|*
		_myPrefs:[],_myPrefsURL:null,_myPrefsNumOfResultsToDisplay:5,
		
		//wxGold Example
		// 1032636891|11228|abABF|90045|12345|30093|22222|33333|44444|55555|66666|77777|88888|111111111|1|1|111111||||||||||||||||||||||y
		_wxGold:[],_wxGoldURL:null,_wxGoldNumOfResultsToDisplay:5,
		
		// Default values
		_linkURL:"http://www.weather.com/weather/local/",
		
		_retrieveWxGold:function(){
			var tmp = GetCookie("WxGold");
			tmp = tmp.split("|");
			for (var i=1;i<tmp.length && i<12;i++){
				if(i==2) continue;
				var id = tmp[i];
				var presentationName = tmp[i] + " not sure how to get city name";
				//presentationName = presentationName.length > 23 ? presentationName.substring(0,20) + "..." : presentationName;
				if(id)this._wxGold.push({id: id, presName: presentationName});
			}
		},		
		_retrieveUP27:function(){
			var tmp = getUserPreferences("27").split("^");
			if(tmp.length > 0 && tmp[0].indexOf(":") > 0 ){
				for (var i=0;i<tmp.length;i++){
					var t = tmp[i].split(":");
					var id = t[0];
					var presentationName = t[2];
					presentationName = presentationName.length > 23 ? presentationName.substring(0,20) + "..." : presentationName;
					this._up27.push({id: id, presName: presentationName});
				}
			}
		},		
		_retrieveMyPerfs:function(){
			var mp = GetCookie("MyPrefs");
			var tmp = unEscJava(mp.split("|").splice(0,1));
			if(tmp.length > 0 && tmp.indexOf("^") > 0 ){
				tmp = tmp.split("^");
				var id = tmp[0];
				var presentationName = tmp[2].replace("--",",");
				presentationName = presentationName.length > 23 ? presentationName.substring(0,20) + "..." : presentationName;
				this._myPrefs.push({id: id, presName: presentationName});
			}
			
			tmp = mp.split("|").splice(3,4);
			tmp = unEscJava(tmp[0]);
			if(tmp.length > 0 && tmp.indexOf(":") > 0 ){
				tmp = tmp.split(":");
				for (var i=0;i<5;i++){
					var id = tmp[i];//alert(id);
					var name = tmp[i+10].replace("--",",");//alert(name);
					name = name.length > 23 ? name.substring(0,20) + "..." : name;						
					if(id !="*" && this._myPrefs[0].id != id ) this._myPrefs.push({id: id, presName: name});
				}
			}
			
		},		
		_getSourceResults:function(data, CSS, destinationURL, NumOfResultsToDisplay){
			if(data.length > 0){
				var node = [''];
				for (var i = 0; i < data.length && i < NumOfResultsToDisplay; i++) {						
					if(i>0) node.push('<BR>');		
					node.push('<a class="', CSS, '" href ="', destinationURL, data[i].id, '" from="recentsearch">');
					node.push(data[i].presName, '</a>');
				}
				return node.join('');
			}		
		},	
		_createResultsDiv:function(results) {
			if(results){
				var divNode = document.createElement('div');
				divNode.innerHTML = results;
				return divNode;
			}
			return null;
		},		
		_createUP27DivResults:function(divCSS, text) {
			var divHeaderText = (text) ?  text: "Recent Searches";
			return this._createHeaderDiv(divCSS, divHeaderText);			
		},		
		_createMyPrefsDivResults:function(divCSS, text) {
			var divHeaderText = (text) ?  text: "My Favorites";
			return this._createHeaderDiv(divCSS, divHeaderText);			
		},		
		_createWxGoldDivResults:function(divCSS, text) {
			var divHeaderText = (text) ?  text: "My Gold";
			return this._createHeaderDiv(divCSS, divHeaderText);			
		},		
		_createHeaderDiv:function(divCSS, text) {
			var divNode = document.createElement('div');
			divNode.className = divCSS;
			divNode.innerHTML = text;
			return divNode;				
		},
		_createCloseIcon:function(contID, contCSSHide){
			var elem = this._isObjectHidden();
			if (elem) {	return elem;}
			var img = document.createElement("img");
			img.id = "img" + this._id;
			img.src = "http://image.weather.com/web/common/icons/sm_close_box.gif";
			img.onclick = function(){document.getElementById(contID).className = contCSSHide;}
			return img;
		},
		_isObjectHidden:function(){
			return document.getElementById("img" + this._id);
		},		
		_eleminateDuplicates:function(from, against){
			var tmp = [];
			var dups = false;
			for (var i = 0; i < from.length; i++) {
				for (var j = 0; j < against.length; j++) {if(against[j].id == from[i].id){dups = true;}	}
				if(!dups) tmp.push({id: from[i].id, presName: from[i].presName});
				else dups = false
			}
			return tmp;
		},
		
		_makeCopy:function(list) { 
			rtn = [];
			for (var i = 0; i < list.length; i++) {						
				rtn.push({id: list[i].id, presName: list[i].presName});
			}
			return rtn;
		}
	}) 
	
	{		
		for(var i=0;i<_config.length;i++){
			var tmp = _config[i].id;
			switch (tmp) {
				case 'up': 	_retrieveUP27();
							_config[i].url ? _up27URL = _config[i].url : _up27URL = _linkURL;
							_config[i].show ? _up27NumOfResultsToDisplay = _config[i].show : "use default";
							break;
				case 'mp': 	_retrieveMyPerfs();
							_config[i].url ? _myPrefsURL = _config[i].url : _myPrefsURL = _linkURL;
							_config[i].show ? _myPrefsNumOfResultsToDisplay = _config[i].show : "use default";
							break;
				case 'wg': 	_retrieveWxGold();
							_config[i].url ? _wxGoldURL = _config[i].url : _wxGoldURL = _linkURL;
							_config[i].show ? _wxGoldNumOfResultsToDisplay = _config[i].show : "use default";
							break;
				//case 'loc': _retrieveWxGold();
			}		
		}
						
		this.hide = function(containerID, containerCSSHide) {
			if (_isObjectHidden()) {_isObjectHidden().onclick();return;}						
		};				
		
		this.createResults = function(containerID, containerCSSShow, containerCSSHide, rsHeaderDivCSS, resultsCSS) {
			if (_isObjectHidden()) {document.getElementById(containerID).className = containerCSSShow;return;}
			var icon = _createCloseIcon(containerID, containerCSSHide);
			var iconPrinted = false;
			var up27div,mpdiv,wgdiv,locdiv;			
			var up27t,mpt,wgt,loct;			
			
			rs = _up27; // Current requirement is to eliminate duplicates from the usrPreference data source.
			for(var i=0;i<_config.length;i++){
				var tmp = _config[i].id;
				switch (tmp) {
					case 'up': 	break;
					case 'mp': 	rs = _eleminateDuplicates(rs,_myPrefs);	break;
					case 'wg': 	rs = _eleminateDuplicates(rs,_wxGold);break;
					/*case 'loc': 	rs = _eleminateDuplicates(rs,_loc);
								wgdiv = _createLocDivResults(containerID, containerCSSShow, rsHeaderDivCSS);
								break;*/
				}		
			}
			
			// Create the container which will hold the links for each data source
			for(var i=0;i<_config.length;i++){
				var tmp = _config[i].id;
				switch (tmp) {
					case 'up': 	up27t= _createResultsDiv(_getSourceResults(rs,resultsCSS,_up27URL, _up27NumOfResultsToDisplay));
								if(up27t){ 
									up27div = _createUP27DivResults(rsHeaderDivCSS);
									if(!iconPrinted){up27div.id = "div" + _id;up27div.appendChild(icon);iconPrinted=true;}
									document.getElementById(containerID).appendChild(up27div);
									document.getElementById(containerID).appendChild(up27t);
									document.getElementById(containerID).className = containerCSSShow;
								}
								break;
					case 'mp': 	mpt= _createResultsDiv(_getSourceResults(_myPrefs,resultsCSS,_myPrefsURL, _myPrefsNumOfResultsToDisplay));
								if(mpt){ 
									mpdiv = _createMyPrefsDivResults(rsHeaderDivCSS);
									if(!iconPrinted){mpdiv.id = "div" + _id;mpdiv.appendChild(icon);iconPrinted=true;}
									document.getElementById(containerID).appendChild(mpdiv);
									document.getElementById(containerID).appendChild(mpt);
									document.getElementById(containerID).className = containerCSSShow;
								}
								break;
					case 'wg': 	wgt= _createResultsDiv(_getSourceResults(_wxGold,resultsCSS,_wxGoldURL, _wxGoldNumOfResultsToDisplay));
								if(wgt){										
									wgdiv = _createWxGoldDivResults(rsHeaderDivCSS);
									if(!iconPrinted){wgdiv.id = "div" + _id; wgdiv.appendChild(icon);iconPrinted=true;}
									document.getElementById(containerID).appendChild(wgdiv);
									document.getElementById(containerID).appendChild(wgt);
									document.getElementById(containerID).className = containerCSSShow;
								}
								break;
					/*case 'loc': loct= _createTableResults(_getSourceResults(_loc));*/
				}		
			}						
		};
		this.getCopyUP27Data = function() { 
			return _makeCopy(_up27);
		};		
		this.getCopyMyPrefsData = function() { 
			return _makeCopy(_myPrefs);
		};
		this.getCopyWxGoldData = function() { 
			return _makeCopy(_wxGold);
		};
	}
}

