function montaTagCloud(){
	var limit = (arguments.length > 0) ? arguments[0] : 20;
	var cores = new Array('#ff0000','#0066cc','#003399','#660099','#ff9900','#99cc33');
	
	var itt = 28;
	var cont = 0;
	var corAnterior = '';
	var tags = new Array();
	
	for(i in json_faq.tags){
		var pos = itt;
		
		if(i <= 2){
			pos = itt - Math.ceil((Math.random()*4));
		}else if(i <= 5){
			pos = itt - Math.ceil((Math.random()*4) + 5);
		}else if(i <= 10){
			pos = itt - Math.ceil((Math.random()*4) + 8);
		}else{
			pos = itt - Math.ceil((Math.random()*4) + 11);
		}
		
		tags.push([json_faq.tags[i].tag,pos]);
	}
	
	var ordena = Math.ceil(Math.random() * 3)-1;
	switch(ordena){
		case 0:
			tags = tags.sort();
			break;
		case 1:
			tags = tags.sort().reverse();
			break;
		case 2:
			tags = tags.reverse();
			break;
	}
	
	for(i in tags){
		var cor = (Math.ceil(Math.random() * cores.length))-1;
		$('#tagcloud').append('<a href="javascript:void(0);" onclick="fnDropdownRenew(); fnFaqBusca(\''+ tags[i][0] + '\');" style="color:' + cores[cor] + '; font-size:' + tags[i][1] + 'px">' + tags[i][0].toLowerCase() + '</a> ');
	}
}

function fnDropdownRenew(){
	$('#assuntosLista').val("");
	$('#detalhesLista').val("");
}

function fnFaqBusca(busca){
	var limit = vLimite;
	
	var idAssunto = $('#assuntosLista').val();
	var idDetalhe = $('#detalhesLista').val();
	
	$('#campoBusca').val(busca);
	busca = busca.trim();
	
	$('#faq_ajaxContent').hide();
	$('.loading').show();
	
	var htmlBusca = '';
	var cont = 0;
	
	var arrP = new Array();
	if(busca == '' && idAssunto == '' && idDetalhe == ''){
		arrP = arrPerguntasFrequentes;
		$('.ttl_perguntas_frequentes').show();
	}else{
		arrP = arrPerguntas;
		$('.ttl_perguntas_frequentes').hide();
	}
	
	for(i in arrP){
		if(cont >= limit)
			break;
		
		if(idAssunto != '' && arrPerguntas[i].detalhe.assunto.id != idAssunto)
			continue;
		
		if(idDetalhe != '' && arrPerguntas[i].detalhe.id != idDetalhe)
			continue;
		
		if(busca == '' || (busca != '' && removeAcento(arrP[i].pergunta.toLowerCase()).indexOf(removeAcento(busca.toLowerCase())) > -1 || removeAcento(arrP[i].resposta.toLowerCase()).indexOf(removeAcento(busca.toLowerCase())) > -1)){
			htmlBusca += '<dl class="espaco_top">';
			htmlBusca += '	<dt class="pergunta" id="pergunta_'+ arrP[i].id +'"><strong>'+ arrP[i].pergunta +'</strong></dt>';
			htmlBusca += '	<dd>'+ arrP[i].resposta +'</dd>';
			htmlBusca += '</dl>';
			cont++;
		}
	}
	if(cont == 0){
		htmlBusca += '<p>';
		htmlBusca += 'Não foi encontrado nenhum resultado';
		
		if(busca != '')
			htmlBusca += ' com a palavra "<strong>'+ busca +'</strong>"';
		
		if(idAssunto != '' || idDetalhe != '')
			htmlBusca += ' na categoria selecionada';
		
		htmlBusca += '.</p>';
		
	}else{
		//if ($.browser.msie) {
            $('body').append('<iframe src="/paravoce/atendimento/faq/faq-send.php?tag=' + busca + '&chave='+ chaveDominio +'" class="display_none">');
			//$('body').find('iframe.display_none').remove();
        //}else{
		//	$.post('http://promocoesvivo.a1.intra/faq_track/functions.php?tag=' + busca + '&chave='+ chaveDominio);
		//}
	}
	
	$('#faq_content').html(htmlBusca);
	
	fnAjaxOnload();
	$('.loading').hide();
	$('#faq_ajaxContent').fadeIn('slow');
}

