var start=0;
var actualPage=1;
$(document).ready(function(){   
	if(totalResults == 0){
		actualPage = 0;
	}
	$('#totalResults').html(totalResults+' ');
	$('#actualPage1').html(actualPage+' ');
	$('#actualPage2').html(actualPage+' ');
	reloadTonos();
	
	if(totalResults<=resultsByPage){
		disableButton($('#imgFw1')[0]);
		disableButton($('#imgFw2')[0]);
	}
});

function goNextResult(){
	start+=resultsByPage;
	if(start>=totalResults)start=totalResults-resultsByPage;
	$('#loading1').css('display','');
	$('#loading2').css('display','');
	$.post(context+'/exclude/search/performGeneralSearch.do',{query: query, page: actualPage*resultsByPage, fm: _fm, fn: _fn},
		function(data){
			removePlayerList(initPlayList.length);
			initPlayList = new Array();
			$('#searchResultsTable').html(data);
			if(start+resultsByPage>=totalResults){
				disableButton($('#imgFw1')[0]);
				disableButton($('#imgFw2')[0]);
			}
			enableButton($('#imgBack1')[0],0);
			enableButton($('#imgBack2')[0],0);
			
			$('#actualPage1').html(++actualPage+' ');
			$('#actualPage2').html(actualPage+' ');
			
			$('#_start').html(resultsByPage*(actualPage-1)+1);
			var end = resultsByPage*actualPage;
			if(end>totalResults)end=totalResults;
			$('#_end').html(end);

			reloadTonos();
			$('#loading1').css('display','none');
			$('#loading2').css('display','none');
	});
}

function goPreviousResult(){
	start-=resultsByPage;
	if(start<0)start=0;
	$('#loading1').css('display','');
	$('#loading2').css('display','');
	$.post(context+'/exclude/search/performGeneralSearch.do',{query: query, page: (actualPage-2)*resultsByPage, fm: _fm, fn: _fn},
		function(data){
			removePlayerList(initPlayList.length);
			initPlayList = new Array();
			$('#searchResultsTable').html(data);
			if(start-resultsByPage<0){
				disableButton($('#imgBack1')[0]);
				disableButton($('#imgBack2')[0]);
			}
			enableButton($('#imgFw1')[0],1);
			enableButton($('#imgFw2')[0],1);

			$('#actualPage1').html(--actualPage+' ');
			$('#actualPage2').html(actualPage+' ');

			$('#_start').html(resultsByPage*(actualPage-1)+1);
			var end = resultsByPage*actualPage;
			if(end>totalResults)end=totalResults;
			$('#_end').html(end);

			reloadTonos();
			$('#loading1').css('display','none');
			$('#loading2').css('display','none');
	});
}

function showNavs(id){
	var div=$('#nav'+id)[0];
	var img=$('#img'+id)[0];
	var display= div.style.display;
	if(display==''){
		div.style.display='none';
		img.src=img.src.replace('activate','desactivate');
	}else{
		div.style.display='';
		img.src=img.src.replace('desactivate','activate');
	}
}

function showMoreNavs(id){
	$('#more'+id)[0].style.display='';
	$('#tm'+id)[0].style.display='none';
}

function hiddeMoreNavs(id){
	$('#more'+id)[0].style.display='none';
	$('#tm'+id)[0].style.display='';
}

function applyFilter(filter){
}

function disableButton(im){
	im.src= im.src.replace('_off', '_disabled');
	im.src= im.src.replace('_on', '_disabled');
	im.onclick=null;
	im.style.cursor='default';
}
function enableButton(im,i){
	im.src= im.src.replace('_disabled', '_off');
	im.onclick=i==0?goPreviousResult:goNextResult;
	im.style.cursor='pointer';
}

function reloadTonos(){
	soundManager.onload();
}


/**
 * El player
 */
soundManager.url = context+'/linked/swf/'; // directory where SM2 .SWFs live
soundManager.defaultOptions.whileplaying = whilePlaying;
soundManager.debugMode = false;
	
soundManager.onload = function() {
	if (!soundManager.supported()){
	}else {
		soundManager.allowPolling = true;  
	  	if(initPlayList){
	  		createPlayerList(initPlayList)
	  	}
  	}
}

function createPlayerList(songs){
	$.each(songs,function(i){
		soundManager.createSound('sound'+i,this.toString());
	});
}

function removePlayerList(size){
	for(i=0;i<size;i++){
		soundManager.destroySound('sound'+i);
	}
}

function whilePlaying(){		
	var width = Math.round(this.position  * 260 / this.durationEstimate);
	soundManager._writeDebug('sound '+this.sID+' playing, '+this.position+' of '+this.duration);
	var progressBarId = "divProgress"+currentIndex;
	var prgObj = document.getElementById(progressBarId);
	prgObj.style.display = "block";
	prgObj.style.width = width + "px";
}

var currentIndex;
function playRingtone(index){
	var imgPlayId = "imgPlay"+index;
	var imgPlay = document.getElementById(imgPlayId);
	imgPlay.src = imgPlay.src.replace('play','stop');	
	
	soundManager.stop("sound"+index);
	if (currentIndex != null)
	{
		soundManager.stop("sound"+currentIndex);

		var progressBarId = "divProgress"+currentIndex;
		var prgObj = document.getElementById(progressBarId);
		prgObj.style.display = "none";
		var imgToOff = document.getElementById("imgPlay"+currentIndex);
		imgToOff.src = imgToOff.src.replace('stop','play');	
	}
		
	if (currentIndex != index)
	{
		currentIndex = index;
		soundManager.play("sound"+index);
	} else {
		currentIndex = null;
	}
}
