/* $Id$ */

function SolrRequest() {
	//set the query for this request
	
}

SolrRequest.SERVER =contextPath+"/solr/search.htm?searchDomain=solr.server&wt=json&json.wrf=solrResponse&rows=3";


SolrRequest.prototype.makeRequest = function(start,channel,category) {
/*
 * Dynamically creates the script tag which then
 * sends the request to the Solr server.
 */

	
	this.jsonReq = new SolrScript(SolrRequest.SERVER+"&q=f_channelLink:"+channel+"&fq=i_categoryLink:"+category+"&start=" + start);

	this.jsonReq.makeTag();
	this.jsonReq.addTag();
}

function requestHandler(start,channel,category) {
/*
 * Called on search form submit and on results page request;
 * starting page argument is optional and defaults to 0.
 */

	var solrReq = new SolrRequest();
	solrReq.makeRequest(start,channel,category);
	document.getElementById("listbehind-the-scenes").style.textDecoration = "underline";
	document.getElementById("listout-takes").style.textDecoration = "underline";
	document.getElementById("list"+category).style.textDecoration = "none";
	//setColor(category);
	return false; //needed to keep the form from performing action
	
}

function requestHandler2(start,channel,category) {
/*
 * Called on search form submit and on results page request;
 * starting page argument is optional and defaults to 0.
 */

	
	var solrReq = new SolrRequest();
	solrReq.makeRequest(start,channel,category);
	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 chan="";
	var cat="";
	var j; 
	// figure out how many results are in the current page
	if (obj.response.numFound - obj.response.start < 3) {
		j = obj.response.numFound - obj.response.start;
	} else {
		j = 3;
	}

	
	
	//resutl htere
	if (obj.response.numFound>0)
	{
	

		for (var i=0; i<j; i++) {
			if(i==0){
				chan=obj.response.docs[i].f_channelLink;
				cat=obj.response.docs[i].i_categoryLink;
			}
			
			

			results+="<div class=\"player_thumb_bloc\">";
		    results+="<a href=\""+contextPath+"/"+obj.response.docs[i].f_channelLink+"/"+obj.response.docs[i].f_categoryLink+"/"+obj.response.docs[i].c_titleLink+"/\" >";	
			results+="<img src=\""+obj.response.docs[i].d_thumbnail+"\" alt=\""+obj.response.docs[i].b_title+"\" width=\"104\" height=\"78\" border=\"0\"/>";
			results+="</a>";
							results+="<div class=\"player_thumb_info\">";
							//results+="<div class=\"player_vid_channel\">"+obj.response.docs[i].e_channel+"</div>";
						  	results+="<div class=\"player_vid_title\">";
							results+="<a href=\""+contextPath+"/"+obj.response.docs[i].f_channelLink+"/"+obj.response.docs[i].i_categoryLink+"/"+obj.response.docs[i].c_titleLink+"/\">";
							results+=obj.response.docs[i].b_title;
							results+="</a>";
							results+="</div>";
							results+="<div class=\"player_vid_desc\">"+obj.response.docs[i].g_description+"</div>";
							results+="</div>";
							results+="</div>";
			
			
		
		}
		
		
	}else{
	  results+="<div class=\"player_thumb_bloc\">Sorry, no result!</div>";
	}
	
	var pageStartNumber=obj.response.start+1;
	var pageEndNumber=obj.response.start+3;
	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 requestHandler2('"+ eval(obj.response.start-3) + "','"+chan+"','"+cat+"')\"><<</a> "
		    //pagelinks +="block1";
		 }
		 
		 pageLinks+=videoCountText;
		 
		 if (obj.response.numFound - obj.response.start >3)
		{
		  pageLinks += "<a href='' onclick=\"return requestHandler2('"+ eval(obj.response.start+3) + "','"+chan+"','"+cat+"')\">>></a> ";
		 // pageLinks +="block4";
		}
	}
	
	 
	   
		// write to document
	document.getElementById('results1').innerHTML = results;
	document.getElementById('pageLinks1').innerHTML = pageLinks;
	document.getElementById('results2').innerHTML = results;
	document.getElementById('pageLinks2').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);
}