var Translator = (function($) {
  'use strict';

  var selected, selectedText;
  
  function init()
  {
	var html = '<div id="translator" style="position: fixed; left: 0px; bottom: 0px; border: solid 1px #000; border-radius: 0px 5px 5px 0px; text-align: center">';
	html += '<div class="translatorttile" style="background: #ccc; padding: 5px 0px;"><h3>Translator</h3></div>';
	html += '<div class="translatorcapture" style="width: 100%; padding: 5px 0px; background: green; cursor: hand" onclick="Translator.enableCapture()">'+ text_capture +'</div>';
	html += '</div>';
	$("body").append(html);
  }
  
  function enableCapture()
  {
	$("#translator .translatorcapture").attr("onclick", "Translator.disableCapture()").text(text_stop).css({background: "red"});
	$(document).on("click", "#container, #footer, #powered", function(e)
	{
		selected = e.target;
		if($(selected).hasClass("translatorcapture"))
		{
			return false;
		}

		//selectedText = $(selected).text();
		selectedText = $(selected).text();
		e.preventDefault();
		e.stopPropagation();
		if($.trim(selectedText) != "")
		{
			_getInfo();
		}
		
		return false;
	});
  }

  function disableCapture()
  {
	$("#translator .translatorcapture").attr("onclick", "Translator.enableCapture()").text(text_capture).css({background: "green"});
	$(document).off("click", "#container, #footer, #powered");
  }
  
  function _getInfo(json)
  {
	disableCapture();
	if(json === undefined)
	{
		$.colorbox({href: url_catalog+'image/loading_translator.gif', overlayClose: false, escKey: false, onClosed: function()
		{
			Translator.enableCapture();
		}});
		send('_getInfo', "find="+selectedText);
		return false;
	}

	var html = '<form id="translatorform">';
	html 	+= '<table>';
	html 		+= '<tr>';
	html 			+= '<td>'+ text_text +'</td>';
	html 			+= '<td><textarea rows="5" cols="30" name="text_from">'+ selectedText +'</textarea></td>';
	html 		+= '<tr/>';
	html 	+= '<tr>';
	html 		+= '<td>'+ text_translator_to +'</td>';
	html 		+= '<td>';
	html 			+= '<textarea rows="5" cols="30" name="text_destination"></textarea>';
	html 			+= '<br />';	
	html 			+= text_of+' ';
	html 			+= '<select name="lang_from">';
					$.each(codes_lang, function(key)
					{
						html 	+= '<option value="'+ this +'">'+ key +'</option>';	
					});
	html 			+= '</select>';
	html 			+= ' '+ text_to +' ';
	html 			+= '<select name="lang_destination">';
					$.each(codes_lang, function(key)
					{
						html 	+= '<option value="'+ this +'">'+ key +'</option>';	
					});
	html 			+= '</select>';
	html 			+= ' <a href="javascript:;" class="button" onclick="Translator.get_translator();">'+ text_get_translation +'</a> ';
	html 		+= '</td>';
	html 	+= '<tr/>';
	html 	+= '<tr>';
	html 		+= '<td>'+ text_files +'</td>';
	html 		+= '<td>';
	$.each(json.result, function()
	{
		html 		+= '<input type="checkbox" name="files[]" value="'+this.file +'" /> '+this.file +', linha: '+ this.line +'<br />';
	});
	html 		+= '</td>';
	html 	+= '<tr/>';	
	html += '</table>';
	html += '<center>';
	html 	+= '<br />';
	html 	+= '<a href="javascript:;" class="button" onclick="Translator.send(\'translator_status\', $(\'#translatorform\').serialize());">'+ text_translation +'</a>';
	html += '</center>';
	html += '</form>';
	$.colorbox({html: html, overlayClose: false, escKey: false, onClosed: function()
	{
		Translator.enableCapture();
	}});
  }

	function get_translator(text)
	{
		var _text_from = $("#translatorform textarea[name='text_from']").val();
		var _text_destination = $("#translatorform textarea[name='text_destination']");
		var lang_from = $("#translatorform select[name='lang_from']").val();
		var lang_destination = $("#translatorform select[name='lang_destination']").val();
		var url_mymemory = 'http://mymemory.translated.net/api/get?q='+ _text_from +'&langpair='+ lang_from +'|'+ lang_destination +'&de='+mymemory_email;
		var width_colorbox = $("#colorbox").width();
		var height_colorbox = $("#colorbox").height();

		$.ajax(
		{
			url: url_mymemory,
			dataType: 'json',
			beforeSend: function()
			{
				alert(text_wait);
				$("#colorbox").width(0).height(0);
			},
			success: function(json)
			{
				_text_destination.val(json.responseData.translatedText);
				$("#colorbox").width(width_colorbox).height(height_colorbox);
			},
			error: function(xhr, ajaxOptions, thrownError)
			{
				alert(error_get_translation);
				$("#colorbox").width(width_colorbox).height(height_colorbox);
			}
		});
	}

  function translator_status(json)
  {
	$.colorbox({html: text_success_translation, overlayClose: false, escKey: false, onClosed: function()
	{
		Translator.enableCapture();
	}});
	
	setTimeout(function()
	{
		$.colorbox.close();
	}, 2000);
  }

  function send(callback, data)
  {
	$.ajax(
	{
		url: 'index.php?route=common/home/translator&token='+token,
		data: data,
		type: 'post',
		dataType: 'json',
		success: function(json)
		{
			if(json.success)
			{
				eval(callback)(json);
			}
			else
			{
				//Translator.enableCapture();
				var errors = "";
				$.each(json.errors, function()
				{
					errors += this+"<br />";
				});

				$.colorbox({html: errors, overlayClose: false, escKey: false, onClosed: function()
				{
					Translator.enableCapture();
				}});
				
				setTimeout(function()
				{
					$.colorbox.close();
				}, 2000);
			}
		},
		error: function(xhr, ajaxOptions, thrownError)
		{
			//Translator.enableCapture();
			$.colorbox.close();
		}
	});
	
	return false;
  }

  return {
    init: init,
    enableCapture: enableCapture,
    disableCapture: disableCapture,
	send: send,
	get_translator: get_translator,
    selected: selected
  };
}(jQuery));

$(function()
{
	Translator.init();
});