/*
#############################################################################
#Script para execução dos comandos do HTEditor.
#Contendo funções para iniciar e escrever na tela o HTEditor.
#
#Versão: 1.0
#Criado em: (16/06/2009) - (18/06/2009) - (19/06/2009) - (28/06/2009)
# - (02/09/2009)
#Modificado em: ()
#Desenvolvido pela: Head Trust
#############################################################################
*/

//Declarando as variaveis
var edtr, area;
var palette = 0;
var imageShow = 0;
var showSelectedColor = 'edtr-palette-selected-color';
var showHexaCodeColor = 'edtr-insert-color';
var edtrHtmlExpands = 'edtr-expands';
var edtrHtmlArea = 'edtr-html-panel-area';
var edtrImgPlt = 'edtr-palette';
var edtrOpacity, edtrExpandsDimensions, edtrDInterval, edtrTInterval;
var edtrExecExpands = 0;


/*
	Principais funções de execução
*/
if(typeof $ != 'function') {
	function $(id){
		return document.getElementById(id);
	}
}

//Retorna o content da janela de edição
function cw(){
	return $(area).contentWindow.document;
}

//Inicializa o Editor
function Initialize(id){
	area = 'edtr-' + id;
	edtr = cw();
	edtr.designMode = "On";
}

/*
	Funções GET e SET
*/

function getArea(){
	return area;
}

function setArea(newValue){
	area = newValue;
}

function getAreaValue(){
	return edtr.body.innerHTML;
}

function setAreaValue(valueArea){
	edtr.write(valueArea);
}

/*
	Função para abrir nova janela
*/

//Abre uma nova janela e faz a chamada do link

function blankSource(url, name){
	window.open(url, name);
}

/*
	Funções para submeter formulário
*/

//Função para a insersão de cores manualmente
function insertColor(event, obj){
	var keypress = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	//(window.Event) ? event.which : event.keyCode;
	if (keypress == 13){
		checkColor(obj)
		return false;
	}
}

function checkColor(obj){
	var insert = obj.value.toUpperCase();
	var allowed = '#0123456789ABCDEF';
	var validate = true;
	if(insert.length == 7){
		for(i = 0; i < insert.length; i++){
			if(allowed.indexOf(insert.charAt(i)) == -1){
				validate = false;
			}
		}
		if((validate == false) || (insert.indexOf('#') == -1)){
			alert("Valor hexadecimal incorreto!");
		} else {
			color(insert);
		}
	} else {
		alert("O valor Hexadecimal deve conter ao menos 6 caracteres precedido de '#'");
	}
}

/*
	Funções de movimento
*/

//Mostra e esconde a paleta de cores
function showPalette(id){
	if(palette == 1){
		$(id).style.display = 'none';
		palette = 0;
	} else {
		$(id).style.display = 'block';
		palette = 1;
	}
}

function showImgTab(id){
	if(imageShow == 1){
		$(id).style.display = 'none';
		imageShow = 0;
	} else {
		$(id).style.display = 'block';
		imageShow = 1;
	}
}

function htmlPanel(){
	if(edtrExecExpands == ''){
		edtrExecExpands = 'grow';
		edtrOpacity = 0;
		edtrExpandsDimensions = 0;
		$(edtrHtmlArea).value = edtr.body.innerHTML;
		var objPanel = $(edtrHtmlExpands);
		objPanel.style.opacity = (edtrOpacity / 100);
		objPanel.style.filter = 'alpha(opacity=' + (edtrOpacity * 100) + ')';
		objPanel.style.display = 'block';
		edtrTInterval = window.setInterval(transparency, 60, 'up');
		edtrDInterval = window.setInterval(resizePanel, 40, 'up');
	}
}

function closeHtmlPanel(){
	if(edtrExecExpands == ''){
		edtrExecExpands = 'shrink';
		edtrOpacity = 80;
		edtrExpandsDimensions = 100;
		var objPanel = $(edtrHtmlExpands);
		edtrDInterval = window.setInterval(resizePanel, 40);
		edtrTInterval = window.setInterval(transparency, 60);
	}
}

