var cores = new Array('','#FFF678','#F2CA7C','#F29165','#F49AC1','#84D2DA','#B9D97F','#5DA9DD','#C7C7C7');
var cor = cores[3];
var cor_grupo = cores[6];
var espessura = 1;
var opacidade = 1;
var tempo = 2;
var boias = [];
var totalBoias = 40;
var pontos = [];
var icones = [];
var marcadores = [];
var cont = [];
var status = [];
var statusgrupo = [];
var statusplat = 1;
var plataforma = "";
status[0] = 1;
var i = 0;
var dirImagens = "../";

function criaMapa(lat, lng, zoom, type, pequeno) {
	if (GBrowserIsCompatible()) {
	    map = new GMap2(document.getElementById("map"));

		GEvent.addListener(map, "load", function() {
			document.getElementById("carregando").style.display = "none";
		});
		if (pequeno == 1){
			map.addControl(new GSmallMapControl());
		} else {
		    map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			
			//coordenadas
			GEvent.addListener(map, "mousemove", function (point) {
				document.getElementById("latlng").innerHTML = coord(point.lat(), point.lng());
			});			
						
		}
		map.setCenter(new GLatLng(lat, lng), zoom, type);
	}
}

function criaPlataforma(lat, lng){
	icones[0] = new GIcon();
	icones[0].image = dirImagens + "icones/lancamento.png";
	icones[0].iconSize = new GSize(17, 17);
	icones[0].iconAnchor = new GPoint(8, 8);
	icones[0].infoWindowAnchor = new GPoint(17, 17);
	plataforma = new GMarker(new GLatLng(lat, lng), icones[0]);
	map.addOverlay(plataforma);
}

function mostraPlat(){
	if (statusplat == 1){
		map.removeOverlay(plataforma);
		statusplat = 0;
	} else {
		map.addOverlay(plataforma);
		statusplat = 1;	
	}
	mudaImg('x_plat');
}

function mudaCor(num, cor){
	map.removeOverlay(boias[num]);
	map.removeOverlay(pontos[num]);
	//for(var i=0;i<marcadores[num].length;i++){
	//	map.removeOverlay(marcadores[num][i]);
	//}
	
	boias[num].color = cores[cor];
	icones[num].shadow = dirImagens + "icones/b"+cor+".png";
	//pontos[num].icon = icones[num];
	//m.image = "icones/m"+cor+".png";
	
	if (status[num] == 1){
		map.addOverlay(boias[num]);
		map.addOverlay(pontos[num]);
		//for(var i=0;i<marcadores[num].length;i++){
		//	map.addOverlay(marcadores[num][i]);
		//}		
	}
	showHide('p'+num, 'none');
	mudaImg('c'+num, cor);
}

function mudaTodas(cor){
	for(i=1;i<=totalBoias;i++) {
   		if(status[i] != undefined){
			mudaCor(i, cor);
			if (statusgrupo[i] != "" && statusgrupo[i] != undefined){
				mudaImg('c'+i+'_g', cor);
			}		
		}
	}
	showHide('p0', 'none');
	mudaImg('c0', cor);
}

function mudaGrupo(num, cor){
	for(i=num;i<num+5;i++) {
   		if(status[i] != undefined){
			mudaCor(i, cor);
		}
  	}
	showHide('p'+num+'_g', 'none');
	mudaImg('c'+num+'_g', cor);
}

function mostra(num, muda){
	if (status[num] == 1){ //apaga
		map.removeOverlay(boias[num]);
		map.removeOverlay(pontos[num]);
		//for(var i=0;i<marcadores[num].length;i++){
		//	map.removeOverlay(marcadores[num][i]);
		//}
		status[num] = 0;
	} else {
		map.addOverlay(boias[num]);
		map.addOverlay(pontos[num]);
		
		//var cor_marcador = icones[num].shadow.charAt(8);
		//m.image = "icones/m"+cor_marcador+".png";
		
		//for(var i=0;i<marcadores[num].length;i++){
		//	map.addOverlay(marcadores[num][i]);
		//}		
		status[num] = 1;
	}
	if (muda == 1 || muda == undefined){
		mudaImg('x'+num);
	}
}

function mostraTodas(){
	var muda = 1;
	for(i=1;i<=totalBoias;i++) {
		if(status[i] != undefined){
			if (status[i] == status[0]){
				muda = 1;
			} else {
				muda = 0;
				status[i] = status[0];
			}
			mostra(i, muda);
			//seletor de grupo
			if (statusgrupo[i] == status[0] && statusgrupo[i] != undefined){
				mudaImg('x'+i+'_g');
				if (statusgrupo[i] == 0){ statusgrupo[i] = 1; } else { statusgrupo[i] = 0; }
			}
		}
	}
	mudaImg('x0');
	if (status[0] == 0){ status[0] = 1;	} else { status[0] = 0;	}
}

function mostraGrupo(num){
	var muda = 1;
	for(i=num;i<num+5;i++) {
		if(status[i] != undefined){
			if (status[i] == statusgrupo[num]){
				muda = 1;
			} else {
				muda = 0;
				status[i] = statusgrupo[num];
			}
			mostra(i, muda);
		}
	}
	mudaImg('x'+num+'_g');
	if (statusgrupo[num] == 0){ statusgrupo[num] = 1; } else { statusgrupo[num] = 0; }
}

