/**
 * @author 			Eric Mantoani <eu@eric.com.br>
 
 * @description		Classe que manipula o mï¿½dulo Voto
 * 
 * @date			06/01/2010
 * @project			Rï¿½dio Cultura Brasil
 * @revision		0.1
 */
var ultimoVoto  = 0;

function votar2(table, table_id, section_id, user_id, voto_config_id){
	//console.log('ultimoVoto: '+ultimoVoto+' voto:'+table_id);
	if(table_id != ultimoVoto){
		ultimoVoto = table_id;
		Voto.votar(table, table_id, section_id, user_id, voto_config_id);
	}
	/*
	else
	{
		window.alert('Você já votou nessa conteúdo antes. Obrigado!');
	}
	*/
}


Voto = function(){
	
	var table;
	var table_id;
	var section_id;
	var user_id;
	var voto_config_id;
	
	var _initHandler = function(table, table_id, section_id, user_id, voto_config_id){
		this.table = table;
		this.table_id = table_id;
		this.section_id = section_id;
		this.user_id = user_id;
		this.voto_config_id = voto_config_id;
	}
	this.initHandler = _initHandler;
	
	function getTable(){
		return Voto.table;
	}
	
	function getTableId(){
		return Voto.table_id;
	}
	
	function getSectionId(){
		return Voto.section_id;
	}
	
	function getUserId(){
		return Voto.user_id;
	}
	
	function getVotoConfigId(){
		return Voto.voto_config_id;
	}
	
	var _roletaRussa = function(DOMElementID){
		$('#'+DOMElementID).html('Roda roda roda...');
		$('#'+DOMElementID).load(URL+'webservice.php?module=cultura_musicas&file=roleta-russa');
	}
	this.roletaRussa = _roletaRussa;
	
	var _pavioCurto = function(DOMElementID){
		$('#'+DOMElementID).html('Carregando...');
		$('#'+DOMElementID).load(URL+'webservice.php?module=voto&file=pavio-curto');
	}
	this.pavioCurto = _pavioCurto;
	
	var _recentes = function(DOMElementID, pagina){
		var pagina = (pagina == null) ? 0 : pagina;
		$('#'+DOMElementID).html('Carregando...');
		$('#'+DOMElementID).load(URL+'webservice.php?module=voto&file=recentes&pagina='+pagina);
	}
	this.recentes = _recentes;
	
	var _maisVotadas = function(DOMElementID, pagina){
		var pagina = (pagina == null) ? 0 : pagina;
		$('#'+DOMElementID).html('Carregando...');
		$('#'+DOMElementID).load(URL+'webservice.php?module=cultura_musicas&file=mais-votadas&pagina='+pagina);
	}
	this.maisVotadas = _maisVotadas;
	
	var _locutor = function(DOMElementID, pagina){
		var pagina = (pagina == null) ? 0 : pagina;
		$('#'+DOMElementID).html('Carregando...');
		$('#'+DOMElementID).load(URL+'webservice.php?module=cultura_musicas&file=locutor&pagina='+pagina);
	}
	this.locutor = _locutor;
	
	var _musicaBusca = function(q, DOMElementID, pagina){
		var pagina = (pagina == null) ? 0 : pagina;
		$('#'+DOMElementID).html('Carregando...');
		$('#'+DOMElementID).load(URL+'webservice.php?module=cultura_musicas&file=musica_busca&dev=1&q='+q+'&pagina='+pagina);
	}
	this.musicaBusca = _musicaBusca;
	
	var _votar  = function(table, table_id, section_id, user_id, voto_config_id){
		
		var table = (table == null) ? getTable() : table;
		var table_id = (table_id == null) ? getTableId() : table_id;
		var section_id = (section_id == null) ? getSectionId() : section_id;
		var user_id = (user_id == null) ? getUserId() : user_id;
		var voto_config_id = (voto_config_id == null) ? getVotoConfigId() : voto_config_id;
		
		$.post(
			URL+'webservice.php?file=votar&module=voto',
			{
				table:table,
				table_id:table_id,
				section_id:section_id,
				user_id:user_id,
				voto_config_id:voto_config_id
			},
			function(responseXML){
                $.xmlDOM(responseXML)
			        .find('response > success')
			        .each(function() {
						xmlSuccess 	= $(this).text();
					});
				
				$.xmlDOM(responseXML)	
					.find('response > alert')
			        .each(function() {
						xmlAlert 	= $(this).text();
					});
				
				$.xmlDOM(responseXML)	
					.find('response > comment')
			        .each(function() {
						xmlComment 	= $(this).text();
					});
				
				switch(xmlSuccess){
					case "-3": // jï¿½ votou
						$('#alert').html('Voc&ecirc; j&aacute; votou neste conte&uacute;do antes! Obrigado! :)');
						$('#alert').removeClass();
						$('#alert').addClass('attemption');
						window.location.hash="alert";
						break;
					
					case "-1": // nï¿½o logado
						location.href=xmlAlert;
						break;
						
					case "0":
						$('#alert').html(xmlAlert);
						$('#alert').removeClass();
						$('#alert').addClass('attemption');
						window.location.hash="alert";
						break;
						
					case "1":
						$('#joinha_'+table_id).addClass('on');
						$('#total-votos_'+table_id).load(URL+'webservice.php?file=totalVotos&module=voto&table='+table+'&table_id='+table_id+'&voto_config_id='+voto_config_id);
						break;
						
					default:
						$('#alert').html(xmlAlert);
						$('#alert').removeClass();
						$('#alert').addClass('attemption');
						window.location.hash="alert";
						break;
				}
						
			        
            }
		);
	}
	this.votar = _votar;
	
}
