/**
 * User: Nero
 * Date: 15-8-19
 */

(function(){

    var oldUrl,vedioId,insetHtml,lastData;
    window.onload = function(){
        addOkListener();
    };


    /**
     * 监听确认和取消两个按钮事件，用户执行插入或者清空正在播放的视频实例操作
     */
    function addOkListener(){
        dialog.onok = function(){
            editor.execCommand('inserthtml', insetHtml);
        };
        dialog.oncancel = function(){
        };
    }

    //定义函数
    function checkUrl(){

        var hasMatched=false;
        oldUrl = $("#J_vedioUrl").val();

        //判断微视还是腾讯视频
        if (oldUrl.indexOf("www.weishi.com") > 0) {
            //判断是否包含/t
            if(oldUrl.indexOf("/t/") > 0){
                vedioId = oldUrl.split("/t/")[1];
                $("#J_vedioIframe").attr("src",'http://z.weishi.com/weixin/player.html?msgid='+vedioId+'&width=300&height=200&auto=0');
                insetHtml = $("#J_insetHtml").html();
                hasMatched=true;
            }else{
                $('#errorTips').html('请输入正确的微视地址');
            }
        }else{

            if(oldUrl.indexOf("v.qq.com") > 0){
                lastData = oldUrl.split("/").pop();
                if(lastData.indexOf("?vid=") > 0){
                    vedioId = lastData.split("?vid=")[1].substring(0,11)
                    $("#J_vedioIframe").attr("src",'http://v.qq.com/iframe/player.html?vid='+vedioId+'&width=300&height=200&auto=0');
                    insetHtml = $("#J_insetHtml").html();
                    hasMatched=true;
                }else{
                    vedioId = lastData.split(".html")[0];
                    $("#J_vedioIframe").attr("src",'http://v.qq.com/iframe/player.html?vid='+vedioId+'&width=300&height=200&auto=0');
                    hasMatched=true;
                    insetHtml = $("#J_insetHtml").html();
                }

            }else{
                $('#errorTips').html('请输入正确的腾讯视频地址');
            }
        }
        if(hasMatched){
            $('#errorTips').html('获取视频信息成功，请点击确定插入视频');
        }
    }
    //按键起检测
    $("#J_vedioUrl").keyup(function(event) {
        if($("#J_vedioIframe").attr("src") == "" ){
            checkUrl();
        }
    });

    //失去焦点
    $("#J_vedioUrl").focusout(function(event) {
        if($("#J_vedioIframe").attr("src") == "" ){
            checkUrl();
        }

    });



})();