
Dingtai.Web = {};


/*************************   Dingtai.Web.Player     *************************/
Dingtai.Web.Player = function() {   
    var type = Dingtai.Web.Player.GetType();
    var id = Dingtai.Web.Player.GetID();
      
    
    var params = "type=" + type + "&" + "id=" + id;
    if (document.referrer.indexOf("/Management/") > 0 ){
        //alert(document.referrer);
        params += "&isApp=true";
    }
    
//    alert(params);
    var url = Dingtai.Const("playerPage");
    jQuery.ajax({
       type: "POST",
       url: url,
       data: params,
       success: function(msg){
            if (msg != "success")
            {
                alert(msg + " 即将为您跳转至首页!");
                location.href = Dingtai.Const("index");
            }
       }
    });
};

Dingtai.Web.Player.GetType = function () {
// UrlRewrite 改造前使用 勿删除
    var fileName = Dingtai.Web.Player.GetFileName();
    var type = fileName.split("_")[0];
    return type;

//UrlRewrite 改造后使用
     var path = location.pathname;
     var ps = path.split("/");
     return ps[2];
};
Dingtai.Web.Player.GetID = function(){
// UrlRewrite 改造前使用 勿删除
    var fileName = Dingtai.Web.Player.GetFileName();
    var ids = fileName.split("_")[1];    
    var id = ids;
    return id;
    
//UrlRewrite 改造后使用    
    var path = location.pathname;
    var ps = path.split("/");
    return ps[ps.length-2];
};
Dingtai.Web.Player.GetFileName = function () {
    var path = location.pathname;
    var ps = path.split("/");
    if (ps.length < 1) { return ""; }
    var fileName = ps[ps.length-1];
    if (fileName.indexOf(".") > 0) {
        fileName = fileName.substr(0,fileName.indexOf("."));   
    }
    return fileName;    
};


jQuery(document).ready(function() {        
        Dingtai.Web.Player();
        Dingtai.Web.Comment.RegsiterClickImg();
});

Dingtai.Web.Player.Init = function() {
    
    Dingtai.Web.Comment(1);    
    setInterval("Dingtai.Web.Comment.AutoRefresh();",5000);

    var type = Dingtai.Web.Player.GetType();
    var id = Dingtai.Web.Player.GetID();
     var params = "{\"type\":\"" + type + "\",\"id\":\"" + id + "\"";
    params += ",\"func\":\"init\"";
    params += "}";
    params = jQuery.parseJSON(params);    
    var url = Dingtai.Const("playerPage");    
    jQuery.getJSON(url, params,
       function(data,textstatus){
        if (data[0].Dig != undefined && data[0].Dig != null ){ jQuery(".GoTop").find("a").html("顶 "+ data[0].Dig);  }
        if (data[0].Pai != undefined && data[0].Pai != null ){ jQuery(".Tread").find("a").html("踩 "+ data[0].Pai);  }
        if (data[0].Point != undefined && data[0].Point != null ){ jQuery(".Point").html( data[0].Point);  }
        if (data[0].PointCount != undefined && data[0].PointCount != null ){ jQuery(".PointCount").html( data[0].PointCount);  }
        if (data[0].TotalView != undefined && data[0].TotalView != null ){ jQuery(".play_views .one").html( data[0].TotalView);  }
        if (data[0].CommentCount != undefined && data[0].CommentCount != null ){ jQuery(".play_views .two").html( data[0].CommentCount);  }
       });
     Dingtai.Web.Player.InitStar("scoring");
    
};

Dingtai.Web.Player.InitStar = function(control) {
    var className = "." + control;
    var as = jQuery(className).find("a");
    var count = 5;    
     as.each(function(i){
        this.onclick = function() {  
           Dingtai.Page.Passport.GetCookieInfor(function(data,textstatus){
                if(data == null || data == undefined || data[0] == null || data[0] == undefined) { 
                    alert("对不起,您还未登录,不能进行评分");
                    return ; 
                }
                var username = data[0].username; 
//                var username = "admin";  
                Dingtai.Web.Player.StarOnclick(username,i)
           });   
        };
        this.onmouseover = function() {
            this.style.cursor = "pointer";
            for(j=0;j<=i;j++){
                    as.eq(j).attr("class","pointstar");
            }
        };
        this.onmouseout = function() {      
            for(j=0;j<=4;j++){
                 as.eq(j).attr("class","");
            }
        };
    });
};

