//ROTINAS PARA TRANSIÇÃO DE IMAGENS
var transicoes_imagens = new Array(), num_transicoes_imagens = 0;

function trocar_imagem_agora(imagem_atual) {
	transicoes_imagens[imagem_atual].avancar_imagem();
}
function criar_intervalos() {
	for (i = 0; i < num_transicoes_imagens; i++) {
		transicoes_imagens[i].img = document.getElementById(transicoes_imagens[i].nome_img);
  	setTimeout('setInterval("trocar_imagem_agora('+i+')", 5000);', 500 * i);
	}
}
function transicao_imagem(nome_img, qtd_img, larg_num) {
	this.nome_img = nome_img;
	this.quantidade = qtd_img;
	this.largura_numero = larg_num;
	this.img_atual = 1;
	//função que troca imagem
	this.avancar_imagem = function() {
		if (this.img) {
  		strnum_antigo = justificar_numero(this.img_atual, this.largura_numero);
	  	this.img_atual++;
		  if (this.img_atual > this.quantidade) this.img_atual = 1;
  		strnum_novo = justificar_numero(this.img_atual, this.largura_numero);
	  	//substituir o antigo pelo novo
		  novo_nome = this.img.src.replace(new RegExp(strnum_antigo), strnum_novo);
  		this.img.src = novo_nome;
		}
	}
	//this.intervalo = setInterval('trocar_imagem_agora('+num_transicoes_imagens+')', 3000);
}

function nova_transicao(nome_img, qtd_img, larg_num) {
	transicoes_imagens[num_transicoes_imagens] = new transicao_imagem(nome_img, qtd_img, larg_num);
	num_transicoes_imagens++;
}


//rotinas auxiliares
function justificar_numero(jnumero, jtamanho) {
	numerostr = String(jnumero);
	while (numerostr.length < jtamanho) numerostr = '0' + numerostr;
	return numerostr;
}

function getLeft(obj) {
	var res = obj.offsetLeft;
	pai = obj;
	while (pai = pai.offsetParent) {
		res += pai.offsetLeft;
	}
	return res;
}
function getTop(obj) {
	var res = obj.offsetTop;
	pai = obj;
	while (pai = pai.offsetParent) {
		res += pai.offsetTop;
	}
	return res;
}

function escapeHTML(str)
{
	return str;
  var div = document.createElement('div');
  var text = document.createTextNode(str);
  div.appendChild(text);
  return div.innerHTML;
}

