// Variaveis
var stb_campo		= null
var janela_activa 	= null
var stb_tag			= null
var stb_url			= null
var fundo_preto		= null
var imagem_album	= null

// Gestor de Eventos
document.addEventListener('mouseup', click_rato, true);

// Função que gere o click do rato
function click_rato(evento) {
	var opc_x
	var opc_y
	var opc_width
	var opc_height
	var rato_x
	var rato_y
	// Se a janela estiver aberta
	if (janela_activa != null)
		if (janela_activa.style.display == 'block'){
			// Obter coordenadas
			opc_x = parseFloat(janela_activa.style.left)
			opc_y = parseFloat(janela_activa.style.top)
			opc_width = janela_activa.offsetWidth
			opc_height = janela_activa.offsetHeight
			// Coordenadas do rato
			evento = evento || window.event;
			if (evento.pageX || evento.pageY){
				rato_x = evento.pageX;
				rato_y = evento.pageY;
			} else if (evento.clientX || evento.clientY) 	{
				rato_x = evento.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
				rato_y = evento.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}
			if (rato_x < opc_x || rato_x > opc_x + opc_width || rato_y < opc_y || rato_y > opc_y + opc_height)
				FecharJanela()
		}
}

// Abrir Janela de Texto
function STBJanela(id, campo, extra){
	// Fechar Janela Anterior
	FecharJanela()
	stb_campo = campo;
	// Carregar dados sobre a Janela
	var texto = null;
	switch(id){
	case 0:
		texto = 'Digite o texto que quer que fique em Negrito:';
		stb_tag = 'B';
		break;
	case 1:
		texto = 'Digite o texto que quer que fique em Itálico:';
		stb_tag = 'I';
		break;
	case 2:
		texto = 'Digite o texto que quer que fique Sublinhado:';
		stb_tag = 'U';
		break;
	case 3:
		texto = 'Digite o link da Imagem:';
		stb_tag = 'IMG';
		break;
	case 4:
		texto = 'Digite o Link da Página:';
		stb_tag = 'URL0';
		break;
	case 5:
		texto = 'Digite o Nome para o Link:';
		stb_tag = 'URL1';
		break;
	case 6:
		texto = 'Digite o texto que quer que fique justificado à esquerda:';
		stb_tag = 'E';
		break;
	case 7:
		texto = 'Digite o texto que quer que fique centrado:';
		stb_tag = 'C';
		break;
	case 8:
		texto = 'Digite o texto que quer que fique justificado à direita:';
		stb_tag = 'D';
		break;
	case 9:
		texto = 'Digite o texto que quer que fique Subescrito:';
		stb_tag = 'SUB';
		break;
	case 10:
		texto = 'Digite o texto que quer que fique Supescrito:';
		stb_tag = 'SUP';
		break;
	case 11:
		texto = 'Digite o texto que quer que fique Riscado:';
		stb_tag = 'STRIKE';
		break;
	case 12:
		texto = 'Digite o texto que quer que fique Colorido:';
		stb_tag = 'COR=#' + extra;
		break;
	case 13:
		texto = 'Digite o texto que quer que fique Tamanho ' + extra + ':';
		stb_tag = extra;
		break;
	case 14:
		texto = 'Digite o Link do Youtube:';
		stb_tag = 'YT';
		break;
	case 15:
		texto = 'Digite a Citação:';
		stb_tag = 'QUOTE';
		break;
	case 16:
		texto = 'Digite o código:';
		stb_tag = 'CODE';
		break;
	}
	// Apresentar Janela
	janela_activa = document.getElementById('stb_tag');
	janela_activa.style.left	= getOffsetLeft(stb_campo) + 'px';
	janela_activa.style.top		= getOffsetTop(stb_campo) + 'px';
	janela_activa.style.display = 'block';
	// Apresentar Descrição
	document.getElementById('stb_descricao').innerHTML = texto;
	// Apresentar valor inicial
	document.getElementById('stb_texto').value = '';
	if (stb_tag == 'IMG' || stb_tag == 'URL0')
		document.getElementById('stb_texto').value = 'http://';
	document.getElementById('stb_texto').focus()
}

// Abrir Tabela
function STBTabela(janela,campo,botao){
	// Fechar Janela Anterior
	FecharJanela()
	stb_campo = campo
	botao = document.getElementById(botao)
	// Apresentar Janela
	janela_activa = document.getElementById(janela);
	janela_activa.style.left	= getOffsetLeft(botao) + 'px';
	janela_activa.style.top		= getOffsetTop(botao) + botao.offsetHeight + 'px';
	janela_activa.style.display = 'block';
}

// Aplicar Tag
function STBTag(){
	var texto = document.getElementById('stb_texto').value;
	// Estiver sido digitado texto
	if (texto != null && texto != ''){
		if (stb_tag == 'URL0') {
			stb_url = texto
			STBJanela(5, stb_campo, null)
		} else {
			if (stb_tag == 'URL1')
				stb_campo.value += '[URL=' + stb_url + ']' + texto + '[/URL]'
			else if (stb_tag.substr(0,3) == 'COR')
				stb_campo.value +='[' + stb_tag + ']' + texto + '[/COR]'
			else
				stb_campo.value +='[' + stb_tag + ']' + texto + '[/' + stb_tag + ']'
			// Fechar Janela
			FecharJanela()
		}
	} else
		// Fechar Janela
		FecharJanela()
}

