/*--------By yxw 08.11.28---------*/
var data,curpage,pagesize,cnum=0;
var userUrl='http://passport.titan24.com/index.php?controller=user&action=touch&username=';
var comment={
	$:function(id){return document.getElementById(id);},

	init:function(){init_flash();},
	/**
	 * 根据文章tid得到评论页URL地址
	 * tid 文章tid
	 * ctypeid 项目id,默认为1
	 * templateid 模板id,默认为1,目前设定为templateid为2时得到json格式数据的页面.
	 * pagesize 每页条数,默认为10
	 * page 页码,默认为1
	 */
	getCommentUrl:function (tid,ctypeid,templateid,pagesize,page){
		var comment_domain='http://comment.titan24.com/';
		var url='';
		
		ctypeid=arguments[1]?arguments[1]:1;	
		templateid=arguments[2]?arguments[2]:1;	
		pagesize=arguments[3]?arguments[3]:10;	
		page=arguments[4]?arguments[4]:1;

		tid=tid.toString();
		if(!tid)return;
		var temp=new Array();
		for(i=0;i<tid.length;i=i+2){			
			temp[i]=tid.substr(i,2)+'/';		
		}
		var str=temp.join('');	
		url= ctypeid + '/'+str+'/' + templateid+'/'+ pagesize+'/'+page+'.htm';	
		url= url.replace(/\/\//g,'\/');
		url= comment_domain +url;
		return url;
	},

	getCommentNum:function (tid,ctypeid,id) {	
		//var url = "http://comment.titan24.com/index.php?ctype="+ctypeid+"&tid="+tid+"&temp="+Math.random();
		var url =  this.getCommentUrl(tid, ctypeid ,2);
		var self=this;	
		FlashRequest.get(url, null, null, "GBK", function(res){					
					self.setCommentNum(res,id);
				}
		);
	},
	setCommentNum:function (str,id3) {		
		var data = eval('(' + str + ')');
		var CommentNumber = data.RecordCount;
		this.$(id3).innerHTML = CommentNumber;
	},

	getCommentHref:function (tid,ctypeid,templateid,id1,id2){
		var commentUrl =  this.getCommentUrl(tid, ctypeid ,templateid);
		if(this.$(id1))this.$(id1).href = commentUrl;
		if(this.$(id2))this.$(id2).href = commentUrl + "#write";
	},

	///////////////////////////////////////////////////////////////////////
	
	getCommentList:function (tid,ctypeid,templateid,pagesize,page,id1,id2) {	
		var url=this.getCommentUrl(tid,ctypeid,templateid,pagesize,page);	
		url=url+'?r='+Math.random();
		//document.write(url);
		curpage= page;
		pagesize= pagesize;
		var self=this;			
		FlashRequest.get(url, null, null, "GBK", function (str) {				
				data = eval('(' + str + ')');					
				var Page = data.Page;
				var PageSize = data.PageSize;
				var PageCount = data.RecordCount;					
				self.reviewPage(tid,ctypeid,templateid,pagesize,page,PageCount,id1,id2);
				self.initCommentList(data.List,id1);
			}
		);	
	},

	initCommentList:function (obj,id){			
		if(!obj)return;	
		var html = "";
		cnum=0
		for(k in obj) {
			html+='<div class="comment">';
			html+='<div class="comtop">';
			html+='<h3 ><strong>'+obj[k].author+'</strong> '+obj[k].location+' 回复：'+obj[k].fromip+'</h3>';
			html+='<div class="time"> '+obj[k].time+'发表</div>';
			html+='</div>';
			html+='<div class="comn" >';
			if(obj[k].quote){	
				html+='<div class="comy">';
				html+=obj[k].quote;	
				html+='</div>';
			}
			html+='<div >'+obj[k].message+'</div>';			
			html+='</div></div>';	
			cnum++;
		}
		
		this.$(id).innerHTML = html;
	},
	
	/*
	reviewPage1:function (tid,ctypeid,templateid,pagesize,page,PageCount,id1,id2) {
		var html = "";
		html += '<a href="#" onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+1+',\''+id1 +'\''+',\''+id2 +'\');return false">第一页</a> ';
		for(var i=1; i<PageCount; i++) {
			if(i == page) {
				html += '<font color="red">[' + i + ']</font> ';
			}else {
				html += '<a href="#"  onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+i+',\''+id1 +'\''+',\''+id2 +'\');return false">[' + i + ']</a> ';
			}
		}
		html += '<a href="#"  onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+PageCount+',\''+id1 +'\''+',\''+id2 +'\');return false">末页</a>';		
		comment.$(id2).innerHTML=html;
	},
	*/
	reviewPage:function (tid,ctypeid,templateid,perpage,curpage,num,id1,id2) {
		var page,offset,pages,from,to,multipage='';
		var pagesize=perpage;	
	
		if(parseInt(num) > parseInt(perpage)) {			
			page = 10;
			offset = 5;
			pages = Math.ceil(num / perpage);			
			if(page > pages) {
				from = 1;
				to = pages;
			}else{
				from = curpage - offset;
				to = curpage + page - offset - 1;
				if(from < 1) {
					to = curpage + 1 - from;
					from = 1;
					if((to - from) < page && (to - from) < pages) {
						to = page;
					}
				}else if(to > pages) {
					from = curpage - pages + to;
					to = pages;
					if((to - from) < page && (to - from) < pages) {
						from = pages - page + 1;
					}
				}
			}
			
			multipage = (curpage > 1 ? '<a href="#"  onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+(curpage - 1)+',\''+id1 +'\''+',\''+id2 +'\');return false" class="pgup">上一页</a> ' : '')+(curpage - offset > 1 && pages > page ? '<a href="#" onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+1+',\''+id1 +'\''+',\''+id2 +'\');return false">第1页</a> ' : '');
			for(var i = from; i <= to; i++) {
				multipage += i == curpage ? '<a href="#" class="current">'+i+'</a> ' : '<a href="#"  onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+i+',\''+id1 +'\''+',\''+id2 +'\');return false">'+i+'</a> ';
			}
			multipage += (to < pages ? ' <a href="#"  onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+pages+',\''+id1 +'\''+',\''+id2 +'\');return false">第'+pages+'页</a>' : '')+(curpage < pages ? '<a href="#"  onclick="comment.getCommentList('+tid+','+ctypeid+','+templateid+','+pagesize+','+(++curpage)+',\''+id1 +'\''+',\''+id2 +'\');return false" class="pgdn"> 下一页</a>' : '');
			multipage = multipage ? multipage : '';
		}		
			comment.$(id2).innerHTML=multipage;		
	},
	
	sendMessage:function (tid,ctypeid,templateid,pagesize,page,id1,id2){
		if(!ctypeid || !tid)return;

		var message = this.$('message').value;
		if(!message) {
			alert("你说些什么呢？");
			this.$('message').focus();
			return false;
		}
		if(message.length > 300) {
			alert("不要太长哦,最多150个汉字");
			return false;
		}	

		var author=this.$('author').value?this.$('author').value:"";
		author = this.$('anonymity').checked==true ? "anonymity" : author;
		if (author=='')	author = "noanonymity";		
	
		//var ck= document.getElementById('ck').value;
		var self=this;	
		FlashRequest.post('http://comment.titan24.com/index.php', "ctype=" + ctypeid +"&anonymity=" + author + "&message=" + message + "&tid=" + tid, null, null, "GBK", function (ret) {
				if(ret=='wait'){
					alert("你发表评论太快了,需要先休息一下！");
				}else if(ret=='nologin'){
					alert("您还没有登录!");
				}else if(ret == '0') {
					alert('发表失败!');	
				}else if(ret == '1') {
					alert('发表成功');					
					//self.getCommentList(tid,ctypeid,templateid,pagesize,page,id1,id2);					
					if(curpage ==1){	
						var d = new Date();
						var month=d.getMonth();
						month= month> 8?month+1:'0'+(month+1);

						var day=d.getDate();
						day= day> 9?day:'0'+day;

						var hours=d.getHours();
						hours= hours> 9?hours:'0'+hours;

						var minute=d.getMinutes();
						minute= minute> 9?minute:'0'+minute;

						var second=d.getSeconds();
						second= second> 9?second:'0'+second;

						var time= d.getFullYear()+ '-'+ month+ '-'+ day+' '+ hours+ ':'+ minute+ ':'+second;	
						var href= (author!='anonymity' && author) ? userUrl+encodeURIComponent(author):author;
						var arr={
								author:author=='anonymity'?'体坛网友':'<a href="'+href+'" target="_blank">'+author+'</a>',
								location:'[来自未知]',
								fromip:'*.*.*.*',
								time:time,
								quote:'',
								message:message
						};
						
						data.List.unshift(arr);	
						if(cnum==pagesize)data.List.pop(); 
						//alert(data.List[0].message);
						self.initCommentList(data.List,id1);
					}

				}else if(ret == '2') {
					alert('该文章不允许评论!');	
				}else if(ret == '3') {
					alert('该文章所在项目不允许评论!');	
				}else if(ret == '4') {
					alert('该文章所在栏目不允许评论!');	
				}else if(ret== '5') {
					alert('文章信息有误');
				}else{			
					alert(ret);
				}
				self.$('message').value='';
			}
		);
	}
}


