s = new SwissKnife();
$(document).ready(function(){
	// Ação do botão de apagar
	$('.apagar a').click(function(){
		return confirm('Deseja mesmo apagar?');							  
	})
	
	// Aredondando os cantos
	montarLayout();						   
})

// Cantos arredondados das divs
function montarLayout(){
	//arredondamento do conteúdo
	$('#conteudo').append('<span class="arredondamento_inferior"></span>');
	
	// Arredondamento das divs dentro do conteúdo
	$('#conteudo div').each(function(){
		$(this).append('<span class="arr_esq"></span><span class="arr_dir"></span>');
	})
	
	// Arredondamento dos H2
	$('#conteudo div h2').each(function(){
		$(this).append('<span class="linha"></span><span class="arr_esq"></span><span class="arr_dir"></span>');
	})
}

// Atualiza as imagens após enviar por flash
function atualizarImagens(){
	// Variáveis Básicas
	$tipo = $('.tipo').attr('value');
	$id_tipo = $('.id_tipo').attr('value');
	
	// Separando num array as imagens já existentes
	ids_imagens = new Array();
	imagens = document.getElementById('listagem_imagens').getElementsByTagName('li');
	for(i=0;i<imagens.length;i++){
		ids_imagens.push(imagens[i].getAttribute('class'));
	}
	
	// Buscando array com as imagens
	$.ajax({
		url: '../../actions/action_atualiza_imagens.php?tipo='+$tipo+'&id_tipo='+$id_tipo,
		cache: true,
		success: function(object_xml){
			raiz = object_xml.getElementsByTagName('imagens')[0];
			for(i=0;i<raiz.getElementsByTagName('imagem').length;i++){
				// Separando a  imagem para facilitar
				imagem = raiz.getElementsByTagName('imagem')[i];
				caminho = imagem.getElementsByTagName('caminho')[0].childNodes[0].nodeValue;
				id_imagem = imagem.getElementsByTagName('id_imagem')[0].childNodes[0].nodeValue;

				// Só insere a imagem se ela já não estiver na tela =D
				if(!s.in_array(ids_imagens,id_imagem)){
					// Criando os elementos
					li = document.createElement('li');
					img = document.createElement('img');
					a1 = document.createElement('a');
					a2 = document.createElement('a');
					
					// Atributos dos elementos
					li.className = id_imagem;
					li.style.display = 'none'
					img.setAttribute('src', '../../../fotos/'+$tipo+'/md_'+caminho);
					img.setAttribute('width', '151');
					a1.setAttribute('href', '../../actions/action_imagens.php?id_imagem='+id_imagem+'&acao=apagar&tipo='+$tipo);
					a1.setAttribute('title', 'Apagar');
					a1.className = 'bt_apagar';
					a2.setAttribute('href', '../../actions/action_imagens.php?id_imagem='+id_imagem+'&acao=recortar&tipo='+$tipo);
					a2.setAttribute('title', 'Recortar');
					a2.className = 'bt_recortar';
					
					// Apendando tudo
					li.appendChild(img);
					li.appendChild(a1);
					li.appendChild(a2);
					document.getElementById('listagem_imagens').appendChild(li);
					$('.'+id_imagem).fadeIn('slow');
				}
			}
		},
		error : function(){
			alert('Erro: Página não encontrada!');
		}
	});
}