var itemOrig;
var rng;

function despMenu(nombre, object, sn){
	var obj;
	var obj2;

	if (document.all) {
		obj = document.all(nombre);
		obj2 = document.all(object);
	} else if (document.getElementById) {
		obj = document.getElementById(nombre);
		obj2 = document.getElementById(object);
	} else if (document.layers) {
		obj = document.layers[nombre];
		obj2 = document.layers[object];
	}
	
	if (sn == 1){
		setRange('ptext');
		
		obj.style.left = BuscarPosX(obj2) + 'px';
		obj.style.top = (BuscarPosY(obj2) + 22).toString() + 'px';
	}
	
	if (obj.style.visibility == "hidden" || obj.style.visibility == ""){
		obj.style.visibility = "visible";
	}
	else{
		obj.style.visibility = "hidden";
	}
}

function BuscarPosX(obj)
{
	var actizq = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			actizq += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		actizq += obj.x;
	return actizq;
}

function BuscarPosY(obj)
{
	var actsup = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			actsup += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		actsup += obj.y;
	return actsup;
}

function destacar(obj, val){
	if (val==1){
		itemOrig = obj.style.backgroundColor;
		obj.style.backgroundColor = "#C5DAF3";
	}
	else{
		obj.style.backgroundColor = itemOrig;
	}
}

/*
   Texto Enriquecido ESPHP version 1.0

   Basado en:
      Cross-Browser Rich Text Editor
      http://www.kevinroth.com/rte/demo.htm
      Written by Kevin Roth (kevin@NOSPAMkevinroth.com - remove NOSPAM)

   Modificado por: Jose Ignacio Rodriguez Burgos
   Correo: jirburgos@hotmail.com
   :D  Si se lo van a tacuachear ponganme algun credito
*/

// Inicializando las variables
var esEditable = false;
var esIE;
var esGecko;
var esSafari;
var esKonqueror;
var esOpera;
var esFirefox;
var viewMode = 1;

function IniciarEditor(){
	// Revisando el tipo de navegador que esta utilizando
	var navegador = navigator.userAgent.toLowerCase();
	esIE = ((navegador.indexOf( "msie" ) != -1) && (navegador.indexOf( "opera" ) == -1) && (navegador.indexOf( "webtv" ) == -1));
	esGecko = (navegador.indexOf( "gecko" ) != -1);
	esSafari = (navegador.indexOf( "safari" ) != -1);
	esKonqueror = (navegador.indexOf( "konqueror" ) != -1);
	esOpera = (navegador.indexOf( "opera" ) != -1);

	// Habilitando el texto enriquecido si el navegador no es Safari o Konqueror
	if(document.getElementById && document.designMode && !esSafari && !esKonqueror) {
		esEditable = true;
	}	
}

function MostrarEditor(editor, html, width, height) {
	if(esEditable){
		document.writeln('<div name="divtools" id="divtools" class="HerramEditor">'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'bold\', \'\')" class="editorboton"><img src="imagenes/iconos/bold.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'italic\', \'\')" class="editorboton"><img src="imagenes/iconos/cursiva.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'underline\', \'\')" class="editorboton"><img src="imagenes/iconos/underline.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'justifyleft\', \'\')" class="editorboton"><img src="imagenes/iconos/left.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'justifycenter\', \'\')" class="editorboton"><img src="imagenes/iconos/center.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'justifyright\', \'\')" class="editorboton"><img src="imagenes/iconos/right.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'insertorderedlist\', \'\')" class="editorboton"><img src="imagenes/iconos/ol.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'insertunorderedlist\', \'\')" class="editorboton"><img src="imagenes/iconos/ul.gif" border="0"></a>'
			+ '<a id="coloresb" name="coloresb" href="javascript:despMenu(\'fontcolores\', \'coloresb\', 1)" class="editorboton"><img src="imagenes/iconos/color.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'createlink\', \'2\')" class="editorboton"><img src="imagenes/iconos/link.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'insertimage\', \'3\')" class="editorboton"><img src="imagenes/iconos/img.gif" border="0"></a>'
			+ '<a id="emoiconb" name="emoiconb" href="javascript:despMenu(\'emoicon\', \'emoiconb\', 1)" class="editorboton"><img src="imagenes/iconos/emoicons.gif" border="0"></a>'
			+ '<a href="javascript:editorCommand(\'' + editor + '\', \'insertParagraph\', \'4\')" class="editorboton" style=\"width: auto;\"><img src="imagenes/iconos/youtube.gif" border="0"></a>'
			+ '<select id="formatblock_' + editor + '" onchange="selectFont(\'' + editor + '\', this.id);">'
			+ '<option value="">-- Estilo --</option>'
			+ '<option value="<h1>">Encabezado 1 &lt;h1&gt;</option>'
			+ '<option value="<h2>">Encabezado 2 &lt;h2&gt;</option>'
			+ '<option value="<h3>">Encabezado 3 &lt;h3&gt;</option>'
			+ '<option value="<h4>">Encabezado 4 &lt;h4&gt;</option>'
			+ '</select>'
			+ '</div>');
		document.writeln('<iframe id="' + editor + '" name="' + editor + '" width="' + width + '" height="' + height + 'px" frameborder="0" style=\"border: 2px inset #8CBAFF;\"></iframe>');
		document.writeln('<input type="hidden" id="hidden' + editor + '" name="hidden' + editor + '" value="">');
		document.getElementById('hidden' + editor).value = html;
		habilitarDiseno(editor, html);
		// En el div de abajo debe ir el texto del checkbox para cambiar a ver codigo
		document.writeln('<div style=\"visibility: hidden; display: none;\"><input name="ckbhtml" id="ckbhtml" type="checkbox" onclick="javascript:CambiarVista(\'' + editor + '\')" />Ver C&oacute;digo</div>');
	}
	else{
		document.writeln('<textarea name="hidden' + editor + '" id="' + editor + '" cols="39" rows="10">' + html + '</textarea>');
	}
}

