/**
 * Google Translate jQuery plugin
 * 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * @author     Shawn Mayzes (http://www.mayzes.org/)
 * @copyright  2011 Shawn Mayzes 
 * @license    http://www.gnu.org/licenses/gpl-3.0.html
 * @version    2.0
 * @link       http://www.superawesomejquery.com/
 */
test_source = 'fikon';
(function($) {
    var defaults = { google : window.google }; 
 
    $.handleError = function(err) {
        if ( window.console ) {
            console.log(err);        	
        } else {
            alert('Error: '+err);
        }
        return false;
    };

    $.translate = function(element, options) {
        if ( typeof options.target == 'undefined' ) {
			// modified by Lukas
			options.target = $(element);
        }

        if ( typeof options.targetLang == 'undefined' ) {
            $.handleError('You need to set a target language for the text to be translated into.');
            return false;
        }

        if ( typeof options.apiKey == 'undefined' ) {
            $.handleError('You need to set a valid Google API key.');
            return false;
        }

        var input = $(element).html().replace(/&/g,'%26');
    	var outputElement = options.target;

		var source = 'https://www.googleapis.com/language/translate/v2'	+ '?key=' + options.apiKey 
		if ( options.sourceLang ){
			source += '&source=' + options.sourceLang;
		}
		source += '&target=' + options.targetLang
			+ '&q=' + input
			+ '&output=json'
			+ '&callback=?';

		test_source = source;
		$.getJSON(source, function(result) {
			alert(typeof(result.data.translations));
			if(typeof(result) == 'object'){
				var output = result.data.translations[0].translatedText;
				$(outputElement).html(output);
			}
		});
    };

    $.fn.googleTranslate = function(options, callback) {
        var options = $.extend({}, $.defaults, options);

	    return this.each(function() {
		    new $.translate(this, options);
        });
    };

})(jQuery);