function sendAndCloseHtmlPanel(){
	edtr.write($(edtrHtmlArea).value);
	closeHtmlPanel();
}

function transparency() {
	if(edtrExecExpands == 'shrink'){
		$(edtrHtmlExpands).style.opacity = (--edtrOpacity / 100);
		$(edtrHtmlExpands).style.filter = 'alpha(opacity=' + (edtrOpacity * 100) + ')';
	} else {
		$(edtrHtmlExpands).style.opacity = (++edtrOpacity / 100);
		$(edtrHtmlExpands).style.filter = 'alpha(opacity=' + (edtrOpacity * 100) + ')';
	}

	if((edtrOpacity == 0) || (edtrOpacity == 80)){
		clearInterval(edtrTInterval);
		if(edtrExecExpands  == 'shrink'){
			$(edtrHtmlExpands).style.display = 'none';
		}
		edtrExecExpands = '';
	}
}

function resizePanel(direction){
	if(edtrExecExpands == 'shrink'){
		$(edtrHtmlExpands).style.width = (--edtrExpandsDimensions) + '%';
		$(edtrHtmlExpands).style.height = (edtrExpandsDimensions) + '%';
	} else {
		$(edtrHtmlExpands).style.width = (++edtrExpandsDimensions) + '%';
		$(edtrHtmlExpands).style.height = (edtrExpandsDimensions) + '%';
	}

	if((edtrExpandsDimensions == 0) || (edtrExpandsDimensions == 100)){
		clearInterval(edtrDInterval);
	}
}

//Mostra a cor a ser selecionada e seu código hexadecimal
function switchColor(color){
	$(showSelectedColor).style.background = color;
	$(showHexaCodeColor).value = color;
}

/*
	Funções para a execução do comando de modificação do texto
*/

function bold(){
	edtr.execCommand('bold', false, null);
}

function italic(){
	edtr.execCommand('italic', false, null);
}

function underline(){
	edtr.execCommand('underline', false, null);
}

function left(){
	edtr.execCommand('justifyleft', false, null);
}

function center(){
	edtr.execCommand('justifycenter', false, null);
}

function right(){
	edtr.execCommand('justifyright', false, null);
}

function justify(){
	edtr.execCommand('justifyfull', false, null);
}

function cut(){
	edtr.execCommand('cut', false, null);
}

function copy(){
	edtr.execCommand('copy', false, null);
}

function pasta(){
	edtr.execCommand('pasta', false, null);
}

function markers(){
	edtr.execCommand('insertunorderedlist', false, null);
}

function numberMarkers(){
	edtr.execCommand('insertorderedlist', false, null);
}

function fontName(value){
	edtr.execCommand('fontname', false, value);
}

function fontSize(value){
	edtr.execCommand('fontsize', false, value);
}

function undo(){
	edtr.execCommand('undo', false, null);
}

function redo(){
	edtr.execCommand('redo', false, null);
}

function color(color){
	edtr.execCommand('forecolor', false, color);
	showPalette(edtrImgPlt);
}

function link(){
	if (document.all) {
		var selection = $(area).document.selection; 
		if (selection != null) {
			rng = selection.createRange();
		}
	} else {
		var selection = $(area).contentWindow.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}

	var url = prompt('Entre com a URL', '');
	if((url != '') && (url)){
		edtr.execCommand('createlink', false, url);
	}
}

function unlink(){
	edtr.execCommand('unlink', false, null);
}

function image(){
	if (document.all) {
		var selection = $(area).document.selection;
		if (selection.type == 'None') {
			var url = prompt('Entre com a URL da imagem', '');
			rng = selection.createRange().text = "<img src=\"" + url + "\" />";
			return true;
		}
	} else {
		var selection = $(area).contentWindow.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}

	var url = prompt('Entre com a URL da imagem', '');
	if((url != '') && (url)){
		edtr.execCommand('InsertImage', false, url);
	}
}
