﻿/**
 * @author mark
 * date 2008/12/19
 * Import: DropthingsUI, Sys, jQuery
 * usage: new Widgets.Behaviors.UI.Speak({ widget: [object], string: [string] });
 */

Type.registerNamespace('Widgets.Behaviors.UI');
Widgets.Behaviors.UI.Speak = function() { this.initialize.apply(this, arguments); };
Widgets.Behaviors.UI.Speak.prototype = {
    initialize: function(args) {
        $.getScript("http://www.ueeeu.com.sg/js/swfobject.js");
    },

    onInit: function() {
        var thisObj = this;
        var elt = this.getWidget().element;

        var activeEvent = function() {
            if ($(this).css("background-image").indexOf("close") == -1) {
                $(this).css("background-image", "url(/img/icon_voice_close.png)");
                thisObj.speak();
            } else {
                $(this).css("background-image", "url(/img/icon_voice.png)");
                thisObj.stop();
            }
        }

        $("<a class='speak_icon' href='javascript:void(0);' style='*margin-left: -5px'>&nbsp;</a>")
            .css("background", "url(/img/icon_voice.png) no-repeat center center")
            .css("padding", "8px")
            .click(activeEvent)
            .appendTo($(elt).find(".widget_title"));
    },

    speak: function() {
        if (!this.getState().enabled)
            return;
        $("#doll").show();

        var words = this.getSelectionText();
        if (words == null || words == "")
            words = $("div.widget_body > div", this.getWidget().element).text();
        this.doSpeaking(words);
    },

    stop: function() {
        $("#doll").remove();
    },

    doSpeaking: function(words) {
        if (words == null || words == "")
            return;

        var thisObj = this;
        Dropthings.Web.Framework.WidgetDataService.GetVoiceUrl(words, function(data) {
            if ($(".speak_icon", thisObj.getWidget().element).css("background-image").indexOf("close") == -1)
                return;

            eval("var jsonData = " + data + ";");
            if ($("#doll_body").length == 0) {
                $("<div id='doll_body'></div>").appendTo("#header");
            }

            var so = new SWFObject('/img/MrGreen.swf', "doll", '100', '100', "9", "#ffffff");
            so.addParam("allowScriptAccess", "sameDomain");
            so.addParam("allowFullScreen", "false");
            so.addParam("FlashVars", 'filename=' + jsonData.url);
            so.addParam("quality", "false");
            so.addParam("allowFullScreen", "high");
            so.addParam("wmode", "transparent");
            so.addParam("movie", '/img/MrGreen.swf');
            so.write("doll_body");

            $("#doll_body").css("float", "right");
            $("#doll_body").css("margin-top", "-80px");
        });
    },

    getSelectionText: function() {
        if (window.getSelection)
            return window.getSelection().toString();
        else if (document.selection && document.selection.createRange)
            return document.selection.createRange().text;
        else
            return '';
    },

    getWidget: function() { throw new Error("No widget instance"); },

    getState: function() { throw new Error("No state instance"); }
};