var lingro = new Object;

typeof(lingro_language) != "undefined" ? lingro.language = lingro_language : lingro.language = '';
typeof(lingro_dictionary) != "undefined" ? lingro.dictionary = lingro_dictionary : lingro.dictionary = '';
lingro.hilight_words = true;

// lingro_light_bookmarklet;


/*******************************************************************************/
/* Other variables (misc.)                                                     */
/*******************************************************************************/
lingro._current_clicked_element = false;
lingro._current_element_class = '';
lingro._split_char = '';


lingro_misc = new Object();

// Offsets for how far the tooltip begins from the mouse pointer (in px)
lingro_misc.OFFSETX = 20;
lingro_misc.OFFSETY = 15;

// Parameters of the tooltip
lingro_misc.TOOLTIP_HEIGHT = 110;
lingro_misc.TOOLTIP_WIDTH = 300;

// Where the tooltip will start (coordinates w/r/t browser window):
lingro_misc.tooltip_start_x;
lingro_misc.tooltip_start_y;

// How much the browser window is scrolled
lingro_misc.scrollx;
lingro_misc.scrolly;

// Global object to hold drag information.

lingro_misc._dragObj = new Object();
lingro_misc._dragObj.zIndex = 99999;

/*******************************************************************************/
/* Main lingro functions                                                       */
/*******************************************************************************/


lingro._makeWordsClickable = function() {
	var translatable_words = lingro_misc.getElementsByClassName(document.body, '*', 'lingro_word');

	for(var i=0; i < translatable_words.length; i++)
	{  
		translatable_words[i].style.cursor = 'pointer';
		translatable_words[i].style.cursor = 'hand';
		translatable_words[i].onclick = function(e) { lingro._translateWord(e, this); }
	}
}

lingro._makeContainersClickable = function() {
	var translatable_containers = lingro_misc.getElementsByClassName(document.body, '*', 'lingro_container');
	
	//alert(translatable_containers);

	if (document.body && document.body.className && document.body.className.match(/lingro_container/)) {
		translatable_containers.push(document.body);
	}

	for(var i=0; i < translatable_containers.length; i++)
	{
		// Don't rewrite stuff for the second time
		if (lingro_misc.getFirstElementByClassName(translatable_containers[i], '*', 'f_api')) {
			continue;
		}

		translatable_containers[i].style.cursor = 'default';
		lingro._makeElementTextClickable(translatable_containers[i]); 
	}


}

/*
 * Send a request to translate a clicked word (event, element).
 */
