//clone function to allow copying of objects that are independent from their source.

Object.prototype.clone = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clone();
    } else newObj[i] = this[i]
  } return newObj;
};


//********************************
// Search Processor
// current developers: Diana Zink (diana.zink@turner.com)
// documentation: http://docs/display/WEBDOC/Search+Processor
// @version $Id:$
// For configuration lines initializing the application - see customSiteSearchJS
//********************************

function SearchProcessor() 
{
	if (SearchProcessor.caller != SearchProcessor.getInstance) {
		if((navigator.userAgent.indexOf('Safari')==-1) && (navigator.userAgent.indexOf('Opera')==-1)) {
			throw new Error("There is no public constructor for StorageManager.");
		}
	}

	// CONFIGURATION variables
	// for search server
	
	this.queryVariable = "q";
	this.contentTypeVariable = 't',
    this.firstResultVariable = 'p',
    this.resultsPerPageVariable = 'm',
    this.siteSiloVariable = 's',

	this.customSearchUrl;
	this.defaultUrl = "http://srch.nba.com/search";
	this.nbaSearchUrl = "http://srch.nba.com/search";
	this.turnerSearchUrl = "http://srch8ref1.turner.com/nba/search";
	this.wnbaSearchUrl = "http://srch.wnba.com/search";
	this.siteSilo = null;

	// PAGINATION Control variables
	this.isNumberPaginationActive = new Boolean();
	this.isPrevNextPaginationActive = new Boolean();
	this.prevNextMiddle = '<span class="prevNextMiddle">&nbsp;|&nbsp;</span>';
	this.prevMarkup = '&#171;',
    this.nextMarkup = '&#187;',
    this.isElipseEnabled = true;
	this.pageFlipFunctionName = 'coreFlipToPage';
	this.disableDefaultNoContentMessage = false;
	// MULTIPLE INSTANCE Control variables
	this.multipleInstanceCallQuery = new Array();
	this.isCallForMultipleInstances = false;
	this.multipleResponseIndexes = 0;
	
	// OPERATIONAL variables
	this.currentTypeIndex = 0;
	this.lookupURL = "";
	this.lookupString = "";
	this.pagingOffset = 1;

	this.allInstancesQuery = new Array();
	this.activeInstance = 0;
}

