﻿(function($) {
	$.extend({
		_jsonp : {
			scripts: {},
			charset: 'gb2312',
			counter: 1,
			head: document.getElementsByTagName("head")[0],
			name: function( callback ) {
				var name = '_jsonp_' +  (new Date).getTime()
					+ '_' + this.counter;
				this.counter++;
				var cb = function( json ) {
					eval( 'delete ' + name );
					callback( json );
					$._jsonp.head.removeChild( $._jsonp.scripts[ name ] );
					delete $._jsonp.scripts[ name ];
				};
				eval( name + ' = cb' );
				return name;
			},
			load: function( url, name ) {
				var script = document.createElement( 'script' );
				script.type    = 'text/javascript';
				script.charset = this.charset;
				script.src     = url;
				this.head.appendChild( script );
				this.scripts[ name ] = script;
			}
		},

		getJSONP : function ( url, callback ){
			var name = $._jsonp.name( callback );
			var url = url.replace( /{callback}/, name );
			$._jsonp.load( url, name );
			return this;
		}
	});
})(jQuery);

$(
	function()
	{
	    //更换验证码
        $("#imgVerifyCode").attr("src","/VerifyCode.aspx?"+Math.random());
        $("#changeVerifyCode").click(function(){
            $("#imgVerifyCode").attr("src","/VerifyCode.aspx?"+Math.random());
        });
        //调用远程ajax服务
		var clubServiceUrl = "/AjaxComment.aspx";
		refreshComments($("#objecttype").text(),$("#objectid").text());
		function refreshComments(referenceType,objectid)
		{
			if (objectid != "")
			{
			    $.getJSONP(clubServiceUrl+"?objectid={0}&type={1}".format(objectid, referenceType),getCommentSummaryWithComments);
			}
		}
		//获取点击次数
		if ($("#objectid").text() != "")
		{
		    $.getJSON(
						clubServiceUrl,
						{
						    type:"getclickcount",
							objectid: $("#objectid").text()
						},
						function(result)
						{
							 $("#clickcount").text(result.Result);
						});
		}
		//提交评论
		$("#saveComment").click(
			function()
			{
			    if($("#txtVerifycode").val() == "")
			    {
			        alert("请填写验证码！");
			        $("#txtVerifycode").focus();
			        return false;
			    }
			    if($("#txtContent").val() == "")
			    {
			        alert("请填写评论内容！");
			        $("#txtContent").focus();
			        return false;
			    }
				$.getJSON(
						clubServiceUrl,
						{
						    type:"savenewscomment",
						    verifycode:$("#txtVerifycode").val(),
							objectid: $("#objectid").text(),
							content: escape($("#txtContent").val())
						},
						function(result)
						{
							if (result.Result)
							{
							    refreshComments($("#objecttype").text(),$("#objectid").text());
							    $("#saveComment").unbind("click");
							    $("#txtVerifycode").val("");
							    $("#txtContent").val("");
							}
							else
							{
							    alert("评论提交失败，请核对验证码，并重试！");
							}
						});
			});
	});
	
    //回调函数，加载评论数据信息
    function getCommentSummaryWithComments(result)
    {
	    if (result.GetCommentSummaryWithComments != null)
	    {
	        $("#count").html(result.CommentSummary.Count);
		    $("#comment").html(result.GetCommentSummaryWithComments.process(result));
	    }
    }
