// copyright © ePals 2008
// author: dave moore
// 
// this script translates forum posts.  it should be called on a jquery
// instance.  each post must have elements of the following classes:
//
// .ForumPostTitle - the title
// .ForumPostContentText - the message body
// .ForumPostTranslatedTitle - placeholder for the translated title
// .ForumPostTranslatedText - placeholder for the translated message body
//

var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}


function shaz(result) {
    alert(result.translation)
}

(function($) {
    $.fn.translate = function(o) {
        return new $translate(this, o);
    };

    var defaults = {
        sourceID: 'EN',
        destID: 'ES',
        MsgBody: ''
    };

    $.translate = function(e, o) {
        this.options = $.extend({}, defaults, o || {});

        // translate the title

        var title = $(e).find(".ForumPostTitleArea").find(".ForumPostTitle").text();
        var body = $(e).find(".ForumPostBodyArea").find(".ForumPostContentText").html();
//alert(body);
body = body.replace(/<\/p>/ig,"<\/p>\n"); 
body = body.replace(/<br>/ig,"<br>\n"); 
//alert(body);
body = body.replace(/(<([^>]+)>)/ig,""); 
var temp_div = document.createElement('div');
temp_div.innerHTML = body;
body = temp_div.firstChild.nodeValue;
//alert(body);
        //body = $("<div>" + body + "</div>").html();
body = body.replace(/^\s+/, "");
body = body.replace(/\s+$/, "");
//alert(body);
        title = $("<div>" + title + "</div>").html();
        //var gb = $(e).find(".ForumPostBodyArea").find("#googlebranding").text();
        var gb = $(e).find(".ForumPostBodyArea").find("#googlebranding");

        //alert("translating" + title + "--" + body);
var mylang = this.options.destID;
        $.post("/translate.aspx",
        { sourceID: this.options.sourceID, destID: this.options.destID, MsgBody: title },
                        function(data) {
            //alert(data);
            var tt = eval( "(" + data + ")" );
//alert(tt.responseData.translatedText);
var ts = $("<div>" + tt.responseData.translatedText + "</div>").html();
            (e).find(".ForumPostBodyArea").find(".ForumPostTranslatedTitle").text(ts);
if (mylang == "ar") {
 (e).find(".ForumPostBodyArea").find(".ForumPostTranslatedTitle").wrapInner("<div dir=\"rtl\" style=\"text-align: right\"></div>");
}
        }
        
        );
        /* google.language.translate(title, this.options.SourceID, this.options.destID, function(translateResult) {
            (e).find(".ForumPostBodyArea").find(".ForumPostTranslatedTitle").text(translateResult.translation);
        });
        */
        //        alert(this.options.MsgBody);
        // translate the body
        //        this.options.MsgBody = escape($(e).find(".ForumPostContentText").text());

        $.post("/translate.aspx",
        { sourceID: this.options.sourceID, destID: this.options.destID, MsgBody: body },
                        function(data) {
            var tb = eval( "(" + data + ")" );
var ts = $("<div>" + tb.responseData.translatedText + "</div>").html();
ts = ts.replace(/\n/g, "<br>");
//alert(tb.responseData.translatedText);
//alert(ts);
            (e).find(".ForumPostBodyArea").find(".ForumPostTranslatedText").html(ts);
if (mylang =="ar") {
 (e).find(".ForumPostBodyArea").find(".ForumPostTranslatedText").wrapInner("<div dir=\"rtl\" style=\"text-align: right\"></div>");
}
        }
                );
        //document.domain = oldDom;
       google.language.getBranding(gb);
    };

    var $translate = $.translate;

    $translate.fn = $translate.prototype = {
        translate: '1.0.0'
    };
    $translate.fn.extend = $translate.extend = $.extend;
})(jQuery);