SearchProcessor.prototype.addSearchInstance = function (configObject)
{
	//This method converts a Configuration object for one instance into a Search Instance object stored in the
	//this.allInstancesQuery with all other search instances for that page. Each instance gets modified later 
	//when the response and markup get added after the call to the server so in the end, each instance
	//carries all information for its search display.
	
	
	if(configObject) {
	
		//var confObj = eval('('+configObject+')');
		var confObj = configObject;
		var queryObject = new Object();
			
		// CONFIGURATION variables
		// for search server
		queryObject.queryVariable = confObj.queryVariable;
		queryObject.customSearchUrl = confObj.customSearchUrl;
		
		if(confObj.contentTypeVariable && confObj.contentTypeVariable !="") {
			queryObject.contentTypeVariable = confObj.contentTypeVariable;
		}
		else{
			queryObject.contentTypeVariable = this.contentTypeVariable;
		}
		
		if(confObj.firstResultVariable && confObj.firstResultVariable !="") {
			queryObject.firstResultVariable = confObj.firstResultVariable;
		}
		else{
			queryObject.firstResultVariable = this.firstResultVariable;
		}
		
		if(confObj.resultsPerPageVariable && confObj.resultsPerPageVariable !="") {
			queryObject.resultsPerPageVariable = confObj.resultsPerPageVariable;
		}
		else{
			queryObject.resultsPerPageVariable = this.resultsPerPageVariable;
		}
		
		if(confObj.disableDefaultNoContentMessage) {
			queryObject.disableDefaultNoContentMessage = confObj.disableDefaultNoContentMessage;
		}
		else{
			queryObject.disableDefaultNoContentMessage = this.disableDefaultNoContentMessage;
		}
		
		if(confObj.siteSiloVariable && confObj.siteSiloVariable !="") {
			queryObject.siteSiloVariable = confObj.siteSiloVariable;
		}
		else{
			queryObject.siteSiloVariable = this.siteSiloVariable;
		}
    		
		if(confObj.extraUrlParameters) {
			queryObject.extraUrlParameters = confObj.extraUrlParameters;
		}
		queryObject.maxResultsRequested = parseInt(confObj.maxResultsToRequest);
		queryObject.siteSilo = confObj.siteSilo;
		queryObject.ctype = confObj.contentTypes[0];

		//for publishing
		queryObject.contentType = "";
		queryObject.contentContainerIds = new Array();
		queryObject.contentIds = new Array();
		queryObject.contentMetaIds = new Array();
		queryObject.numberPaginationIds = new Array();
		queryObject.prevNextPaginationIds = new Array();
		
		queryObject.contentType = queryObject.ctype.contentType;
		queryObject.contentContainerIds.push(queryObject.ctype.contentContainerId);
		queryObject.contentPublishingFunction = queryObject.ctype.contentPublishingFunction;
		queryObject.contentIds.push(queryObject.ctype.contentId);
		queryObject.contentMetaIds.push(queryObject.ctype.contentMetaId);
		queryObject.numberPaginationIds.push(queryObject.ctype.numberPaginationIds);
		queryObject.prevNextPaginationIds.push(queryObject.ctype.prevNextIds);
		
		//Operational
		queryObject.currentPage = 1;
		queryObject.currentResult = 1;
		queryObject.resultsReceived = 0;
		if(this.isCallForMultipleInstances === true){
			queryObject.partOfMultipleRequest = true;
		}
		else{
			queryObject.partOfMultipleRequest = false;
		}
		//for pagination 
		queryObject.resultsPerPage = parseInt(confObj.pagination.resultsPerPage);
		queryObject.isNumberPaginationActive = new Boolean(confObj.pagination.isNumberPaginationActive);
		queryObject.maxPaginationLinks = parseInt(confObj.pagination.maxPaginationLinks);
		queryObject.isPrevNextPaginationActive = new Boolean(confObj.pagination.isPrevNextPaginationActive);
		
		if(confObj.pagination.prevNextMiddleMarkup && confObj.pagination.prevNextMiddleMarkup !="") {
			queryObject.prevNextMiddle = confObj.pagination.prevNextMiddleMarkup;
		}
		else{
			queryObject.prevNextMiddle = this.prevNextMiddle;
		}
		
		
		if(confObj.pagination.prevMarkup && confObj.pagination.prevMarkup !="") {
			queryObject.prevMarkup = confObj.pagination.prevMarkup;
		}
		else{
			queryObject.prevMarkup = this.prevMarkup;
		}
		
		if(confObj.pagination.nextMarkup && confObj.pagination.nextMarkup !="") {
			queryObject.nextMarkup = confObj.pagination.nextMarkup;
		}
		else{
			queryObject.nextMarkup = this.nextMarkup;
		}
		
		if(typeof(confObj.pagination.isElipseEnabled)!=undefined) {
			queryObject.isElipseEnabled = confObj.pagination.isElipseEnabled;
		}
		else{
			queryObject.isElipseEnabled = this.isElipseEnabled;
		}
		
		queryObject.currentTypeIndex = this.currentTypeIndex;
		this.allInstancesQuery.push(queryObject);
	}	
}

SearchProcessor.prototype.addMultipleSearchInstances = function(multipleSearchInstance)
{
	//When multiple insances of search are applied to one call to the server this method is used instead
	//it distributes the multiple instances and sets the Search Processor to Multiple Instance Mode. 
	//This method applies only to the first display of results on the page. When pagination is used the Search
	//Processor reverts back to single instance per call mode.
	
	this.isCallForMultipleInstances	= true;
	this.multipleInstanceCallQuery.push(multipleSearchInstance);
	for ( var i = 0; i < multipleSearchInstance.instances.length; i++ ) {
		this.addSearchInstance(multipleSearchInstance.instances[i].configuration);
	}
}

