/* $Id$ */

function SolrRequest() {
	//set the query for this request
	this.solrQuery = document.getElementById("searchBox").query.value;
}

SolrRequest.SERVER = ctx+"/search/solrsearch.htm?searchDomain=solr.server&hl=on&fl=id,entryTitle,entryTeaser,pubtime&wt=json&json.wrf=solrResponse&rows=20&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 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 < 20) {
		j = obj.response.numFound - obj.response.start;
	} else {
		j = 20;
	}

	
	
	//resutl htere
	if (obj.response.numFound>0)
	{
	
		 results+="<div class=\"blue_header\"><span>Blog Posts</span></div>";
		for (var i=0; i<j; i++) {

			results+="<div class=\"blog\">";
			results+="<h3><a href=\""+ ctx+"/blog/entry/"+obj.response.docs[i].id+"\">" +obj.response.docs[i].entryTitle+"</a></h3>";
			results+=obj.response.docs[i].entryTeaser;
			results+="<p><a href=\""+ ctx+"/blog/entry/"+obj.response.docs[i].id+"\")\" class=\"continue\">continue reading...</a></p>";
			results+="<div class=\"permalink\"><img src=\""+ ctx+"/images/alloygirl_thumb.gif\")\" />Posted on: "+obj.response.docs[i].pubtime+"&nbsp;|&nbsp; <a href=\""+ ctx+"/blog/entry/"+obj.response.docs[i].id+"\")\">Read Comments</a></div>"; 
					
		
			results+="</div>";
			
		
		}
		//results+="</ul>";
		
	}else{
	  results+="<div class=\"blue_header\"><span>Blog Posts</span></div><br /><br />  Sorry, no result!</div>";
	}
		
		//
	// create page links
	if(pubyear>0)
	{
	   var pageLink="/blog/"+pubyear+"/"+pubmonth+"/";
	 }else{
	    var pageLink="/blog/page/";
	 }
	 
	   
	var pages = Math.ceil(obj.response.numFound/20);
	if(pages>1)
	{
	   if(obj.response.start!=0)
	     {
		    var pagePrevious= eval((obj.response.start)/20)
		    pageLinks +=  "<a id=\"previous_btn\" href='"+pageLink+pagePrevious+"/'>PREVIOUS</a>";
		 }
		if(obj.response.start<180)
		 {
			 var initialLimit=0;
			 var upperLimit=60;
			 	
		  }
		  else{
		    var initialLimit=((obj.response.start/20)-18);
			var upperLimit=((obj.response.start/20)+20);
		}
		if(pages<20 || upperLimit>pages)
		{
		  var upperLimit=pages;
		}
		for (var i=initialLimit; i<upperLimit; i++) {
	    if(i!=(obj.response.start/20))
		{
		pageLinks += "<a class=\"hide_paging\" href='"+pageLink+eval(i+1)+"/'>" + eval(i+1) + "</a> ";
		}
		else{
		  pageLinks += "<a class=\"hide_paging\">"+eval(i+1)+"</a> ";
		}
		 
		
		
		
		
	 }
	 
	 if (obj.response.numFound - obj.response.start >20)
		{
		 	var pageNext= eval((obj.response.start+40)/20)
		    pageLinks +=  "<a id=\"next_btn\" href='"+pageLink+pageNext+"/'>NEXT</a>";
		}
	}
	// write to document
	document.getElementById('blog_module').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);
}