/* $Id$ */

function SolrRequest() {
	//set the query for this request
	this.solrQuery = document.getElementById("searchBox").query.value;
	



}


SolrRequest.SERVER = ctx+"/search/solrsearch.htm?searchDomain=solr.roller&hl=on&fl=id,entryTitle,handle,pubtime&wt=json&json.wrf=solrResponse&q=";


SolrRequest.prototype.makeRequest = function(page) {
/*
 * Dynamically creates the script tag which then
 * sends the request to the Solr server.
 */

	
	this.jsonReq = new SolrScript(SolrRequest.SERVER + this.solrQuery +"&sort=&start=" + page);

	this.jsonReq.makeTag();
	this.jsonReq.addTag();
}

function requestHandler(page) {
/*
 * Called on search form submit and on results page request;
 * starting page argument is optional and defaults to 0.
 */
	var pageNum = page || 0;
	var solrReq = new SolrRequest();
	solrReq.makeRequest(pageNum);
	return false; //needed to keep the form from performing action
}

function solrResponse(obj) {
/*
 * Catches the response from the Solr server. Displays
 * results and adds page links to paginated results. This
 * function assumes 10 results per page.
 */
	var results = "";
	var pageLinks = "";
	var j;
	// figure out how many results are in the current page
	if (obj.response.numFound - obj.response.start < 10) {
		j = obj.response.numFound - obj.response.start;
	} else {
		j = 10;
	}

	
	
	//resutl htere
	if (obj.response.numFound>0)
	{
		var counter=0;
		 results+="<ul class=\"roller_archive_list\">";
		for (var i=0; i<j; i++) {
			if (counter%2==0) {
				results+="<li class=\"evenPageGroup\">";
			} else {
				results+="<li class=\"oddPageGroup\">";
			}
			//results+="<div class=\"search_item_detail_box\">";
			var chan = document.getElementById("searchBox").chan.value;
			/*results+="<a href=\""+ ctx+"/"+chan+"/"+obj.response.docs[i].handle+"/entry/"+obj.response.docs[i].id+"\" class=\"pers-info\"><span class=\"roller_archive_title\">"+obj.response.docs[i].entryTitle+"</span></a><span class=\"roller_archive_date\">";
			results+=obj.response.docs[i].pubtime;
			results+="</span>"; */
			
			//If Statement for blogs
			if(chan == "4"){
			results+="<a href=\""+ ctx+"/life/blog/"+obj.response.docs[i].handle+"/entry/"+obj.response.docs[i].id+"\" class=\"pers-info\"><span class=\"roller_archive_title\">"+obj.response.docs[i].entryTitle+"</span></a><span class=\"roller_archive_date\">";
			results+=obj.response.docs[i].pubtime;
			results+="</span>";
			}
			else{
			results+="<a href=\""+ ctx+"/entertainment/blog/"+obj.response.docs[i].handle+"/entry/"+obj.response.docs[i].id+"\" class=\"pers-info\"><span class=\"roller_archive_title\">"+obj.response.docs[i].entryTitle+"</span></a><span class=\"roller_archive_date\">";
			results+=obj.response.docs[i].pubtime;
			results+="</span>";
			}
			//END - If Statement for blogs

			results+="</li>";
			
			counter++;
		}
		results+="</ul>";
		
	}else{
	  results+="No Result";
	}
		
		//
	// create page links
	var pages = Math.ceil(obj.response.numFound/10);
	if(pages>1)
	{
	   if(obj.response.start!=0)
	     {
		   pageLinks += "<a href='#' onclick='requestHandler(" + eval(obj.response.start-10) + ")'>Previous</a> ";
		 }
		if(obj.response.start<90)
		 {
			 var initialLimit=0;
			 var upperLimit=10;
			 	
		  }
		  else{
		    var initialLimit=((obj.response.start/10)-9);
			var upperLimit=((obj.response.start/10)+10);
		}
		if(pages<10 || upperLimit>pages)
		{
		  var upperLimit=pages;
		}
		for (var i=initialLimit; i<upperLimit; i++) {
	    if(i!=(obj.response.start/10))
		{
		pageLinks += "<a href='#' onclick='requestHandler(" + eval(i*10) + ")'>" + eval(i+1) + "</a> ";
		}
		else{
		  pageLinks += eval(i+1);
		}
		 
		
		
		
		
	 }
	 
	 if (obj.response.numFound - obj.response.start >10)
		{
		  pageLinks += "<a href='#' onclick='requestHandler(" + eval(obj.response.start+10) + ")'>Next</a> ";
		}
	}
	// write to document
	document.getElementById('search_results').innerHTML = results;
	document.getElementById('pages').innerHTML = pageLinks;
}

function SolrScript(url) {
/*
 * This class manages the dynamic script tag. Script
 * tag is added with id="solrScript".
 */
    this.url = url;
    this.headTag = document.getElementsByTagName("head").item(0);
	// clean up previous dynamic script tag
	if (document.getElementById("solrScript")) {
		this.headTag.removeChild(document.getElementById("solrScript"));
	}
}

SolrScript.prototype.makeTag = function () {
    this.scriptTag = document.createElement("script");
    this.scriptTag.setAttribute("type", "text/javascript");
    this.scriptTag.setAttribute("src", this.url + '&time=' + (new Date()).getTime());
    this.scriptTag.setAttribute("id", "solrScript");
}

SolrScript.prototype.addTag = function () {
    this.headTag.appendChild(this.scriptTag);
}