SearchProcessor.prototype.getSearchUrl = function(searchInstance)
{
	//picks the search url depending on domain or custom search url choice specified in the configuration
    var server = window.location.hostname;
    if (searchInstance.customSearchUrl) {
    	return searchInstance.customSearchUrl;
    }
    else if (server.match(/turner.com/i)) {
    	return this.turnerSearchUrl;
    }
    else if (server.match(/wnba.com/)) {
    	return this.wnbaSearchUrl;
    }
    else if (server.match(/nba.com/)) {
        return this.nbaSearchUrl;
    }
    else {
        return this.defaultUrl;
    }
}

SearchProcessor.prototype.getQuery = function (requestedString)
{	
	//gets the query string from the url address.
	var queryString = window.location.search.substring(1);
	var query = queryString.split("&");
	for (i=0;i<query.length;i++) {
		var finalText = query[i].split("=");
		if (finalText[0] == requestedString) { 
			return finalText[1];
		}
	}
}

SearchProcessor.prototype.dataLookup = function (activeQueryIndex)
{
	//fetches server data using CSI Manager for single call per instance (single mode only)
	var currentSearchInstance = this.allInstancesQuery[activeQueryIndex];

	this.lookupURL = this.getSearchUrl(currentSearchInstance);
	var requestedString = this.getQuery(currentSearchInstance.queryVariable);

	if(requestedString){
		this.allInstancesQuery[activeQueryIndex].lookupString = requestedString;
 		var searchParameter = currentSearchInstance.queryVariable + "=" + requestedString;
		var requestParameters = "&" + currentSearchInstance.contentTypeVariable + "=" + currentSearchInstance.activeContentType + "&" + currentSearchInstance.firstResultVariable + "=" + currentSearchInstance.pagingOffset + "&" + currentSearchInstance.resultsPerPageVariable + "=" + currentSearchInstance.resultsPerPage;
		
		if (currentSearchInstance.siteSilo) {
			requestParameters += "&"+ currentSearchInstance.siteSiloVariable +"="+currentSearchInstance.siteSilo;
		}
		
		if(currentSearchInstance.extraUrlParameters && currentSearchInstance.extraUrlParameters != "") {
			requestParameters += currentSearchInstance.extraUrlParameters;
		}
		
		var finalRequestQuery = searchParameter + requestParameters;
		
		var csiMgr = CSIManager.getInstance().call(this.lookupURL,finalRequestQuery,currentSearchInstance.activeContentId,function(respObj) {
			return SearchProcessor.getInstance().responseProcessing(respObj, activeQueryIndex);
		});
		
	}
}

SearchProcessor.prototype.generatePipeDeliniation = function (lookupArray)
{	
	//this method is used in this.multiplesDataLookup for generation of pipe delineated results when the values
	//in the array are different. The method returns a string.
	var finalString = "";
    var alength = lookupArray.length;
    var areValuesDifferent = false;
	for ( var i = 0; i < alength; i++ ) {
		for(var j = 1; j < (alength-i); j++){
          	if(lookupArray[j-1] != lookupArray[j]){
          		areValuesDifferent = true;
          		break;
      		}
		}
	}
	if (areValuesDifferent === true){
		finalString = lookupArray.join("|");
	}
	else {
		finalString = lookupArray[0];
	}
	return finalString;
}

