$(document).ready(function(){
	
	$("#navselect").livequery('change', function() {
		var offset = $('#navselect option:selected').val();
		var extra = $("#navextra").val();
		$("#main").load("main.php?offset="+offset+extra);		
		return false;
	});
	
	$(".botao_nav a").livequery('click', function() {
		var href = this.href.split("?")[1];
		var extra = $("#navextra").val();
		$("#main").load("main.php?"+href+extra); 	
		return false;
	});

	var seleccionados = new Array();	
	$(".checkbox_lista").livequery('click', function() { 
		var val_check = $(this).val();
		if($(this).is(':checked')){
			seleccionados.push(val_check);
		}else {
			var index = $.inArray(val_check, seleccionados);
			seleccionados.splice(index,1);	
		}
	});	

	var accao = "";
	$(".botao_layer a").livequery('click', function() { 
		var cmd = $(this).text();
		if(seleccionados.length > 0){
			if(cmd.toLowerCase() == "apagar"){
				$("#Layer2").modal();
				accao = cmd;
			}else {
				$("#Layer1").modal();
				accao = cmd;
			}
			return false;
		}else {
			alert("Escolha um ou mais registos na lista para "+cmd.toLowerCase()+".");
			return false;
		}
	});	

	var newIdCat = "";
	$("#newIdCat").livequery('change', function() {
		newIdCat = $('#newIdCat option:selected').val();
	});

	$(".layer_confirmar").livequery('click', function() {
		options = {};
		options["cmd"] = accao;
		options["linhasMarcadas[]"] = seleccionados;
		if(accao.toLowerCase() == 'copiar' || accao.toLowerCase() == 'mover'){
			if(newIdCat == ""){
				alert("Escolha uma categoria.");
				return false;
			}else options["newIdCat"] = newIdCat;
		}
		$("#main").load("main.php",options);
		seleccionados = new Array();
		$.modal.close();
		return false; 
	});

	$(".layer_cancelar").livequery('click', function() { 
		$.modal.close();
		return false;
	});	

	/*$(".accao_registo_lista").livequery('click', function() { 
		var href = this.href.split("?")[1];	
		$("#main").load("main.php?"+href); 	
		return false;
	});	*/

	$(".selectMedia").livequery('change', function() {
		var options = {};
		options["tipo"] = $(this).attr("id").split("selectMedia")[1];
		var id = $(this).attr("id");
		options["idCat"] =  $("#"+ id +" :selected").val();
		$("#main").load("main.php", options);	
	}); 
	
	
});

function js_array_to_php_array (a){
	var a_php = "";
	var total = 0;
	for (var key in a){
		++ total;
	    a_php = a_php + "s:" +
	             String(key).length + ":\"" + String(key) + "\";:" +
	             String(a[key]).length + ":\"" + String(a[key]) + "\";";
	 }
	a_php = "a:" + total + ":{" + a_php + "}";
	return a_php;
}

function muda(tipo, idCat){
	parent.nav.location.href="nav.php?tipo="+ tipo + "&idCat=" + idCat;
	document.location.href="main.php?tipo="+ tipo + "&idCat=" + idCat;
}

function mudaOffset(){
	if (arguments.length>1)
		document.location.href="main.php?offset=" + arguments[0] + arguments[1];
	else document.location.href="main.php?offset=" + arguments[0];
}

function delchk(){ 
	return (confirm("Deseja apagar este registo ? Não está a funcionar - corrigir!!"));
}


function open_pop(nome, campo){
	window.open('popup.php?popup='+ nome +'&campo=' + campo,'popup','scrollbars=yes,width=900,height=1000');
	return false;
}

function open_password(nome){
	MM_openBrWindow('popup/password.popup.php?id='+nome,'popup','scrollbars=yes,width=387,height=400');
}

/*
function open_popup(campo){
	
//MM_openBrWindow('popup/imagem.popup.php?campo=' + campo,'popup','scrollbars=yes,width=700,height=800');
	window.open('popup/imagem.popup.php?campo=' + campo,'popup','scrollbars=yes,width=700,height=800');
}

function open_popupf(){
MM_openBrWindow('popup/ficheiro.popup.php','popup','scrollbars=yes,width=700,height=800');
}

function open_popupr(campo){
MM_openBrWindow('popup/relacao.popup.php?campo=' + campo,'popup','scrollbars=yes,width=700,height=800');
}

function open_pop_old(nome, campo){
MM_openBrWindow('popup/'+nome +'.popup.php?campo=' + campo,'popup','scrollbars=yes,width=700,height=800');
}*/

/*
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}*/

$("#textoprocurar").live("keypress", {}, keypressInBox);

function keypressInBox(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) { //Enter keycode                        
        e.preventDefault();
		
        $("#dataform").submit();
    }
};

