/* $Id$ */
var chan="";
var cat="";
	
function SolrRequest() {
	//set the query for this request
	
}

SolrRequest.SERVER =contextPath+"/solr/search.htm?searchDomain=solr.server&wt=json&json.wrf=solrResponse&rows=4";


SolrRequest.prototype.makeRequest = function(start,channel,category) {

/*
 * Dynamically creates the script tag which then
 * sends the request to the Solr server.
 */

if(category=='all'){
	this.jsonReq = new SolrScript(SolrRequest.SERVER+"&q=f_channelLink:"+channel+"&sort=&start=" + start);
	
}else{

	this.jsonReq = new SolrScript(SolrRequest.SERVER+"&q=f_channelLink:"+channel+"&fq=i_categoryLink:"+category+"&sort=&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.
 */
   
	
	chan=channel;
	cat=category;
	var solrReq = new SolrRequest();
	solrReq.makeRequest(start,channel,category);
	//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.
 */

	chan=channel;
	cat=category;
	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 = "<div id=\"video_list_wrapper\">";
	//var pageLinks = "";
	var chan="";
	var cat="";
	var j; 

	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="hollywood-is-like-high-school";
				cat=obj.response.docs[i].i_categoryLink;
			}			
			

			results+="<div class=\"player_thumb_bloc\">";
		    results+="<a href=\""+contextPath+"/"+chan+"/"+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+"/"+chan+"/"+obj.response.docs[i].c_titleLink+"/\">";
			results+=obj.response.docs[i].b_title;
			results+="</a>";
			results+="</div><div class=\"player_vid_desc\">";
			
			results+="<a href=\""+contextPath+"/"+chan+"/"+obj.response.docs[i].c_titleLink+"/\">"+obj.response.docs[i].g_description+"</a></div>";
			results+="</div>";
			results+="</div>";		
		}
		
		
	}else{
	  results+="<div class=\"player_thumb_bloc\">Sorry, no result!</div>";
	}
	
	var pageStartNumber=obj.response.start+0;
	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) {
		   results+="</div>";
		  results += "<div id=\"left_arrow\"><img src=\""+contextPath+"/shows/hollywood-is-like-high-school/images/graphics/buttons/left_btn_off.jpg\" id=\"prev_lg_arrow\"/></div>"
	   } else {
		 
		 /*var numLastBoxes = (obj.response.numFound - obj.response.start);
		 if (numLastBoxes <= 3) {
			

			 var empties = 3 - numLastBoxes;
			 for (var i=0; i<empties; i++) {
				results+="<div class=\"player_thumb_bloc\">";
				results+="<img src=\""+contextPath+"/shows/21questions/images/"+"no_image_box.gif\" alt=\""+""+"\" width=\"205\" height=\"118\" border=\"0\"/>";
				if (cat=="season-two") {
				results+="<div class=\"player_thumb_info\">";
				results+="<div class=\"player_vid_title\">";
				results+="<span style=\"color:#DC8593;font-size:12px;font-style:italic\">New episodes every Tuesday!</span>";
				results+="</div>";				
				results+="</div>";					

				}
			
				results+="</div>";	
			 }
		 }*/
			 results+="</div>";
		   results += "<div id=\"left_arrow\"><a href='' onclick=\"return requestHandler2('"+ eval(obj.response.start-3) + "','"+chan+"','"+cat+"')\"><img src=\""+contextPath+"/shows/hollywood-is-like-high-school/images/graphics/buttons/left_btn_on.jpg\" id=\"prev_lg_arrow\"/></a></div>"
		  }
		 //pageLinks+=videoCountText;
		 
		 if (obj.response.numFound - obj.response.start <= 3) {
			 results += "<div id=\"right_arrow\"><img src=\""+contextPath+"/shows/hollywood-is-like-high-school/images/graphics/buttons/right_btn_off.jpg\"id=\"next_lg_arrow\"/></div>";
		 } else {
		  results += "<div id=\"right_arrow\"><a href='' onclick=\"return requestHandler2('"+ eval(obj.response.start+3) + "','"+chan+"','"+cat+"')\"><img src=\""+contextPath+"/shows/hollywood-is-like-high-school/images/graphics/buttons/right_btn_on.jpg\"id=\"next_lg_arrow\"/></a></div>";
		}
	}
	
	 
	   
		// write to document
	document.getElementById('results').innerHTML = results;
	//document.getElementById('pageLinks').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);
}
