
if (navigator.appName.indexOf( 'Microsoft' ) >= 0)
	window.IE = true;
else window.IE = false;

if (!window.Node)
var Node = {

 	ELEMENT_NODE : 1,
 	ATTRIBUTE_NODE : 2,
 	TEXT_NODE : 3,
 	CDATA_SECTION_NODE : 4,
 	ENTITY_REFERENCE_NODE : 5,
 	ENTITY_NODE : 6,
 	PROCESSING_INSTRUCTION_NODE : 7,
 	COMMENT_NODE : 8,
 	DOCUMENT_NODE : 9,
 	DOCUMENT_TYPE_NODE : 10,
 	DOCUMENT_FRAGMENT_NODE : 11,
 	NOTATION_NODE : 12

};

function getX( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}


/**
 * Show element with notice
 */
function showNotice()
{
	var closed = getCookie( "noticeClosed" );
	if ( closed == '1' )
		return true;

	var html = '	\
	<div style="padding: 10px; width:195px; height:95px; background-repeat:no-repeat; background-image:url(http://it-docs.org/fileadmin/stuff/template/english/images/speach.gif);">	\
		<div style="float:right;"><a href="javascript: hideNotice(); void 0;"><img style="margin:0px; border:0px;" src="/fileadmin/stuff/template/english/images/close.gif"></a></div>	\
		<strong>Привет</strong>, я Ваш переводчик!<br />	\
		Если какое-то английское слово на сайте вызовет у Вас затруднение, нажав клавишу <strong>Caps Lock</strong>,\
		подведите к нему мышь, выберите слово в меню.';

	var div = document.createElement( "div" );

	div.setAttribute( "id", "noticeElement" );

	div.style.borderWidth = "0px";
	div.style.padding = "15px";
	div.style.margin = "0px";
	div.style.textAlign = "left";
	div.style.fontSize = "10px";
	div.style.width = "210px";
	div.style.height = "110px";
	div.setAttribute( "class", "transparent" );

	div.style.position = "absolute";

	var leftPosition = getX ( $('translatorIMG') );
	var topPosition = getY ( $('translatorIMG') );

	if ( !parseInt( leftPosition ) )
		leftPosition = 0;
	if ( !parseInt( topPosition ) )
		topPosition = 0;

	leftPosition +=	115;
	topPosition += 105;

	div.style.left = leftPosition + "px";
	div.style.top = topPosition + "px";

	div.innerHTML = html;

	var body = document.getElementsByTagName("body")[0];
	body.appendChild( div );

	var menuHideEffect = new fx.Opacity(div,
		{
			duration: 3000,
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(0, 1);

	setCookie( "noticeClosed", "0", 365, "/");
	setTimeout("hideNotice()", 15000);

	return true;


} // function


/**
 * Hide element with notice
 */
function hideNotice()
{
	var closed = getCookie( "noticeClosed" );
	if ( closed == '1' )
		return true;

	var container = $("noticeElement");
	if ( !container )
		return false;

	var menuHideEffect = new fx.Opacity(container, 
		{
			duration: 1000, 
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(1, 0);
	setCookie( "noticeClosed", "1", 365, "/");

	return true;

} // function

/**
 * Show Help Translate Submenu
 */
function showHelpTranslateSubMenu( elementObject )
{

	removeAjaxTranslation();

	var word = elementObject.getAttribute( 'word' );
	var wordString = word.substr( 0, 1 ).toUpperCase() + word.substr( 1 );

	var html = '<div><div style="float:right;"><a href="javascript: hideHelpTranslateSubmenu(); removeAjaxTranslation(); void 0;"><img style="margin:0px; border:0px;" src="/fileadmin/stuff/template/english/images/close.gif"></a></div><strong><a href="javascript: void 0;" onclick="translate(\''+ word +'\'); void 0;">' + wordString + '<strong></div>';

	var existing = document.getElementById( "helpTranslateSubMenu" );
	if ( existing )
	{
		var parent = existing.parentNode;
		parent.removeChild( existing );
	}

	var div = document.createElement( "div" );

	var leftPosition = getX ( elementObject );
	var topPosition = getY ( elementObject );
	topPosition += elementObject.offsetHeight;

	div.setAttribute( "id", "helpTranslateSubMenu" );

	div.style.border = "1px solid #cccccc";
	div.style.backgroundColor = "#ffffff";

	div.style.padding = "5px";
	div.style.margin = "0px";
	div.style.textAlign = "left";
	div.style.fontSize = "11px";

	div.style.position = "absolute";

	if ( !parseInt( leftPosition ) )
		leftPosition = 0;
	if ( !parseInt( topPosition ) )
		topPosition = 0;

	div.style.left = leftPosition + "px";
	div.style.top = topPosition + "px";
	var divWidth = word.length;
	if ( divWidth == false || divWidth == null || divWidth == 'undefined' )
		divWidth = 10;
	divWidth *= 10;
	divWidth += 25;

	div.style.width = divWidth + 'px';
	div.innerHTML = html;

	var body = document.getElementsByTagName("body")[0];
	body.appendChild( div );

	var menuHideEffect = new fx.Opacity(div,
		{
			duration: 1500,
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(0, 1);
	return true;


} // function


/**
 * Hide Help Translate Submenu
 */
function hideHelpTranslateSubmenu()
{
	var container = $("helpTranslateSubMenu");
	if ( !container )
		return false;

	var menuHideEffect = new fx.Opacity(container, 
		{
			duration: 1000, 
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(1, 0);
	hideAjaxTranslation();
	return true;

} // function


function findTextInTree( rootNode, text )
{

	if ( rootNode.nodeType == window.Node.ELEMENT_NODE )
	{
		for ( var i = 0; i < rootNode.childNodes.length; i++ )
		{
			var done = findTextInTree( rootNode.childNodes[ i ], text );
			if ( done )
				return done;

		} // for

	} // if

	if ( rootNode.nodeType == window.Node.TEXT_NODE )
	{
	        window.alls += rootNode.nodeValue;

		if ( rootNode.nodeValue.indexOf( text ) >= 0 && rootNode.nodeValue.indexOf( text ) <= 15 )
			return [rootNode, rootNode.parentNode];

	} // if

	return false;

} // function


// translate help

function onTranslateHelperOver( e, object )
{
	if ( !window.helpingTranslation )
		return;

	if ( !e )
		e = window.event;

	object.style.cursor = 'help';
	showHelpTranslateSubMenu( object );
	return;

}

function onTranslateHelperOut( e, object )
{
	if ( !window.helpingTranslation )
		return;

	if ( !e )
		e = window.event;

	object.style.cursor = '';
	return;

}

function translateHelper( e, object )
{

	if ( !window.helpingTranslation )
		return;

	if ( !e )
		e = window.event;

	e.cancelBubble = true;
	if (e.stopPropagation) 
		e.stopPropagation();
	
	var whatHappened = false;
	if ( e.target )
		whatHappened = e.target;
	else whatHappened = e.srcElement;

	var word = object.getAttribute( 'word' );
	return;

}

function getAllFilteredTextNodes( r )
{
	var textNodes = new Array();
	function travel( rootElement )
	{
			switch( rootElement.nodeType )
			{
				case 1 : {
					var children = rootElement.childNodes;
					var i;
					for ( i = 0; i < children.length; i++ )
						travel( children[ i ] );

					break;

				} // case 1

				case 3 : {
					// textNodes[ textNodes.length ] = rootElement;
					var re = /[a-z]+/i;
					if ( rootElement.nodeValue.match( re ) )
					{
						var newString = fillStringToHelpTranslate( rootElement.nodeValue );
						var span = document.createElement( 'span' );
						span.innerHTML = newString;
						span.setAttribute( 'class', 'translateTextBlock' );
						var parent = rootElement.parentNode;
						parent.replaceChild( span, rootElement );

					}

					return;

				} // case 3

			} // switch

			return;

	} // function

	travel( r );

	var filteredTexts = new Array();
	var re = /[a-z]+/i;

	for ( index in textNodes )
	{
		var string = textNodes[ index ].nodeValue;
		if ( 
			(	string != false	) && 
			(	string != 'undefined'	) && 
			(	string != null	) && 
			(	string.match( re ) )
		)
			filteredTexts[ filteredTexts.length ] = textNodes[ index ];

	} // for

	return filteredTexts;

} // function

function fillStringToHelpTranslate( string )
{
	var inWord = false;
	var currentWord = '';
	var letterRe = /[a-z]/i;
	var result = '';

	if ( window.IE )
	{
		if ( string.substr( 0, 1 ) == ' ' )
			string = '|||---12345-000-54321---|||' + string.substr( 1 );

		if ( string.substr( string.length - 1, 1 ) == ' ' )
			string = string.substr( 0, string.length - 1 ) + '|||---12345-000-54321---|||';

	} // if

	for ( var i = 0; i < string.length; i++ )
	{
		var letter = string.substr( i, 1 );
		if ( letter.match( letterRe ) )
		{
			if ( !inWord )
				inWord = true;
			currentWord += letter;

		} 
		else
		{
			if ( inWord )
			{
				inWord = false;
				var alias = currentWord.toLowerCase();
				result += '<span onmouseover="onTranslateHelperOver( event, this );" onmouseout="onTranslateHelperOut( event, this );" onclick="translateHelper( event, this ); void 0;" class="translateHelper" word="' + alias + '">' + currentWord + '</span>';
				currentWord = '';

			}

			result += letter;

		} // else

	} // for

	if ( inWord )
	{
		inWord = false;
		var alias = currentWord.toLowerCase();
		result += '<span onmouseover="onTranslateHelperOver( event, this );" onmouseout="onTranslateHelperOut( event, this );" onclick="translateHelper( event, this ); void 0;" class="translateHelper" word="' + alias + '">' + currentWord + '</span>';
		currentWord = '';

	}

	return result;

} // string

function keyD( e ) {

	if ( e == null || e == 'undefined' || !e )
		e = window.event;

	k = parseInt( e.keyCode );
	if ( k == 20 )
	{
		if ( window.helpingTranslation )
		{
			stopHelpTranslate();
			window.helpingTranslation = false;

		}
		else 
		{
			startHelpTranslate();
			window.helpingTranslation = true;
			window.helpingTranslationStartedEarly = true;

		} // else
	}

}

window.helpingTranslation = false;
window.helpingTranslationStartedEarly = false;

function startHelpTranslate()
{
	if ( !window.helpingTranslationStartedEarly )
	{
		var rootEl = document.getElementById( 'textContainer' );
		var texts = getAllFilteredTextNodes( rootEl );
		if ( window.IE )
		{
			rootEl.innerHTML = strReplace( rootEl.innerHTML.toString(), '|||---12345-000-54321---|||', ' ');
		}

	}

	window.helpingTranslationStartedEarly = true;
	window.helpingTranslation = true;

} // function

function stopHelpTranslate()
{
	hideHelpTranslateSubmenu();
	window.helpingTranslation = false;

} // function

function strReplace(haystack, needle, replacement) 
{
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

function ajaxRequest(url, data) {
    var myAjax = new Ajax.Request(
        url,
        	{
						method: 'post', 
						parameters: data, 
						onComplete: ajaxResponse
					}
    );
}

function ajaxResponse( originalRequest ) {
		if ( originalRequest == null || originalRequest.responseText == null )
			alert( "An error. Sorry!" );


		var parts = originalRequest.responseText.split( '<!---------->' );
		var html = parts[ 0 ];
		var js = parts[ 1 ];

		showAjaxTranslation( html );
		eval( js );

		return true;

}

/**
 * Show Ajax translation
 */
function showAjaxTranslation( text )
{

	var html = '<div><div style="float:right;"><a href="javascript: hideAjaxTranslation(); void 0;"><img style="margin:0px; border:0px;" src="/fileadmin/stuff/template/english/images/close.gif"></a></div>' + text + '</div>';

	var existing = document.getElementById( "helpAjaxTranslation" );
	if ( existing )
	{
		var parent = existing.parentNode;
		parent.removeChild( existing );
	}

	var div = document.createElement( "div" );

	var helperObj = $("helpTranslateSubMenu")
	if ( helperObj == null || !helperObj )
		return;

	var divWidth = 350;
	var leftPosition = getX ( helperObj );
	var screenWidth = parseInt( screen.width );

	if ( !screenWidth )
		screenWidth = 1280;

	var halfWidth = screenWidth / 2;

	if ( leftPosition > halfWidth )
		leftPosition += helperObj.offsetWidth - divWidth - 12;

	var topPosition = getY ( helperObj );
	topPosition += helperObj.offsetHeight - 1;


	div.setAttribute( "id", "helpAjaxTranslation" );

	div.style.border = "1px solid #dddddd";
//	div.style.backgroundColor = "#ffffff";

	div.style.padding = "5px";
	div.style.margin = "0px";
	div.style.textAlign = "left";
	div.style.fontSize = "11px";

	div.style.position = "absolute";

	if ( !parseInt( leftPosition ) )
		leftPosition = 0;
	if ( !parseInt( topPosition ) )
		topPosition = 0;

	div.style.left = leftPosition + "px";
	div.style.top = topPosition + "px";
	div.style.width = divWidth + 'px';

	div.innerHTML = html;

	var body = document.getElementsByTagName("body")[0];
	body.appendChild( div );

	var menuHideEffect = new fx.Opacity(div,
		{
			duration: 1500,
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(0, 1);
	return true;


} // function


function hideAjaxTranslation()
{
	var container = $("helpAjaxTranslation");
	if ( !container )
		return false;

	var menuHideEffect = new fx.Opacity(container, 
		{
			duration: 1000, 
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(1, 0);
	return true;

} // function

function removeAjaxTranslation()
{
	if ( $("helpAjaxTranslation") )
		$("helpAjaxTranslation").parentNode.removeChild( $("helpAjaxTranslation") );
}

function translate( word )
{
	var url = '/translate.php';
	var data = 'translate_word=' + word;
	ajaxRequest( url, data );

	return;

}

/* ---- chat -------- */

function sendChatMessage()
{
	chatAjaxRequest();
  $('chat_question_id').value = '';
	return true;
}

function deleteChatNotice(){
   var el = $('chatNoticeElement');
   if ( !el )
     return;

    var body = document.getElementsByTagName("body")[0];
    body.removeChild( el );

   return true;
}

/**
 * Show element with notice
 */
function showChatNotice( text )
{
        hideChatNotice();

	var html = '	\
	<div style="padding: 10px; width:195px; height:95px; background-repeat:no-repeat; background-image:url(http://it-docs.org/fileadmin/stuff/template/english/images/speach.gif);"> \
		<div style="float:right;"><a href="javascript: hideChatNotice(); void 0;"><img style="margin:0px; border:0px;" src="/fileadmin/stuff/template/english/images/close.gif"></a></div>' + text;

	var div = document.createElement( "div" );

	div.setAttribute( "id", "chatNoticeElement" );

	div.style.borderWidth = "0px";
	div.style.padding = "15px";
	div.style.margin = "0px";
	div.style.textAlign = "left";
	div.style.fontSize = "10px";
	div.style.width = "210px";
	div.style.height = "110px";
	div.setAttribute( "class", "transparent" );

	div.style.position = "absolute";

	var leftPosition = getX ( $('translatorIMG') );
	var topPosition = getY ( $('translatorIMG') );

	if ( !parseInt( leftPosition ) )
		leftPosition = 0;
	if ( !parseInt( topPosition ) )
		topPosition = 0;

	leftPosition +=	115;
	topPosition += 105;

	div.style.left = leftPosition + "px";
	div.style.top = topPosition + "px";

	div.innerHTML = html;

	var body = document.getElementsByTagName("body")[0];
	body.appendChild( div );

	var menuHideEffect = new fx.Opacity(div,
		{
			duration: 3000,
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(0, 1);
	return true;


} // function

function hideChatNotice()
{

deleteChatNotice();
return;

	var container = $("chatNoticeElement");
	if ( !container )
		return false;

	var menuHideEffect = new fx.Opacity(container, 
		{
			duration: 1000, 
			onComplete: function()
			{

			}
		}
	);

	menuHideEffect.custom(1, 0);
	return true;

} // function

function getChatKey()
{
	window.chatKey = getCookie( 'chat_key' );
	if ( !window.chatKey || window.chatKey.lenght == 0 )
	{
		var currentTime = new Date();
		currentTime = currentTime.getTime().toString();
		setCookie( 'chat_key', currentTime );
		window.chatKey = currentTime;
	}

	return window.chatKey;
	
}

function showChatWelcomeMessage()
{
        window.chatModeIsOn = 0;
	$('start_chat_button').style.display = 'block';
	$('stop_chat_button').style.display = 'none';
	$('chat_form').style.display = 'none';
        hideChatNotice();
}

function hideChatWelcomeMessage()
{
        window.chatModeIsOn = 1;
	$('start_chat_button').style.display = 'none';
	$('stop_chat_button').style.display = 'block';
	$('chat_form').style.display = 'block';
        hideChatNotice();
}

function chatAjaxRequest() {

		var chatKey = getChatKey();
		var chatQuestion = $('chat_question_id').value;

		if ( chatKey.lenght == 0 )
		{
			alert( "Failed to get chat key. Check whether cookies are enabled." );
			return false;

		} // if

		if ( chatQuestion.length == 0 )
		{
			alert( "Enter your question first. ;-)" );
			return false;

		} // if

		var url = '/chat/src/chat.php'
		var data = 'chat_key=' + chatKey + '&chat_question=' + escape( chatQuestion );

    var myAjax = new Ajax.Request(
        url,
        	{
						method: 'post', 
						parameters: data, 
						onComplete: chatAjaxResponse
					}
    );
}

function chatAjaxResponse( originalRequest ) {

		if ( originalRequest == null || originalRequest.responseText == null )
			alert( "An error. Sorry!" );

		var text = originalRequest.responseText;
                showChatNotice( text );
		return true;

}

