/*
	Suggestion Engine API 
	Date : Dec 28, 2009
*/

var latest_se_call = ''; //Used to make sure only the latest suggestion is used

/**
 * This function fetches suggestions based on the passed in src_text
 * SideNote: SE stands for "Suggestion Engine"
 *
 * @param text src_text - The text for which to fetch suggestions
 * @param text src_lang - The language of src_text
 * @param text tar_lang - The target language for which the suggestions translation exists
 * @param text number_of_results - Number of suggestions to fetch, max 4
 * @param text the_win -
 */
function SE(src_text, src_lang, tar_lang, number_of_results, the_win, the_obj, txt_obj) {

    this.src_text = src_text;
    this.src_lang = src_lang;
    this.tar_lang = tar_lang;
    this.num_res  = number_of_results;

    var sePath= ChatConfig.SUGGESTION_ENGINE_URL + "?input=" + src_text + "&lang=" + src_lang + "&tlng=" + tar_lang + "&noi=" + number_of_results;

    if($.trim(src_text) == "") {
        $(document).trigger("SE_EMPTYTEXT");
        return NULL;
    }

    latest_se_call = src_text;

    $.ajax({
        type: "GET",
        url: sePath,
        dataType : "json",
        timeout : ChatConfig.SE_TIMEOUT,
        success: function(data){
            if(src_text != latest_se_call) return;

            if(!data)
                $(document).trigger("SE_INVALIDJSONFEED");
            else
                $(document).trigger("SE_FOUND", [data, the_win, the_obj, txt_obj]);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown){$(document).trigger("SE_INVALIDJSONFEED");}
    });

    return this;
}

//Used for translating suggestions
function QuickTranslate(input, parameters) {

    if(!input || !parameters) return false;

    var sePath= ChatConfig.QUICK_TRANSLATE_URL //+ "?id=" + suggest_id + "&fromlang=" + from_lang + "&tolang=" + target_lang + "&input=" + suggestion_text;
    var params = {input : input, src_lang : parameters.src_lang, target_lang : parameters.target_lang, mid : parameters.mid, rootid : parameters.rootid, relatedid : parameters.relatedid , predo : 'quick'};

    /*for(var i in parameters){
        if(i == 'from' || i == 'to') continue;
        params[i] = parameters[i];
    }*/

    $.ajax({
       type: "POST",
       url: sePath,
       data : params,
       dataType : "json",
       timeout : ChatConfig.QT_TIMEOUT,
       success: function(data){
            try{
                if(data.message !=undefined)
                    Logger.log('TE.QuickTranslate : ['+data.type.toUpperCase()+']', '"'+data.message+'"',data);
                else
                    Logger.log('TE.QuickTranslate : ['+data.type.toUpperCase()+']','"'+data.input+'"','>','"'+data.output+'"',data);
            }catch(e){}

            if(!data){
                $(document).trigger("SE_INVALIDJSONFEED");
                return;
            }

            data.mid = parameters.mid;
            data.src_lang = parameters.src_lang;
            data.target_lang = parameters.target_lang;
            data.jid = parameters.to;
            data.input = input;

            if(data.type && data.type == "error")
                $(document).trigger("translation_failed", [data]);
            else
                $(document).trigger("translation_recieved", [data]);
       },
       error : function(XMLHttpRequest, textStatus, errorThrown){
           params.jid = parameters.to;
           try{ Logger.log('TE.QuickTranslate : error ',params,textStatus, errorThrown); }catch(e){}
           $(document).trigger("translation_failed", [params]);
       }
     });

    return this;
}
