// 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
//

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").text();
        //var gb = $(e).find(".ForumPostBodyArea").find("#googlebranding").text();
        var gb = $(e).find(".ForumPostBodyArea").find("#googlebranding");

        //alert("translating" + title + "--" + body);

        $.post("/translate.aspx",
        { sourceID: this.options.sourceID, destID: this.options.destID, MsgBody: title },
                        function(data) {
            //alert(data);
            var tt = eval( "(" + data + ")" );
            (e).find(".ForumPostBodyArea").find(".ForumPostTranslatedTitle").text(tt.responseData.translatedText);
        }
        
        );
        /* 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 + ")" );
            (e).find(".ForumPostBodyArea").find(".ForumPostTranslatedText").text(tb.responseData.translatedText);
        }
                );
        //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);