Dingtai.Web.Player.StarOnclick= function(username,i) {
           
            var type = Dingtai.Web.Player.GetType();
            var id = Dingtai.Web.Player.GetID();
            var params = "username=" + username+"&type=" + type + "&" + "id=" + id + "&" + "func=point&val=" + (i+1);         
            var url = Dingtai.Const("playerPage");
            jQuery.ajax({
               type: "POST",
               url: url,
               data: params,
               success: function(msg){
               if(msg != "True"){
                     alert(msg);
                     return;
               }               
                Dingtai.Web.Player.Init();
               }
            });        
};

Dingtai.Web.Player.Ding = function() {
    var type = Dingtai.Web.Player.GetType();
    var id = Dingtai.Web.Player.GetID();
    var params = "type=" + type + "&" + "id=" + id + "&" + "func=ding";
//    alert(params);
    var url = Dingtai.Const("playerPage");
    jQuery.ajax({
       type: "POST",
       url: url,
       data: params,
       success: function(msg){
           Dingtai.Web.Player.Init();
       }
    });
};

Dingtai.Web.Player.Cai = function() {
    var type = Dingtai.Web.Player.GetType();
    var id = Dingtai.Web.Player.GetID();
    var params = "type=" + type + "&" + "id=" + id + "&" + "func=cai";
    var url = Dingtai.Const("playerPage");
    jQuery.ajax({
       type: "POST",
       url: url,
       data: params,
       success: function(msg){
          Dingtai.Web.Player.Init();
       }
    });
};

Dingtai.Web.Player.Collect = function() {
    Dingtai.Page.Passport.GetCookieInfor(Dingtai.Web.Player.CollectCallBack);   
};

Dingtai.Web.Player.CollectCallBack =  function (data,textstatus){
        if(data == null || data == undefined || data[0] == null || data[0] == undefined) { 
            if( confirm("对不起,您还未登录,是否将页面添加到收藏夹中?") ) { Dingtai.Web.Player.AddFavorite(); }
            return ; 
        }

        var username = data[0].username;
//        var username= "admin";
      
        var type = Dingtai.Web.Player.GetType();
        var id = Dingtai.Web.Player.GetID();
        var params = "type=" + type + "&" + "id=" + id + "&" + "func=collect" + "&" + "url=" + location.href + "&" + "username=" + username;
        var url = Dingtai.Const("playerPage");
        jQuery.ajax({
           type: "POST",
           url: url,
           data: params,
           success: function(msg){
//           alert(msg);
                if(msg == "True" ) { alert("收藏成功!"); }
                else {  alert(msg);}
           }
        });
};

Dingtai.Web.Player.Send = function() {
    Dingtai.Page.Passport.GetCookieInfor(Dingtai.Web.Player.SendCallBack);   
};

Dingtai.Web.Player.SendCallBack = function(data,textstatus) {
     if(data == null || data == undefined || data[0] == null || data[0] == undefined) { 
        alert("对不起,您还未登录,不能发送信息!");
        return ; 
    }
    var type = Dingtai.Web.Player.GetType();
    var id = Dingtai.Web.Player.GetID();
     var params = "type=" + type + "&t=s&" + "id=" + id ;
     location.href = "/my/addmessage.aspx?" + params;
};

Dingtai.Web.Player.AddFriend = function() {
    Dingtai.Page.Passport.GetCookieInfor(Dingtai.Web.Player.AddFriendBack);   
};

Dingtai.Web.Player.AddFriendBack =  function (data,textstatus){
        if(data == null || data == undefined || data[0] == null || data[0] == undefined) { 
            alert("对不起,您还未登录,不能加为好友!");
            return ; 
        }

        var username = data[0].username;
//        var username= "admin";
      
        var type = Dingtai.Web.Player.GetType();
        var id = Dingtai.Web.Player.GetID();
        var params = "type=" + type + "&" + "id=" + id + "&" + "func=friend" + "&" + "username=" + username;
        var url = Dingtai.Const("playerPage");
        jQuery.ajax({
           type: "POST",
           url: url,
           data: params,
           success: function(msg){
//           alert(msg);
                if(msg.toLowerCase() == "true") { alert("添加成功!"); }
                else { alert(msg);}
           }
        });
};

