
var Products = {
	search: function(path){
		$('#resultados').loading();
		removePlayerList(initPlayList.length);
		$('#contentDiv').load(context+'/ajax/product/search.do?path='+path+'&query='+encodeURIComponent($('#searchTxt').val())+'&productType='+productType,function(){soundManager.onload(); $('#resultados').loading({hide:true}); });
	},
	
	searchCategory: function(cmb){
		$('#resultados').loading();
		removePlayerList(initPlayList.length);
		$('#contentDiv').load(context+'/ajax/product/search.do?'+encodeURI(cmb.val()),function(){soundManager.onload(); $('#resultados').loading({hide:true});});
	},

	pagination: function(pars){
		$('#loading1').css('display','block');
		if(!document.isSound)
			$('#loading2').css('display','block');
			
		removePlayerList(initPlayList.length);
		$('#contentDiv').load(context+'/ajax/product/search.do?'+encodeURI(pars),function(){
			soundManager.onload();
			$('#loading1').css('display','none');
			if(!document.isSound)
				$('#loading2').css('display','none');
		});
	},
	
	searchByLetter: function(letter){
		$('#resultados').loading();
		removePlayerList(initPlayList.length);
		$('#contentDiv').load($(letter).attr('href'),function(){soundManager.onload(); $('#resultados').loading({hide:true});});
	},
	
	searchNewest: function(pars){
		$('#newest').loading();
		$('#newest').load(context+'/ajax/product/theNewest.do?'+pars.replace(/\s/gi,'+'),function(){$('#newest').loading({hide:true});});
	}
};

$(function(){
	
	$.ajaxSetup({
		contentType: "text/html; charset=utf-8"
	});
	
	$('#categorias').change(function(){
		Products.searchCategory($(this));
	});
});


/**
 * 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()){
		//alert('tu explorador no soporta sonidos!');
	}else {
		soundManager.allowPolling = true;  
	  	// SM2 has loaded - now you can create and play sounds!
	  	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(){
	if($('#resultados').length>0)$('#resultados').loading({hide:true});		
	var width = Math.round(this.position  * 480 / 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('off','on');	
	
	soundManager.stop("sound"+index);
	if (currentIndex != null){
		if($('#resultados').length>0)$('#resultados').loading({hide:true});
		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('on','off');	
	}
		
	if (currentIndex != index)
	{
		$('#resultados').loading();
		currentIndex = index;
		soundManager.play("sound"+index);
		deselectRow(index);
	} else {
		currentIndex = null;
	}
}

/* functions for rollover events */
function selectRow(index) {
	if (currentIndex != index){
		var rowId = "divProgress"+index;
		var row = document.getElementById(rowId).parentNode;
		row.style.backgroundColor = "#82ceff";
	}
}
function deselectRow(index) {
	var rowId = "divProgress"+index;
	var row = document.getElementById(rowId).parentNode;
	row.style.backgroundColor = "";
	
}
