/*
 * DAAPI Widget 
 * Wade Couch
 * 7/9/2008 - BetterRecipes Redesign
 * 
 */		
 
	var ProcessUrl = "http://sitelife.betterrecipes.com/ver1.0/Direct/Process";
	var PersonaPagePath = "http://recipes.betterrecipes.com/betterrecipes/persona.jsp?&plckUserId=";
	
	var searchObject = "Comment"; 	//User or Comment
	var searchParam = ""; 			// if User, "DisplayName:"
	var searchValue = "a* b* c* d* e* f* g* h* i* j* k* l* m* n* o* p* q* r* s* t* u* v* w* x* y* z*"; 			//any comment with letters!
	var numPerPage=10; 			//cannot be more than 10
	var pageNum=1;
	
	function SearchPassthrough() {
		if (SearchFeild.value !=''){
			searchValue= SearchFeild.value+'*';
		}else{
			searchValue='';
		}
		searchObject = "User";
		searchParam = "DisplayName:";
		pageNum=1;
		doDiscovery();
	}
		
		
		
	function PageDown(){
		pageNum=pageNum-1;
		if (!pageNum > 0){
			prevLinkTop.style.display = 'none';
			prevLinkBot.style.display = 'none';
			 pageNum = 1;
		}
		doDiscovery();
	}
	
	
	function PageUp(){
		pageNum=pageNum+1;
		doDiscovery();
	}		
		
				
	function doDiscovery(){
		OutputHtml = '';
		if (SearchFeild.value != ''){
			searchValue = SearchFeild.value;
		}
		var requestBatch = new RequestBatch();
		requestBatch.AddToRequest( new SearchAction(searchObject, searchParam+searchValue, numPerPage, pageNum) );
		if (window.console) {
			console.log("Requesting "+searchObject+"(s) matching:" + searchValue);
		}
		requestBatch.BeginRequest(ProcessUrl, renderCallback);
	}
		
		

		function renderCallback(responseBatch){
			/*~*/
			if (window.console) {
				console.dir(responseBatch);
			}
			var searchString = null;
			try {
				var resultCount = responseBatch.Responses[0].SearchResult.NumberOfSearchResults;
				var searchString = responseBatch.Responses[0].SearchResult.SearchString;
			} 
			catch (e) {
				outputElem.innerHTML += "Error running a search (see Firebug console)<br />";
				alert(responseBatch);
			}
			
			
			
			/*Records Display*/
			var bottomRecord = (pageNum*numPerPage);
			var topRecord = bottomRecord-(numPerPage-1);			
			var LastWholePage = (Math.floor(resultCount / numPerPage)*10)/10;
			
			if (LastWholePage<0){
				LastWholePage=1;
			}
			
			var LastPage = LastWholePage;
			var LeftOvers = resultCount % numPerPage;

			if (LeftOvers > 0) {
				LastPage = LastPage + 1;
			}			

			if (pageNum==LastPage){
				bottomRecord = resultCount;
				if (LeftOvers>0){
					topRecord = bottomRecord - (LeftOvers - 1);					
				}else{
					topRecord = bottomRecord - (numPerPage - 1);						
				}
			}
			
			var RecordsValues=topRecord+' to '+bottomRecord+' of '+resultCount;
								
			RecordsShown.innerHTML = RecordsValues;
			RecordsShownBottom.innerHTML = RecordsValues;

			
			function displayNavLinks(){
				//alert("CurrentPage: "+pageNum+", LastWholePage: "+LastWholePage+", LastPage: "+LastPage+", LeftOvers: "+LeftOvers);
									
				if (resultCount > numPerPage) {
					nextLinkTop.style.display = 'block';
					nextLinkBot.style.display = 'block';
				}
				if (pageNum>=LastPage) {
					nextLinkTop.style.display = 'none';
					nextLinkBot.style.display = 'none';							
				}
				if (pageNum > 1){
					prevLinkTop.style.display = 'block';
					prevLinkBot.style.display = 'block';	
				}
				if (pageNum == 1){
					prevLinkTop.style.display = 'none';
					prevLinkBot.style.display = 'none';	
				}
			}			
			
			var OutputHtml = '';	
			
			if (resultCount > 0) {		
				if (searchString != null) {
					widdgetWrapper.style.display = 'block';	
					displayNavLinks();
						var OutputHtml = '';
						OutputHtml += '<div class="segment">';
						OutputHtml += '<table class="cooks_tbl" cellspacing="0" cellpadding="0">';
						OutputHtml += '<tbody>';
						if (searchObject=="Comment"){
										for (var i = 0; i < numPerPage; i++) {
												/* Odd ~ Even Switch for BG Color */
												var ModOrNot = i % 2;
												if (ModOrNot != 0) {
													ClassMod = ' Class="odd"';
												}
												else {
													ClassMod = '';
												}
	
										var MyResult = responseBatch.Responses[0].SearchResult.SearchResults[i].Author.UserKey.Key;
										var MyResultAvatar = responseBatch.Responses[0].SearchResult.SearchResults[i].Author.AvatarPhotoUrl;
										var MyResultDname = responseBatch.Responses[0].SearchResult.SearchResults[i].Author.DisplayName;
										var MyResultLUp = responseBatch.Responses[0].SearchResult.SearchResults[i].Author.LastUpdated;
										
										OutputHtml += '<tr' + ClassMod + '>';
										OutputHtml += '<td>';
										OutputHtml += '<img class="image-left" title="' + MyResultDname + '" src="' + MyResultAvatar + '" />';
										OutputHtml += '<a href="' + PersonaPagePath + MyResult + '&userId=' + MyResult + '">' + MyResultDname + '</a>';
										OutputHtml += '<br/>' + MyResultLUp;
										OutputHtml += '<td>';
										OutputHtml += '<tr>';
										outputElem.innerHTML = OutputHtml;									
										}
							
								OutputHtml += '</tbody>';
								OutputHtml += '</table>';
								OutputHtml += '</div>';
								outputElem.innerHTML = OutputHtml;
						}else{
									for (var i = 0; i < numPerPage; i++) {
											/* Odd ~ Even Switch for BG Color */
											var ModOrNot = i % 2;
											if (ModOrNot != 0) {
												ClassMod = ' Class="odd"';
											}
											else {
												ClassMod = '';
											}

									var MyResult = responseBatch.Responses[0].SearchResult.SearchResults[i].UserKey.Key;
									var MyResultAvatar = responseBatch.Responses[0].SearchResult.SearchResults[i].AvatarPhotoUrl;
									var MyResultDname = responseBatch.Responses[0].SearchResult.SearchResults[i].DisplayName;
									var MyResultLUp = responseBatch.Responses[0].SearchResult.SearchResults[i].LastUpdated;
									
									OutputHtml += '<tr' + ClassMod + '>';
									OutputHtml += '<td>';
									OutputHtml += '<img class="image-left" title="' + MyResultDname + '" src="' + MyResultAvatar + '" />';
									OutputHtml += '<a href="' + PersonaPagePath + MyResult + '&userId=' + MyResult + '">' + MyResultDname + '</a>';
									OutputHtml += '<br/>' + MyResultLUp;
									OutputHtml += '<td>';
									OutputHtml += '<tr>';
									outputElem.innerHTML = OutputHtml;									
									}
						
							OutputHtml += '</tbody>';
							OutputHtml += '</table>';
							OutputHtml += '</div>';
							outputElem.innerHTML = OutputHtml;
						}

				}
				
			}			
			else{
				// Default to the Top5 here....
	//	searchObject = "Comment"; 	//User or Comment
	//	searchParam = ""; 			// if User, "DisplayName:"
		//searchValue = "t"; 			//string appended with *  'b' is arbitrary...
				//alert(searchString);
				OutputHtml = '';
				outputElem.innerHTML = OutputHtml;
				widdgetWrapper.style.display = 'block';
				RecordsShown.style.display = 'block';
				RecordsShownBottom.style.display = 'none';
				RecordsShown.innerHTML = resultCount+' Cooks Found';
				prevLinkTop.style.display = 'none';
				prevLinkBot.style.display = 'none';
				nextLinkTop.style.display = 'none';
				nextLinkBot.style.display = 'none';
			}
		}
	