﻿var DEBUG = true;
var anim_open_page, anim_frame;
var secondaryFrameHeight, secondaryFrame_isClose = null;
var actualBodyHeight;
var BodyLoaded = 0;

var msg0 = 'Introduceti in campul de mai jos textul fara diacritice si cu o simpla apasare de buton transformati-l in text cu diacritice.';
var msg1 = 'Cuvintele marcate cu ro&#351;u sunt ambigue. Ap&#259;sa&#355;i (da&#355;i click) pe ele pentru  a le corecta.';
var msg2 = 'Salva&#355;i textul transformat &icirc;n format <input type="radio" name="rtt" id="rtt_text" value="text" checked onclick="changeTextFormat();"> <label for="rtt_text" onclick="changeTextFormat();">text</label> sau <input type="radio" name="rtt" id="rtt_html" value="html" onclick="changeTextFormat();"> <label for="rtt_html" onclick="changeTextFormat();">html</label>.';
var msgWait = 'A&#351;tepta&#355;i...';

var text_tEditor, text_oEditor;
var textOrigModified = false;
var textTradModified = false;

function toggleSample(name, reEvaluate, methodName) {
	var el = $(name);
	if (!window[name] || reEvaluate) {
		eval(el.originalText || el.innerText || el.innerHTML.unescapeHTML());
	}
	if (methodName === false) return;
	methodName = methodName || 'toggle';
	return window[name][methodName]();
}

function showPanel(idx) {
	var pIds = ["panel_o", "panel_t", "panel_f"];
	var tIds = ["tab0", "tab1", "tab2"];
	var msgs = [msg0, msg1, msg2];
	var btnLabels = ['Transforma&#355;i &icirc;n text cu diacritice <span style="font-size:110%">&raquo;</span>', 'Salva&#355;i textul transformat <span style="font-size:110%">&raquo;</span>', 'Selecteaz&#259; textul cu diacritice'];
	for (var i=0; i<3; i++) {
		var p = $(pIds[i]);
		var t = $(tIds[i]);
		if (i!=idx) {
			p.style.display = "none";
			t.className = "";
		}
	}
	for (var i=0; i<3; i++) {
		var p = $(pIds[i]);
		var t = $(tIds[i]);
		if (i==idx) {
			p.style.display = "block";
			t.className = "active";
			$("button_label").innerHTML = btnLabels[i];
		}
	}
	displayTopMsg(msgs[idx]);
	if (idx == 1 && _iframeTop <= 58) {
		var iframe = $("text_tWidgIframe");
		_iframeTop = getElementTop(iframe);
		_iframeLeft = getElementLeft(iframe);
	}
}

function getSelectedPanel() {
	var tIds = ["tab0", "tab1", "tab2"];
	var idx = -1;
	for (var i=0; i<3; i++) {
		var t = $(tIds[i]);
		if (t.className == "active") {
			idx = i;
			break;
		}
	}
	return idx;
}

function displayTopMsg(msg) {
	var holder = document.getElementById("msg_holder");
	holder.innerHTML = msg;
}

function doOnPreLoad() {
	adjustCSS();
	if (!submitted) {
		try { $("text_o").focus(); } catch (ex) {}
	} else {
		try { $("text_tWidgIframe").contentWindow.focus(); } catch (ex) {}
	}
	anim_open_page = new Animator({
		onComplete: function() { 
			if (!secondaryFrame_isClose) {
				anim_frame.play(); 
			} else {
				$('pageHolder').style.display = "none";
				$("screenGlass").style.display = "none";
				secondaryFrame_isClose = null;
			}
		}
	})
    .addSubject(new ColorStyleSubject($("screenGlass"), 'background-color', "#ffffff", "#666666"));
	actualBodyHeight = document.getElementsByTagName("BODY")[0].offsetHeight;
	secondaryFrameHeight = Math.round((actualBodyHeight*90)/100) + 2;
	anim_frame = new Animator({
	    //transition: Animator.makeEaseIn(2),
		onComplete: function() { 
			if (secondaryFrame_isClose) {
				anim_open_page.reverse();
			} else {
				$("pageFrame").style.height = (parseInt($("pageHolder").style.height) - 20) + "px";
				$("pageFrame").style.width = "100%";
				secondaryFrame_isClose = null;
			}
		},
	    duration: 1000
	}).addSubject(new NumericalStyleSubject($("pageHolder"), 'height', "1", secondaryFrameHeight));
	//anim_glass = new Animator().addSubject(new NumericalStyleSubject($("screenGlass"), 'opacity', 1, 0.25));	
	//anim_open_page.play();
	//anim_frame.reverse();	
	var link1 = $("link1");
	link1.onclick = function() { openSecondaryPage('ghid.php', 80, 44); }
	link1.href = "#ghid";
	var link2 = $("link2");
	link2.onclick = function() { openSecondaryPage('sugestii.php', 80, 42); }
	link2.href = "#sugestii";
}
function doOnLoad() {
	BodyLoaded = 1;
	InitContext();
}

function openSecondaryPage(url, percentHeight, percentWidth) {
	if (!anim_open_page || !anim_frame) {
		return;
	}
	var pageHolder = $("pageHolder");
	if (percentHeight) {
		pixelHeight = Math.round((actualBodyHeight*parseInt(percentHeight))/100) + 2;
		anim_frame.subjects[0].to = parseFloat(pixelHeight);
	}
	if (!percentWidth) {
		percentWidth = 50;
	} else {
		percentWidth = parseInt(percentWidth);
	}
	pageHolder.style.width = percentWidth + "%";
	pageHolder.style.left = Math.round((100-percentWidth)/2) + "%";
	secondaryFrame_isClose = false;
	changeSrc($("pageFrame"), url);
	$("pageFrame").style.height = "0px";
	$("pageFrame").style.width = "0px";
	$("pageFrame").style.display = "none";
	$("screenGlass").style.display = "block";
	pageHolder.style.height = "1px";
	pageHolder.style.display = "block";
	anim_open_page.play();
	return false;
}

