var caminhoImgGrande = "";

function iniciaFuncoes(cig) {
	document.onmousedown = iniciamovimento;
	document.onmouseup = terminamovimento;
	document.onmousemove = move;
	caminhoImgGrande = cig;
}

function busca(onde, oque) {
	oque = oque.substring(1);
	var i;
	for (i = 0; i < onde.length; i++) {
		if (onde[i] == oque) {
			return i;
		}
	}
	
	return -1;
}

// ===============================================================

function abreWire() {
	if (avalia(wire)) {
		var nome = processaNome(document.getElementById("imgGrande"));
		window.open("pags_3d/3dwire" + nome + ".htm", "wire", "width=790,height=530,top=0,left=0,scrollbars=1,toolbar=0,resizable=1");
	}
}

function abreHigh() {
	if (avalia(high)) {
		var nome = processaNome(document.getElementById("imgGrande"));
		window.open("pags_3d/3dhigh" + nome + ".htm", "wire", "width=790,height=530,top=0,left=0,scrollbars=1,toolbar=0,resizable=1");
	}
}

function mostraWire() {
	if (avalia(wire)) {
		event.srcElement.style.cursor="hand";
	}
	else {
		event.srcElement.style.cursor="default";
	}
}

function mostraHigh() {
	if (avalia(high)) {
		event.srcElement.style.cursor="hand";
	}
	else {
		event.srcElement.style.cursor="default";
	}
}

function avalia(qual) {
	var img = document.getElementById("imgGrande");

	if (img.style.display == "block") {
		var nome = processaNome(img);
		
		var i = busca(qual,nome);
		
		if (i > -1) {
			return true;
		}
	}
	
	return false;
}

// ===============================================================

function mostraSobre() {
	var img = document.getElementById("imgGrande");

	if (img.style.display == "block") {
		var sobre = document.getElementById("sobre");
		
		var nome = processaNome(img);
		if (document.getElementById("sobre" + nome) != null) {
			sobre.innerHTML = document.getElementById("sobre" + nome).innerHTML;
		}
		else {
			sobre.innerHTML = img.alt;
		}
		
		sobre.style.display = "block";	
		event.srcElement.style.cursor="hand";
	}
	else {
		event.srcElement.style.cursor = "default";
	}
}

function fechaSobre() {
	document.getElementById("sobre").style.display = "none";
}

// =========================================================

function abreImagem(qual) {
	var img = processaNome(qual);
	document.getElementById("imgGrande").style.display="block";
	document.getElementById("imgGrande").src = caminhoImgGrande + "i" + img + ".jpg";
	document.getElementById("imgGrande").alt = qual.alt;
	fechaSobre();
}

function fechaImagem() {
	var x = document.getElementById("imgGrande").width-18,
	    y = 16;
		
	if (event.offsetX >= x && event.offsetY <= y) {
		document.getElementById("imgGrande").style.display="none";
		document.getElementById("imgGrande").src = null;
		document.getElementById("imgGrande").alt = "";
	}
	fechaSobre();
}

function verificaFechar() {
	var x = document.getElementById("imgGrande").width-18,
	    y = 16;
		
	if (event.offsetX >= x && event.offsetY <= y) {
		document.getElementById("imgGrande").style.cursor="hand";
	}
	else {
		document.getElementById("imgGrande").style.cursor="default";
	}
}

function processaNome(img) {
	return img.src.substring(img.src.lastIndexOf("/")+2,img.src.length-4); // tira o b do começo do arquivo
}

// =========================================================================

var ativo = false;
var movobj, mx, my, pxleft, pxtop;

function iniciamovimento(){
	if (event.srcElement.id=="imgGrande") {
		ativo = true;
		movobj = event.srcElement;

		if (movobj.style.pixelLeft <= 0) movobj.style.pixelLeft="22";
		if (movobj.style.pixelTop <= 0) movobj.style.pixelTop="70";

		pxleft = movobj.style.pixelLeft;
		pxtop = movobj.style.pixelTop;
		mx = event.clientX;
		my = event.clientY;
	}
}

function move(){
	if (event.button==1 && ativo) {
		movobj.style.pixelLeft = pxleft + event.clientX - mx;
		movobj.style.pixelTop = pxtop + event.clientY - my;
		return false;
	}
}

function terminamovimento() {
	ativo = false;
}
