// JavaScript Document
$(document).ready(function (){ 
							    $("body").append('<input id=\"thelastpage\" type=\"hidden\" name=\"modelid\" value=\"1\" />');
								$("body").append('<input id=\"cpage\" type=\"hidden\" name=\"cpage\" value=\"1\" />');
								$("body").append('<input id=\"ctype\" type=\"hidden\" name=\"ctype\" value=\"1\" />');
								
								dtitle = $("#pldivs > ul > li");
								var length = dtitle.length;
								for(var k=0;k<length;k++){
								   $(dtitle.get(k)).bind('click',{"type":k},commemt_select);
								}
								
                                comment_list();
								$("#sendinfo").click(Comment_add);
							});

function commemt_select(e){
	   $("#ctype").val(e.data.type+1);
	   $("#cpage").val(1);
       comment_list();
	}


function comment_list(){
	                       type = $("#ctype").val();
	                       type = !type ? 1 : type;
						   
                           var itemid  = $("#itemid").val();
						   var modelid = $("#modelid").val();
						   var page    = $("#cpage").val();
						   var level   = -1;
						   
						   if(type==1){
                               level   = -1;
						   }else if(type==2){
							   level   = 0;
						   }else if(type==3){
							   level   = 1; 
						   }else{
							   level   = 2;
						   }
						   
						   var url = "/cache/comment/"+modelid+"/"+(itemid%10)+"/"+(itemid>=10 ? parseInt(itemid%100/10) : 0)+"/";
						   url     += (level==-1 ? 99 :  level)+"_"+itemid+"_"+page+".txt?key="+Math.random();
						   
			               $.ajax({
			                     type     : "GET",
				                 url      : url,
                                 dataType : "json",
				                 global   : false,
				                 success  : comment_success,
				                 beforeSend:init_comment,
								 complete : function(XMLHttpRequest, textStatus){
													     textStatus=='error' ? init_error() : null;
													  }
 			                     });	
	}

function init_error(){
	   var html = '<table width=\"95%\" height=\"39\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#FFDBB3\">'+
              '  <tr>'+
              '    <td height=\"37\" bgcolor=\"#FFF3E6\">&nbsp; 暂无评论信息...</td>'+
              '  </tr>'+
              '</table>';  
	   $("#comment").html(html); 
	}	

function init_comment(){
	   var html = '<ul>'+
              '  <li>'+
              '    &nbsp;<img src=\"/images/loading.gif\" width=\"20\" height=\"20\" /> 正在加载评论...'+
              '  </li>'+
              '</ul>';  
	   $("#comment").html(html);		  
	}

function updateLevel(e){
	   var turl    = "/zmr.php?a=update&cid="+e.data.id+"&type="+e.data.type;
	   var supp    = "#sup"+e.data.key;
       var usupp   = "#usup"+e.data.key;
	   var vsupp   = "#vsup"+e.data.key;
       var vusupp  = "#vusup"+e.data.key;
	   
	   $.ajax({
			   type     : "GET",
			   url      : turl,
               dataType : "json",
		       global   : false,
			   success  : function(json){
				           if(json.status){
							  if(e.data.type==1){
								  $(supp).html('支持('+(parseInt($(vsupp).val())+1)+')');
							  }else{
								  $(usupp).html('反对('+(parseInt($(vusupp).val())+1)+')');
							  } 
						   }	
				   }	   		 
 			  });
	}

