// JavaScript Document

var comment = function(module, id, tags, login_state) {
	
	//登录状态
	this.login_state = login_state;
	
	//对象目标
	this.target = null;

	//行数
	this.row = 20;

	//模版
	this.tpl = null;
	
	//自身的对象名称
	this.tags = tags;

	//评论模块
	this.module = module;

	//评论目标ID
	this.id = id;

	//评论区域
	this.comment	= document.createElement("div");

	//页码区域
	this.page		= document.createElement("div");
	
	this.page.className = 'comment_page';

	//表单区域
	this.form_div	= document.createElement("div");

	this.form_div.className = 'comment_form';

	this.page_index = null;

}

comment.prototype.reinit = function(target, id) {
	this.id = id;
	this.comment.innerHTML = "";
	this.form_div.innerHTML = "";
	document.getElementById(target).innerHTML = "";
	this.init(target);
}

//初始化数据，加载模版
comment.prototype.init = function(target) {
	var o = this;
	this.target = $("#"+target);

	this.target.append(this.comment);
	this.target.append(this.page);
	this.target.append(this.form_div);

	this.form = document.createElement("form");
	var _textarea = document.createElement("textarea");
	_textarea.className = "comment_textarea";
	_textarea.name = "content";

	this.form.appendChild(_textarea);
	this.form_submit = document.createElement("input");
	this.form_submit.type = "submit";
	this.form_submit.className = 'comment_submit';
	this.form_submit.value = "猛击发送";		
	this.form.appendChild(this.form_submit);
	
	this.form.onsubmit = function() {

		if(_textarea.value.length < 2) {
			alert('内容不能少于2个字');
			_textarea.focus();
			return false;
		}

		if(_textarea.value.length > 200) {
			alert('内容不能大于200个字');
			_textarea.focus();
			return false;
		}

		$.post('/comment.php', {"action":"insert", "module":o.module, "id":o.id, "content":_textarea.value}, function(data) {
			if(data.state == 1) {
				_textarea.value = "";
				o.goto(1);
				window.location = "#" + o.tags;
			}else{
				alert(data.msg);
			}
		}, 'json')

		return false;
	}

	if(this.login_state <= 0) {
		this.form.style.display = 'none';
		this.form_div.innerHTML = '<div style="line-height:30px; text-align:center;">需要<a href="/login.php" class="tlink" title="立即登录">登录</a>才可以进行评论哟！</div>';
	}

	this.form_div.appendChild(this.form);

	this.but_prev = document.createElement("input");
	this.but_prev.type = "button";
	this.but_prev.value = "上一页";

	this.but_next = document.createElement("input");
	this.but_next.type = "button";
	this.but_next.value = "下一页";

	this.but_page = document.createElement("input");
	this.but_page.type = "button";

	this.page.style.display = "none";
	this.page.appendChild(this.but_prev);
	this.page.appendChild(this.but_page);
	this.page.appendChild(this.but_next);

	$.get('/comment.html', function(tpl) {
		o.tpl = tpl;
		o.goto(1);
	});
}

//跳转到指定页
comment.prototype.goto = function(page) {
	var o = this;
	this.page_index = page;
	$.post('/comment.php', {"action":"select", "tags":this.tags, "row":this.row, "page":page, "module":this.module, "id":this.id}, function(data) {
		if(data.pcount > 1) {
			o.setPage(data.prev, data.next, data.pcount, data.page);
		}
		jtemplate.display(o.tpl, data, function(html) {
			$(o.comment).html(html);
		});
	}, 'json');	
}

//设置页码
comment.prototype.setPage = function(prev, next, pcount, page) {
	var o = this;
	
	this.page.style.display = "block";

	this.but_page.value = "第"+page+"页";

	this.but_prev.onclick = function() {
		o.goto(prev);
	}

	this.but_next.onclick = function() {
		o.goto(next);
	}
}

comment.prototype.del = function(id, type) {
	var o = this;
	if(confirm("确定要删除吗？")) {
		$.post('/comment.php', {"action":"delete", "type":type, "module":o.module, "id":id}, function(data) {
			if(data.state == 1) {
				o.goto(1);
				window.location = "#" + o.tags;
			}else{
				alert(data.msg);
			}
		}, 'json')
	}
}

comment.prototype.reply = function(id) {
	var o = this;
	if($('#' + this.tags + "_reply_" + id).html() != "") {
		$('#' + this.tags + "_reply_" + id).html('');
		return false;
	};

	var _form = document.createElement("form");
	var _textarea = document.createElement("textarea");
	_textarea.className = "comment_textarea";
	_textarea.name = 'content';

	var _submit = document.createElement("input");
	_submit.type = 'submit';
	_submit.className = 'comment_submit';
	_submit.value = '确定';

	_form.onsubmit = function() {

		if(_textarea.value.length < 2) {
			alert('内容不能少于2个字');
			_textarea.focus();
			return false;
		}

		if(_textarea.value.length > 200) {
			alert('内容不能大于200个字');
			_textarea.focus();
			return false;
		}

		$.post('/comment.php', {"action":"reply", "module":o.module, "id":id, "content":_textarea.value}, function(data) {
			if(data.state == 1) {
				o.goto(o.page_index);
			}else{
				alert(data.msg);
			}
		}, 'json')
		
		return false;
	}

	var cancel = document.createElement("input");
	cancel.type = 'button';
	cancel.className = 'comment_cancel';
	cancel.value = '取消';

	cancel.onclick = function() {
		$('#' + o.tags + "_reply_" + id).html('');
		return false;
	}

	_form.appendChild(_textarea);
	_form.appendChild(_submit);
	_form.appendChild(cancel);

	var _div = document.createElement('div');
	_div.className = 'comment_reply_popup';

	_div.appendChild(_form);

	$('#' + this.tags + "_reply_" + id).append(_div);

}