function habilitarDiseno(editor, html) {
	var mainContent= "<html id=" + editor + "><head></head><body>" + html + "</body></html>" ;
	if(document.all){
		var edit = frames[editor].document;
		edit.open();
		edit.write(mainContent);
		edit.close();
		edit.designMode = "On" ;
	}
	else{
		var edit = document.getElementById(editor).contentWindow.document;
		edit.write(mainContent);
		edit.designMode =  "On" ;
		document.getElementById(editor).contentDocument.designMode = "on" ;
	}
}

function editorCommand(editor, command, option) {
	var mainField;
	var isIcon = false;
	mainField = document.getElementById(editor).contentWindow;
	try {
		if(option=='2'){
			option = prompt('Url', 'http://');
		}
		else if(option=='3'){
			option = prompt('Url de la Imagen', 'http://');
		}
		else if(option=='4'){
			option = prompt('URL Video YouTube', '');
			if(option.length>0){
				option = '[youtube]' + option + '[/youtube]';
			}
		}
		else if(command == 'insertimage'){
			isIcon = true;
		}
		
		if (command=='forecolor'){
			if (document.all) {
				rng.select();
			}
		}

		mainField.focus();
		//alert(option);
		mainField.document.execCommand(command, false, option);
		mainField.focus();
		
		if (command=='forecolor') despMenu('fontcolores', 'coloresb', 1);
		if (isIcon) despMenu('emoicon', 'emoiconb', 1);
	} catch (e) { }
}

function selectFont(editor, selectname) {
	var idx = document.getElementById(selectname).selectedIndex;
	if (idx != 0) {
		var selected = document.getElementById(selectname).options[idx].value;
		var cmd = selectname.replace('_' + editor, '');
		editorCommand(editor, cmd, selected);
		document.getElementById(selectname).selectedIndex = 0;
	}
}

function CambiarVista(editor){
	var mainField;
	var obj;
	obj = document.getElementById('divtools');

	if(document.getElementById("ckbhtml").checked == 1){
		obj.style.visibility = 'hidden';

		var iHTML;
		if(document.all) {
			iHTML = frames[editor].document.body.innerHTML;
			frames[editor].document.body.innerText = iHTML;
		}
		else{
			mainField = document.getElementById(editor).contentWindow;
			iHTML = document.createTextNode(mainField.document.body.innerHTML);
			mainField.document.body.innerHTML = "";
			mainField.document.body.appendChild(iHTML);
		}
	}
	else{
		obj.style.visibility = 'visible';

		var iText;
		if(document.all) {
			iText = frames[editor].document.body.innerText;
			frames[editor].document.body.innerHTML = iText;
		}
		else{
			mainField = document.getElementById(editor).contentWindow;
			iText = mainField.document.body.ownerDocument.createRange();
			iText.selectNodeContents(mainField.document.body);
			mainField.document.body.innerHTML = iText.toString();
		}
	}
}

function setRange(editor) {
	//function to store range of current selection
	var oRTE;
	if (document.all) {
		oRTE = frames[editor];
		var selection = oRTE.document.selection; 
		if (selection != null) rng = selection.createRange();
	} else {
		oRTE = document.getElementById(editor).contentWindow;
		var selection = oRTE.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
	return rng;
}

function stripHTML(oldString) {
	//function to strip all html
	var newString = oldString.replace(/(<([^>]+)>)/ig,"");
	
	//replace carriage returns and line feeds
	newString = newString.replace(/\r\n/g," ");
	newString = newString.replace(/\n/g," ");
	newString = newString.replace(/\r/g," ");
	
	//trim string
	newString = trim(newString);
	
	return newString;
}

function actualizarhtml(){
	if (esEditable){
		if (document.all) {
			if (frames['ptext'].document.designMode != "On") readOnly = true;
		} else {
			if (document.getElementById('ptext').contentDocument.designMode != "on") readOnly = true;
		}

		if (document.getElementById('ckbhtml').checked) document.getElementById('ckbhtml').click();
		var oHdnField = document.getElementById('hiddenptext');
		
		//convert html output to xhtml (thanks Timothy Bell and Vyacheslav Smolin!)
		if (oHdnField.value == null) oHdnField.value = "";
		var generateXHTML = false;

		if (document.all) {
			if (generateXHTML) {
				oHdnField.value = get_xhtml(frames['ptext'].document.body, lang, encoding);
			} else {
				oHdnField.value = frames['ptext'].document.body.innerHTML;
			}
		} else {
			if (generateXHTML) {
				oHdnField.value = get_xhtml(document.getElementById('ptext').contentWindow.document.body, lang, encoding);
			} else {
				oHdnField.value = document.getElementById('ptext').contentWindow.document.body.innerHTML;
			}
		}
	}
	return true;
}

//Script para cambiar de categoria
function cambiarCat(dir){
	document.location.href = dir+document.getElementById('catcamb').value;
}