lingro._translateWord = function(e, el, dict, tooltip_x, tooltip_y) {

	// Make sure IE gets the event
	// if (!e) e = window.event;

	// If we're changing the style of the clicked word, change the className
	if (lingro.hilight_words) {
		if (lingro._current_clicked_element) {
			lingro._current_clicked_element.className = lingro._current_element_class;
		}
	
		if (el) {
			lingro._current_element_class = el.className;
			el.className = 'f_w_e';
		}
	}

	// Figure out where to show the iframe
	if (!(tooltip_x && tooltip_y)) {
		lingro_misc.calculateTooltipPosition(e.clientX, e.clientY);
	} else {
		lingro_misc.tooltip_start_x = tooltip_x;
		lingro_misc.tooltip_start_y = tooltip_y;
	}

	var x = lingro_misc.tooltip_start_x;
	var y = lingro_misc.tooltip_start_y;


	// Get the container, creating if necessary
	var iframe_container = document.getElementById('frn_iframe_container');

	if (!iframe_container) {
		iframe_container = document.createElement('div');
		iframe_container.id = 'frn_iframe_container';
		document.body.appendChild(iframe_container);
	}

	var dictionary = '';
	var language = ''

	// Figure out the dictionary to use
	if (dict && dict.match(/[a-z]{2,3}_[a-z]{2,3}/)) {
		dictionary = dict;
	} else if (dict && dict.match(/[a-z]{2,3}/)) {
		language = dict;
	} else {
		dictionary = lingro.dictionary;
		language = lingro.language;
	}

	if (!dictionary && language) {
		dictionary = language + '_eng';
	}

	// alert(x + ' ' + y);

	// If a word was clicked for the second time, close tooltip
    if (lingro._current_clicked_element == el) {
        iframe_container.innerHTML = '';
		el.className = lingro._current_element_class;
		lingro._current_clicked_element = false;
        
    } else { // otherwise, get stuff
		height = 170;
		width = 305;

		// IE size mod
		if (navigator.appVersion.search(/MSIE/) != -1) {
			height = 210;
			width = 335;
		}
		

		var style_string = " style=\"position: absolute; top: " + (y + 5) + 'px; left: ' + (x + 2) + "px;";
		style_string += " width: " + width + "px; height: " + height + "px; overflow: hidden;"
					 	+ " background: none; border: 0px black solid; z-index: 99999\" ";

		var other_args = ' border="0" allowtransparency="true" ';

		var word = el.innerHTML.replace(/^\s+|\s*$|[ .;?|!:\t\n]/, '');
		word = word.replace(/^\s*/, '').replace(/\s*$/, '');
		word = escape(word);

		var source_url = ' src="http://lingro.com/widgets/tooltipWord?word=' + word + '&onetime_dictionary=' + dictionary
							+ '&url=' + window.location.href + '"';

		var dragger =  '<div id="frn_dragger" onmousedown="lingro_misc._dragStart(event, \'frn_iframe\');" style="position: absolute; top: '
						+ (y + 5 + 4) + 'px; left: ' + (x + 2 + 80) + 'px; width: 190px; height: 23px; '
						+ 'background-color: none; cursor: pointer; cursor: hand; z-index: 99999">'
						+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
						+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
						+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';
		
		var closer =  '<div id="frn_closer" style="position: absolute; top: ' + (y + 5 + 4) + 'px; left: '
						+ (x + 4 + 275) + 'px; width: 20px; height: 20px; cursor: pointer; cursor: hand; '
						+ ' background-image: url(http://lingro.com/images/tooltip_close.png) no-repeat; z-index: 99999">'
						+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';
       
	   	/*
		if (navigator.appVersion.search(/MSIE/) != -1) {
			x -= 4;
		}*/

		var informer = '<div onclick="var frn_nw = window.open(\'http://lingro.com\', \'_blank\'); frn_nw.focus();" id="frn_informer" style="position: absolute; top: ' + (y + 5 + 8 + 127) + 'px; left: '
						+ (x + 4 + 215 - 74) + 'px; height: 20px; cursor: pointer; cursor: hand; '
						+ ' z-index: 99999; display: none;"><span style="position: relative; bottom: 8px; color: #fff; font-size: 11px; font-family: sans-serif; #bottom: 6px; #left: -2px;">'
						+ 'translations by </span><img src="http://lingro.com/images/logo-smaller2.png" style="#position: relative; vertical-align: baseline;" id="frn_l_s" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>'



		iframe_container.innerHTML = '<iframe id="frn_iframe" ' + source_url + style_string + other_args + '></iframe>'
						+ dragger + closer + informer;
	
		document.getElementById('frn_closer').onclick = function() {
			// document.getElementById('frn_informer').innerHTML = '';
        	iframe_container.innerHTML = '';
			lingro._current_clicked_element.className = lingro._current_element_class;
			lingro._current_clicked_element = false;
		}
		
		setTimeout('document.getElementById("frn_informer").style.display = "block"; lingro_misc.fixTransparentPNG(document.getElementById("frn_l_s"))', 1000);

		lingro._current_clicked_element = el;
    }


	return false;
}

/*
 * Takes a container element and rewrites its contents to be clickable.
 */