function closeSecondaryPage() {
	//alert(0)
	secondaryFrame_isClose = true;
	changeSrc($("pageFrame"), "blank.html");
	$("pageFrame").style.display = "none";
	anim_frame.reverse();
	return false;
}

function changeSrc(frame, url) {
	if (document.all) {
		frame.contentWindow.location.href = url;
	} else {
		frame.src = url;
	}
}

function showSecondaryFrame() {
	if (secondaryFrame_isClose != null) {
		window.setTimeout("showSecondaryFrame()", 50);
		return;
	}
	var frame = $("pageFrame");
	frame.style.display = "inline";
	if (typeof(frame.contentWindow.onShow) != "undefined" && frame.contentWindow.onShow) {
		window.setTimeout('$("pageFrame").contentWindow.onShow();', 100);
	}
}

function adjustCSS() {
	if (!document.all) {
		$("bigDiv").style.height = "85%";
		$("panel_o").style.height = "91%";
		$("panel_t").style.height = "91%";
		$("panel_f").style.height = "91%";
	} else {
		$("bigDiv").style.width = "99%";
	}
}

function $(id) {
    return document.getElementById(id);
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


function text2html(text) {
	text = text.replace(/ă/g, '&#259;');
	text = text.replace(/ş/g, '&#351;');
	text = text.replace(/ţ/g, '&#355;');
	text = text.replace(/Ă/g, '&#258;');
	text = text.replace(/Ş/g, '&#350;');
	text = text.replace(/Ţ/g, '&#354;');
	return text;
}

function html2text(text) {
	text = text.replace(/&#259;/g, 'ă');
	text = text.replace(/&#351;/g, 'ş');
	text = text.replace(/&#355;/g, 'ţ');
	text = text.replace(/&#258;/g, 'Ă');
	text = text.replace(/&#350;/g, 'Ş');
	text = text.replace(/&#354;/g, 'Ţ');
	text = text.replace(/&icirc;/g, 'î');
	text = text.replace(/&Icirc;/g, 'Î');
	text = text.replace(/&acirc;/g, 'â');
	text = text.replace(/&Acirc;/g, 'Â');
	return text;
}

function get_words(text) {
	return text.split(/[\s,-]+/);
}

function reduce_diacriticals(text) {
	text = html2text(text);
	text = text.replace(/ă/g, 'a');
	text = text.replace(/ş/g, 's');
	text = text.replace(/ţ/g, 't');
	text = text.replace(/î/g, 'i');
	text = text.replace(/â/g, 'a');
	text = text.replace(/Ă/g, 'A');
	text = text.replace(/Ş/g, 'S');
	text = text.replace(/Ţ/g, 'T');
	text = text.replace(/Î/g, 'I');
	text = text.replace(/Â/g, 'A');
	return text;
}

function contains_diacriticals(text) {
	var d = ['ă', 'î', 'ş', 'ţ', 'â', 'Ă', 'Î', 'Ş', 'Ţ', 'Â', '&#259;', '&#351;', '&#355;', '&#258;', '&#350;', '&#354;', '&icirc;', '&Icirc;', '&acirc;', '&Acirc;'];
	var found = false;
	for (var i=0; i<d.length; i++) {
		if (text.indexOf(d[i]) != -1) {
			found = true;
			break;
		}
	}
	return found;
}

function actionToStep1() {
	if (trimString(document.forms['panel_o'].elements["text_o"].value) == "") {
		alert("Introduceţi un text în câmpul de la pasul 1.");
		return;
	}
	if (textOrigModified) {
		document.forms['panel_o'].submit();
		displayTopMsg(msgWait);
	} else {
		showPanel(1);
	}
}

function strip_tags(str) {
	var reg = new RegExp("</?(?:(?:[aA][a-zA-Z])|(?:[b-zB-Z!]))[^>]*>", "g");
	return str.replace(reg, "");
}

function actionToStep2() {
	if (trimString(document.forms['panel_o'].elements["text_o"].value) == "") {
		alert("Introduceţi un text în câmpul de la pasul 1.");
		return;
	}
	var iframe = $("text_tWidgIframe");
	var iframeBody = iframe.contentWindow.document.getElementsByTagName("body")[0]
	var val = trimString(strip_tags(iframeBody.innerHTML));
	document.forms['panel_f'].elements["text_f"].value = val;
	showPanel(2);
}

function changeTextFormat() {
	var format = "text";
	if ($("rtt_html").checked) {
		format = "html";
	} 
	var val = document.forms['panel_f'].elements["text_f"].value;
	if (format == "html") {
		document.forms['panel_f'].elements["text_f"].value = text2html(val);
	} else {
		document.forms['panel_f'].elements["text_f"].value = html2text(val);
	}
}

function clickActionButton() {
	var idx = getSelectedPanel();
	if (idx == 0) {
		actionToStep1();
	}
	if (idx == 1) {
		actionToStep2();
	}
	if (idx == 2) {
		copy(document.forms['panel_f'].elements["text_f"]);
	}
}


function copy(inElement) {
	inElement.select();
	if (inElement.createTextRange) {
		var range = inElement.createTextRange();
		if (range && BodyLoaded==1)
			range.execCommand('Copy');
		} else {
/*    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="img/_clipboard.swf" FlashVars="clipboard='+escape(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
*/
  }
}

