var justloaded = 0;


function previewURL()
{
	var lang = document.getElementById('lang').value;
	var file = document.getElementById('file').value;

	if(lang) lang = '/'+lang;
	document.getElementById('url').innerHTML = lang+'/'+file;
}

function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
	imageFile=escapeQuotesHTML(imageFile);
	speedTip=escapeQuotesHTML(speedTip);
	tagOpen=escapeQuotes(tagOpen);
	tagClose=escapeQuotes(tagClose);
	sampleText=escapeQuotes(sampleText);
	var mouseOver="";

	document.write("<a href=\"javascript:insertTags");
	document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\" onMouseOver='showDescr(\"" + speedTip + "\");' onMouseOut=\"kill();\">");
	document.write("<img style='padding: 3px;' src=\""+imageFile+"\" border=\"0\" alt=\""+speedTip+"\" "+mouseOver+">");
	document.write("</a>");
	justloaded = 0;
	return;
}

function escapeQuotes(text) {
	var re=new RegExp("'","g");
	text=text.replace(re,"\\'");
	re=new RegExp("\\n","g");
	text=text.replace(re,"\\n");
	return escapeQuotesHTML(text);
}

function escapeQuotesHTML(text) {
	var re=new RegExp('&',"g");
	text=text.replace(re,"&amp;");
	var re=new RegExp('"',"g");
	text=text.replace(re,"&quot;");
	var re=new RegExp('<',"g");
	text=text.replace(re,"&lt;");
	var re=new RegExp('>',"g");
	text=text.replace(re,"&gt;");
	return text;
}

function insertTags(tagOpen, tagClose, sampleText) {
	var txtarea = document.getElementById('body');
	// IE
	if(document.selection) {
		var theSelection = document.selection.createRange().text;
		if(!theSelection) theSelection=sampleText;
		txtarea.focus();

		if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
		} else {
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
		}

	// Mozilla
	} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
		var replaced = false;
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		if(endPos-startPos) replaced=true;
		var scrollTop=txtarea.scrollTop;
		var myText = (txtarea.value).substring(startPos, endPos);
		if(!myText) { myText=sampleText;}
		if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
		} else {
			subst = tagOpen + myText + tagClose;
		}
		txtarea.value = txtarea.value.substring(0, startPos) + subst +
			txtarea.value.substring(endPos, txtarea.value.length);
		txtarea.focus();
		//set new selection
		if(replaced){
			var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
			txtarea.selectionStart=cPos;
			txtarea.selectionEnd=cPos;
		}else{
			txtarea.selectionStart=startPos+tagOpen.length;
			txtarea.selectionEnd=startPos+tagOpen.length+myText.length;
		}
		txtarea.scrollTop=scrollTop;
	}
	// All other browsers get no toolbar.
	// reposition cursor if possible
	if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
}


//--------------------------------------------------------------------------
//--------------------------------------------------------------------------

var ie = document.all;		//test for IE
var nn = document.layers;	//test for NN
var dragging;

//Hides Description Text--whether called by the message mechanism, or by the popup mechanism
function hideDescr(layer_name)
{
	document.getElementById(layer_name).style.display = "none";
}

//************************BELOW IS THE POPUP MESSAGE CODE****************************/
var offsetx=5; //x offset of popup from mouse pointer
var offsety=15; //y offset of popup from mouse pointer

if (document.documentElement && document.documentElement.scrollTop)
{
	rightedge = document.documentElement.clientWidth - 150;
	bottomedge = document.documentElement.scrollTop + document.body.clientHeight - 20;
}
else if(document.body){
	rightedge = document.body.clientWidth - 150;
	bottomedge = document.body.scrollTop + document.body.clientHeight - 20;
}
else{
	rightedge = window.innerWidth - 170;
	bottomedge = window.pageYOffset + window.innerHeight - 20;
}


//This function inserts appropriate text into the popup by the mouse
function showDescr(description)
{
	document.getElementById('popup').innerHTML = description;
	document.getElementById('popup_container').style.display = "block";
}

with (document)
{
	document.write("<div id = 'popup_container' style='position:absolute; left:0px; top:0px; border: 0px solid red; display: none; z-index:100;'><div id='popup'> </div></div>");
}


function kill()
{
	document.getElementById('popup_container').style.display = "none";
}