SearchProcessor.prototype.multiplesDataLookup = function ()
{
	//fetches server data using CSI Manager for Multiple instances per call (multiple mode only)
	var currentSearchInstance = this.multipleInstanceCallQuery[0];

	this.lookupURL = this.getSearchUrl(currentSearchInstance);
	var requestedString = this.getQuery(currentSearchInstance.queryVariable);

	if(requestedString){
		var activeContentTypes = "";
		var resultsPerPage = "";
		var activeContentTypesArray = new Array();
		var resultsPerPageArray = new Array();
		var responseIds = new Array();
		
		for ( var i = 0; i < this.allInstancesQuery.length; i++ ) {
			var instance = this.allInstancesQuery[i];
			if(instance.partOfMultipleRequest === true){
				instance.lookupString = requestedString;
				activeContentTypesArray.push(instance.activeContentType);
				resultsPerPageArray.push(instance.resultsPerPage);
				responseIds.push(instance.activeContentId);
			}
		}
		
		activeContentTypes = this.generatePipeDeliniation(activeContentTypesArray);
		resultsPerPage = this.generatePipeDeliniation(resultsPerPageArray);
		
 		var searchParameter = currentSearchInstance.queryVariable + "=" + requestedString;
 		
		var requestParameters = "&" + currentSearchInstance.contentTypeVariable + "=" + activeContentTypes + "&" + currentSearchInstance.firstResultVariable + "=" + this.pagingOffset + "&" + currentSearchInstance.resultsPerPageVariable + "=" + resultsPerPage;
		
		if (currentSearchInstance.siteSilo) {
			requestParameters += "&"+ currentSearchInstance.siteSiloVariable +"="+currentSearchInstance.siteSilo;
		}
		
		if(currentSearchInstance.extraUrlParameters && currentSearchInstance.extraUrlParameters != "") {
			requestParameters += currentSearchInstance.extraUrlParameters;
		}
		
		var finalRequestQuery = searchParameter + requestParameters;
		
		this.isCallForMultipleInstances	= false;
		
		var csiMgr = CSIManager.getInstance().call(this.lookupURL,finalRequestQuery,responseIds,function(respObj) {
			return SearchProcessor.getInstance().multipleResponseProcessing(respObj);
		});
	}
	
}

SearchProcessor.prototype.responseProcessing = function (responseObj, responseIndex)
{
	var allInstancesQueries = this.allInstancesQuery;
	
	
	if(responseObj.criteria) {
		for(var aiq = 0; aiq<allInstancesQueries.length; aiq++) {
		
			if(responseObj.criteria.length <= 1){
				if(allInstancesQueries[aiq].activeContentType == responseObj.criteria[0].target){
					this.allInstancesQuery[aiq].responseObj = responseObj;
					this.allInstancesQuery[aiq].markup = this.distributePublishing(aiq);
					return this.allInstancesQuery[aiq].markup;
				}
			}
		
			else if(responseObj.criteria.length>1 && responseIndex != undefined) {
				if(allInstancesQueries[aiq].activeContentType == responseObj.criteria[responseIndex].target) {	
						var originalResponse = responseObj;
						var modifiedResponse = new Object;
						var blankCArray = new Array();
						var blankRArray = new Array();
						modifiedResponse = originalResponse.clone()
						modifiedResponse.criteria = blankCArray;
						modifiedResponse.criteria.push(originalResponse.criteria[aiq]);
						modifiedResponse.results = blankRArray;
						modifiedResponse.results.push(originalResponse.results[aiq]);
						this.allInstancesQuery[aiq].responseObj = modifiedResponse;
						this.allInstancesQuery[aiq].markup = this.distributePublishing(aiq);
						return this.allInstancesQuery[aiq].markup;
				}
			}
					
		}
	}
}

//handles server response using CSI Manager for Multiple instances per call (multiple mode only)
SearchProcessor.prototype.multipleResponseProcessing = function (responseObj)
{
    //condition required because CSI Manager will respond with a call to this function for every iD called.
    if(this.multipleResponseIndexes < 1){
		var originalResponse = responseObj;
		var allInstancesQueries = this.allInstancesQuery;
		
		if(originalResponse.criteria) {
			for (var i = 0; i < originalResponse.criteria.length; i++ ) {
				if(responseObj.criteria[i].target) {
					for(var aiq = 0; aiq<allInstancesQueries.length; aiq++) {
						if((allInstancesQueries[aiq].activeContentType == responseObj.criteria[i].target) && (allInstancesQueries[aiq].partOfMultipleRequest === true)) {
							var modifiedResponse = new Object;
							var blankCArray = new Array();
							var blankRArray = new Array();
							modifiedResponse = originalResponse.clone()
							modifiedResponse.criteria = blankCArray;
							modifiedResponse.criteria.push(originalResponse.criteria[i]);
							modifiedResponse.results = blankRArray;
							modifiedResponse.results.push(originalResponse.results[i]);
							this.allInstancesQuery[aiq].responseObj = modifiedResponse;
							this.allInstancesQuery[aiq].markup = this.distributePublishing(aiq);
						}
					}
				}
			}
			this.multipleResponseIndexes++
			return this.allInstancesQuery[0].markup;
		}
	}
	else {
		var onecall = 0
		var allInstancesQ = this.allInstancesQuery;
		for(var j = 0; j<allInstancesQ.length; j++) {
			if((allInstancesQ[j].partOfMultipleRequest === true && onecall<1)) {
				var returnMarkup = this.allInstancesQuery[j + this.multipleResponseIndexes].markup;
				onecall ++
				this.multipleResponseIndexes++
			}
		}
		return returnMarkup;
	}
}