// Abrir Janela
function abrir_janela(botao, janela){
	// Fechar Janela Anterior
	FecharJanela()
	// Abrir caixa desejada
	var origem_activa = document.getElementById(botao)
	janela_activa = document.getElementById(janela);
	janela_activa.style.left = getOffsetLeft(origem_activa) + 'px';
	janela_activa.style.top =  (getOffsetTop(origem_activa) + origem_activa.offsetHeight) + 'px';
	janela_activa.style.display ='block';
}

// Adicionar Emotions
function STBEmotion(emote, stb_campo){
	stb_campo.value += emote
}

// Fechar Janela de Texto
function FecharJanela(){
	if (janela_activa != null) 
		janela_activa.style.display = 'none';
    if (fundo_preto != null)
    	fundo_preto.style.display = 'none';
	document.getElementsByTagName("html")[0].style.overflow = 'auto'; 
    if (imagem_album != null){
    	imagem_album.parentNode.removeChild(imagem_album);
        imagem_album = null;
    }
}


// Obtensão do valor absoluto (Esquerdo)
function getOffsetLeft(elemento){
	var mOffsetLeft = elemento.offsetLeft;
	var mOffsetParent = elemento.offsetParent;
	while(mOffsetParent){
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}

// Obtensão do valor absoluto (Topo)
function getOffsetTop(elemento){
	var mOffsetTop = elemento.offsetTop;
	var mOffsetParent = elemento.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

window.size = function(){
	var w = 0;
	var h = 0;
	//IE
	if(!window.innerWidth){
		//strict mode
		if(!(document.documentElement.clientWidth == 0)){
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		//quirks mode
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	//w3c
	} else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function(){
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;
	//IE
	if(!window.pageYOffset){
		//strict mode
		if(!(document.documentElement.scrollTop == 0)){
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		//quirks mode
		} else {
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	//w3c
	} else {
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}
	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;
	return{x:_x,y:_y};
}

// Abrir Janela de Apagar
function abrir_apagar(botao, texto, href){
	// Fechar Janela Anterior
	FecharJanela()
	// Abrir caixa desejada
	janela_activa = document.getElementById('janela_apagar');
	var coords = window.center({width:janela_activa.offsetWidth,height:janela_activa.offsetHeight})
	janela_activa.style.left = (coords.x - 300) + 'px';
	janela_activa.style.top = (coords.y - 75) + 'px';
	janela_activa.style.display ='block';
	// Alterar textro
	document.getElementById('apagar_descricao').innerHTML = texto;
	// Mudar Link
	document.getElementById('apagar_sim').href = href;
    // Criar Fundo Preto
    if (fundo_preto == null){
        fundo_preto = document.createElement("div");
        fundo_preto.id = "capa";
        document.getElementsByTagName("body")[0].appendChild(fundo_preto);
    }
    document.getElementsByTagName("html")[0].style.overflow = 'hidden';
    fundo_preto.style.width = document.body.scrollWidth + "px";
	fundo_preto.style.height = (document.body.scrollHeight + 600) + "px";
	fundo_preto.style.display = 'block';
}

// Novo Captcha
function novo_capcha() {
	document.getElementById('img_captcha').src='captcha.asp?'+Date();    
}

// Foto do Album
function foto_do_album(imagem_src){
	// Fechar Janela Anterior
	FecharJanela()
    // Criar Fundo Preto
    if (fundo_preto == null){
        fundo_preto = document.createElement("div");
        fundo_preto.id = "capa";
        document.getElementsByTagName("body")[0].appendChild(fundo_preto);
    }
    document.getElementsByTagName("html")[0].style.overflow = 'hidden';
    fundo_preto.style.width = document.body.scrollWidth + "px";
	fundo_preto.style.height = (document.body.scrollHeight + 600) + "px";
	fundo_preto.style.display = 'block';
    // Criar Imagem
	imagem_album = new Image();
	imagem_album.src = imagem_src;
    if (imagem_album.width > imagem_album.height){
    	var imgwidth = 640;
        var imgheight = Math.round((640/imagem_album.width) * imagem_album.height);
    } else {
    	var imgwidth = Math.round((480/imagem_album.height) * imagem_album.width);
        var imgheight = 480;
    }
	var coords = window.center({width:imgwidth,height:imgheight})
	imagem_album.style.left = (coords.x) + 'px';
	imagem_album.style.top = (coords.y) + 'px';
    imagem_album.style.width = imgwidth + 'px'
    imagem_album.style.height = imgheight + 'px'
	imagem_album.style.position = 'absolute';
    imagem_album.style.padding = '2px';
    imagem_album.style.border = '1px #ccc solid';
    imagem_album.style.display = 'block';
    imagem_album.style.background = '#eee';
    document.getElementsByTagName("body")[0].appendChild(imagem_album);
    janela_activa = imagem_album;
}
