var initialState = true;
var thequery = "";
//var localePath = "";
//var localeName = "";
var queryName = "q";
var debug = false;
var allowSearchByTyping = true;
var str = "";

function getTheResults(query){

	if(!allowSearchByTyping){
		return;
	}

	thequery = query;
	query = $.trim(query);
	
	// We want to search the entire content, not just meta data
	searchService.searchByContent(localeName, query+"~0.7", 0, 3, showTheResults);

	return false;
}

function showTheResults(result){
	str = "";
	i=1;
	if(result.totalHits > 0){
		if(result.groups.product){
			// productMsgLabel is not getting populated hence hardcoding it, with the value in properties file.
				
			//str+="<p>"+productMsgLabel+"</p><ul>";			
				if (productMsgLabel){
					str+="<p>"+productMsgLabel+"</p><ul>";
					}
				else if (localePath == "fr-CA")
				{	
					str+="<p>Produits</p><ul>";
				}
				else
				{	
					str+="<p>Products</p><ul>";
				}		
			
				$.each(result.groups.product, function(n,value){
					if((value.pojo || debug)&&i<=10) {
						if (value.pojo.name != undefined) {
							// For Product pages
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\"" +"/procell"+ value.pojo.URI + "\">" + value.pojo.name + "</a></li>";
						} else {
							// For Series pages (Product Accessories)
							procellUrl = value.pojo.URI;
							procellUrl = procellUrl.slice(procellUrl.indexOf("/series"));
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\"" +"/procell/"+ localePath + procellUrl + "\">" + value.pojo.title + "</a></li>";
						}
					}
					i++;
				});
				if(i>10)	
					str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+"/product view all')\" href=\"/" +"procell/"+ localePath + "/search-results.jspx?category=product&"+queryName+"=" + thequery + "\">"+SrchMoreResults+"</a></li>";
				str+="</ul>";
		}
		
		if(result.groups.promotions){
				str+="<p>Promotions</p><ul>";
				i=1;
				$.each(result.groups.promotions, function(n,value){
					if((value.pojo || debug)&&i<=10)
						str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+value.pojo.URI+"\">" + value.pojo.title + "</a></li>";
					i++;
				});
				if(i>10)
					str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+"/promotions view all')\" href=\"/" + localePath + "/search-results.jspx?category=promotions&"+queryName+"=" + thequery + "\">"+SrchMoreResults+"</a></li>";
				str+="</ul>";
		}
	
		if(result.groups.toolsInfo){
				str+="<p>Tools &amp; Info</p><ul>";
				i=1;
				$.each(result.groups.toolsInfo, function(n,value){
					if((value.pojo || debug)&&i<=10){
						if(value.type=='faqCategory')
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">FAQ: " + value.pojo.id + "</a></li>";
						else if(value.type=='companyInfomation')
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">" + value.pojo.id + "</a></li>";
						else if(value.type=='contact')
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">" + value.pojo.id + "</a></li>";
						else if(value.type=='toolsLanding')
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">" + value.pojo.id + "</a></li>";
						else if(value.type=='compareTool')
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">" + value.pojo.id + "</a></li>";
						else if(value.type=='batteryCare')
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">" + value.pojo.id + "</a></li>";
						else if(value.type=='pressRelease')
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">" + value.pojo.id + "</a></li>";
						else if(value.type=='page') {
							str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+ value.pojo.URI+"')\" href=\""+"/procell"+value.pojo.URI+"\">" + value.pojo.title + "</a></li>";
						}
						i++;
					}	
				});
				if(i>10)
					str += "<li><a class=\"norLink\" onclick=\"searchTrack('search/"+thequery+"/toolsInfo view all')\" href=\"/" +"procell/"+ localePath + "/search-results.jspx?category=toolsInfo&"+queryName+"=" + thequery + "\">"+SrchMoreResults+"</a></li>";
				str+="</ul>";
		}
		str +="<p id=\"search-viewAll\"><a onclick=\"searchTrack('search/"+thequery+"/View All Search Results')\" href=\"/" +"procell/"+ localePath + "/search-results.jspx?"+queryName+"=" + thequery + "\" title=\"View All Search Results\" class=\"norLink\"><img src=\"/images/common/ico-yarror.png\"><span>"+ viewSearchResult +"</span></a></p>";
		
	}
	
	$("#search-popup").html(str);
	
	// force the 'master' DIV to position:static to enable
	// the modal window to display on top most layer. Was a
	// problem in IE6 only
	$("#master").css('position','static');
	
	
	
}

function callBack(){
	if(str.length > 0){
		return true;
	}else{
		return false;
	}
}

function moreResults(url){
	$.ajax({
		url:url,
		type:'get',
		success:function(response){
		$("#content").html(response)
	}
	});
	$("#Tips-SearchResult radio").attr("checked","false");
}

function searchTrack(param3){
	trackingPage(param3);
	//trackingEvent('Search Bar', 'Search Link Click', param3);
	//trackingEvent('Search Results', 'Search Enter', 'Search Enter '+ $("#searchForm input").val()); 
}

$(function(){
	$("input:radio").checkbox();
	
	$(window).bind('resize',function (){
		if($("#search-popup").css("display")=="none")
			return;
		
		if (jQuery.browser.msie)
		{
			
			$("#search-popup").positionBy({
				target:$("#search-from"),
				targetPos: 3,
				elementPos:0,
				x:0,
				y:3
			}).show("fast");
		}
		else
		{
			$("#search-popup").positionBy({
				target:$("#search-from"),
				targetPos: 3,
				elementPos:0,
				x:0,
				y:22
			}).show("fast");
		}
	});
});
