
//flash
function GerarSWF(arquivo,altura,largura,ide){
	document.writeln('<object id="globalnav-object" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + largura + '" height="' + altura + '" id="' + ide + '" name="' + ide + '">');
	document.writeln('<param name="movie" value="' + arquivo + '" />');
	document.writeln('<param name="FlashVars" value="loc=en_US&htmlApp=false&gatewayURL=gwurl" />');
	document.writeln('<param name="wmode" value="opaque" />');
	document.writeln('<embed id="globalnav-embed" src="' + arquivo + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="opaque" flashvars="loc=en_US&htmlApp=false&gatewayURL=gwurl" salign="tl" scale="noscale" id="' + ide + '" width="' + largura + '" height="' + altura + '"></embed>');
	document.writeln('</object>');
}

//confirmar
function confirmar(texto){
	var conf = window.confirm(texto+"?");
	if(conf){
		return true;
	}
	else{
		return false;
	}
}

//pre imagens
function prever(lar,altura,ide,tabela,campo,pasta){
	var w = lar;
	var h = altura;
	var lado = (screen.width - w) / 2;
	var topo = (screen.height - h) / 2;
	urv="id="+ide+"&tab="+tabela+"&cam="+campo+"&pas="+pasta
	var k = window.open('pre_imagens.asp?'+urv+'',''+ide+'','scrollbars=no, width='+lar+', height='+altura+', top='+topo+', left='+lado+'')
	k.focus();
}

//pre flash
function prever2(lar,altura,ide,tabela,campo,pasta,swflar,swfalt){
	var w = lar;
	var h = altura;
	var lado = (screen.width - w) / 2;
	var topo = (screen.height - h) / 2;
	urv="id="+ide+"&tab="+tabela+"&cam="+campo+"&pas="+pasta+"&swflar="+swflar+"&swfalt="+swfalt
	var u = window.open('pre_flash.asp?'+urv+'',''+ide+'','scrollbars=no, width='+lar+', height='+altura+', top='+topo+', left='+lado+'')
	u.focus();
}

//pre imagens
function classificacao(escalao,epoca){
	var w = 700;
	var h = 400;
	var lado = (screen.width - w) / 2;
	var topo = (screen.height - h) / 2;
	urv="escalao="+escalao+"&epoca="+epoca
	var c = window.open('l_classificacao.asp?'+urv+'','campeonato','scrollbars=yes, width='+w+', height='+h+', top='+topo+', left='+lado+'')
	c.focus();
}

//acções dos botões
function redireccionar(endereco){
	window.location.href = endereco;
}

function editar(endereco){
	qt = $(".chk:checked").length;
	if(qt != 1){
		alert(' - Seleccione um registo.');
	}
	else{
		if(endereco.indexOf("?") == -1){
			window.location.href = endereco+"?id="+$(".chk:checked").val();
		}
		else{
			window.location.href = endereco+"&id="+$(".chk:checked").val();
		}
	}
}

//hack datas
$(function(){
	$('.datapick').live('click', function() {
		$(this).datepicker({showOn:'focus', changeMonth: true, changeYear: true}).focus();
	});
});

//checkboxes tabelas
$("#caixa1").live("change", function() {
	if(this.checked){
		v = true;
	}else{
		v = false;
	}
	
	$('.chk').each(function() {
     		this.checked = v;
   	});
});

/*
 * Natural Sort algorithm for Javascript - Version 0.4 - Released under MIT license
 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
 * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams
 */
function naturalSort(a, b){
	// setup temp-scope variables for comparison evauluation
	var re = /(^[0-9]+\.?[0-9]*[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
		sre = /(^[ ]*|[ ]*$)/g,
		hre = /^0x[0-9a-f]+$/i,
		ore = /^0/,
		// convert all to strings and trim()
		x = a.toString().replace(sre, '') || '',
		y = b.toString().replace(sre, '') || '',
		// chunk/tokenize
		xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
		yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
		// hex or date detection
		xD = parseInt(x.match(hre)) || (new Date(x)).getTime(),
		yD = parseInt(y.match(hre)) || xD && (new Date(y)).getTime() || null;
	// natural sorting of hex or dates - prevent '1.2.3' valid date
	if ( y.indexOf('.') < 0 && yD )
		if ( xD < yD ) return -1;
		else if ( xD > yD )	return 1;
	// natural sorting through split numeric strings and default strings
	for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
		// find floats not starting with '0', string or 0 if not defined (Clint Priest)
		oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
		oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
		// handle numeric vs string comparison - number < string - (Kyle Adams)
		if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1; 
		// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
		else if (typeof oFxNcL !== typeof oFyNcL) {
			oFxNcL += ''; 
			oFyNcL += ''; 
		}
		if (oFxNcL < oFyNcL) return -1;
		if (oFxNcL > oFyNcL) return 1;
	}
	return 0;
}


//jquery
$(document).ready(function() {
	//plugin para datatables aceitar datas dd/mm/yy
	jQuery.fn.dataTableExt.aTypes.push(
		function ( sData )
		{
			if (sData.match(/^(0[1-9]|[12][0-9]|3[01])\-(0[1-9]|1[012])\-(19|20|21)\d\d$/))
			{
				return 'uk_date';
			}
			return null;
		} 
	);

 	jQuery.fn.dataTableExt.oSort['uk_date-asc']  = function(a,b) {
		var ukDatea = a.split('-');
		var ukDateb = b.split('-');
	
		var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
		var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
	
		return ((x < y) ? -1 : ((x > y) ?  1 : 0));
	};

	jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
		var ukDatea = a.split('-');
		var ukDateb = b.split('-');
	
		var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
		var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
	
		return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
	};
	
	//plugin para ordenar
	jQuery.fn.dataTableExt.oSort['natural-asc']  = function(a,b) {
		return naturalSort(a,b);
	};
	
	jQuery.fn.dataTableExt.oSort['natural-desc'] = function(a,b) {
		return naturalSort(a,b) * -1;
	};

	//menus
	$("ul.sf-menu").superfish({ 
   		animation: {height:'show'},   // slide-down effect without fade-in 
   		delay:     500               // 1.2 second delay on mouseout 
	}); 

	//datas
	$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['pt-BR']));

	//editar
	$(".edit").editInPlace({
    	url: "ajax.asp?tipo=nolugar",
		saving_image: "images/ajax-loader.gif",
		value_required: true,
		save_button: '<button class="inplace_save">Guardar</button>',
		cancel_button: '<button class="inplace_cancel">Cancelar</button>',
		show_buttons: true
	});
	
	$(".editselect").editInPlace({
		url: "editarnolugar.asp?edit=select",
		saving_image: "images/ajax-loader.gif",
		value_required: true,
		field_type: "select",
		select_options: "Esquerda,Baixo"
	});
	
	$(".editarea").editInPlace({
		//callback: function(unused, enteredText) { return enteredText; },
		url: "editarnolugar.asp?edit=area",
		field_type: "textarea",
		textarea_rows: "7",
		textarea_cols: "35",
		use_html: true,
		saving_image: "images/ajax-loader.gif",
		save_button: '<button class="inplace_save">Guardar</button>',
		cancel_button: '<button class="inplace_cancel">Cancelar</button>',
		show_buttons: true
	});
	
	//tabs
	$( "#tabs" ).tabs();

	//pretyfoto
	$("a[tag^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'facebook' /* light_rounded / dark_rounded / light_square / dark_square */
	});
	
	//validar formulários
	$('#validar').click(function(){ 
   		$('#formulariogeral').submit(); 
	}) 
	
	$("#formulariogeral").validationEngine({
		success : function(){  
			$("#formulariogeral").submit();
		} 
	});

//Tipos e Marcas
    $("#tipo").change(function() {
        $("#marca").html('<option value="">Selecione uma Marca</option>');
        if($(this).val() != ""){
            $.ajax({
                   type: "POST",
                   url: "ajax.asp",
                   data: "tipo=ajaxmarca&id="+$(this).val(),
                   success: function(msg){
                    $("#marca").html(msg);
                   }
             });
        }
    });

	
	
	
});


