﻿function CommentManager() {
    if (CommentManager.instance != null) {
        throw new Error("不能创建多个实例!");
    }
    CommentManager.instance = this;

    this.commentList = new Array();
    this.panel = null;
    this.listPanel = null;
    this.count = 0;
    this.produectId = 0;
    this.serverPageUrl = "";
    this.toCommentsUrl = "";
    this.loggedIn = 0;
}

CommentManager.Instance = function() {
    if (CommentManager.instance == null)
        return new CommentManager();
    return CommentManager.instance;
}

CommentManager.prototype.Show = function(divId, pid, serverurl, loggedin) {
    this.panel = document.getElementById(divId);
    this.produectId = pid;
    this.serverPageUrl = serverurl + "/comment.ashx";
    this.toCommentsUrl = serverurl + "/comments.aspx";
    this.loggedIn = loggedin;
    this.DrawPanel();
    this.GetComments();
}

CommentManager.prototype.DrawPanel = function() {
    var html = "   <div id='Div_CommentList'></div>"
             + "   <div id='Div_ToComments' style='display:none;margin-top:5px;text-align: right;'><a href='" + this.toCommentsUrl + "?pid=" + this.produectId + "'>查看所有讨论>></a></div>";
    this.panel.innerHTML = html;
    $("#productcommenttabv2").children("ul").append('<li onclick="newCommentDialog(' + this.loggedIn + ');" style="margin: 4px 20px; float: right; color: #666666; font-weight: normal;cursor: pointer;">发表评论</li>');

    this.listPanel = document.getElementById("Div_CommentList");
    this.toCommentsPanel = document.getElementById("Div_ToComments");
}

CommentManager.prototype.GetComments = function()
{      
    HttpHelper.Send(formatUrl(this.serverPageUrl+"?m=get&pid={0}&pi=0&ps=10",this.produectId),null,"CommentManager.Instance().GetCommentEnd");
}
CommentManager.prototype.GetCommentEnd = function(success) {
    if (!success)
        return;

    this.CreateListPanel();
    $("#productcommentcountv2").text("用户讨论("+this.count+")");
    $("#productcommentv2").find(".sa-nav-blank").text("用户讨论(" + this.count + ")");
    if (this.count > 0)
        this.toCommentsPanel.style.display = "";
    else
        this.toCommentsPanel.style.display = "none";
}
CommentManager.prototype.CreateListPanel = function()
{    
    var html = "";
    if(this.commentList == null || this.commentList.length == 0)
    {
        html = "还没有人对这个产品进行过讨论."
    }
    else
    {
        html = "<table style='width:100%;table-layout:fixed;'>";
        for(var i=0;i<this.commentList.length;i++)
        {    
            html +="<tr style='background-color: #f8f8f8;color: #888888;'>"
                 +"     <td style='width:30px;'></td>"
                 +"     <td>"
                 +"         <span style='font-weight: bold;'>"
                 +              this.commentList[i].name+":"
                 +"         </span>"
                 +"         <span>"
                 +              this.commentList[i].content
                 +"         </span>"
                 +"     </td>"
                 +"     <td style='width:120px;'>"
                 +"         <span>"
                 +              this.commentList[i].time
                 +"         </span>"
                 +"     </td>"
                 +" </tr>"
                 +" <tr>"
                 +"     <td style='height: 3px;'></td>"
                 +" </tr>";
        }
        html += "</table>";
    }
    this.listPanel.innerHTML = html;    
}
CommentManager.prototype.Comment = function() {
    if ($("#commentcontentv2").val() == "") {
        $("#commentmsgv2").text("您还没有输入评论内容 :)");
        window.setTimeout("$('#commentmsgv2').text('');", 2500);
        return;
    }

    this.Commenting("处理中,请稍等");

    HttpHelper.Send(formatUrl(this.serverPageUrl + "?m=add&pid={0}", this.produectId), "Txt_Comment=" + $("#commentcontentv2").val(), "CommentManager.Instance().CommentEnd");
}
CommentManager.prototype.Commenting = function(v)
{    
    $("#commentmsgv2").text(v);
}
CommentManager.prototype.CommentEnd = function(success)
{
    if(!success)
    {
        alert("对不起,在提交您的请求时,发生异常,请稍后再试!");
        return;
    }
    $("#commentmsgv2").text("");
    $("#commentcontentv2").val("");
    Boxy.get("#commentboxv2").hide();
    this.GetComments();
}


function newCommentDialog(loggedIn) {
    var html = "";
    if (loggedIn == 1)
        html = '\
            <div id="commentboxv2">\
                <div id="commentmsgv2" style="color:red;font-size: 12px;"></div>\
                <textarea id="commentcontentv2" rows="4" style="width: 680px; border: solid 1px #99cc00;"></textarea>\
                <div id="commentdialogbuttonv2" style="text-align: center; font-size: 12px;">\
                    <a id="commentdialogbuttonconfirm" href="#" onclick="CommentManager.Instance().Comment();return false;">确定</a>\
                    <a id="commentdialogbuttoncancel" href="#" onclick="Boxy.get(this).hide(); return false;">取消</a>\
                </div>\
            </div>';
    else
        html = '\
            <div id="commentboxv2">\
                <div id="commentmsgv2" style="color:red;font-size: 12px;"></div>\
                请登录后进行操作.\
                <a href="/login.aspx?returnurl=' + getRelativePath() + '">登录</a>\
            </div>';

    var dia = Boxy.get("#commentboxv2");
    if (dia) {
        dia.setContent(html);
        dia.show();
    }
    else
        new Boxy(
        html,
                {
                    title: "添加讨论",
                    closeText: "关闭",
                    modal: true
                });

    return false;
}

function getRelativePath() {
    var url = window.location.href; //当前url
    var t = url.substring(url.indexOf("//") + 2, url.length);
    var result = t.substring(t.indexOf("/"), t.length);
    return result;
}




function CommentEntity()
{
    this.name = "";
    this.content = "";
    this.time = "";
}







function HttpHelper(){}
HttpHelper.Create = function()
{    
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
    try 
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
    catch (e) 
    {
        try 
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        catch (E)
            {
                xmlhttp = false;
            }
    }
    @else
    xmlhttp = false;
    @end @*/

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
    {
        try 
            {
                xmlhttp = new XMLHttpRequest();
            } 
        catch (e) 
        {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
HttpHelper.Send = function(url, content, endEvent) {
    var h = null;
    try {
        h = HttpHelper.Create();
        h.open("Post", url, true);
        h.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    catch (e) {
        alert("Error:" + e);
        eval(endEvent + "(false)");
    }
    h.onreadystatechange = function() {
        try {
            if (h.readyState == 4) {
                if (h.status == 200) {
                    eval(h.responseText);
                    eval(endEvent + "(true)");
                }
                else {
                    throw new Error("http error:" + h.status);
                }
            }
        }
        catch (e) {
            alert("Error:" + e);
            eval(endEvent + "(false)");
        }
    }
    if (content == null || content == "")
        content = " ";
    h.send(content);
}
function formatUrl()
{
	if (arguments.length < 2)
	{
		return arguments;
	}
	var newStr = arguments[0];
	for (var i = 0; i<arguments.length - 1; i++)
	{
		newStr = newStr.replace("{" + i + "}", encodeURI(arguments[i + 1]));
	}
	return newStr;
}
function SetinnerText(o,v)
{
    if(o==null)
        return;
    if(window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)
        o.innerText = v;
     else
        o.textContent = v;
}

