﻿MCL.Voice = {
    isRead: 0,
    errcount: 0,
    Init: function() {
        for (var i = 0; i < $(".ttsfemale").length; i++) {
            var temp = $(".ttsfemale").eq(i).html();
            var html = temp + "<img src=\"" + HostDefault + "/images/201006/ico/sound_16x16.gif\" width=\"16\" height=\"16\" style='cursor:pointer' onclick=\"MCL.Voice.GetSpeech('" + encodeURIComponent(temp.replace(new RegExp("&nbsp;", "ig"), "").replace(new RegExp(" ", "ig"), "")) + "',0,false)\"/> ";
            $(".ttsfemale").eq(i).html(html)
        }
        for (var i = 0; i < $(".ttsmale").length; i++) {
            var temp = $(".ttsmale").eq(i).html().replace("&nbsp;", "");
            var html = temp + "<img src=\"" + HostDefault + "/images/201006/ico/sound_16x16.gif\" width=\"16\" height=\"16\" style='cursor:pointer' onclick=\"MCL.Voice.GetSpeech('" + encodeURIComponent(temp.replace(new RegExp("&nbsp;", "ig"), "").replace(new RegExp(" ", "ig"), "")) + "',1,false)\"/> ";
            $(".ttsmale").eq(i).html(html)
        }
    },
    GetSpeech: function(speech, type, isLogin) {
        if (typeof isLogin == 'undefined') {
            isLogin = true;
        }
        if (isLogin) {
            var indexspeech = $(".chinese").eq(0).html().replace(new RegExp("<(.[^><]*)>", "ig"), "");
            var indexspeech1 = $(".chinese").eq(1).html().replace(new RegExp("<(.[^><]*)>", "ig"), "");
            if (encodeURIComponent(indexspeech) != speech && encodeURIComponent(indexspeech1) != speech) {
                if (MCL.Account.StudentID == 0) {
                    MCL.Account.Login();
                    return;
                }
            }
        }
        this.initSwf();
        var stype = 0;
        if (typeof type == 'number') {
            stype = type;
        }


        $.getScript(HostVoice + "/VoinceService.aspx?speech=" + speech + "&type=" + stype,
        function() {
            MCL.Voice.errcount = MCL.Voice.errcount + 1;

            if (typeof (sVoince) != 'undefined') {
                if (sVoince.File == "") {
                    if (MCL.Voice.errcount < 4) {
                        window.setTimeout("MCL.Voice.GetSpeech('" + sVoince.Speech + "'," + sVoince.Type + ",false)", 5000);
                    }
                    else {
                        MCL.Voice.errcount = 0;
                        alert("please try later");
                    }
                }
                else {
                    MCL.Voice.asplay(HostVoice + "/media/" + sVoince.File);
                    MCL.Voice.errcount = 0;
                }

            }
            else {
                if (typeof (sVoinceErr) != 'undefined') {
                    alert(sVoinceErr);
                }
            }
            MCL.Voice.isRead = 0;
        }
          );

    },
    getFlashObject: function(movieName) {
        if (window.document[movieName]) {
            return window.document[movieName];
        }
        if (navigator.appName.indexOf("Microsoft Internet") == -1) {
            if (document.embeds && document.embeds[movieName])
                return document.embeds[movieName];
        }
        else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
        {
            return document.getElementById(movieName);
        }
    },

    asplay: function(c) {
        var asound = MCL.Voice.getFlashObject("asound");
        if (asound) {
            asound.SetVariable("f", c);
            asound.GotoFrame(1);
        }
    },

    asstop: function() {
        var asound = MCL.Voice.getFlashObject("asound");
        if (asound) {
            asound.GotoFrame(3);
        }
    },

    initSwf: function() {
        if (!document.getElementById("asound")) {
            var html_object = "<object style=\"height:0px;width:0px;overflow:hidden;\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"0\" height=\"0\" id=\"asound\" align=\"absmiddle\">";
            html_object = html_object + "<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
            html_object = html_object + "<param name=\"movie\" value=\"/video/asound.swf\" /><param name=\"quality\" value=\"high\" />";
            html_object = html_object + "<embed src=\"/video/asound.swf\" quality=\"high\" width=\"0\" height=\"0\" name=\"asound\" align=\"absmiddle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
            html_object = html_object + "</object>";
            $("body").append(html_object);
        }
    }
};
MCL.Character =
{
    bihua: function(code) {
        var temp = "";
        temp = " <object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"130\" height=\"130\">";
        temp = temp + "<param name=\"movie\" value=\"/video/Char.swf?code=" + code + "\" />";
        temp = temp + "<param name=\"quality\" value=\"high\" />";
        temp = temp + "<embed src=\"/video/Char.swf?code=" + code + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"130\" height=\"130\"></embed>";
        temp = temp + "</object>";
        return temp;
    },
    py: function(path) {
        MCL.FlashPlayer.play(HostDefault + "/Data/Character/mp3/" + path);
    },
    diyes: function() {
        var txt = $.trim(this.getRangeText());
        if (txt.length > 1) {
            txt = txt.substring(0, 1);
            var reg = /[\u4e00-\u9fa5]/;
            if (!reg.test(txt)) {
                txt = "";
            }
        }

        if (txt != "") {
            var temp = "";
            temp = temp + "<table><tr><td width='200'>拼&nbsp;&nbsp;音: $py$</td><td rowspan=\"4\">";
            temp = temp + "$swf$";
            temp = temp + "</td></tr>";
            temp = temp + "<tr><td>笔&nbsp;&nbsp;画: $bh$</td></tr>";
            temp = temp + "<tr><td>五&nbsp;&nbsp;笔: $wb$</td></tr>";
            temp = temp + "<tr><td>繁&nbsp;&nbsp;体: $ft$</td></tr></table>";
            MCL.Window.show("", "", 330);
            MCL.Window.loading();


            $.ajax({
                type: "POST",
                url: HostService + "DictCharacter.ashx",
                dataType: "json",
                data: "char=" + encodeURIComponent(txt) + "&r=",
                success: function(json) {
                    if (json.IsSuccess == "True") {
                        var py = "";
                        if (json.DataTable.Phoneticsymbol.length > 0) {

                            for (var i = 0; i < json.DataTable.Phoneticsymbol.length; i++) {
                                py = py + "<a href='javascript:void(0)' class='volume' onclick='MCL.Character.py(\"" + json.DataTable.Phoneticsymbol[i].path + "\")'>" + json.DataTable.Phoneticsymbol[i].py + "</a>";
                            }
                        }

                        temp = temp.replace("$py$", py);
                        temp = temp.replace("$bh$", json.DataTable.Totalstrokes);
                        temp = temp.replace("$wb$", json.DataTable.Wubi);
                        temp = temp.replace("$ft$", json.DataTable.Traditional);
                        temp = temp.replace("$bm$", json.DataTable.BasicMeanings);
                        temp = temp.replace("$swf$", MCL.Character.bihua(json.DataTable.Swfpath.replace(".swf", "")));
                        MCL.Window.show(temp, "", 330);
                    }
                    else {
                        MCL.Window.error(json.Err);
                    }
                },
                error: function() {
                    MCL.Window.error();
                }
            }
        );

        }
    },
    //获取选择的文本
    getRangeText: function() {
        var text = "";

        if (document.getSelection) {  //IE
            text = document.getSelection() || document.getSelection();
        }
        else
            if (document.selection.createRange) {
            text = document.selection.createRange().text;
        }

        return text;
    }
};
MCL.Syllable = {
    init: function(picObj, voObj, zi) {
        document.getElementById(picObj).innerHTML = "<img src=\"" + HostDefault + "/images/Courseware/Chinese_for_Beginners/" + zi + "_ima.gif\" width=\"238\" height=\"201\">";
        document.getElementById(voObj).innerHTML = "<img src=\"" + HostDefault + "/images/201006/ico/sound_16x16.gif\" width=\"16\" height=\"16\" style='cursor:pointer' onclick=\"MCL.Syllable.speech('" + zi + "')\"/> ";
    },
    speech: function(zi) {
        MCL.FlashPlayer.play(HostDefault + "/Data/Character/Syllable/" + zi + ".mp3");
    }
};
MCL.Rating = {
    grade: 0,
    channel: 0,
    channelObjectID: 0,
    domark: function(star) {
        $("#GlobalStar>p>img").each(function(i) { if (i < star) { this.src = '/images/201006/ico/star_bronze.png'; } else { this.src = '/images/201006/ico/star_silver.png'; } });
        this.grade = star;
    },
    post: function() {
        if (this.grade == 0)
        { MCL.Window.error('error'); }
        else {
            MCL.Window.posting();
            $.ajax({ type: "GET",
                url: "/webservice/onlinerating.ashx",
                dataType: "json",
                data: "channel=" + MCL.Rating.channel + "&id=" + MCL.Rating.channelObjectID + "&grade=" + this.grade,
                success: function(json) {
                    if (json.Msg == "true") {
                        if (json.Err != "") {
                            MCL.Window.error(json.Err);
                        }
                        else {
                            MCL.Window.success('success');
                        }
                    }
                    else {
                        MCL.Window.error('error');
                    }
                },
                error: function() { MCL.Window.error('error'); }
            });
        }
    },
    init: function(c, id) {
        this.channel = c;
        this.channelObjectID = id;
        var imgrating = "<div id='GlobalStar'>";
        imgrating = imgrating + "<p><img src=\"/images/201006/ico/star_silver.png\"  onclick='MCL.Rating.domark(1)' onmouseover='MCL.Rating.domark(1)'  />";
        imgrating = imgrating + "<img src=\"/images/201006/ico/star_silver.png\"  onclick='MCL.Rating.domark(2)' onmouseover='MCL.Rating.domark(2)' />";
        imgrating = imgrating + "<img src=\"/images/201006/ico/star_silver.png\"  onclick='MCL.Rating.domark(3)' onmouseover='MCL.Rating.domark(3)'  />";
        imgrating = imgrating + "<img src=\"/images/201006/ico/star_silver.png\"  onclick='MCL.Rating.domark(4)' onmouseover='MCL.Rating.domark(4)'  />";
        imgrating = imgrating + "<img src=\"/images/201006/ico/star_silver.png\"  onclick='MCL.Rating.domark(5)' onmouseover='MCL.Rating.domark(5)'  /></p>";
        imgrating = imgrating + "<p><input type='button' class='btn' value='Post' onclick='MCL.Rating.post()'></p>";
        imgrating = imgrating + "</div>";
        if (MCL.Account.StudentID == 0) {
            MCL.Account.Login();
        }
        else {
            MCL.Window.show(imgrating, 'vote');
        }

    }
};
MCL.Media = {
    Video: {
        flv: "",
        width: 0,
        height: 0,
        init: function(vpath, ipath, vwidth, vheight, iscontrol) {
        MCL.Media.Video.flv = "http://www.mychineselearning.com/DocResource/Courseware/"+vpath;
            MCL.Media.Video.width = vwidth;
            MCL.Media.Video.height = vheight;
            document.getElementById("VideoPlayer").style.width = vwidth + "px";
            document.getElementById("VideoPlayer").style.height = vheight + "px";
            if (ipath != "") {
                document.getElementById("VideoPlayer").innerHTML = "<img src='" + ipath + "' style='cursor:pointer; width:" + vwidth + "px; height:" + vheight + "px'/>";

            }
            document.getElementById("VideoPlayControl").style.width = MCL.Media.Video.width + "px";
            if (!iscontrol) {
                document.getElementById("VideoPlayControl").style.display = "none";
            }
            $f("VideoPlayer", "/video/player/flowplayer-3.2.5.swf", {
                // all clips can be found from blip.tv
                clip: { url: MCL.Media.Video.flv, height: MCL.Media.Video.height, width: MCL.Media.Video.width, autoPlay: true, autoBuffering: true },

                // don't use default controls
                plugins: { controls: null }

                // controlbar plugin. auto-gnerated inside div#controls
            }).controls("VideoPlayControl", { duration: 25 }).setVolume(100);
        }
    }
}

