﻿DomLoaded.load(InterMessage);
//初始化IM
function InterMessage(){
    var commentContorl = $("CommentControl"), CommentsList = $("commentsbox");
	InitCommentControl.call(commentContorl);
	if(CommentsList != null){
	    CommentsList.Relate = commentContorl;
	    InitCommentsListEvent.call(CommentsList);
	}
}
//绑定控制
function InitCommentControl(){
	this.Checked = function(){
		this.SetStatus("msgbotton loading", "choosed");
	}
	this.UnChecked = function(){
		this.SetStatus("msgbotton", "");
	}
	this.SetStatus = function(pStyle,sStyle){
		this.parentNode.className = pStyle;
		this.className = sStyle;
		this.blur();
	}
	this.onclick = function(){
	    ImageLoad("/skins/message/bg_head.png","/skins/message/bg_body.png","/skins/message/bg_foot.png");
	    if($("message") == null) BuildMessage.call(this, false);
	}
	CheckIsAutoOpen.call(this);
}
//检测是否自动开启
function CheckIsAutoOpen(){
    var commentID = document.location.hash;
    if(/comment_/.test(commentID) && $("message") == null)
        BuildMessage.call(this, true);
}
//建立IM对象
function BuildMessage(IsAutoScorll){
	this.Checked();
	this.Relate = BuildCommentDOM.call(this, IsAutoScorll);
	document.body.appendChild(this.Relate);
	InitFlexEffect.call([this, this.Relate], InitCommentsBox);
	GetComments.call(this.Relate);
}
//建立IM DOM
function BuildCommentDOM(IsAutoScorll){
	var MessageBox = document.createElement("div");
	MessageBox.className = "message";
	MessageBox.id = "message";
	MessageBox.Relate = this;
	MessageBox.Return = DealReturnProcesse;
	MessageBox.PostString = "action=build&id="+ this.rel;
	MessageBox.IsAutoScorll = IsAutoScorll;
	return MessageBox;
}
//得到Comments
function GetComments(){
    var URL = "/comments/index.xhtml?temp=" + new Date();
    if(this.URL) URL = this.URL;
	var Total = new ajax.$x(URL,"POST",returnStatus,CheckError,this.PostString,this,false);
}
function returnStatus(){
	var res = this.req.responseXML.documentElement;
	res = res.getElementsByTagName("return")[0].firstChild.nodeValue;
	if(this.relate.Return)
		this.relate.Return.call(this.relate, res);
}
function CheckError(){
    if(this.relate.Relate)
        this.relate.Relate.UnChecked();
}
function DealReturnProcesse(ReturnValue){
	this.innerHTML = ReturnValue;
	SetConstantRelate.call(this);
	if(this.IsAutoScorll) this.oMsglist.ScrollTo();
	if(this.Relate.SetDisplayTop) this.style.top = this.Relate.SetDisplayTop;
	this.Open();
	this.Relate.UnChecked();
}
//设置永久关联
function SetConstantRelate(){
    this.oSpan = $a.call($a.call(this, "h3"), "span"); /*提示信息*/
    this.oSpan.Relate = this;
    this.oMessageList = $("postlist");/*评论内容*/
    this.oMessageList.Relate = this;
    this.oForm = $fm("MsgPost");/*提交评论表单*/
    this.oForm.Relate = this;
	BindDrag.call($a.call(this, "h2"));
	BindMessageList.call(this.oMessageList, $a.call($("footwrap"), "a"));
	BindCommentForm.call(this.oForm);
	BindListDefultEvent.call(this);
}
//绑定评论默认事件
function BindListDefultEvent(){
    SetTempRelate.call(this)
	BindFontSize.call($("fontsize"),this);
	BindPageNumber.call($("pagenumber"),this);
}
//设置临时关联
function SetTempRelate(){
    this.oMsglist = $("msglist");/*评论列表*/
    this.oMsglist.Relate = this;
    AutoScroll.call(this.oMsglist);/*绑定滚动*/
    this.oMsglist.ScrollTo = function(){
        var id = document.location.hash.replace("#", "");
        if(id != ""){
            var oObj = $("comment_" + id);
            if(/comment_/.test(id))oObj = $(id);
            if(oObj != null){
                var top = oObj.offsetTop - GetFirstOffsetTop.call(this);
                this.Scroll(top);
                return false;
            }
        }
    }
}
//得到列表对象的滚动高度（Except IE）
function GetFirstOffsetTop(){
    var firstChild = $a.call(this, "dl");
    return firstChild.offsetTop;
}
//初始IM面板
function InitCommentsBox(){
    this.MaxHeight = 586;
    this.Speed = 0.06;
    this.Delay = 10;
    this.CookieName = "CommentsPanel";
    FlexEffect.call(this);
}
//绑定拖拽
function BindDrag(){
	this.Relate = this.parentNode.parentNode;
	this.onmousedown = Drag;
	$a.call(this, "a").onclick = function(){
		$("message").Hidden();
	}
}
//绑定IM Form
function BindCommentForm(){
	this.setStyle = function(CssClass, Msg){
	    this.Relate.oSpan.parentNode.className = CssClass;
	    this.Relate.oSpan.innerHTML = Msg;
	}
	this["cancel"].onclick = function(){
		this.form["comment"].value = "";
		this.form.Relate.Hidden();
	}
	InitPost.call(this["post"]);
}
//初始化添加评论
function InitPost(){
    this.Return = RetuenMessagePost;
	this.onclick = function(){
	    if(this.form["comment"].value.length < 10
	        || this.form["comment"].value.length > 500){
	        this.form.setStyle("error", "The post length at 10 - 500.");
		}else{
		    this.PostString = "action=post&id="+ this.form.Relate.Relate.rel+"&msg="+this.form["comment"].value;
		    this.form.setStyle("loading", "Now post your comment, wait...");
		    GetComments.call(this);
		}
	}
}
//返回添加信息
function RetuenMessagePost(ReturnValue){
    if(ReturnValue == "" || ReturnValue.length > 200){
        this.form.setStyle("error", "Unknown error!");
    }else if(!/ok/.test(ReturnValue)){
        this.form.setStyle("error", ReturnValue);
    }else{
        this.form["comment"].value = "";
        this.form.setStyle("", "The comment has post succeed!");
        this.form.Relate.oMessageList.Refresh("/comments/page_1.xhtml");
    } 
}
//绑定字体控制
function BindFontSize(Main){
	this.FontSize = $a.call(this, "span");
	this.Relate = Main.oMsglist;
	InitSize.call(this.FontSize);
	SetFontSize.call(this.Relate,this.FontSize.innerHTML);
	BindSizeEvent.call($as.call(this, "a"));
}
//字体控制
function BindSizeEvent(){
	this[0].onclick = function(){
		this.parentNode.FontSize.Reduce();
	}
	this[1].onclick = function(){
		this.parentNode.FontSize.Add();
	}
}
//字体对象
function InitSize(){
	this.Add = function(){
		if(this.innerHTML < 14){
			this.innerHTML++;
			SetFontSize.call(this.parentNode.Relate, this.innerHTML)
		}
	}
	this.Reduce = function(){
		if(this.innerHTML > 9){
			this.innerHTML--;
			SetFontSize.call(this.parentNode.Relate, this.innerHTML)
		}
	}
}
function SetFontSize(iSize){
	this.style.fontSize = iSize + "px";
	this.style.lineHeight = iSize + "px";
	document.cookie = "Font=size=" + iSize + "; expires=" + AddDays(365) + "; path=/";
}
//绑定分页
function BindPageNumber(Main){
    this.Relate = Main.oMessageList;
	var links = $as.call(this, "a");
	BatchBindEvent(links, "onclick", PageClick);
}
//分页行为
function PageClick(){
	this.parentNode.parentNode.Relate.Refresh(this.href);
	return false;
}
//绑定刷新内容事件
function BindMessageList(reFreshBtn){
    this.Return = ReturnPageFresh;
    this.DisappearFun = SetCommentContent;
    this.ShowFun = SetCommentRelate;
    AlphaEffect.call(this);
    this.Refresh = function(url){
        if(!this.Status){
            this.URL = url + "?temp=" + new Date();
            this.PostString = "action=page&id="+ this.Relate.Relate.rel;
            this.Relate.oForm.setStyle("loading", "Now loading comments content, wait...");
            GetComments.call(this);
        }
    }
    reFreshBtn.Retale = this;
    reFreshBtn.onclick = function(){
        this.Retale.Refresh("/comments/page_1.xhtml");
    }
}
//返回刷新评论
function ReturnPageFresh(ReturnValue){
    this.res = ReturnValue;
    this.Disappear();
    this.Relate.oForm.setStyle("", "Comments have loading complete!");
}
//设置消失时事件
function SetCommentContent(){
    this.innerHTML = this.res;
}
//设置结束后事件
function SetCommentRelate(){
    BindListDefultEvent.call(this.Relate);
}
//初始化评论列表事件
function InitCommentsListEvent(){
    BatchBindEvent($as.call(this, "a"), "onclick", commentLinkClick);
}
//外置评论链接事件
function commentLinkClick(){
    document.location = this.href;
    this.Relate = this.parentNode.parentNode.Relate;
    this.DisplayTop = this.parentNode.parentNode.offsetTop + "px";
    if(this.Relate.Relate == null){
        this.Relate.SetDisplayTop = this.DisplayTop;
        BuildMessage.call(this.Relate, true);
    }else if(!this.Relate.IsOpen){
        this.Relate.Relate.style.top = this.DisplayTop;
        this.Relate.onclick();
    }
    if(this.Relate.Relate.oMsglist)
        this.Relate.Relate.oMsglist.ScrollTo();
    this.blur();
}