function newPos(e)
{
	if (!ie)
	{
		rightedge  = window.innerWidth - 20;
		bottomedge = window.pageYOffset + window.innerHeight - 20;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		rightedge = document.documentElement.clientWidth - 150;
		bottomedge = document.documentElement.scrollHeight - 20;
	}
	else
	{
		var rightedge = document.body.clientWidth - 20;
		var bottomedge = document.body.scrollHeight - 20;
	}

	if(ie)
	{
		document.getElementById("popup_container").style.left = Math.min(rightedge, event.clientX + document.body.scrollLeft + offsetx);
		document.getElementById("popup_container").style.top  = Math.min(bottomedge, event.clientY + document.body.scrollTop + offsety);

		if(dragging)
		{
			document.getElementById("smileys").style.left=nowX+event.clientX - doffsetx;
			document.getElementById("smileys").style.top=nowY+event.clientY - doffsety;
		}
	}
	else if(nn)
	{
		nn["popup_container"].left = Math.min(rightedge, e.pageX + offsetx);
		nn["popup_container"].top = Math.min(bottomedge, e.pageY + offsety);
	}
	else
	{
		document.getElementById("popup_container").style.left = Math.min(rightedge, e.pageX + offsetx) + "px";
		document.getElementById("popup_container").style.top = Math.min(bottomedge, e.pageY + offsety) + "px";

		if(dragging)
		{
			document.getElementById("smileys").style.left=nowX+e.clientX - doffsetx;
			document.getElementById("smileys").style.top=nowY+e.clientY - doffsety;
		}
	}
}


if(document.layers) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = newPos;
document.onmousedown=ddInit;
document.onmouseup=Function("dragging=false;");