function comment_success(json){
	   $("#comment_total").html(json.cl0.comments); 
	   $("#comment_good").html(json.cl0.good);
	   $("#comment_common").html(json.cl0.common);
	   $("#comment_bad").html(json.cl0.bad);
	   
	   var html = '';
	   var top  = ((json.cl0.page-1)*10)+1;
	   $.each(json,function (key,value){
							   $("#thelastpage").val(json.cl0.pages); 
							   if(key!='cl0'){
								    html  += '<ul>'+
									         ' <li class=\"u\">&nbsp;TOP: '+top+'&nbsp;名称:'+value.name+' 发表时间: '+value.time+'</li><li class="d"><a id=\"sup'+key+'\" style=\"cursor:pointer\">支持('+value.support+')</a> / <a id=\"usup'+key+'\" style=\"cursor:pointer\">反对('+value.against+')</a>&nbsp;</li>'+
											 '<li class="c">'+value.content+'<input type=\"hidden\" id=\"vsup'+key+'\" name=\"vsup'+key+'\" value=\"'+value.support+'\" /><input type=\"hidden\" id=\"vusup'+key+'\" name=\"vusup'+key+'\" value=\"'+value.against+'\" /></li>'+
											 '</ul>';
											 top++;	
								   }
							 })
	   
	    var lastPage = parseInt($("#cpage").val())-1; 
		    if(lastPage<1){
				  lastPage = 1;
				}
		var nextPage = parseInt($("#cpage").val())+1;
		    if(nextPage>json.cl0.pages){
				  nextPage = json.cl0.pages;
				}
										
		html += '<div>'+
                '<span style=\"line-height:20px; margin:0px 8px 0px 8px;\">共 '+json.cl0.records+' 条评论</span>'+
				'<span style=\"line-height:20px; margin:0px 8px 0px 8px;\">共'+json.cl0.pages+'页</span>'+
                '<span style=\"line-height:20px; margin:0px 8px 0px 8px;\">&nbsp;当前页码&nbsp;'+json.cl0.page+' </span>'+
                '<span style=\"line-height:20px; margin:0px 8px 0px 8px; \"><a href=\"javascript:Comment_Page_go(1)\">首页</a> <a href=\"javascript:Comment_Page_go('+lastPage+')\">上一页</a> <a href=\"javascript:Comment_Page_go('+nextPage+')\">下一页</a> <a href=\"javascript:Comment_Page_go('+json.cl0.pages+')\">最后一页</a></span>'+
				'</div>';
      				
		$("#comment").html(html);
		
		var supp,usupp;
		$.each(json,function (key,value){ //初始化事件绑定
						        supp   = "#sup"+key;
								usupp  = "#usup"+key;
								$(supp).bind('click',{'id':value.id,'type':1,'key':key},updateLevel);
								$(usupp).bind('click',{'id':value.id,'type':2,'key':key},updateLevel);
							  });		
	}
	
	function Comment_Page_go(page){
		    $("#cpage").val(page);
			comment_list();
		}
	
	//add
	function Comment_add(){
		   if(is_fast()){
			     alert('您留言的速度太快了,稍微休息一下。');  
	             return false;
			   } 
		    
		   var say = $("#say").val();
		   if(say.length>100){
			     alert('评论内容不能太长!');
				 return false;
			   }else if (say.length<1){
				     alert('评论内容不能为空!');
					 return false;
				   }
         
		   var itemid  = $("#itemid").val();
		   var modelid = $("#modelid").val();
		   $.ajax({
			       type     : "POST",
				   url      : "/zmr.php?a=add&model="+modelid+"&itemid="+itemid+"&comment_level="+$("input[@type=radio][@id=comment_level][@checked]").val(),
				   data     : "cm="+encodeURIComponent(say),
                   dataType : "json",
				   global   : false,
				   timeout  : 5000,
				   success  : function (json){
					                if(json.status==1){
										  alert("评论发送成功!");
										  $("#say").val('');
										  $("#cpage").val($("#thelastpage").val());
										  comment_list();
										}else{
											  alert("评论发送失败!");
											}
								 },
		           complete : function (XMLHttpRequest, textStatus){
					        if(textStatus=="timeout"){
								  alert('超时,请稍后再发送信息!');
								}
						    var html = '<input name=\"sendinfo\" type=\"button\" id=\"sendinfo\" value=\" 提交评论 \"/>';
			                $("#sendrs").html(html);
							$("#sendinfo").click(Comment_add); 	
					   },						 
				   beforeSend:init_comment_add			   		 
 			                     });
		}
		
	function init_comment_add(){
		    $("#sendinfo").attr("disabled","disabled");
			var html = '<img src=\"/images/loading.gif\" width=\"20\" height=\"20\" /> 正在提交...';
			$("#sendrs").html(html);
		}

     //checking speed of comment
     function is_fast(){
	         var t_abs  = 5;
                 var sp     = 6;
			 var cookies = document.cookie.split(";");
			 var cl     = cookies.length;
			 var dat    = new Date();
			 var c_tm   = ""+dat.getHours()+dat.getMinutes()+dat.getSeconds();

			 for(var i=0;i<cl;i++){
			    tmp    = cookies[i].split("=");
				if(tmp[0]=='comment_speed'){
				  sp = Math.abs(parseInt(tmp[1])-parseInt(c_tm));
				  break;
				}
			 }
			 
             document.cookie = 'comment_speed='+c_tm;
		     if(sp<t_abs){
				 return true;
			  }else{
				 return false;
		      }
	 }   