function play(){
	map.clearOverlays();
	map.addOverlay(plataforma);
	for(var j=1; j<total+1; j++){
		cont[j] = 0;
	}	
	animacao();
}

function animacao(){
	for(var j=1; j<total+1; j++){
		if (cont[j] < boias[j].length){
			var indice = cont[j];
			map.addOverlay(boias[j][indice]);
			cont[j]++;
		} 
		if (cont[j]-1 == boias[j].length-1){  //marca ponto
			map.addOverlay(pontos[j]);
		}
	}
	if (cont[1] < boias[1].length){
		window.setTimeout(animacao, tempo);
	}
}

function showHide(identifier) {
	var argv = showHide.arguments;  
	var argc = showHide.arguments.length;  
	var xv = (argc > 1) ? argv[1] : null;  
    var objeto = xGetStyleObj(identifier);
    if(objeto) {
        if (xv != null){
            objeto.display = xv;
        } else if (objeto.display == "none") {
            objeto.display = "block";
        } else {
            objeto.display = "none";
        }
        return true;
    } else {
        return false;
    }
}

function xGetStyleObj(e) {
    if(document.getElementById && document.getElementById(e)) {
        return document.getElementById(e).style;
    } else if (document.all && document.all(e)) {
        return document.all(e).style;
    } else if (document.layers && document.layers[e]) {
        return document.layers[e];
    } else {
        return false;
    }
}

function mudaImg(imagem, cor) {
	var path = dirImagens + 'imagens/';
	var src = document.getElementById(imagem).src.split('/');
	antiga = src.pop();
	
	if (antiga == 'checked.gif') {
		var nova = 'unchecked.gif';
	} else if (antiga == 'checked_mais.gif') {
		var nova = 'unchecked_mais.gif';
	} else if (antiga == 'checked_mais2.gif') {
		var nova = 'unchecked_mais2.gif';		
	} else if (antiga == 'unchecked.gif') {
		var nova = 'checked.gif';
	} else if (antiga == 'unchecked_mais.gif') {
		var nova = 'checked_mais.gif';
	} else if (antiga == 'unchecked_mais2.gif') {
		var nova = 'checked_mais2.gif';		
	} else {
		var nova = 'cx'+cor+'.gif';
	}
	document.getElementById(imagem).src = path + nova;
}

function downloadArq(num){
	popup('login.php?arq='+num,'login',2,2);
}

function menu(num){
	for (var i=0; i<6; i++){
		parent.frames['cabecalho'].document.getElementById('menu_'+i).style.display = "none";
	}
	parent.frames['cabecalho'].document.getElementById('menu_'+num).style.display = "block"; 
	return false;
}

function popup(theURL,winName,width,height,features) { 
    var window_width = width;
    var window_height = height;
    var newfeatures = features;
	var window_top = 0;
	var window_left = 0;
    newWindow = window.open(''+ theURL + '',''+ winName + '','width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',resizable=no' + newfeatures + '');
    newWindow.focus();
}

function popup_centro(theURL,winName,width,height,features) { 
    var window_width = width;
    var window_height = height;
    var newfeatures = features;
    var window_top = (screen.height-window_height)/2;
    var window_left = (screen.width-window_width)/2;
    newWindow = window.open(''+ theURL + '',''+ winName + '','width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',resizable=no' + newfeatures + '');
    newWindow.focus();
}

function valida(formulario) {
	var validou = tmt_validateForm(formulario);
	if (validou) { formulario.submit(); }
}


function coord(lat, lng) {
	var retorna = "";
	for(var i=0; i<2; i++){
		if (i==0){
			var valor = lat; var str1 = "S"; var str2 = "N";
		} else {
			retorna += ", ";
			var valor = lng; var str1 = "W"; var str2 = "E";
		}
		var grau = Math.abs(parseInt(valor));
		var mi   = parseInt((Math.abs(valor)-grau)*60);
		var seg  = arredonda(((((Math.abs(valor)-grau)*60)-mi)*60), 2);
		if (valor < 0){
			var comp = str1;
		} else {
			var comp = str2;
		}
		retorna += grau + "&ordm; " + mi + "' " + seg + "&quot; " + comp;
	}
	return(retorna);
}

function arredonda(num, casas) {
	if (num > 8191 && num < 10485) {
		num = num-5000;
		var resultado = Math.round(num*Math.pow(10,casas))/Math.pow(10,casas);
		resultado = resultado+5000;
	} else {
		var resultado = Math.round(num*Math.pow(10,casas))/Math.pow(10,casas);
	}
	return(resultado);
}


function validaBusca(form){
	var selecionados = 0;
	var validou = tmt_validateForm(form);
	
	for (var i=0; i < document.busca.derivadores.options.length; i++){
		if (document.busca.derivadores.options[i].selected){
			selecionados = 1;
		}
	}
	
	if (validou){
		if (selecionados == 0){
			if (dirImagens != "../"){ //portugues
				alert("Nenhum derivador foi selecionado.");	
			} else {
				alert("You didn't select any drifters.");	
			}
		} else {
			form.submit();	
		}
	}
}

function mostraAjuda(){
	if (document.getElementById('ajuda').style.display=='block'){
		document.getElementById('ajuda').style.display='none';
	} else {
		document.getElementById('ajuda').style.display='block';
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}