
var OVERALL_STORED_MOVIES = "";
var MIN_ID_TOKEN = 0;
var CHECK_UPDATES_INTERVAL = 30000;
var CURRENT_PAGE = 0;
var STORED_PAGES = new Array();
var SORT_KEY = 0;
var SEARCH_STRING = "";
var LAST_ID = 0;
var t = null;

function SearchSlider(data, container) {
    data = $(data);
	this.container = container;

//	var movieSearchDataNode = data.getElementsByTagName("MOVIESEARCHDATA");
//	if (movieSearchDataNode.length == 0) {
//		window.status = 'could not find MOVIESEARCHDATA node';
//		return;
//	}
//	movieSearchDataNode = movieSearchDataNode[0];

    SEARCH_STRING = $("searchString").innerHTML;
    this.sort(0);
	//this.reload(data, movieSearchDataNode, false);
}
SearchSlider.prototype.reload = function(data, movieSearchDataNode, paging) {
	this.data = data;
	if (movieSearchDataNode.getAttribute("overallStoredMovies") != null) OVERALL_STORED_MOVIES = movieSearchDataNode.getAttribute("overallStoredMovies");
	if (movieSearchDataNode.getAttribute("minIdToken") != null) LAST_ID = movieSearchDataNode.getAttribute("minIdToken");
	if (movieSearchDataNode.getAttribute("sortKey") != null) SORT_KEY = (movieSearchDataNode.getAttribute("sortKey") == null) ? "0" : movieSearchDataNode.getAttribute("sortKey");
    if (movieSearchDataNode.getAttribute("searchString") != null) SEARCH_STRING = (movieSearchDataNode.getAttribute("searchString") == null) ? "0" : movieSearchDataNode.getAttribute("searchString");
	
    
    //$('matches').innerHTML = " ("+ OVERALL_STORED_MOVIES + ")";
    
    
    
	document.getElementById("__sortType0").className = (SORT_KEY == "0") ? "current" : "";
	document.getElementById("__sortType1").className = (SORT_KEY == "1") ? "current" : "";
	document.getElementById("__sortType2").className = (SORT_KEY == "2") ? "current" : "";
	
	var stored_list = new Array();

	if (!paging) {
		//var live_list = new Array();

		//var liveNodes = this.data.getElementsByTagName("LiveMovie");
		//for (var i=0;i<liveNodes.length && i < 5;i++)
        //{
		//	live_list.push({id:liveNodes[i].getAttribute("id"), title:liveNodes[i].getAttribute("title"), broadcasterId:liveNodes[i].getAttribute("bid"), location:liveNodes[i].getAttribute("location"), thumb:liveNodes[i].getAttribute("thumb")});
		//}
		
		document.getElementById("liveTab").style.display = "none";
		document.getElementById("happeningContent").style.display = "none";

		//this.live_data = live_list;

		//this.drawLive(this.live_data);
	}

	var storedNodes = this.data.getElementsByTagName("StoredMovie");
	var insertToPage = CURRENT_PAGE;
    for (var i=0;i<storedNodes.length;i++)
    {     
		var id = storedNodes[i].getElementsByTagName("id")[0].firstChild.nodeValue; 
        var title = storedNodes[i].getElementsByTagName("title")[0].firstChild.nodeValue; 
        var taken = storedNodes[i].getElementsByTagName("taken")[0].firstChild.nodeValue; 
        var bid = storedNodes[i].getElementsByTagName("bid")[0].firstChild.nodeValue; 
        var views = storedNodes[i].getElementsByTagName("views")[0].firstChild.nodeValue; 
        var location = storedNodes[i].getElementsByTagName("location")[0].firstChild.nodeValue; 
        var thumb = storedNodes[i].getElementsByTagName("thumb")[0].firstChild.nodeValue; 
        
        
        
        
        
        
        
        
        stored_list.push({id:id, taken:taken, broadcasterId:bid, title:title, views:views, location:location, thumb:thumb});
        if((i != 0) && (i%15 == 0))
        {
          STORED_PAGES[insertToPage] = stored_list;
          stored_list = new Array();
          insertToPage++; 
        }
    } // End of for
    
    
    if(!STORED_PAGES[CURRENT_PAGE])
    {
      this.stored_data = stored_list; 
    }
    else
    {
       this.stored_data = STORED_PAGES[CURRENT_PAGE];
    }


	this.debug = false;

	this.drawStored(this.stored_data);
	
	if (this.stored_data.length == 0) {
		$('videosContent').innerHTML = "<div class='noMoviews'>no flix found</div>";
	}
}