function inicia_ajax() {
  var ajax = null;
  try {
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(ee) {
    try {
       ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(eee) {
      try {
		    ajax = new XMLHttpRequest();
      } catch(eeee) {
        ajax = false;
      }
    }
  }
  return ajax;
}

function addEvent(obj, evType, fn) {
  if (obj.addEventListener){ 
    obj.addEventListener(evType, fn, false); 
    return true; 
  } else if (obj.attachEvent){ 
    var r = obj.attachEvent("on"+evType, fn); 
    return r; 
  } else { 
    return false; 
  } 
}

//variáveis
pasta = 'over/';
imgs = new Array();
//localiza o texto da imagem over ou normal
function get_src(nome, over) {
	inicio_texto = nome.substring(0, nome.indexOf('imagens/') + 8);
	fim_texto = nome.substring(nome.lastIndexOf('/') + 1, nome.length);
	if (over) nome = inicio_texto + pasta + fim_texto;
	else nome = inicio_texto + fim_texto;
	return nome;
}

//altera as imagens normal/over
function mudar_imagem(img, over) {
	img.src = get_src(img.src, over);
}

//carrega as imagens de rollover antecipadamente
function carregar_imagens() {
	for (i = 0; i < document.images.length; i++) {
		img_item = document.images[i]; objmenu = false; alvo = img_item;
		while ((alvo = alvo.offsetParent) && (!objmenu)) if (alvo.id == 'tabela_menu') objmenu = true;
		if (objmenu) {
			//verifica link superior
			ligacao = img_item.parentNode;
			if (ligacao && ligacao.title) img_item.alt = ligacao.title;
			else continue;
			//carrega imagens
			imgs[i] = new Image();
			imgs[i].src = get_src(img_item.src, true);
			//prepara eventos
			img_item.onmouseover = function(evt) {
				evt = evt?evt:event;
				alvo = evt.target?evt.target:evt.srcElement;
				if (typeof(alvo) != 'undefined' && typeof(mudar_imagem) != 'undefined') mudar_imagem(alvo, true);
				if (alvo.alt) window.status = alvo.alt;
				return true;
			}
			img_item.onmouseout = function(evt) {
				evt = evt?evt:event;
				alvo = evt.target?evt.target:evt.srcElement;
				if (typeof(alvo) != 'undefined' && typeof(mudar_imagem) != 'undefined') mudar_imagem(alvo, false);
				window.status = '';
				//return true;
			}
		}
	}
}

// ------ ROTINAS PARA MUDAR IMAGEM DA "GALERIA" NA TELA -------
function alterar_galeria(cat, numero) {
	if (img = document.getElementById('imagem_' + cat)) {
		numero = String(numero);
		if (numero.length == 1) numero = '0' + numero;
		img.src = cat + '/' + numero + '.jpg';
	}
}


// ROTINAS PARA EVITAR A SELEÇÃO DE TEXTO
var pode = false;

function habilita_selecao() {
	pode = true;
	selecao_de_texto();
}

function selecao_de_texto() {
	if (!pode) {
		document.body.oncontextmenu = function() { return false;	};
		document.body.ondragstart = function() {	return false;	};
		document.body.onselectstart = function() {	return false;	};
	} else {
		document.body.oncontextmenu = null;
		document.body.ondragstart = null;
		document.body.onselectstart = null;
	}
}

//ROTINA PARA FORÇAR ATUALIZAÇÃO DE IMAGEM
function atualizar_imagem(img) {
	if (img) img.onclick = function() {
		temp = img.src.indexOf('?');
		if (temp > 0) novo_src = img.src.substr(0, temp);
		else novo_src = img.src;
		img.src = novo_src + '?' + Math.ceil(Math.random() * 100000);
	}
}

// ------ ROTINAS PARA FRAMES -------
function frame_root() {
	obj = window;
	cont = 0;
	while (typeof(window.parent) != 'undefined' && cont < 10) {
		obj = window.parent;
		cont++;
	}
	return obj;
}

function atualiza_titulo() {
	if (obj = frame_root()) {
		//pega título da página
		titulo = document.title;
		//verifica ::: e retira o que
		tp = titulo.lastIndexOf(':::');
		if (tp > 0) {
			titulo = titulo.substring(0, tp);
		}
		obj.document.title = titulo;
	}
}
//verifica se existe um frame pai
function verifica_frame() {
	//se a URL do frame pai não é diferente da URL frame atual, então redireciona para a raiz
	if (obj = frame_root()) {
		if (String(document.location) == String(obj.document.location)) {
			pagina = encodeURI(document.location);
			obj.location = '/?frameurl=' + pagina;
		}
	}
}

function dimensiona_iframe() {
	//evento para redimensionar
	dimensionamento = function() {
		//redimensionamento de iframe
		FFVersion = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
		FFextraHeight = (parseFloat(FFVersion) >= 0.1) ? 40 : 0;

		//ponteiros para frame, pai e tabela
		var iframe = window.parent.document.getElementById('FConteudo');
		var tpai = window.parent.document.getElementById('TPai');
		var tabela = document.getElementById('TConteudo');		
		//tira o 100% que atrapalha
		var tabela100 = window.parent.document.getElementById('TN100');
		var tabela1002 = window.parent.document.getElementById('TN1002');
		tabela100.style.height = '1px';
		tabela1002.style.height = '1px';

		//realiza alteração de altura
		if (iframe && !window.opera) {
			//iframe.style.display="block"
			if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) { //ns6 syntax
				iframe.height = iframe.contentDocument.body.offsetHeight + FFextraHeight; 
			} else if (iframe.Document && iframe.Document.body.scrollHeight) {//ie5+ syntax
				iframe.height = iframe.Document.body.scrollHeight;
			}
			//volta o 100%
			volta100 = function() {
				var tabela100 = window.parent.document.getElementById('TN100');
				var tabela1002 = window.parent.document.getElementById('TN1002');
				tabela100.style.height = '100%';
				tabela1002.style.height = '100%';
			}
			setTimeout(volta100, 250);
		}
	}
	
	//inicia evento
	addEvent(window, 'load', dimensionamento);
}


//rotina que inicia o que é necessário para as páginas logo após a tag <body>
function inicio_pagina(frame) {
	if (frame) {
		verifica_frame();
		//dimensiona_iframe();
		atualiza_titulo();
	} else {
	}
	selecao_de_texto();
}

//addEvent(window, 'load', selecao_de_texto);
addEvent(window, 'load', carregar_imagens);
addEvent(window, 'load', criar_intervalos);
window.defaultStatus = "Ateliê Marisa Almeida";