lingro._makeElementTextClickable = function(container_el)
{
	if (container_el.attributes['language'])
		dict = container_el.attributes['language'].value
	else if (container_el.attributes['dictionary'])
		dict = container_el.attributes['dictionary'].value;
	else 
		dict = ''; // explicit string
    
	text_obj = container_el.cloneNode(true);
    text = text_obj.innerHTML;
    text = text.replace(/\s*/, ' ');

    // Create a dummy element to hold the clickified stuff
    dummy_el = document.createElement('span');

	var textn = text;
	textn = textn.replace(/</g, 'LINGRO_MARK_X<').replace(/>/g, '>LINGRO_MARK_Y').split(/LINGRO_MARK_[XY]/)
	var recent_tag = ''
	
	var res_array = new Array();
	for (var i = 0; i < textn.length; i++) {
		var token = textn[i];

		// Add tags without changing
		if (/* token.match(/^\s*<(.*)>/) */ token.search('<') != -1 && token.search('>') != -1 ) {
			res_array.push(token);

			token.match(/^\s*<(.*)>/m);
			recent_tag = RegExp.$1.split(/\s/)[0];
		} else {
			// But rewrite text blocks

			// Split into words
			var items = token.split(/([\s\/])/);
			lingro._split_char = ' '; //RegExp.$1;

			// alert(items.join('|'));
			for (var j = 0; j < items.length; j++) {
				if (recent_tag == 'a' || recent_tag == 'script' || recent_tag == 'style' || recent_tag == '!--') {
					res_array.push(items[j]);
				}
				else if (items[j].match(/^\s*\/\s*$/)) {
					res_array.push('/');
				}
				else if (! items[j].match(/^\s*$/)) {
					(items[j].match(/\w/)) ? l = lingro._split_char : l = '';
					if (items[j].match(/^[<>\t \s\n\.,\-=_`~+)(!@#$%^&*?$'·{}\[\]\/";:\|]*$/m)) { 
						l = '';
					} else {
						// console.log(items[j]);
						l = lingro._split_char;
					}
					res_array.push(l + '<span class="f_api" onclick="lingro._translateWord(event, this, \'' + dict +'\')">' + items[j] + '</span>');
					// lingro._split_char = ' ';
				}
			}
		}

	}

	var res = res_array.join(' ');
	res = res.replace(/\s</g, '<');
	res = res.replace(/\s*\/\s*/mg, '/');

    container_el.innerHTML = res;
}


lingro._CSS = function() {
	return ''
	+ '.f_w_e'
	+ '{       '
	+ '	color: black !important;'
	+ '	cursor: pointer !important;'
	+ '	cursor: hand !important;'
	+ '	background-color: #fffad2 !important;'
	+ ''
	+ '	text-decoration: none !important;'
	+ '	display:inline !important;'
	+ '	float: none !important;'
	+ '	'
	+ '	border-color: #bbb !important;'
	+ '	border-style: solid !important;'
	+ '	border-width: 1px 2px 2px 1px !important;'
	+ ''
	+ '	padding-left: 1px !important;'
	+ '	'
	+ '	margin:0px !important;'
	+ '	margin-left: -2px !important;'
	+ '	margin-right: -2px !important;'
	+ ''
	+ '	padding: 0px !important;'
	+ '	border-left:   2px solid #eeeaaa !important;'
	+ '	border-top:    2px solid #eeeaaa !important;'
	+ '	border-right:  2px solid #cb7 !important;'
	+ '	border-bottom: 2px solid #cb7 !important;'
	+ '}'
	+ ''
	+ '.f_api {'
	+ 'display: inline !important;'
	+ 'padding: 0px !important;'
	+ 'margin: 0px !important;'
	+ ''
//	+ 'border-bottom: 1px dotted #a0c3ff;'
	+ 'cursor: pointer;'
	+ 'cursor: hand;'
	+ '}'
	+ '';
}



/*******************************************************************************/
/* Miscellaneous lingro functions                                              */
/*******************************************************************************/


/* Gets information about the browser dimensions and scroll and figures out
   where to put the tooltip. Sets the variables: tooltip_start_x, tooltip_start_y,
   as well as scrollx and scrolly. Also recalculates window size */
lingro_misc.calculateTooltipPosition = function(mousex, mousey)
{
	// If the topbar is shown, make sure that the tooltip will open close to the cursor
	// mousey -= parseInt(document.body.style.marginTop);

	lingro_misc.getWindowSize(); 

	lingro_misc.getScrollXY();

	lingro_misc.tooltip_start_x = mousex + lingro_misc.OFFSETX + lingro_misc.scrollx;
	lingro_misc.tooltip_start_y = mousey + lingro_misc.OFFSETY + lingro_misc.scrolly;


	/*document.getElementById("msg").innerHTML = 'mousex: ' + mousex + '<br>' +
	  'mousey: ' + mousey + '<br>' + 'tooltip_start_x: ' + tooltip_start_x +
	  '<br> tooltip_start_y: ' + tooltip_start_y + '<br>';

	  document.getElementById("browserx").innerHTML = browserx;
	  document.getElementById("browsery").innerHTML = browsery; REPORTING*/ 

	// if we'd have both vertical and horizontal scroll, pop up on the left top
	if ( (lingro_misc.tooltip_start_x + lingro_misc.TOOLTIP_WIDTH > lingro_misc.browserx + lingro_misc.scrollx - 20) &&
			(lingro_misc.tooltip_start_y + lingro_misc.TOOLTIP_HEIGHT > lingro_misc.browsery + lingro_misc.scrolly - 20) )
	{
		lingro_misc.tooltip_start_x = mousex + lingro_misc.scrollx - lingro_misc.TOOLTIP_WIDTH - 20;	
		lingro_misc.tooltip_start_y = mousey + lingro_misc.scrolly - lingro_misc.TOOLTIP_HEIGHT - 20;	
	}

	if (lingro_misc.tooltip_start_x + lingro_misc.TOOLTIP_WIDTH > lingro_misc.browserx + lingro_misc.scrollx - 20) // avoid horizontal scroll
	{
		lingro_misc.tooltip_start_x = lingro_misc.browserx + lingro_misc.scrollx - lingro_misc.TOOLTIP_WIDTH - 20;
	}

	if (lingro_misc.tooltip_start_y + lingro_misc.TOOLTIP_HEIGHT > lingro_misc.browsery + lingro_misc.scrolly - 20) // avoid vertical scroll
	{
		lingro_misc.tooltip_start_y = lingro_misc.browsery + lingro_misc.scrolly - lingro_misc.TOOLTIP_HEIGHT - 20;
	}

	if (lingro_misc.tooltip_start_x < 0)
	{
		lingro_misc.tooltip_start_x = 0;
	}

	if (lingro_misc.tooltip_start_y < 0)
	{
		lingro_misc.tooltip_start_y = 0;
	}
}

// Gets the size of the browser window and fills browserx and browsery (global)
// with correct information
lingro_misc.getWindowSize = function()
{
	if( typeof(window.innerWidth) == 'number' )
	{
		//Non-IE
		lingro_misc.browserx = window.innerWidth;
		lingro_misc.browsery = window.innerHeight;
	}
	else if ( document.documentElement &&
			(document.documentElement.clientWidth || document.documentElement.clientHeight) )
	{
		//IE 6+ in standards compliant mode
		lingro_misc.browserx = document.documentElement.clientWidth;
		lingro_misc.browsery = document.documentElement.clientHeight;
	}
	else if ( document.body && (document.body.clientWidth || document.body.clientHeight) )
	{
		//IE 4 compatible
		lingro_misc.browserx = document.body.clientWidth;
		lingro_misc.browsery = document.body.clientHeight;
	}

	if(typeof(frn_ajax_dict) != 'undefined')
		frn_ajax_dict.resizeForSafari();
}

/* Get the current value of the web browser window scroll and populate scrollx and scrolly */
lingro_misc.getScrollXY = function()
{
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement &&
			( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}

	lingro_misc.scrollx = scrOfX;
	lingro_misc.scrolly = scrOfY;
	return;
}

lingro_misc.getElementsByClassName = function(oElm, strTagName, strClassName)
{   
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;

    for(var i=0; i<arrElements.length; i++)
    {   
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className))
        {   
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements);
}

lingro_misc.getFirstElementByClassName = function(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;

	for(var i=0; i<arrElements.length; i++)
	{
		oElement = arrElements[i];      
		if(oRegExp.test(oElement.className))
		{
			return oElement;
		}   
	}
	
	return null; // (arrReturnElements);
}

/*******************************************************************************/
/* Tooltip dragging functions                                                  */
/*******************************************************************************/

/*
<!--************************************************************************-->
<!--* Taken from Generic Drag Demo code                                    *-->

"may be used, redistributed and/or modified
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 2 of the License or (at your option)
any later version."

<!--*                                                                      *-->
<!--* Copyright 2001 by Mike Hall                                          *-->
<!--************************************************************************-->
*/

lingro_misc.explicit_copyright_notice = "Dragging code copyright 2001 by Mike Hall (GPL v2)";

lingro_misc.Browser = function()
{

	var ua, s, i;

	this.isIE    = false;
	this.isNS    = false;
	this.version = null;

	ua = navigator.userAgent;

	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	// Treat any other "Gecko" browser as NS 6.1.

	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}

lingro_misc._browser = new lingro_misc.Browser();

lingro_misc._dragStart = function(event, id)
{
	var el;
	var x, y;

	// If an element id was given, find it. Otherwise use the element being
	// clicked on.

	if (id)
		lingro_misc._dragObj.elNode = document.getElementById(id);
	else
	{
		if (lingro_misc._browser.isIE)
			lingro_misc._dragObj.elNode = window.event.srcElement;
		if (lingro_misc._browser.isNS)
			lingro_misc._dragObj.elNode = event.target;

		// If this is a text node, use its parent element.

		if (lingro_misc._dragObj.elNode.nodeType == 3)
			lingro_misc._dragObj.elNode = lingro_misc._dragObj.elNode.parentNode;
	}

	// Get cursor position with respect to the page.

	if (lingro_misc._browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
	}
	if (lingro_misc._browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	// Save starting positions of cursor and element.

	lingro_misc._dragObj.cursorStartX = x;
	lingro_misc._dragObj.cursorStartY = y;
	lingro_misc._dragObj.elStartLeft  = parseInt(lingro_misc._dragObj.elNode.style.left, 10);
	lingro_misc._dragObj.elStartTop   = parseInt(lingro_misc._dragObj.elNode.style.top,  10);

	// Save starting position of dragger
	var dragger = document.getElementById('frn_dragger');
	lingro_misc._dragObj.draggerStartLeft  = parseInt(dragger.style.left, 10);
	lingro_misc._dragObj.draggerStartTop   = parseInt(dragger.style.top,  10);
	
	var closer = document.getElementById('frn_closer');
	lingro_misc._dragObj.closerStartLeft  = parseInt(closer.style.left, 10);
	lingro_misc._dragObj.closerStartTop   = parseInt(closer.style.top,  10);
	
	var informer = document.getElementById('frn_informer');
	lingro_misc._dragObj.informerStartLeft  = parseInt(informer.style.left, 10);
	lingro_misc._dragObj.informerStartTop   = parseInt(informer.style.top,  10);


	if (isNaN(lingro_misc._dragObj.elStartLeft)) lingro_misc._dragObj.elStartLeft = 0;
	if (isNaN(lingro_misc._dragObj.elStartTop))  lingro_misc._dragObj.elStartTop  = 0;
	
	if (isNaN(lingro_misc._dragObj.draggerStartLeft)) lingro_misc._dragObj.draggerStartLeft = 0;
	if (isNaN(lingro_misc._dragObj.draggerStartTop))  lingro_misc._dragObj.draggerStartTop  = 0;
	
	if (isNaN(lingro_misc._dragObj.closerStartLeft)) lingro_misc._dragObj.closerStartLeft = 0;
	if (isNaN(lingro_misc._dragObj.closerStartTop))  lingro_misc._dragObj.closerStartTop  = 0;
	
	if (isNaN(lingro_misc._dragObj.informerStartLeft)) lingro_misc._dragObj.informerStartLeft = 0;
	if (isNaN(lingro_misc._dragObj.informerStartTop))  lingro_misc._dragObj.informerStartTop  = 0;

	// Update element's z-index.

	lingro_misc._dragObj.elNode.style.zIndex = ++lingro_misc._dragObj.zIndex;
	informer.style.zIndex = lingro_misc._dragObj.zIndex;
	dragger.style.zIndex = lingro_misc._dragObj.zIndex;
	closer.style.zIndex = lingro_misc._dragObj.zIndex;

	// Capture mousemove and mouseup events on the page.

	if (lingro_misc._browser.isIE) {
		document.attachEvent("onmousemove", lingro_misc._dragGo);
		document.attachEvent("onmouseup",   lingro_misc._dragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (lingro_misc._browser.isNS) {
		document.addEventListener("mousemove", lingro_misc._dragGo,   true);
		document.addEventListener("mouseup",   lingro_misc._dragStop, true);
		event.preventDefault();
	}
}

lingro_misc._dragGo = function(event) {

	var x, y;

	// Get cursor position with respect to the page.

	if (lingro_misc._browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
	}
	if (lingro_misc._browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	// Move drag element by the same amount the cursor has moved.

	lingro_misc._dragObj.elNode.style.left = (lingro_misc._dragObj.elStartLeft + x - lingro_misc._dragObj.cursorStartX) + "px";
	lingro_misc._dragObj.elNode.style.top  = (lingro_misc._dragObj.elStartTop  + y - lingro_misc._dragObj.cursorStartY) + "px";

	// do the same for the 'frn_dragger' element
	var dragger = document.getElementById('frn_dragger');
	dragger.style.left = (lingro_misc._dragObj.draggerStartLeft + x - lingro_misc._dragObj.cursorStartX) + "px";
	dragger.style.top  = (lingro_misc._dragObj.draggerStartTop  + y - lingro_misc._dragObj.cursorStartY) + "px";
	
	var closer = document.getElementById('frn_closer');
	closer.style.left = (lingro_misc._dragObj.closerStartLeft + x - lingro_misc._dragObj.cursorStartX) + "px";
	closer.style.top  = (lingro_misc._dragObj.closerStartTop  + y - lingro_misc._dragObj.cursorStartY) + "px";
	
	var informer = document.getElementById('frn_informer');
	informer.style.left = (lingro_misc._dragObj.informerStartLeft + x - lingro_misc._dragObj.cursorStartX) + "px";
	informer.style.top  = (lingro_misc._dragObj.informerStartTop  + y - lingro_misc._dragObj.cursorStartY) + "px";


	if (lingro_misc._browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (lingro_misc._browser.isNS)
		event.preventDefault();
}

lingro_misc._dragStop = function(event) {

	// Stop capturing mousemove and mouseup events.

	if (lingro_misc._browser.isIE) {
		document.detachEvent("onmousemove", lingro_misc._dragGo);
		document.detachEvent("onmouseup",   lingro_misc._dragStop);
	}
	if (lingro_misc._browser.isNS) {
		document.removeEventListener("mousemove", lingro_misc._dragGo,   true);
		document.removeEventListener("mouseup",   lingro_misc._dragStop, true);
	}
}

lingro_misc.fixTransparentPNG = function(img)
{
    if (navigator.appVersion.search(/MSIE/) == -1) {
        return;
    }

    if (img.id && img.id == 'frn_menu4_image' && img.src && img.src.match(/eng/)) {
        img.height = 22;
        img.width = 18;
    }
    
    var imgName = '';
    if (img.src) {
        imgName = img.src.toUpperCase();
    }

    if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
    {   
        // save the previous onclick handler
        var onclickhandler; // = img.onclick + "";
        if (img.onclick)
        {   
            onclickhandler = img.onclick.toString();
            onclickhandler = onclickhandler.replace(/\n/g, '');
            onclickhandler = onclickhandler.replace(/function.*?\(\)\s*{/, '');
            onclickhandler = onclickhandler.replace(/}\s*$/, '');
        } else {
            onclickhandler = "return false";
        }
        
        /*
        if (onclickhandler)
            alert(onclickhandler);
        */

        var imgID = (img.id) ? "id='" + img.id + "' " : ""
        var imgClass = (img.className) ? "class='" + img.className + "' " : ""
        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
        var imgStyle = "display:inline-block;" + img.style.cssText
        if (img.align == "left") imgStyle = "float:left;" + imgStyle
        if (img.align == "right") imgStyle = "float:right;" + imgStyle
        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle

        var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " onclick=\"" + onclickhandler + "\" style=\"" + "width:" + img.width + "px; "
            + " height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
        img.outerHTML = strNewHTML;
    }


}

lingro_misc.addOnloadEvent = function(new_func)
{
	var original_onload = window.onload;

	if (typeof window.onload != 'function') {
		window.onload = new_func;
	} else {
		window.onload = function() {
			if (original_onload) { original_onload(); }
			new_func();
		}
	}
}


/*****************************************************************************/

lingro._go = function() {
	//alert('pies');

	if (document.body) {
		if (typeof(lingro_light_bookmarklet) != "undefined") {
			document.body.innerHTML = '<span class="lingro_container" style="margin: 0px !important; padding: 0px !important">'
				+ document.body.innerHTML + '</span>';
			document.body.innerHTML += '<style>' + lingro._CSS() + '</style>';
		} else {
			// document.write(lingro._writeCSS());
//			var x = document.createElement('style');
//			x.outerHTML = lingro._CSS();
//			x.innerHTML = lingro._CSS();
//			document.body.appendChild(x);
//			if(navigator.userAgent.match(/Safari/))
//				document.write('<style>' + lingro._CSS() + '</style>');
//			else
//				document.body.innerHTML += '<style>' + lingro._CSS() + '</style>';
		}

		// document.body.innerHTML += lingro._writeCSS();

		lingro._makeWordsClickable();
		lingro._makeContainersClickable();
	} else {
		setTimeout(function() { lingro._go() }, 500);
	}

}

lingro.onJSLoad = function() {
	if(typeof(lingro_is_extension) == "undefined" || !lingro_is_extension) {
		if (typeof(lingro_light_bookmarklet) == "undefined") {
			document.write('<style>' + lingro._CSS() + '</style>');
		}

		window.onload = function() { lingro._go(); }

		if (typeof(lingro_light_bookmarklet) != "undefined")
		{
			lingro._go();
		}

	}
}




lingro.translateWordFromExtension = function(word, dict, x, y) {


	// Get the container, creating if necessary
	var iframe_container = document.getElementById('frn_iframe_container');

	if (!iframe_container) {
		iframe_container = document.createElement('div');
		iframe_container.id = 'frn_iframe_container';
		document.body.appendChild(iframe_container);
	}

	var dictionary = '';
	var language = '';

	// Figure out the dictionary to use
	if (dict && dict.match(/[a-z]{2,3}_[a-z]{2,3}/)) {
		dictionary = dict;
	} else if (dict && dict.match(/[a-z]{2,3}/)) {
		language = dict;
	} else {
		dictionary = lingro.dictionary;
		language = lingro.language;
	}

	if (!dictionary && language) {
		dictionary = language + '_eng';
	}


	height = 170;
	width = 305;


	var style_string = " style=\"position: absolute; top: " + (y + 5) + 'px; left: ' + (x + 2) + "px;";
	style_string += " width: " + width + "px; height: " + height + "px; overflow: hidden;"
		+ " background: none; border: 0px black solid; z-index: 99999\" ";

	var other_args = ' border="0" allowtransparency="true" ';

	//	var word = el.innerHTML.replace(/^\s+|\s*$|[ .;?|!:\t\n,'"”“]/, '');
	word = word.replace(/^\s*/, '').replace(/\s*$/, '');
	word = escape(word);

	var source_url = ' src="http://lingro.com/widgets/tooltipWord?word=' + word + '&onetime_dictionary=' + dictionary
		+ '&url=' + window.location.href + '"';

	var dragger =  '<div id="frn_dragger" onmousedown="lingro_misc._dragStart(event, \'frn_iframe\');" style="position: absolute; top: '
		+ (y + 5 + 4) + 'px; left: ' + (x + 2 + 80) + 'px; width: 190px; height: 23px; '
		+ 'background-color: none; cursor: pointer; cursor: hand; z-index: 99999">'
		+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
		+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
		+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';

	var closer =  '<div id="frn_closer" style="position: absolute; top: ' + (y + 5 + 4) + 'px; left: '
		+ (x + 4 + 275) + 'px; width: 20px; height: 20px; cursor: pointer; cursor: hand; '
		+ ' background-image: url(http://lingro.com/images/tooltip_close.png) no-repeat; z-index: 99999">'
		+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';

	var informer = '<div onclick="var frn_nw = window.open(\'http://lingro.com\', \'_blank\'); frn_nw.focus();" id="frn_informer" style="position: absolute; top: ' + (y + 5 + 8 + 127) + 'px; left: '
		+ (x + 4 + 215 - 74) + 'px; height: 20px; cursor: pointer; cursor: hand; '
		+ ' z-index: 99999; display: none;"><span style="position: relative; bottom: 8px; color: #fff; font-size: 11px; font-family: sans-serif; #bottom: 6px; #left: -2px;">'
		+ 'translations by </span><img src="http://lingro.com/images/logo-smaller2.png" style="#position: relative; vertical-align: baseline;" id="frn_l_s" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>'



		iframe_container.innerHTML = '<iframe id="frn_iframe" ' + source_url + style_string + other_args + '></iframe>'
		+ dragger + closer + informer;

	document.getElementById('frn_closer').onclick = function() {
		iframe_container.innerHTML = '';
		lingro._current_clicked_element.className = lingro._current_element_class;
		lingro._current_clicked_element = false;
	}

	setTimeout('document.getElementById("frn_informer").style.display = "block"; lingro_misc.fixTransparentPNG(document.getElementById("frn_l_s"))', 1000);



	return false;
}


lingro.onJSLoad();