function fnAjaxOnload(){
	$('.itens-perguntas-frequentes > dl > dd').hide();
	$('.itens-perguntas-frequentes dl:first').addClass('sem_border');
	$('.itens-perguntas-frequentes > dl > dt').click(function(){
															  
		//Controle de metrica pegando as variareis
		var mCategoria = ($("#assuntosLista option:selected").val() != '') ? $("#assuntosLista option:selected").text() : '';
		    mCategoria = ($("#detalhesLista option:selected").val() != '') ? mCategoria +'-'+$("#detalhesLista option:selected").text() : mCategoria;
		    mCategoria = (mCategoria == '') ? 'perguntasfrequentes' : retirarAcento(mCategoria);
		var mPergunta   = retirarAcento($(this).find('strong').html()) ;
		
		if($(this).parent().children('dd').css('display') == 'block'){
			$(this).removeClass('on');
			$(this).parent().children('dd').hide();
		}else{
			$('.itens-perguntas-frequentes > dl > dt').removeClass('on');
			$('.itens-perguntas-frequentes > dl > dd').hide();
			$(this).addClass('on');
			$(this).parent().children('dd').show();
			
			var idPergunta = $(this).attr('id').split('_')[1];
			//if ($.browser.msie) {
				$('body').append('<iframe src="/paravoce/atendimento/faq/faq-send.php?pClick=' + idPergunta + '&chave='+ chaveDominio +'" class="display_none">');

				//Metrica do faq
				$('body').append('<iframe src="/metrica/faq.gif?regional='+ getCookie('regional') +'&cat='+mCategoria+'&itm='+ escape(mPergunta) +'" class="display_none">');
				//$('body').find('iframe.display_none').remove();
			//}else{
			//	$.post('http://promocoesvivo.a1.intra/faq_track/functions.php?pClick=' + idPergunta + '&chave='+ chaveDominio);
			//}
		}
	});
	
	//$('.itens-perguntas-frequentes dl:first > dt').click();
}


function Assunto(pId, pAssunto){
	this.id = pId;
	this.nome = pAssunto;
}
function Detalhe(pId, objAssunto, pDetalhe){
	this.id = pId;
	this.assunto = objAssunto;
	this.nome = pDetalhe;
}
function Pergunta(pId, objDetalhe, pPergunta, pResposta){
	this.id = pId;
	this.detalhe = objDetalhe;
	this.pergunta = pPergunta;
	this.resposta = pResposta;
}

var arrAssuntos = new Array();
var arrDetalhes = new Array();
var arrPerguntas = new Array();
var arrPerguntasFrequentes = new Array();

function createFaqObjects(){
	for(i in json_faq.faq){
		var assunto = new Assunto(json_faq.faq[i].id_assunto , json_faq.faq[i].assunto);
		arrAssuntos.push(assunto);
		
		for(j in json_faq.faq[i].detalhes){
			var detalhe = new Detalhe(json_faq.faq[i].detalhes[j].id_detalhe , assunto , json_faq.faq[i].detalhes[j].detalhe);
			arrDetalhes.push(detalhe);
			
			for(k in json_faq.faq[i].detalhes[j].perguntas){
				var pergunta = new Pergunta(json_faq.faq[i].detalhes[j].perguntas[k].id_pergunta , detalhe , json_faq.faq[i].detalhes[j].perguntas[k].pergunta , json_faq.faq[i].detalhes[j].perguntas[k].resposta);
				arrPerguntas.push(pergunta);
			}
		}
	}
	
	for(i in json_faq.frequentes){
		var pergunta = new Pergunta(json_faq.frequentes[i].id_pergunta , null , json_faq.frequentes[i].pergunta , json_faq.frequentes[i].resposta);
		arrPerguntasFrequentes.push(pergunta);
	}
}

function montaDropDownAssuntos(){
	if(arrAssuntos != undefined && arrAssuntos != null){
		for(i in arrAssuntos){
			document.getElementById('assuntosLista').options.add(new Option(arrAssuntos[i].nome,arrAssuntos[i].id));
		}
	}
}

function assuntosLista_onChange(idAssunto){
	$('#detalhesLista option').remove();
	document.getElementById('detalhesLista').options.add(new Option('Selecione o Detalhe',''));
	
	if(idAssunto != ''){
		for(i in arrDetalhes){
			if(arrDetalhes[i].assunto.id == idAssunto){
				document.getElementById('detalhesLista').options.add(new Option(arrDetalhes[i].nome,arrDetalhes[i].id));
			}
		}
		$('#detalhesLista').attr('disabled',false);
	}else{
		
		$('#detalhesLista').attr('disabled',true);
	}
}