SearchProcessor.prototype.distributePublishing = function (queryIndex)
{
	//After data is received this function distributes the data to all functions that publish pagination and results
	var activeQuery = this.allInstancesQuery[queryIndex];
	var responseObj = activeQuery.responseObj;
	
	if(responseObj.results[0]){
		if(responseObj.criteria[0].target) {
			if(responseObj.criteria[0].target == activeQuery.contentType) {
				if(responseObj.metaResults) {
					this.publishMeta(queryIndex);
				}
				
				if(activeQuery.isNumberPaginationActive == true) {
					this.pagination(queryIndex);
				}
			
				if(activeQuery.isPrevNextPaginationActive == true) {
					this.prevNextPagination(queryIndex);
				}
		
				if(responseObj.results[0][0] || activeQuery.disableDefaultNoContentMessage === true) {
					if(activeQuery.activeContentType == activeQuery.contentType) {	
						return activeQuery.contentPublishingFunction(responseObj, activeQuery.lookupString);
					}
					
				}
				else  {
					var noContentMessage = "<p>Sorry, no results were returned for " + activeQuery.lookupString+"</p>";
					return noContentMessage;
				}
			}
		}
	}
}

SearchProcessor.prototype.publishMeta = function (qIndex)
{

	var activeQuery = this.allInstancesQuery[qIndex];
	var meta = activeQuery.responseObj.metaResults;
	
	if(activeQuery.contentMetaIds[activeQuery.currentTypeIndex]){
		if(meta[activeQuery.activeContentType]) {
			document.getElementById(activeQuery.contentMetaIds[activeQuery.currentTypeIndex]).innerHTML = "" + meta[activeQuery.activeContentType] + "";
		}
		else {
			document.getElementById(activeQuery.contentMetaIds[activeQuery.currentTypeIndex]).innerHTML = "0";
		}
	}
		
	activeQuery.resultsReceived = activeQuery.responseObj.metaResults[activeQuery.activeContentType];
	
	
}

SearchProcessor.prototype.pageChecker = function (desiredPage, desiredFirstResult, qindex)
{
	var activeQuery = this.allInstancesQuery[qindex];
	
	var finalFirstResult = 1;
	var resPerPage = activeQuery.resultsPerPage;
	var totalCount =  activeQuery.resultsReceived;
	var totalPages = Math.ceil(totalCount/resPerPage);
	
	if(desiredPage || desiredFirstResult) {
		if(desiredFirstResult && !desiredPage) {
			desiredPage = Math.ceil(desiredFirstResult/resPerPage);
		}
		
		if(desiredPage > totalPages) {
			desiredPage=totalPages;
		}
		else if(desiredPage < 1) {
			desiredPage=1;
		}
		if((resPerPage*(desiredPage-1) + 1) >= 0) {
			finalFirstResult = (resPerPage*(desiredPage-1) + 1);
		}
		else {
			finalFirstResult = 1;
		}
	}
	else {
		finalFirstResult = 1;
	}
	this.allInstancesQuery[qindex].currentResult = finalFirstResult;
	return finalFirstResult;
}

//changes page based on the index number of first result on page and queryIndex (the index of the instance in this.allInstancesQuery). 
SearchProcessor.prototype.changePage = function (newFirstResult, queryIndex, newPage)
{
	var firstResult = SearchProcessor.getInstance().pageChecker(newPage, newFirstResult, queryIndex);
	this.allInstancesQuery[queryIndex].pagingOffset = firstResult;
	SearchProcessor.getInstance().dataLookup(queryIndex);
}


