//jQuery ai invés de $
jQuery.noConflict();

//Inicialização dos elementos...
jQuery(document).ready(
	function(){ setup_all(); }
);

function setup_all(){
	//configura estados->cidades
	setup_estados();
	
	//configura máscaras
	setup_mascaras();
	
	//Configura FCKEditor
	setup_FCK();
}

function setup_mascaras(){
	//configura campos com a classe "data" para terem o calendário do jquery
	jQuery('.data').datepicker();
}

//novo estados->cidades
function setup_estados(){
	jQuery(".estado").change(function(){
		jQuery(".cidade").each(function(){
			//recria o componente pois quando tem muitas cidades demora para apagar uma a uma...
			jQuery(this).replaceWith('<select id="'+this.id+'" name="'+this.name+'" class="'+jQuery(this).attr('class')+'"><option>Carregando cidades...</option></select>');
		});
		jQuery(".cidade").load(cakebase+'/admin/cidades/lista/'+this.value);
	});
}

function setup_FCK(){
	var js_dir = cakebase + '/js/';
	jQuery(".fckEditor").each(function(){
		var bFCKeditor = new FCKeditor(this.id);		
		bFCKeditor.Width = 700;
		bFCKeditor.Height = 500;
		bFCKeditor.BasePath = js_dir;
		bFCKeditor.ToolbarSet = 'Convenio';
		bFCKeditor.ReplaceTextarea();
	});
	jQuery(".fckEditor2").each(function(){
		var bFCKeditor = new FCKeditor(this.id);		
		bFCKeditor.Width = 820;
		bFCKeditor.Height = 250;
		bFCKeditor.BasePath = js_dir;
		bFCKeditor.ToolbarSet = 'Convenio';
		bFCKeditor.ReplaceTextarea();
	});
}

function trocaElemento(IdListFonte,IdListAlvo){   
	listFonte = document.getElementById(IdListFonte);
	listAlvo = document.getElementById(IdListAlvo);
	for(i = 0; i < listFonte.options.length; i++) {
		if(listFonte.options[i].selected == true) {
			elementoSelecionado = listFonte.options[i];
			listAlvo.options[listAlvo.options.length] =  new Option(elementoSelecionado.text,elementoSelecionado.value, null, null);
			listFonte.options[i] = null;
			i--;
		}
    }
	sortlist(IdListAlvo);
}

function selecionaElementos(IdList){
	list = document.getElementById(IdList);
	for (var i = 0; i < list.options.length; i++) {
		list.options[i].selected = true;
	}
}

function abreJanelaPopUp(element){
	 var largura = 640;
     var altura = 335;
     var esquerda = ((screen.width - largura) / 2);
     var topo = ((screen.height - altura) / 2);
	 window.open (element.href,element.name,"channelmode=0,directories=0,fullscreen=no,location=0,menubar=0,resizable=0,scrollbars=yes,status=0,titlebar=0,toolbar=0,top="+topo+"px,left="+esquerda+"px,width="+largura+"px,height="+altura+"px");
	 return false;
}

function sortlist(IdListAlvo) {
    var listAlvo = document.getElementById(IdListAlvo);
    arrTexts = new Array();
    for(i=0; i<listAlvo.length; i++) {
        arrTexts[i] = listAlvo.options[i].text+'|'+listAlvo.options[i].value;
    }
    arrTexts.sort();
    for(i=0; i<listAlvo.length; i++) {
        el = arrTexts[i].split('|');
        listAlvo.options[i].text = el[0];
        listAlvo.options[i].value = el[1];
    }
}

//função para exibir/ocultar itens
function showHide(id){
	var item = document.getElementById(id);
	
	if(jQuery(item).is(":visible")){
		jQuery(item).slideUp();
	}
	else {
		jQuery(item).slideDown();
	}
	return false;
}