function smileys()
{
	var style = "style = 'position: absolute; left: 600px; top: 250px; width:150px; visibility: hidden;'";
	document.write("<div id='smileys' " + style + "><table bgcolor='#ffffff' cellspacing=0 cellpadding=0 style='border: 1px solid;'>");
	document.write("<tr style='background-color:#dde; width: 120px;'><td id='titleBar' style='cursor:move; padding: 3px;'><a href='#smiley' onClick='showSmileys();'");
	document.write("><IMG title='close' style='padding: 2px; float: right;' align=absmiddle border=0 src='/img/interface/actions/no.png'> </a> emoticons: </td></tr><tr><td style='padding: 5px;'>");
	addButton('/img/interface/emoticons/angry.png', ':@', ':@', '', '');
	addButton('/img/interface/emoticons/bat.png', ':[', ':[', '', '');
	addButton('/img/interface/emoticons/beer.png', '(B)', '(B)', '', '');
	addButton('/img/interface/emoticons/teeth.png', ':D', ':D', '', '');
	addButton('/img/interface/emoticons/bigsmile.png', '[:D', '[:D', '', '');
	addButton('/img/interface/emoticons/buta.png', '(buta)', '(buta)', '', '');
	document.write("<br>");
	addButton('/img/interface/emoticons/cake.png', '(^)', '(^)', '', '');
	addButton('/img/interface/emoticons/camera.png', '(C)', '(C)', '', '');
	addButton('/img/interface/emoticons/cat.png', '(@)', '(@)', '', '');
	addButton('/img/interface/emoticons/clock.png', '(O)', '(O)', '', '');
	addButton('/img/interface/emoticons/cocktail.png', '(D)', '(D)', '', '');
	addButton('/img/interface/emoticons/confused.png', ':S', ':S', '', '');
	document.write("<br>");
	addButton('/img/interface/emoticons/confusedbox.png', '[:S', '[:S', '', '');
	addButton('/img/interface/emoticons/cry.png', ';(', ';(', '', '');
	addButton('/img/interface/emoticons/cup.png', '(cup)', '(cup)', '', '');
	addButton('/img/interface/emoticons/dog.png', '(&)', '(&)', '', '');
	addButton('/img/interface/emoticons/tongue.png', ':P', ':P', '', '');
	addButton('/img/interface/emoticons/embarassed.png', ':$', ':$', '', '');
	document.write("<br>");
	addButton('/img/interface/emoticons/film.png', '(~)', '(~)', '', '');
	addButton('/img/interface/emoticons/foot_in_mouth.png', ':-X', ':-X', '', '');
	addButton('/img/interface/emoticons/innocent.png', '0:)', '0:)', '', '');
	addButton('/img/interface/emoticons/kiss.png', '(K)', '(K)', '', '');
	addButton('/img/interface/emoticons/love.png', '(L)', '(L)', '', '');
	addButton('/img/interface/emoticons/unlove.png', '(U)', '(U)', '', '');
	document.write("<br>");
	addButton('/img/interface/emoticons/wink.png', ';)', ';)', '', '');
	addButton('/img/interface/emoticons/note.png', '(8)', '(8)', '', '');
	addButton('/img/interface/emoticons/omg.png', ':O', ':O', '', '');
	addButton('/img/interface/emoticons/phone.png', '(T)', '(T)', '', '');
	addButton('/img/interface/emoticons/present.png', '(G)', '(G)', '', '');
	addButton('/img/interface/emoticons/sad.png', ':(', ':(', '', '');
	document.write("<br>");
	addButton('/img/interface/emoticons/shade.png', 'B-D', 'B-D', '', '');
	addButton('/img/interface/emoticons/sleep.png', '|-)', '|-)', '', '');
	addButton('/img/interface/emoticons/smile.png', ':)', ':)', '', '');
	addButton('/img/interface/emoticons/smilebox.png', '=]', '=]', '', '');
	addButton('/img/interface/emoticons/smiley-26.png', 'X-P', 'X-P', '', '');
	addButton('/img/interface/emoticons/star.png', '(*)', '(*)', '', '');
	document.write("<br>");
	addButton('/img/interface/emoticons/lightbulb.png', '(I)', '(I)', '', '');
	addButton('/img/interface/emoticons/thumbs_up.png', '(Y)', '(Y)', '', '');
	addButton('/img/interface/emoticons/thumbs_down.png', '(N)', '(N)', '', '');
	addButton('/img/interface/emoticons/undecided.png', ':-/', ':-/', '', '');
	addButton('/img/interface/emoticons/email.png', '(E)', '(E)', '', '');
	addButton('/img/interface/games.png', '(ps)', '(ps)', '', '');
	document.write("<br>");
	addButton('/img/interface/emoticons/rose.png', '(F)', '(F)', '', '');
	addButton('/img/interface/emoticons/wilted_rose.png', '(w)', '(w)', '', '');
	addButton('/img/interface/penguin.png', '(P)', '(P)', '', '');
	addButton('/img/interface/phppg.png', '(pg)', '(pg)', '', '');
	addButton('/img/interface/actions/translate.png', '(fish)', '(fish)', '', '');
	addButton('/img/interface/actions/warning.png', '(!)', '(!)', '', '');
	document.write("<br>");
	addButton('/img/interface/kcmprocessor.png', '(CPU)', '(CPU)', '', '');
	addButton('/img/interface/personal.png', '(R)', '(R)', '', '');
	document.write("</td></tr></table></div>");
}


//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//-------------- DRAGGABLE LAYER SCRIPT: -----------------------------------

function ddInit(e){
  div_handle = ie ? document.all.smileys : document.getElementById("smileys");
  drag_active = ie ? event.srcElement : e.target;
  top_tag = ie ? "BODY" : "HTML";

  while (drag_active.id!="titleBar" && drag_active.tagName!=top_tag)
  {
    drag_active=ie ? drag_active.parentElement : drag_active.parentNode;
  }

  if (drag_active.id=="titleBar")
  {
    doffsetx=ie ? event.clientX : e.clientX;
    doffsety=ie ? event.clientY : e.clientY;
    nowX=parseInt(div_handle.style.left);
    nowY=parseInt(div_handle.style.top);
    dragging=true;
    document.onmousemove=newPos;
  }
}


function showSmileys()
{
	var test = document.getElementById('smileys').style.visibility;
	if(test == "hidden")
	{
		document.getElementById('smileys').style.visibility="visible";
		var new_top = document.documentElement.scrollHeight - 500;
		document.getElementById('smileys').style.top = new_top;
	}
	else
	{
		document.getElementById('smileys').style.visibility="hidden";
	}
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------


function toggleUpload()
{
	if(document.getElementById('fileUpload').style.visibility == "hidden")
		document.getElementById('fileUpload').style.visibility = "visible";
	else
		document.getElementById('fileUpload').style.visibility = "hidden";
}