//SearchSlider.prototype.drawLive = function(data) {

//	var thumbs_container  = $('liveThumbs');
//	var details_container = $('liveDetails');
//	thumbs_container.innerHTML  = '';
//	details_container.innerHTML = '';

//	this.live_length = (NUM_OF_LIVE_MOVIES>5?5:NUM_OF_LIVE_MOVIES);
//	
//	$('liveNum').innerHTML = '('+this.live_length+')';
//	for(var i=0;i<this.live_length;i++)
//	{
//		this.drawThumbnail({id:data[i].id,broadcasterId:data[i].broadcasterId, location:data[i].location,title:data[i].title,thumb:data[i].thumb}, true, thumbs_container, details_container);
//	}
//}
SearchSlider.prototype.drawStored = function(data)
{
	var stored_container  = $('videosContent');
	stored_container.innerHTML  = '';

	var row_count = ((Math.floor(OVERALL_STORED_MOVIES/5)+1)>3?3:(Math.floor(OVERALL_STORED_MOVIES/5)+1));

	for (var r=0;r<row_count;r++)
	{
		var thumbs_container = document.createElement("DIV");
		thumbs_container.className = "items";
		var details_container = document.createElement("DIV");
		details_container.className = "itemsDetals";
		
		var max_in_row = ((r*5+5)>data.length?data.length:(r*5+4));
		for (var i=r*5;i<=max_in_row;i++)
		{
			if(data[i]) this.drawThumbnail({id:data[i].id,taken:data[i].taken,views:data[i].views,broadcasterId:data[i].broadcasterId, location:data[i].location,title:data[i].title,thumb:data[i].thumb}, false, thumbs_container, details_container);
		}
		stored_container.appendChild(thumbs_container);
		stored_container.appendChild(details_container);
		
		thumbs_container = null;
		details_container = null;
	} // End of for

	var page_count = Math.ceil(OVERALL_STORED_MOVIES/15);
	
	var page_container = $('page_container');
	page_container.innerHTML = '';
	
	var prev_page = document.createElement('A');
	prev_page.href = 'javascript:searchSlider.viewPage('+(CURRENT_PAGE-1)+');';
	prev_page.style.visibility = (CURRENT_PAGE > 0) ? "visible" : "hidden";
	prev_page.className='back';
	page_container.appendChild(prev_page);

	if (page_count > 1)
    {
/*
		for (var i=0;i<page_count;i++)
		{
			var curr_page = document.createElement('A');
			curr_page.className = (CURRENT_PAGE == i) ? 'currentNum' : 'num';
			curr_page.innerHTML = new String((i+1));
			if (CURRENT_PAGE != i) curr_page.href = 'javascript:searchSlider.viewPage('+i+');';
			page_container.appendChild(curr_page);
			curr_page = null;
		}
*/
		var firstPage = (CURRENT_PAGE-2 > 0) ? CURRENT_PAGE-2 : 0;
		var lastPage = (firstPage+5 < page_count) ? firstPage+5 : page_count;
		if (lastPage-firstPage < 5) {
			firstPage = Math.max(page_count-5, 0);
			lastPage = page_count;
		}
		for (var i=firstPage;i<lastPage;i++) {
			var a = page_container.appendChild(document.createElement("a"));
			a.href = "javascript:searchSlider.viewPage("+i+")";
			a.innerHTML = eval(i+1);
			a.className = (i == CURRENT_PAGE) ? "currentNum" : "num";
		}
	}
	
	var next_page = document.createElement('A');
	next_page.href = 'javascript:searchSlider.viewPage('+(CURRENT_PAGE+1)+');';
	next_page.style.visibility = (CURRENT_PAGE < page_count-1) ? "visible" : "hidden";
	next_page.className='next';
	page_container.appendChild(next_page);

	next_page = null;
	prev_page = null;
	page_container = null;
	
	//STORED_PAGES[CURRENT_PAGE] = data;
}
SearchSlider.prototype.drawThumbnail = function(thumbnail, isLive, thumbs_container, details_container)
{
	this.thumbLink = document.createElement("A");
	this.thumbLink.href = 'javascript:Common.JumpTo("'+thumbnail.id+'");';
	this.thumbImg = document.createElement("IMG");
	this.thumbImg.src = thumbnail.thumb;//GET_IMAGE_URL.replace(/\[SID\]/, thumbnail.id)+"&size=1";
	this.thumbImg.style.width = "160px";
	this.thumbImg.style.height = "120px";
	this.thumbImg.onerror = Common.ShowDefaultImage;
	this.thumbImg.alt = '';
	this.thumbLink.appendChild(this.thumbImg);
	thumbs_container.appendChild(this.thumbLink);

	this.detailDiv = document.createElement("DIV");
	this.detailDiv.className = "item";
	this.detailLink = document.createElement("A");
	this.detailLink.href = 'javascript:Common.JumpTo("'+thumbnail.id+'");';
	this.detailLink.className = 'name';
	this.detailLink.innerHTML = thumbnail.title;
	this.detailDiv.appendChild(this.detailLink);
	this.detailLoc = document.createElement("SPAN");
	this.detailLoc.className = 'place';
	this.detailLoc.innerHTML = "<a href='/userprofile/index?userId="+thumbnail.broadcasterId+"'>"+thumbnail.location+"</a>";
	this.detailDiv.appendChild(this.detailLoc);
	if (isLive == false)
	{
		this.detailAdd = document.createElement("P");
		this.detailAdd.className = 'Aired';
		this.detailAdd.innerHTML = 'Aired: '+thumbnail.taken+' <br> '+Common.FormatNumber(thumbnail.views)+' '+Common.GetViewsLabel(thumbnail.views);
		this.detailDiv.appendChild(this.detailAdd);
	}
	details_container.appendChild(this.detailDiv);
}

