// This Static Class controls behavior of a search engine
// Settings Below allow some customization of the search results
bam.imports(bam.url);
var MLBSearch = {	
	"version": "1.0",
	ServerAddress: "/search/",		 						//Used in conjuction with Proxy to construct a url
	//ServerAddress: "http://localhost:8080/searchclient/", 	//Used in conjuction with Proxy to construct a url
	Proxy: "index.jsp",											//Proxy file
	Site: "mlb",												//Site parameter used by a proxy page
	StartingPage: 0,											//Default starting page
	ResultsPerPage: 10,											//Page size or number of results to be displayed per page
	ResultsPerSite: 0,											
	AnchorsPerPage: 10,											//Max number of pages that will be displayed in navigation at a time (browseable)
	TeamCodeFilter: "",											//Team code filter
	GetResults: function(searchStr, tStart, tPageSize, tSiteSize, callBack) {		
		searchStr = searchStr.replace(/\+/g,"__"); //'+' symbol is for AND clause	
		var encStr = searchStr; //recently changed due to proper url assembly fix in jsp
		var _req = bam.url.Location(location);
		var _cid = _req.getParam("c_id");
		var _tid = _req.getParam("teamCode");
		var data = {query: encStr, 
					start: (tStart || MLBSearch.StartingPage), 
					hitsPerPage: (tPageSize || MLBSearch.ResultsPerPage), 
					hitsPerSite: (tSiteSize || MLBSearch.ResultsPerSite), 
					site: MLBSearch.Site};
			if(!!_cid) {	//Persist c_id
				data.c_id = _cid;
			}
			if(!!_tid) {	//Persist teamCode
				data.teamCode = _tid;
			}
		if(MLBSearch.Proxy && $.trim(MLBSearch.Proxy).length > 0) {
			var teamCode = MLBSearch.TeamCodeFilter;
			if($.trim(teamCode).length > 0) {
				data.teamCode = teamCode;		
			}
			location.href = MLBSearch.ServerAddress + MLBSearch.Proxy + bam.url.buildSearch(data);
		} else {
			throw new Error("Proxy page is not specified");
		}
	},
	//Renders a single result box
	RenderResult: function(dataObj) {
		var div = document.createElement("div");			
		var out = div.cloneNode(false);
		var boxDiv = out.appendChild(div.cloneNode(false));
			boxDiv.className = "searchResultBox";
		var titleDiv = boxDiv.appendChild(div.cloneNode(false));
			titleDiv.className = "searchResTitle";				
		var titleLink = titleDiv.appendChild(document.createElement("a"));
			titleLink.setAttribute("href", dataObj.url);
			if (MLBSearch.ResultFormat=="json") {
				titleLink.setAttribute("target", "_blank");
			}
			titleLink.innerHTML = dataObj.title;
		var bodyDiv = boxDiv.appendChild(div.cloneNode(false));
			bodyDiv.className = "searchResBody";
			bodyDiv.innerHTML = dataObj.summary;
		var urlDiv = boxDiv.appendChild(div.cloneNode(false));
			urlDiv.className = "searchResLink";
			urlDiv.innerHTML = dataObj.url;
		return out.innerHTML;
	},
	//Renders navigation links
	RenderPageNav: function(data) {
		var out = ""; 
		var pageSize = data.hitsPerPage;
		var totalCount = data.total;
		var currentPage = data.start;
		var lastPage = data.end;
		var numOfPages = Math.ceil(totalCount / pageSize);
		
		var startAnchorNumber = 0;
		var remainder = (currentPage)%(MLBSearch.ResultsPerPage*MLBSearch.AnchorsPerPage);
		if(remainder === 0){
			startAnchorNumber = (currentPage)/(MLBSearch.ResultsPerPage*MLBSearch.AnchorsPerPage)*MLBSearch.ResultsPerPage;
		} else 
		if(currentPage > (MLBSearch.ResultsPerPage * MLBSearch.AnchorsPerPage)) {
			startAnchorNumber = Math.floor((currentPage)/(MLBSearch.ResultsPerPage*MLBSearch.AnchorsPerPage))*MLBSearch.ResultsPerPage;
		}
		var clickedAnchorNumber = Math.ceil(currentPage/MLBSearch.ResultsPerPage)+1;  //clicked anchor number
						
		var paginationTemplate = "";
			paginationTemplate += "<a class=\"[LINK_STYLE]\" href=\"/search/index.jsp#\" " + 
			"onclick=\"s.prop17='[NUMBER]';bam.tracking.track({async:{isDynamic:true, compName:'MLB Search', compActivity:'[COMP_ACTIVITY]', actionGen:true}, callback:function(){TextSearch.Search([START], [HITS_PER_PAGE], [HITS_PER_SITE]);}});return false;\"" +
			">[NUMBER]<\/a>";
		
		var tplPage = null;	
		//populating pagination template
		 for(var i=startAnchorNumber; i<numOfPages; i++){
			 if(((startAnchorNumber+1)>MLBSearch.AnchorsPerPage) && (i==startAnchorNumber)){ //'First' link code
				 tplPage = paginationTemplate.replace(/\[START\]/g, 0*MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_PAGE\]/g, MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_SITE\]/g, MLBSearch.ResultsPerSite);
				 tplPage = tplPage.replace(/\[COMP_ACTIVITY\]/g, "First");				 
				 tplPage = tplPage.replace(/\[LINK_STYLE\]/g, "navLink");				 
				 tplPage = tplPage.replace(/\[NUMBER\]/g, "<b>&laquo; First</b>");
				 out += tplPage;
				 
				 if((startAnchorNumber-MLBSearch.AnchorsPerPage) >= 0){  //'Prev' link code
					// alert(startAnchorNumber-MLBSearch.AnchorsPerPage);
					 tplPage = paginationTemplate.replace(/\[START\]/g,(startAnchorNumber-MLBSearch.AnchorsPerPage)*MLBSearch.ResultsPerPage);
					 tplPage = tplPage.replace(/\[HITS_PER_PAGE\]/g, MLBSearch.ResultsPerPage);
					 tplPage = tplPage.replace(/\[HITS_PER_SITE\]/g, MLBSearch.ResultsPerSite);	
					 tplPage = tplPage.replace(/\[COMP_ACTIVITY\]/g, "Previous");
					 tplPage = tplPage.replace(/\[LINK_STYLE\]/g, "navLink");
					 tplPage = tplPage.replace(/\[NUMBER\]/g, "&lsaquo; Prev");
					 out += tplPage;
				}
			 }
			 if(i < (startAnchorNumber + MLBSearch.AnchorsPerPage)){	//numbered anchors code
				 tplPage = paginationTemplate.replace(/\[START\]/g, i*MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_PAGE\]/g, MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_SITE\]/g, MLBSearch.ResultsPerSite);
				 if(i == (clickedAnchorNumber-1)){
					tplPage = tplPage.replace(/\[LINK_STYLE\]/g, "pageLink selectedLink");
				 } else {
					tplPage = tplPage.replace(/\[LINK_STYLE\]/g, "pageLink");
				 }
				 tplPage = tplPage.replace(/\[COMP_ACTIVITY\]/g, "Page "+(i+1)+" click");
				 tplPage = tplPage.replace(/\[NUMBER\]/g, (i+1));				
				 out += tplPage;
			} 
			if(i == (startAnchorNumber + MLBSearch.AnchorsPerPage)){ //'Next' link code
				 tplPage = paginationTemplate.replace(/\[START\]/g, i*MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_PAGE\]/g, MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_SITE\]/g, MLBSearch.ResultsPerSite);
				 tplPage = tplPage.replace(/\[COMP_ACTIVITY\]/g, "Next");
				 tplPage = tplPage.replace(/\[LINK_STYLE\]/g, "navLink");
				 tplPage = tplPage.replace(/\[NUMBER\]/g, "Next &rsaquo;");
				 out += tplPage;
			}
			//'Last' link code goes below
			var tempNumOfPages = numOfPages;
			if(numOfPages%MLBSearch.AnchorsPerPage === 0) //if this condition is true then last if condition below will always be true as well
				numOfPages = numOfPages - 1;  // needs to be restored after following if condition to include even last result
			var lastStartAnchorNumber = numOfPages - (numOfPages%MLBSearch.AnchorsPerPage); 
			if((numOfPages - (startAnchorNumber+1)) > MLBSearch.AnchorsPerPage && i==lastStartAnchorNumber){ //last link code
				 tplPage = paginationTemplate.replace(/\[START\]/g, lastStartAnchorNumber*MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_PAGE\]/g, MLBSearch.ResultsPerPage);
				 tplPage = tplPage.replace(/\[HITS_PER_SITE\]/g, MLBSearch.ResultsPerSite);
				 tplPage = tplPage.replace(/\[COMP_ACTIVITY\]/g, "Last");
				 tplPage = tplPage.replace(/\[LINK_STYLE\]/g, "navLink");
				 tplPage = tplPage.replace(/\[NUMBER\]/g, "<b>Last &raquo;</b>");
				 out += tplPage;
			}
			if(numOfPages%MLBSearch.AnchorsPerPage !== 0) //if condition before the last one will always evaluate to same value as of this if condition due to change in value of numOfPages
				numOfPages = tempNumOfPages;
			//'Last' link code ends here
		 }
		 return out;
	}
};