$(function() {
	var url = window.location.href;
	var title = encodeURIComponent($('h1').text());
	
	$.post('/service/?m=cookie&a=news', {'url' : url, 'title' : title}, function (data) {
		$('ul#lastcookie').html(data);
	});
	
	$('#cLogin').click(function () {
		popupLogin();
		return false;
	});
	
	$('#cLogout').click(function () {
		$.post("/service/index.php", {'m' : 'Logout', 'a' : 'common'}, function(data){
			$('#runCommonScript').html(data);
		});
		return false;
	});
	
	// 验证码
	$('#validate').click(function () {
		$(this).attr('src', '/service/index.php?m=validateCode&update=' + Math.random());
	});
	
	commentList();
	
	$('#submit_reply').click(function () {
		var id = $('#id').val();
		var classid = $('#classid').val();
		var content = encodeURIComponent($('#reply_content').val());
		var verity = $('#submit_reply_verity').val();
		var nomember = $('#nomember').attr('checked') ? 1 : 0;
		if(content == ''){
			alert('您没什么话要说吗？');
			$('#reply_content').focus();
			return false;
		}
		if(verity == ''){
			alert('验证码不能为空');
			$('#verity').focus();
			return false;
		}
		$.post('/service/?m=comment&a=cSave', {'id' : id, 'classid' : classid, 'content' : content, 'verity' : verity, 'nomember' : nomember}, function(data){
			if(data == 'success'){
				$('#reply_content').val('');
				$('#submit_reply_verity').val('');
				commentList();
			} else {
				alert(data);
			}
		});
		return false;
	});
});

var commentList = function () {
	var id = $('#id').val();
	var classid = $('#classid').val();
	$.post('/service/?m=comment&a=cList', {'id' : id, 'classid' : classid}, function(data){
		$('#message').html('<h3>' + $('#message h3').html() + '</h3>' +  data);
	});
}

//评论表情更多更少
show_more_face = function(){
	if(document.getElementById("reply_content").className == "small"){
		document.getElementById("reply_content").className = "big";
		document.getElementById("icons").className = "allface";
		document.getElementById("face_button").innerHTML = "↑收起";
	}else{
		document.getElementById("reply_content").className = "small";
		document.getElementById("icons").className = "liteface";
		document.getElementById("face_button").innerHTML = "更多表情↓";
	}
}
//插入表情
insert_face = function(sid){
	if(sid){
		var textObj = document.getElementById('reply_content');
		textObj.focus();
		var html = "[:s"+sid+"]";
		if($.browser.msie){
			document.selection.createRange().text = html;
		}else{
		  var rangeStart=textObj.selectionStart;       
		  var rangeEnd=textObj.selectionEnd;       
		  var tempStr1=textObj.value.substring(0,rangeStart);       
		  var tempStr2=textObj.value.substring(rangeEnd);       
		  textObj.value=tempStr1+html+tempStr2;
		}
	}
}