Dingtai.Web.Player.AddFavorite = function ()
{
    try {  
       if (document.all)
      {
        window.external.AddFavorite(location.href, document.title);
      }
      else if (window.sidebar)
      {
         window.sidebar.addPanel(document.title,location.href, "");
      }
  }  
  catch (e)  
  {
    alert(e);  
    alert("加入收藏失败,请手动添加.");     
  }   
}

Dingtai.Web.Player.CopyToClip = function() {
    Dingtai.Web.Player.Copy_Clip(location.href + "  " + document.title + "  " + " 来自鼎太网络产品 -  鼎太播客V5.0");
    alert("视频信息已复制到系统剪贴板, 请粘贴给您的好友吧... ^_^ ");
};

Dingtai.Web.Player.Copy_Clip = function (copy){
if (window.clipboardData){
window.clipboardData.setData("Text", copy);}
else if (window.netscape){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
//if (!clip) return;
var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
//if (!trans) return;
trans.addDataFlavor('text/unicode');
var str = new Object();
var len = new Object();
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
var copytext=copy;
str.data=copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid=Components.interfaces.nsIClipboard;
//if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);}
//return false;
};

/*************************   Dingtai.Web.Comment     *************************/


Dingtai.Web.Comment = function (pageIndex){
    if (pageIndex == undefined || pageIndex == null || pageIndex <= 0 ) { pageIndex = 1; }
           
    var type = Dingtai.Web.Player.GetType();
    var id = Dingtai.Web.Player.GetID();
    
    var params = "{\"type\":\"" + type + "\",\"id\":\"" + id + "\",\"page\":\"" + pageIndex + "\"";
    params += ",\"pageSize\":\"" + Dingtai.Web.Comment.PageSize + "\" , \"myrandom\":\"" + Math.random() + "\"";
    params += "}";
    params = jQuery.parseJSON(params);    
    var url = Dingtai.Const("commentPage");
    jQuery.getJSON(url, params , Dingtai.Web.Comment.Callback)
    jQuery("#logo").ajaxError(function(event,request, settings){
        /*alert("Error: " + event.toString() + "  " + settings.toString());*/
    });         
};

Dingtai.Web.Comment.PageSize = 5;
Dingtai.Web.Comment.RecordCount = 0;
Dingtai.Web.Comment.ShowMutilPageCount = 5;

Dingtai.Web.Comment.Callback = function (data,textstatus){
    var max = data.length;
    //常规参数数量  msg , type,id,page,pageSize,pageCount,recordCount
    var commonParamsCount = 7;
    var dataLength = 0;
    
    if (max > commonParamsCount){
        dataLength = max - commonParamsCount;
    }
    else if (max == commonParamsCount){
        if (data[0].msg != "暂无评论!") { alert(data[0].msg); }  
        else {        
           
        }      
        return;
    }
    else {
   
    }    
    var type = data[max-(commonParamsCount - 1)].type;
    var id = data[max-(commonParamsCount - 2)].id;        
    var pageIndex = data[max-(commonParamsCount - 3)].page;
    var pageSize = data[max-(commonParamsCount - 4)].pageSize;
    var pageCount = data[max-(commonParamsCount - 5)].pageCount;
    var recordCount = data[max-(commonParamsCount - 6)].recordCount;
//    alert(commonParamsCount);    
    
//    if (pageIndex == 1) {Dingtai.Web.Comment.RecordCount = recordCount; }
    Dingtai.Web.Comment.RecordCount = recordCount;
    
    //添加分页
    Dingtai.Web.Comment.PutPage(type,id,pageIndex,pageCount,pageSize);  
    //添加列表显示
    Dingtai.Web.Comment.PutList(data,dataLength,commonParamsCount,pageIndex,pageSize,pageCount);
    
    Dingtai.Scripts.StringsFilter();
};

Dingtai.Web.Comment.PutList = function(data,dataLength,commonParamsCount,pageIndex,pageSize,pageCount) {
    var control = ".comments_text > ul"
    var lis = jQuery(control + " > li");
    jQuery(control).html("");
    lis = jQuery(control + " > li");
//    alert(lis.length);
    //为UL添加 LI
    if (lis.length<dataLength){ 
        for(var j = lis.length;j<dataLength;j++){
            jQuery(control).append("<li></li>");
        }
    }
    lis = jQuery(control);
    //去除通用参数对象  MSG , pageCount
    for (var k = 0 ; k<commonParamsCount; k++) {data.pop(); }
    
    Dingtai.Web.Comment.Output(data,lis,"CreationDate",pageIndex,pageSize,pageCount);
};

Dingtai.Web.Comment.Output = function(data,targetObject,sort,pageIndex,pageSize,pageCount) {
     //jQuery对JSON排序 按Point逆序
//     data.sort(function(a,b){
//            return a[sort]<b[sort]?1:(a[sort]==b[sort]?(a["ID"]<b["ID"]?1:-1):-1);
//        });
    
    var url = location.protocol + "//" + location.host + "";
    
    for (var i = 0;i<data.length;i++){
         var name = "";
         name = data[i].UserName;
         var detail = data[i].Context;
         detail = Dingtai.Web.Comment.decode(detail);
                  
        var str = "眨眼,微笑,大笑,顽皮,绅士,怒火,抓狂,不屑,大哭,做贼心虚,晕,无语,瞌睡,害羞,惊讶,哈哈大笑,色,酷,骷髅,囧";
//        var str = "眨眼,微笑,大笑";
        var imgs  = str.split(",");
        var temp = "";
        var targetStr = "";    
        var reg;    
        
        for( var j = 0 ;j<imgs.length ;j++){
            detail = Dingtai.Web.Comment.AnalysisImage(imgs[j],detail,j);                                       
        }
        //alert(detail);
        var html =   "<span class=\"floor\">" + (Dingtai.Web.Comment.RecordCount - i - ((pageIndex-1) * pageSize) ) + "楼</span><span class=\"avatar\"><img src=\"" + url + data[i].ImageUrl + "\" width=\"29\" height=\"29\" alt=\"系统头像\" /></span><span class=\"text\"><b>会员名: " + name + "  " + data[i].CreationDate + "</b><br />" + detail + "<br /></span>";
         targetObject.find("li").eq(i).html(html);
    }
};

Dingtai.Web.Comment.AnalysisImage = function(img, content, j) {
    var temp = "";
    var targetStr = "";
    var reg;
    temp = "(\\[" + img + "\\])";
    reg = new RegExp(temp);
    //        eval("reg = new RegExp(" + temp + ");");
    var start = content.search(reg);
    if (start > 0) {
        //alert("content: " + content + "  reg: " + reg.toString() + " start : " + start);
    }
    if (start >= 0) {
        var imglen = Dingtai.AscLength(img) + Dingtai.AscLength("[") * 2;
        var conlen = Dingtai.AscLength(content);
        //alert("content: |" + content + "|  reg: " + reg.toString()+" start: " +start+  " imglen : " + imglen+  " img.length : " + img.length + " conlen : "  + conlen + " content.slice: " + content.slice(start+img.length+2,content.length));
        var content1 = Dingtai.Web.Comment.AnalysisImage(img, content.slice(start + img.length + 2, content.length), j);
        targetStr = "<img src='/html/Skins/default/images/expression_" + (j + 1) + ".gif' width='20' height='20' title='" + img + "'/>";
        if (start > 0) {
            //content = content.slice(0, start - 1) + targetStr + content1;
            content = content.slice(0, start) + targetStr + content1;
            //            alert(content);
        } else {
            content = targetStr + content1;
        }
    }
    return content;

};


Dingtai.Web.Comment.PutPage = function(type,val,pageIndex,pageCount,pageSize) {

   //添加分页         
    var ul_page = jQuery(".comments_page ul");
    ul_page.html("");
    
    ul_page = jQuery(".comments_page ul");
    if (pageIndex == 1) { ul_page.append("<li class='previous'><span>上一页</span></li>"); } 
    else { ul_page.append("<li class='previous'><a href='"+ Dingtai.Web.Comment.GetClickStr(pageIndex - 1) +"'>上一页</a></li>"); }
    
    var preMore = false;
    var count = Dingtai.Web.Comment.ShowMutilPageCount;
    if ((pageCount > count && pageIndex > count) || pageCount - pageIndex < count) { preMore = true;}
//    if (pageCount - pageIndex > 0) { preMore = true;}
    var nextMore = false;
    if (pageCount > count && pageIndex <= pageCount - count) { nextMore = true;}

    
//    if (preMore) { ul_page.append("<li class='pages'><a href='"+ Dingtai.Web.Comment.GetClickStr(pageIndex - count) +"'>...</a></li>"); }
    
    var start = 0;
    if(pageIndex > count) { start = pageIndex - count + 3; }
    else { start = 1; }
    var end = 0;
    if(pageIndex > pageCount - count) { end = pageCount; if (pageCount > count) { start = pageCount - count + 1;}}
    else if(pageIndex < 5) { end =count; }
    else { end =pageIndex + count - 3; }
    
    for(var i = start; i<=end;i++){
        if (i==pageIndex) { ul_page.append("<li class='pages'><span>" + i +"</span></li>"); }
        else { ul_page.append("<li class='pages'><a href='"+ Dingtai.Web.Comment.GetClickStr(i) +"'>" + i +"</a></li>"); }
     }

//     if (nextMore) { ul_page.append("<li class='pages'><a href='"+ Dingtai.Web.Comment.GetClickStr(pageIndex + count) +"'>...</a></li>"); }
    
    if (pageIndex == pageCount) { ul_page.append("<li class='next'><span>下一页</span></li>");} 
    else { ul_page.append("<li class='next'><a href='"+ Dingtai.Web.Comment.GetClickStr(pageIndex + 1) +"'>下一页</a></li>"); }
    
     var recordCount = Dingtai.Web.Comment.RecordCount;
     var pageInfor = jQuery(".comments_page .comments_pageInfor");
     if (pageIndex == pageCount) { 
        start = recordCount- (pageSize * (pageCount - 1));
        end = 1;
    }
    else {
        start = recordCount - ((pageIndex-1) * pageSize);
        end = (recordCount - pageIndex * pageSize + 1);
    }
     
     var infor = "第" + start + "—" + end + "条 共" + recordCount  + "条";
     pageInfor.html(infor);
};

Dingtai.Web.Comment.decode = function(detail) {
    detail = decodeURIComponent(detail);
    return detail;
};

Dingtai.Web.Comment.encode = function(detail) {
    detail = encodeURIComponent(detail);
    return detail;
};

Dingtai.Web.Comment.GetClickStr = function (pageIndex) {
    return "javascript:Dingtai.Web.Comment(" + pageIndex + ");";
};

Dingtai.Web.Comment.Submit = function() {


    Dingtai.Page.Passport.GetCookieInfor(Dingtai.Web.Comment.SubmitCallback);
};
Dingtai.Web.Comment.SubmitCallback = function(data, textstatus) {


    var name = "匿名用户";
    if (data == null || data == undefined || data[0] == null || data[0] == undefined) { name = "匿名用户"; }
    else {
        if (data[0].username != "") { name = data[0].username; }
    }

    var context = jQuery("#comments_context");

    var msg = jQuery.trim(context.val());
    var type = Dingtai.Web.Player.GetType();
    var id = Dingtai.Web.Player.GetID();
    var params = "";

    if (msg.length == 0) { alert("对不起，评论内容不能为空哦！"); return; }

    params = "{\"type\":\"" + type + "\",\"id\":\"" + id + "\",\"msg\":\"" + Dingtai.Web.Comment.encode(msg) + "\"";
    params += ",\"pageSize\":\"" + Dingtai.Web.Comment.PageSize + "\"";
    params += ",\"name\":\"" + name + "\"";
    params += "}";

    params = jQuery.parseJSON(params);

    var url = Dingtai.Const("submitcommentPage");
    //    url = "/AjaxPage/SubmitCommentHandler.aspx";
    // alert (url);
    jQuery.get(url, params, function(msg) {

        if (msg != "fail") {
            context.val("");
            Dingtai.Web.Comment(1); Dingtai.Web.Player.Init();
        }
        else { alert("评论失败!"); }
    });
};


Dingtai.Web.Comment.RegsiterClickImg = function(control) {
    jQuery(".MyComments_box a").each(function(i){
//        this.href = "javascript:return ;";
    });
    jQuery(".MyComments_box img").each(function(i){
        this.onclick = function() {           
            var context = jQuery("#comments_context");
            context.val(context.val() + " [" + this.alt + "]");             
        };
        this.onmouseover = function() {
            this.style.cursor = "pointer";
        };
    });
};

Dingtai.Web.Comment.AutoRefresh = function () {
    var chk = jQuery("#autoRefresh");
//    alert(chk.length);
    if (chk[0] == undefined) return;
    if (chk[0].checked) {  Dingtai.Web.Player.Init();}
};