
var wthreec = (document.getElementById) ? true: false;
var ie5 = (wthreec && document.all) ? true : false;
var req;

function loadXMLDoc(url, data)
{
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType)
		{
			req.overrideMimeType('text/xml');
		}
	}
	else
		if (window.ActiveXObject)
		{ // IE
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					req = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		}

	if (!req)
	{
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	req.onreadystatechange = processReqChange;
	if (debugMode)
		debugReq = 'debug&';
	else
		debugReq = '';
	url_s = url+'?'+debugReq + urlEncodeData(data)+"&hash=" + Math.random();
	if (debugMode)
		alert(url_s);
	req.open("GET", url_s, true);
	req.send();

	ab = window.setTimeout("req.abort();", 5000);
}

function processReqChange()
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			if (is_sent)
			{
				alert('Request has been sent.');
				if (debugMode)
				{
					alert(req.responseText);
				}
				hideWindow(tWindow);
				is_sent = false;
			}
		}
		else
		{
			alert("Failed to send request:\n" +
			req.statusText);
			hideWindow(tWindow);
		}
	}
}

function urlEncodeData(data)
{
	var query = [];
	if (data instanceof Object)
	{
		for (var k in data)
		{
			query.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k]));
		}
		return query.join('&');
	}
	else
	{
		return encodeURIComponent(data);
	}
}

var cWindow = null;
var startX = 0;
var startY = 0;
var IE = document.all ? true : false;
if (!IE)
	document.captureEvents(Event.MOUSEMOVE, Event.MOUSEUP);

function onWindowTitleMouseDown(evt)
{
	var xMouse=(ie5)?event.x:evt.pageX;
	var yMouse=(ie5)?event.y:evt.pageY;
	var tId = (ie5)?event.srcElement.getAttribute("id"):evt.target.id;
	var wId = tId.substr(0,tId.length-6);
	eWindow = document.getElementById(wId);
	eWindow.style.cursor = 'move';
	cWindow = eWindow;
	startX = xMouse;
	startY = yMouse;
	document.onmousemove = onWindowTitleMouseMove;
	document.onmouseup = onWindowTitleMouseUp;
}

function onWindowTitleMouseMove(evt)
{
	if (cWindow == null)
		return false;

	var eWindow = cWindow;
	var xMouse=(ie5)?event.x:evt.pageX;
	var yMouse=(ie5)?event.y:evt.pageY;

	deltaX = xMouse - startX;
	deltaY = yMouse - startY;
	startX = xMouse;
	startY = yMouse;

	eWindow.style.top = parseInt(eWindow.style.top) + deltaY;
	eWindow.style.left = parseInt(eWindow.style.left) + deltaX;
}

function hideWindow(windowid)
{
	document.getElementById(windowid).style.display = 'none';
}

function showWindow(windowid,x)
{
	document.getElementById(windowid).style.display = 'block';
	if (x>0)
		document.getElementById(windowid).style.top = 300+x+'px';
}

function onWindowTitleMouseUp(evt)
{
	if (cWindow == null)
		return false;
	var eWindow = cWindow;
	eWindow.style.cursor = 'auto';
	cWindow = null;
	document.onmousemove = null;
	document.onmouseup = null;
}

function createTextareaWindow(title,windowid,textid,callback,x,y)
{
	var code = '';
//	code += "<link href=\"http://www.to3000.com/_shared/glavred/frontend.css\" rel=\"stylesheet\" type=\"text/css\" />";
	code += "<div class='windowStyle' id='"+windowid+"'>";
	code += "<div class='wTitle' id='"+windowid+"_title'><img src='http://www.to3000.com/_shared/glavred/close.gif' align=right class='close' onClick='hideWindow(\""+windowid+"\");'>";
	code += ""+title+"</div>";
	code += "<div class='wContainer'><p style='padding: 1px; margin: 1px;'>You can edit the selected text. The original and edited text will be e-mailed to locky@stranslation.com</p>";
	code += "<textarea id='"+textid+"' cols=50 rows=7 ></textarea><br>";
	code += "<center><input type=button value='Send' align='right' onclick='"+callback+";'></center></div></div>";
	document.write(code);
	eWindow = document.getElementById(windowid);
	document.getElementById(windowid+"_title").onmousedown=onWindowTitleMouseDown;
	eWindow.style.top = y;
	eWindow.style.left = x;
	wTempX = x;
	wTempY = y;
	return windowid;
}


/*	Main Part */

var selectionText;
var debugMode = false;
var is_sent = false;
var backendUrl = 'http://www.to3000.com/_shared/glavred/backend.php';

function getUserSelection()
{
	var selectedRange = document.selection.createRange();
	return selectedRange.text;
}

function catchKey(event)
{
	key = event.keyCode? event.keyCode: event.charCode;
	if (event.altKey)
	{
		switch (key)
		{
			case   82: // R
			case  114: // r
			case 1050: // cyrillic K
			case 1082: // cyrillic k
				if (document.getSelection)
				{
					selectedText = document.getSelection();
				}
				else if (document.selection)
				{
					selectedText = getUserSelection();
				}
				if (selectedText == '')
				{
					alert('Nothing to do.'); return;
				}
//				alert('Glavred is comming....');
				i = 1;
				showWindow(tWindow,(document.all)?document.body.scrollTop:window.pageYOffset);
				tmField = document.getElementById('tField');
				tmField.value = selectedText;
				selectionText = selectedText;
			break;
			case 'H':
			case 'h':
				alert('Correct the text, then press "Send" button.');
			break;
		}
	}
}


function sendCorrection(textid)
{
	var tField = document.getElementById('tField');
	tNew = tField.value;
	var postData = {'url':document.location,'text':tNew,'text_original':selectionText,'email':'locky@stranslation.com'}
	loadXMLDoc(backendUrl, postData);
	is_sent = true;
}

var tWindow = createTextareaWindow('Glavred', 'wTextEdit', 'tField', 'sendCorrection("tField")', 400, 300);
hideWindow(tWindow);

if(document.addEventListener)
{
	document.addEventListener("keypress", catchKey, false);
}
else
{
	document.attachEvent("onkeydown", catchKey);
}
document.write("<link href=\"http://www.to3000.com/_shared/glavred/frontend.css\" rel=\"stylesheet\" type=\"text/css\" />");