SearchProcessor.prototype.pagination = function (qIndex)
{
	var activeQuery = this.allInstancesQuery[qIndex];
	
	var pageType = activeQuery.currentTypeIndex;
	var total = activeQuery.resultsReceived;
	var current = activeQuery.currentResult;
	var perPage = activeQuery.resultsPerPage;
	var totalLinks = activeQuery.maxPaginationLinks;
	var isElipseEnabled = activeQuery.isElipseEnabled;
	var prevMarkup = activeQuery.prevMarkup;
	var nextMarkup = activeQuery.nextMarkup;
	
	var totalPages=Math.ceil(total/perPage);
	var i = 0;
	var startingPoint;
	var newInnerHTML="";
	
	//fix current:
	current = (current-1)/perPage+1;
	
	if(current > totalPages){
		current=totalPages;
	}
	else if(current < 1){
		current=1;
	}	

	if(totalLinks > totalPages) {
		totalLinks=totalPages;
	}
	
	newInnerHTML="<ul class='pagingLinks'>";    //clear out the div
	
		if(current > Math.ceil(totalLinks/2)) {
			startingPoint=current-Math.floor(totalLinks/2);
			while((startingPoint+totalLinks)>totalPages) {
				startingPoint--;
			}
		}
		else{
			startingPoint = 0;
		}
		//previous
		if(current != 1){
			newInnerHTML +="<li class='ends prev'><span class='iefix'><a href='javascript:" + this.pageFlipFunctionName + "("+((current-2)*perPage+1)+"," + qIndex +");'>" + prevMarkup + " </a></span></li>";
		}
		
		if(isElipseEnabled === true){
			if((startingPoint+1) > 1){
					if((startingPoint+1)>2) {
						newInnerHTML +="<li><span class='iefix'><a onclick='" + this.pageFlipFunctionName + "("+1+"," + qIndex +");'>"+1+"</a></span></li>";
					}
					newInnerHTML+="<li class='dotdotdot'> &#133; </li>";	
			}
		}		
		//body:
		//parse through all the page links
		for(i = 0; i < totalLinks; i++)
		
			//avoid only one link
			if(totalLinks != 1){
			
				if((startingPoint+i+1) == current) {
					newInnerHTML += "<li><span class='iefix'><a  id='selectedPage'>"+(startingPoint+i+1)+"</a></span></li>";
				}
				else{
					newInnerHTML +="<li><span class='iefix'><a onclick='" + this.pageFlipFunctionName + "("+((startingPoint+i)*perPage+1)+"," + qIndex +");'>"+(startingPoint+i+1)+"</a></span></li>";
				}
			}
		
			if(isElipseEnabled === true){
				if((startingPoint+i) < totalPages){
				newInnerHTML+="<li class='dotdotdot'> &#133; </li>";
	
				if((startingPoint+i)<(totalPages-1)) {
					newInnerHTML +="<li><span class='iefix'><a onclick='" + this.pageFlipFunctionName + "("+((totalPages-1)*perPage+1)+"," + qIndex +");'>"+totalPages+"</a></span></li>";
				}
			}
		}
			
		//next
		if(current != totalPages) {
			newInnerHTML +="<li class='ends next'><span class='iefix'><a href='javascript:" + this.pageFlipFunctionName + "("+((current)*perPage+1)+"," + qIndex +");'> " + nextMarkup + "</a></span></li>";
		}
			
	newInnerHTML += "</ul>";
	
	for(var idCount=0; idCount<activeQuery.numberPaginationIds[0].length; idCount++){
		try{
			document.getElementById(activeQuery.numberPaginationIds[0][idCount]).innerHTML= newInnerHTML;
		}
		catch(err){
			alert("invalid prevNextIds value");
		}
	}

}