SearchSlider.prototype.viewPage = function(page)
{
	CURRENT_PAGE = page;
	if (STORED_PAGES[page])
    {
      this.drawStored(STORED_PAGES[page]);
    }
    else // the user is asking for the next batch
    {
      this.refresh();
    } 
}

SearchSlider.prototype.refresh = function() {
	flix.showReloader('Loading...','load');
		
	//debugger;
    //var url = "/moviesearch/data?search_string="+$('txtSearch').value+"&minIdToken="+MIN_ID_TOKEN+"&maxIdToken="+MIN_ID_TOKEN+'&sortKey='+SORT_KEY;
    var url = "/moviesearch/data?search_string="+SEARCH_STRING+"&minIdToken="+LAST_ID+'&sortKey='+SORT_KEY;

	url += '&r='+Math.random()*10000;
	var poster = new Ajax(url, {method:'get', onComplete:function() {
		flix.hideReloader();
		searchSlider.reload(this.transport.responseXML.documentElement, this.transport.responseXML.documentElement, true);
	}}).request();
}

SearchSlider.prototype.sort = function(type) {
	if (SORT_KEY == type) this.viewPage(0);
	else {
		STORED_PAGES = new Array();
		LAST_ID = 0;
        SORT_KEY = type;
		this.viewPage(0);
	}
}

