/*------------------------------------------*/
/* Site */
/*------------------------------------------*/

var url = 'http://www.ligasemdor.com.br/';


$(document).ready(function() {
	
	$('.visibility').click(function() { 
		
		var id = $(this).attr('id');
		$('#detalhes'+id).toggle('clip');
	});
	
	
	
	$('#pag').change(function(){
		var link = $('#pag option:selected').val();
		$('#link').val(link);
	})
	
	$('#links').change(function(){
		var link = $('#links option:selected').val();
		var titulo = $('#links option:selected').text();
		var insertLink = '<a href="'+link+'">'+titulo+'</a>';
		if( link != 0 )
			{
				var t = tinyMCE;
				t.execCommand('mceInsertContent', false, insertLink);
			}
	})
	
	$('#btnArquivo').click( function () {
		var Element = $('#arquivo option:selected');
		var Valor = '<a href="ficheiros/'+Element.val()+'"><img width="35" height="35" src="'+Element.attr('id')+'" id="'+Element.val()+'"></a>';
		var ValorAnterior = $('#arquivos_hide').val();
		$('#arquivos_hide').val(ValorAnterior+";"+Valor);
		var t = tinyMCE;
		t.execCommand('mceInsertContent', false, Valor);
	})
	
	$('#btnVideo').click( function () {
		var Element = $('#arquivoV option:selected');
		var tipo = $('#arquivoV option:selected').val();
		var Valor = '<a href="#"><img src="'+Element.attr('id')+'" id="'+Element.text()+'" width="35" height="35"></a>';
		var ValorAnterior = $('#arquivos_hide').val();
		$('#arquivos_hide').val(ValorAnterior+";"+Valor);
		
		var t = tinyMCE;
		t.execCommand('mceInsertContent', false, Valor);
	})
	
	var tabAtual = 1
	 
	mudarTab = function(numeroTab) {
		$("#tab_"+tabAtual).toggle()
		$("#tab_"+numeroTab).toggle()
		tabAtual = numeroTab
	}
	
	
	$('#divLista tbody').sortable({
		revert:true,
		placeholder: "ui-state-highlight",
		connectWith: "#teste",
		update : function() { 
			var order = $('#divLista tbody').sortable('serialize'); 
			$.ajax({
				type : 'post',
				url : url+'admin/pagina/ordenar',
				data : order,
				success : function (html){
					if( html == '1' )
					{
						alert('Um erro ocorreu....'+html);
					}
				}
			});	
		}
	});
	
	$( "#divLista tbody" ).disableSelection();

	jQuery('.container-img').sortable({
		revert:true,
		connectWith: ".galeria-item",
		update : function() { 
			var order = $('.container-img').sortable('serialize'); 
			$.ajax({
				type : 'post',
				url : url+'admin/arquivo/ordenar',
				data : order,
				success : function (html){
					if( html == '1' )
					{
						alert('Um erro ocorreu....'+html);
					}
				}
			});	
		}
	});

	jQuery( ".container-img" ).disableSelection();
	
	
	jQuery('#galerias tbody').sortable({
		revert:true,
		connectWith: ".galeria-item",
		update : function() { 
			var order = $('#galerias tbody').sortable('serialize'); 
			$.ajax({
				type : 'post',
				url : url+'admin/arquivo/ordenar-galeria',
				data : order,
				success : function (html){
					alert( html );
					if( html == '1' )
					{
						alert('Um erro ocorreu....'+html);
					}
				}
			});	
		}
	});

	jQuery( "#galerias tbody" ).disableSelection();
	
	
});

startList = function() {
	if (document.all&&document.getElementById) {
	navRoot = document.getElementById("navigation");
	for (i=0; i<navRoot.childNodes.length; i++) {
	node = navRoot.childNodes[i];
	if (node.nodeName=="LI") {
	node.onmouseover=function() {
	this.className+=" over";
	  }
	  node.onmouseout=function() {
	  this.className=this.className.replace
		(" over", "");
	   }
	   }
	  }
	 }
	}
	window.onload=startList;



	
/*------------------------------------------*/
/* Fim do site */
/*------------------------------------------*/


/*------------------------------------------*/
/* Ajax */
/*------------------------------------------*/



function Excluir(id)
{
	$.ajax({
		type: 'post',
		url: '../admin/link/excluir',
		data : 'id='+id,
		success: function(html){
			if(html == 1)
				{	
					$('#row'+id).fadeOut(1000);
				}
			else
				{
					alert('houve um erro..');
				}
			
		}
	});
}

function ExcluirPagina(id)
{
	$.ajax({
		type: 'post',
		url: '../admin/pagina/excluir',
		data : 'id='+id,
		success: function(html){
			if(html == 1)
				{	
					$('#row_'+id).fadeOut(1000);
				}
			else
				{
					alert('houve um erro..');
				}
			
		}
	});
}


function ExcluirGaleria(id)
{
	$.ajax({
		type: 'post',
		url: url+'admin/arquivo/excluir',
		data : 'id='+id,
		success: function(html){
			$('#row_'+id).fadeOut(1000);
		}
	});
}


function ExcluirArquivo(id)
{
	$.ajax({
		type: 'post',
		url: url+'admin/arquivo/item/excluir',
		data : 'id='+id,
		success: function(html){
			if(html == 1)
				{	
					$('#row_'+id).fadeOut(1000);
				}
			else
				{
					alert('houve um erro..');
				}
			
		}
	});
}


function ExcluirImagem(id)
{
	$.ajax({
		type: 'post',
		url: url+'admin/arquivo/imagem/excluir',
		data : 'id='+id,
		success: function(html){
			if(html == 1)
				{	
					$('#row'+id).fadeOut(1000);
				}
			else
				{
					alert('houve um erro..');
				}
			
		}
	});
}

function ExcluirApoio(id)
{
	$.ajax({
		type: 'post',
		url: url+'admin/template/excluir',
		data : 'id='+id,
		success: function(html){
			if(html == 1)
				{	
					$('#row'+id).fadeOut(1000);
				}
			else
				{
					alert('houve um erro..');
				}
			
		}
	});
}

function ProximaPagina(page, id) 
{ 
	$.ajax({
		type: 'post',
		url: '../www.ligasemdor.com.br/proxima-pagina', 
		data : 'page='+page+'&id='+id,
		 beforeSend: function(){
		  $('#carregando').fadeIn();  
			}, 
		success: function(html){
			if(html != 'erro')
				{	
					$('#galeria_itens').html(html);
				}
			else
				{
					alert('houve um erro..');
				}
			
		}
	});
}

function ToggleDescricao(id)
{
	$('#text'+id).toggle('clip');
}

function EditaDescricao(id)
{
	var descricao = $('#descricao'+id).val(); 
	$.ajax({
		type: 'post',
		url: url+'admin/arquivo/descricao', 
		data : 'text='+descricao+'&id='+id, 
		success: function(html){
			if(html == 'Erro')
				{	
					alert('Um erro ocorreu.....');
				}
				else
				{
					alert('Atualizado!!!!');
				}
			
		}
	});
}


function setar_pagina_mae( id_pagina )
{
	id_filha = $('#select_'+id_pagina).find('option').filter(':selected').val();
	
	$.ajax({
		type: 'post',
		url: url+'admin/pagina/mae', 
		data : 'id_pagina='+id_pagina+"&id_filha="+id_filha, 
		success: function(html){
			alert( html );
		}
	});
}