SearchProcessor.prototype.prevNextPagination = function (qIndex)
{
	var activeQuery = this.allInstancesQuery[qIndex];
	var currentPageTypeIndex = activeQuery.currentTypeIndex;
	var totalResults = activeQuery.resultsReceived;
	var currentFirstResult = activeQuery.currentResult;
	var resPerPage = activeQuery.resultsPerPage;
	var middleHTML = activeQuery.prevNextMiddle;
	
	
	var totalPages=Math.ceil(totalResults/resPerPage);
	var prevNextHTML="";
	
	prevNextHTML = '<div id="imageNav" class="resultsSubNav">';
	
	if(totalResults > resPerPage) {
		if(currentFirstResult > resPerPage){
			prevNextHTML += '<span id="imagePrevNum"><a onclick="' + this.pageFlipFunctionName + '('+ (currentFirstResult - resPerPage) +',' + qIndex +');">&#171; Previous <span id="imagePrevNum">' + resPerPage +'</span></a></span>';
		}
		if((currentFirstResult > resPerPage) && ((currentFirstResult + resPerPage) <= totalResults)){
			prevNextHTML += middleHTML;
		}
		
		if((currentFirstResult + resPerPage) <= totalResults){
			prevNextHTML += '<span id="imageNext"><a onclick="' + this.pageFlipFunctionName + '('+ (currentFirstResult + resPerPage) +',' + qIndex +');">Next <span id="imageNextNum">' + resPerPage +'</span> &#187;</a></span>';
		}
	}
	prevNextHTML += '</div>';
	
	for(var idCount=0; idCount<activeQuery.prevNextPaginationIds[0].length; idCount++){
		try{
			document.getElementById(activeQuery.prevNextPaginationIds[0][idCount]).innerHTML= prevNextHTML;
		}
		catch(err){
			alert("invalid prevNextIds value");
		}
	}
}


SearchProcessor.prototype.initialize = function ()
{
	
	if((this.isCallForMultipleInstances === true) && (this.multipleInstanceCallQuery.length > 0) && (this.allInstancesQuery.length > 0)) {
		for(var i=0; i<this.allInstancesQuery.length; i++) {
			
			var activeQuery = this.allInstancesQuery[i];
			this.allInstancesQuery[i].pagingOffset = this.pagingOffset;
			this.allInstancesQuery[i].currentResult = 1;
			this.allInstancesQuery[i].activeContentId = activeQuery.contentIds[activeQuery.currentTypeIndex];
			this.allInstancesQuery[i].activeContentType = activeQuery.contentType;
		
		} 
		this.multiplesDataLookup();
	}
	else if((this.isCallForMultipleInstances === false) && this.allInstancesQuery.length > 0 ) {
		for(var i=0; i<this.allInstancesQuery.length; i++) {
			
			var activeQuery = this.allInstancesQuery[i];
			this.allInstancesQuery[i].pagingOffset = activeQuery.currentResult;
			this.allInstancesQuery[i].activeContentId = activeQuery.contentIds[activeQuery.currentTypeIndex];
			this.allInstancesQuery[i].activeContentType = activeQuery.contentType;
			
			this.changePage(1, i);	
		} 
	}
}

SearchProcessor.__instance__ = null; // static property

SearchProcessor.getInstance = function ()
{
	if (this.__instance__ == null) {
		this.__instance__ = new SearchProcessor();
	}
	return this.__instance__;
}

//Call this function from links that change the page on an instance of the search.
//qindex is the index of the instance in this.allInstancesQuery. 
function coreFlipToPage(firstPageItem, qindex){
	SearchProcessor.getInstance().changePage(firstPageItem, qindex);
}

//Call this function from links that manipulate the url of the search for purposes such as sorting
//or data filtering on one instance of the search.
//qindex is the index of the instance you want to change in this.allInstancesQuery. 
function coreAddSearchAttribute(attributeString, qindex, isCallEnabled){
	SearchProcessor.getInstance().allInstancesQuery[qindex].extraUrlParameters = attributeString;
	SearchProcessor.getInstance().changePage(1, qindex);
}

function coreUpdateAttributeWithoutCalling(attributeString, qindex){
	SearchProcessor.getInstance().allInstancesQuery[qindex].extraUrlParameters = attributeString;
}

