/* $Id$ */

function SolrRequest2() {
	//set the query for this request
	this.solrQuery = document.getElementById("searchBox").query.value;
}

SolrRequest2.SERVER = ctx+"/solr/search.htm?searchDomain=solr.server&wt=json&json.wrf=searchAll";


SolrRequest2.prototype.makeRequest = function(start) {
/*
 * Dynamically creates the script tag which then
 * sends the request to the Solr server.
 */
 	
	
	this.jsonReq = new SolrScript(SolrRequest2.SERVER+"&q="+this.solrQuery+"&sort=&hl=true&hl.fragsize=0&hl.snippets=20&hl.fl=title&start=" + start);
	this.jsonReq.makeTag();
	this.jsonReq.addTag();
}

function requestHandlerMain(start) {
		
/*
 * Called on search form submit and on results page request;
 * starting page argument is optional and defaults to 0.
 */

	var solrReqMain = new SolrRequest2();
	solrReqMain.makeRequest(start);
	return false; //needed to keep the form from performing action
}


function searchAll(obj) {
	
/*
 * Catches the response from the Solr server. Displays
 * results and adds page links to paginated results. This
 * function assumes 30 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 < 9) {
		j = obj.response.numFound - obj.response.start;
	} else {
		j = 9;
	}

	
	
	//result here
	if (obj.response.numFound>0)
	{
	

		for (var i=0; i<j; i++) {
			

			//results+=obj.response.docs[i].title;
			results+="<div class=\"thumbnail_photo\">";
			results+="<a href=\""+ctx+"/photos/entry/"+obj.response.docs[i].id+"/category/"+obj.response.docs[i].categoryId+"/0\">";
			results+="<img src=\""+"http://images.gurl.com/"+obj.response.docs[i].thumbPath.substring(obj.response.docs[i].thumbPath.indexOf("photos"))+"\" /></a>";
			results+="</div>";
			//var id = obj.response.docs[i].k_id;
			//var hl=obj.highlighting[id];
			

		}
	}else{
	  results+="<div id=\"no_result\">Sorry! We can't find the image you were looking for.</div>";
	}
	
	var pageStartNumber=obj.response.start+1;
	var pageEndNumber=obj.response.start+9;
	if(obj.response.numFound<pageEndNumber)
	{ 
          pageEndNumber=obj.response.numFound
	}
	
	 var  videoCountText= pageStartNumber+" To "+pageEndNumber+" of "+obj.response.numFound;
	
	var numFound = Math.ceil(obj.response.numFound);
	if(numFound>1)
	{
	   if(obj.response.start!=0)
	     {
		   pageLinks += "<a href='' onclick=\"return requestHandlerMain('"+ eval(obj.response.start-9) + "')\"> <<</a> "
		    //pagelinks +="block1";
		 }
		 
		 pageLinks+=videoCountText;
		 
		 if (obj.response.numFound - obj.response.start >9)
		{
		  pageLinks += "<a href='' onclick=\"return requestHandlerMain('"+ eval(obj.response.start+9) + "')\"> >></a> ";
		 // pageLinks +="block4";
		}
	}
	
	 
	   
		// write to document
		document.getElementById('pageLinks').innerHTML = pageLinks;
		document.getElementById('search_results').innerHTML = results;
	
	

}

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);
}