$(function(){
	colocaflashs();
	colocaflashobj("main_menu", "images/flash/top.swf", "flash_main_menu", 900, 350, {sel:$('#main_menu').attr('sel')});

	$('#div_votacao').html('<img voto="1" src="images/spacer.gif" class="img_voto" alt=""><img voto="2" src="images/spacer.gif" class="img_voto" alt=""><img voto="3" src="images/spacer.gif" class="img_voto" alt=""><img voto="4" src="images/spacer.gif" class="img_voto" alt=""><img voto="5" src="images/spacer.gif" class="img_voto" alt="">');
	$('#div_votacao img[voto]').each(function(){
		$(this).click(function(){
		  votando($(this).parent(),$(this).parent().attr('tbl'),$(this).parent().attr('itemid'),$(this).attr('voto'))
		})
		$(this).mouseover(function(){
			$(this).parent().attr('class','votos'+$(this).attr('voto'));
		})
	});
	$('#div_votacao').mouseout(function(){
		$(this).attr('class','votos'+$(this).attr('media'));
	})
	$('form').submit(function(){
		checaform(this);
	});
});

function colocaflashobj(id_target, swf, id_obj, width, height, flash_vars){
	var so = new SWFObject(swf,id_target,width,height,'8');
	so.addParam('allowscriptaccess','always');
	so.addParam('wmode','transparent');
	so.addParam('flashvars',flash_vars);
	so.write(id_target);
}

function donwloadfileid(id){
	donwloadfileid_remove();
	$('body').append('<div id="div_download_iframe" style="position:absolute;width:500px;height:500px;top:-500px;left:-500px;"><iframe src="../admin/util/filedownload.php?id='+id+'" style="width:1px;height:1px;"></iframe></div>');
}
function donwloadfileid_remove(){
	$('#div_download_iframe').remove();
}


function mudaestado(obj_est, id_sel_cid, url){
	estado = $(obj_est).val();
	if(isNaN(estado)||estado==''){
		$(id_sel_cid).html('<option value="">--- selecione o estado ---</option>');
	}else{
		$(id_sel_cid).html('<option value="">carregando....</option>');
		$.get(url,function(data){
			$(id_sel_cid).html('<option value="">--- selecione ---</option>'+data);
		})
	}

}



function colocaflashs(){
	var param = {};
	param.wmode = 'transparent';
	for(i=1;i<=5;++i){
		$('[flash'+i+']').each(function (j) {
			var flashvars = 'txt='+$(this).attr('flash'+i+'');
			wid = $(this).width();
			if(isNaN(wid)){
				wid = wid.substring(0,wid.length-2);
			}
			hei = $(this).height();
			if(isNaN(hei)){
				hei = hei.substring(0,hei.length-2);
			}
			colocaflashobj($(this).attr('id'), 'images/flash/titulo'+i+'.swf', $(this).attr('id')+"swf", wid, hei, flashvars);
		})
	}
	
	$('.autoflash').each(function (j) {
		wid = $(this).width();
		if(isNaN(wid)){
			wid = wid.substring(0,wid.length-2);
		}
		hei = $(this).height();
		if(isNaN(hei)){
			hei = hei.substring(0,hei.length-2);
		}
		$(this).attr('id','autoflash'+j);
		colocaflashobj($(this).attr('id'), $(this).attr('src'), 'autoflash'+j, wid, hei, $(this).attr('vars'));
	})
	
	
}

function loadmapa(id,tipo){
	$("#area_itens").hide("slow");
	$.ajax({
		type: "GET",
		url: "../admin/gui/flash_scripts/xml.mapa_list.php?id="+id+"&tipo="+tipo,
		dataType: "xml",
		success: function(xml) {
			$('#area_itens').html('');
			$(xml).find('titulo').each(function(){
				var nomecidade = $(this).text();
				var so = new SWFObject("images/flash/titulo4.swf?txt="+nomecidade, "flash_ultimo", 256, 25, "9", "", "high", "", "index.php?noflash=1");
				so.write('ultimo');
			});
			$(xml).find('rep').each(function(){
				$(this).find('nome').each(function(){
					$('<h3></h3>')
						.html($(this).text())
						.appendTo('#area_itens');
				})
				$(this).find('dados').each(function(){
					$('<p></p>')
						.html($(this).text())
						.appendTo('#area_itens');
				})
			});
			$("#area_itens").show("slow");
		}
	});
}

function checaform(f){
	erro = true;
	$(f).find('[obrig]').each(function(){
		if($(this).val() == ''){
			if(erro == true){
				alert('O campo '+$(this).attr('obrig')+' é obrigatório.');
				$(this).focus();
				erro = false;
			}
		}
	})
	if(erro == true){
		$(f).find('input[type=file][filtra]').each(function(){
			ext_val = $(this).attr('filtra');
			ext_val_array = ext_val.split(',');
			valido = false;
			ext_invalida = '';
			arquivo = $(this).val();
			var ext = (arquivo.substring(arquivo.length-3,arquivo.length)).toLowerCase();
			ext_invalida = ext;
			if(ext != ''){
			   for (i = 0; i < ext_val_array.length; i++) {
					if (ext_val_array[i] == ext) {
						valido = true;
					}
				}
				if(valido != true){
					alert('Atenção: Somente são permitidos arquivos com a extensão ('+ext_val+')\nO arquivo selecionado tem a extensão "'+ext_invalida+'"');
					erro = false;
				}
			}
		});
	}
	return erro;
	valido = false;
	ext_invalida = '';
	$(form).find('input[type=file][filtra]').each(function(){
		arquivo = $(this).val();
		ext_val = $(this).attr('filtra');
		ext_val_array = ext_val.split(',');
		var ext = arquivo.substring(arquivo.length-3,arquivo.length);
		ext_invalida = ext;
	     for (var i = 0; i < ext_val_array.length; i++) {
			if (ext_val_array[i] == ext) {
				valido = true;
				break;
			}
      }
	});
	if(valido == true){
		return true;
	}else{
		alert('Atenção: Somente são permitidos arquivos com a extensão ('+ext_val+')\nO arquivo selecionado tem a extensão "'+ext_invalida+'"');
		return false;
	}
}

function printarea(tipo, id){
	$.jPrintArea('print/print.php?tipo='+tipo+'&id='+id);
}

function votando(div,tbl,id,v){
	$(div).hide();
	$.post('index.php',{tbl:tbl,id:id,v:v},function(data){
		if(data != ''){
			Shadowbox.open({
				player:	'html',
				content:	data,
				height:	210,
				width:	450
			});
			votandosometimer();
		}else{
			alert('Desculpe, algum erro ocorreu na votação')
		}
		$(div).show();
	})
}
var timermsg = 0;
function votandosometimer(){
	timermsg = setTimeout(function(){
		clearTimeout(timermsg);
		Shadowbox.close();
	},8000);
}


Shadowbox.init({
	language: 'pt-BR',
	adapter: 'jquery',
	players:  ['iframe','html','img'],
	overlayColor: '#bbe372',
	overlayOpacity: 0.75,
	continuous: true,
	displayCounter: true,
	displayNav: true
});

function cadastro(url){
	Shadowbox.open({
		player:	'iframe',
		content:	'cadastro.php?'+url,
		height:	460,
		width:	650
	});
}


function listacidadessel(pg,v,id,def){
	if(v==''){
		$('#'+id).html(def);
	}else{
		$('#'+id).html('<option value="">carregando cidades .....</option>');
		$('#'+id).load(pg+v);
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}	

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

