//合并原来的/publicjs/table.js 和 /publicjs/visit_log.js

var visit_log_req;
var visit_log_flag=false;
var visit_flag_int =0
//js延迟执行
var visit_Timeout = setTimeout(visit_exc,5000);
//if(typeof(onContent)=="function"){
//	onContent(visit_log);
//}
//else{
//	if (document.all) {
//		window.attachEvent('onload',visit_log);
//	}
//	else {
//		window.addEventListener('load',visit_log, false);
//	}
//}
function visit_exc(){
	if(!visit_log_flag){
		//visit_log("1");
		visit_log_setStatinfo();
	}
}

/**
 * Description:根据flag判断被访问的类型，并取其点击数，评论数等信息
 * Input: flag标志位
 */
function visit_log_setStatinfo(){
	var callback = visit_log_Statinfo;
	Blog.setStatinfo(callback);
}

function visit_log_Statinfo(data){
//	alert(data);
	if(data == "1")
		visit_log("1")
}

/*
  Description:根据flag判断被访问的类型，并取其点击数，评论数等信息
  Input: flag标志位

*/
function visit_log(visit_flag){
	
	if(visit_restrict()){//防刷新判断
		return false;
	}
	
	var v_url = window.location.href;
	if(typeof(htmltype)=="undefined"&&v_url.indexOf("isfrienddiary.shtml")==-1){
		htmltype = 	"diary_browse";
	}
	
	if(htmltype=="post_detail"){//帖子动态载入，使用js触发统计访问
		return false;
	}

	var v_vuserid=userid;//被访问者id
	var v_objectid=objid;//被访问对象
	var v_pobjectid="";//被访问博客id
	var v_rtype="";//被访问对象类型
	var v_ptype="";//被访问博客类型
	
	if(typeof(htmltype)=="undefined"){
		htmltype = 	"diary_browse";
	}
	if(typeof(activityid)!="undefined"){//活动
		v_rtype=6;
		v_ptype=2;
		v_pobjectid=blogid;
	}
	else if((htmltype=="diary_browse"||typeof(diaryid)!="undefined")&&v_url.indexOf("isfrienddiary.shtml")==-1){
		v_ptype=(blogtype=="2")?2:1;
		v_pobjectid=blogid;
		if(v_ptype=="2")
			v_rtype=5;//话题
		else{
			v_rtype=1;//日志
		}
	}
	else if(htmltype=="music_browse")
		v_rtype=7;//音乐
	else if(htmltype=="photo_browse")
		v_rtype=2;//图片
	else if(htmltype=="video_browse")
		v_rtype=8;//视频
	else{
		v_rtype=(blogtype=="2")?9:3;
		v_objectid = blogid;
	}
	var v_userid="";
	var v_username="";
	var v_nickname="";
	var v_ouserid="";
	var v_state="0";
	if(public_islogin()){
		var cookies = GetUserinfoCookie();
		v_userid=cookies[1].split("=")[1];
		v_username=cookies[2].split("=")[1];
		v_nickname=cookies[3].split("=")[1];
		v_ouserid=cookies[6].split("=")[1];
		if(cookies.length>7){
			v_state=cookies[7].split("=")[1];
		}
	}
	var v_url = window.location.href;
	if((v_rtype==9||v_rtype==3)&&v_url.indexOf("diary,")!=-1){
		v_ptype=(blogtype=="2")?2:1;
		v_pobjectid=blogid;
		if(v_ptype=="2")
			v_rtype=5;//话题
		else{
			v_rtype=1;//日志
		}
	}
	var v_urltitle = objtitle;
	if(v_rtype==5||v_rtype==1){
		var diarytitle = document.getElementById("remaildiarytitle_"+diaryid);
		if(diarytitle!=null){
		 	v_urltitle = diarytitle.value;
		 }
	}
	
	if(visit_flag!="0"){//只取统计信息，不进行访问统计
		visit_flag="1";
	}
//	alert("v_objectid's value is:" + v_objectid);
	var args =new Array(v_vuserid,v_objectid,v_pobjectid,v_rtype,v_ptype,v_url,visit_flag,v_userid,v_username,v_nickname,v_ouserid,v_urltitle,v_state);
	var callback = visitServer;
	Blog.getStatinfo("","",args,callback);
	visit_log_flag=true;
}

/**
被动触发的访问记录
v_vuserid：被访问对象所有者,v_objectid：被访问对象,v_pobjectid：被访问对象父对象,
v_rtype：被访问对象类型,
v_ptype：被访问对象父类型
v_urltitle：被访问对象标题
**/
function visit_log_passivity(v_vuserid,v_objectid,v_pobjectid,v_rtype,v_ptype,v_urltitle){
	
	var v_userid="";//访问者
	var v_username="";
	var v_nickname="";
	var v_ouserid="";
	var v_state="0";
	if(public_islogin()){
		var cookies = GetUserinfoCookie();
		v_userid=cookies[1].split("=")[1];
		v_username=cookies[2].split("=")[1];
		v_nickname=cookies[3].split("=")[1];
		v_ouserid=cookies[6].split("=")[1];
		if(cookies.length>7){
			v_state=cookies[7].split("=")[1];
		}
	}
	var args =new Array(v_vuserid,v_objectid,v_pobjectid,v_rtype,v_ptype,window.location.href,1,v_userid,v_username,v_nickname,"",v_urltitle);
	Blog.getStatinfo("","",args,visitServer);
	visit_log_flag=true;
}


/*
  Description:显示日志，博客等的评论数，点击数，引用数
  Input:date状态信息
*/
function visitServer(date){
	if(date!=null){
		var rtype = date.rtype;
		var countDIV = document.getElementById("count_page");
		if(countDIV!=null&&date!=null&&date.blogcncount.length!=0){
			var html = "";
			var hits = date.blogcncount.toString();
			
			if(5-hits.length>0){
				for(var i=0;i<(5-hits.length);i++){
					html=html+"<img src=\"http://style.blogcn.com/blogcnpage/sysflash/t3/"+counttype+"/0.gif\">";
				}
			}
			for(var i=0;i<hits.length;i++){
				html=html+"<img src=\"http://style.blogcn.com/blogcnpage/sysflash/t3/"+counttype+"/"+hits.substring(i, i + 1)+".gif\">";
			}
			countDIV.innerHTML=html;
		}
		if(rtype==1||rtype==5){//日志或话题

			var diarypopulartotalDIV = document.getElementById("diarypopulartotal");
			if(diarypopulartotalDIV!=null){
				diarypopulartotalDIV.innerHTML = date.hits;
			}
			var diarycommentDIV = document.getElementById("diarycomment");
			if(diarycommentDIV!=null&&diarycommentDIV.innerHTML==""){
				diarycommentDIV.innerHTML = date.commentcout;
			}
			var diarytrackbackDIV = document.getElementById("diarytrackback");
			if(diarytrackbackDIV!=null){
				diarytrackbackDIV.innerHTML = date.hrefcount;
			}
		}
		else if(rtype==2){//图片
			var albumPicHitsDIV = document.getElementById("albumPicHits");
			if(albumPicHitsDIV!=null){
				albumPicHitsDIV.innerHTML = date.hits;
			}
			var commentsDIV = document.getElementById("div_Comments");
			if(commentsDIV!=null&&commentsDIV.innerHTML==""){
				commentsDIV.innerHTML = date.commentcout;
			}
		}
		else if(rtype==6){//活动
			var diarypopulartotalDIV = document.getElementById("diarypopulartotal");
			if(diarypopulartotalDIV!=null){
				diarypopulartotalDIV.innerHTML = date.hits;
			}
			var diarycommentDIV = document.getElementById("diarycomment");
			if(diarycommentDIV!=null&&diarycommentDIV.innerHTML==""){
				diarycommentDIV.innerHTML = date.commentcout;
			}
			var diarytrackbackDIV = document.getElementById("diarytrackback");
			if(diarytrackbackDIV!=null){
				diarytrackbackDIV.innerHTML = "0";//date.hrefcount;
			}
		}
		else if(rtype==7){//音乐
			showStatistics(date);
		}
		else if(rtype==8){//视频
			showStatistics(date);
		}
	}
}





/*
  Description:将数据记录以表格形式显示
*/
function tableaddRows (ele, data, cellFuncs,col)
{

    var orig = ele;
    ele = document.getElementById(ele);
    if (ele == null)
    {
        alert("in table.js.tableaddRows");
        return;
    }
        tableaddRowsInner(ele, data, cellFuncs,col);
}

/*
  Description:将数据以表格形式显示
  Input:ele控件对像， data需展示的数据源 cellFuncs列数
*/
function tableaddRowsInner (ele, data, cellFuncs,col)
{
    
    var constomdiv = '<table cellpadding="0" cellspacing="0" border="0" width="90%">';
    if (DWRUtil._isArray(data))
    {
        // loop through data source
        for (var i = 0; i < data.length; i++)
        {
             var frag = document.createElement("div");
             if ((i+1)%col==1)
             {
               // constomdiv = constomdiv+'<tr align="center">';
                 constomdiv = constomdiv+'<tr align="left">';
                
             }
             tableaddRowInner(frag, data[i], cellFuncs);
             constomdiv=constomdiv+frag.innerHTML;
             if ((i+1)%col==0)
             {
               constomdiv = constomdiv+ '</tr>';
             }
             if ((i+1)%col!=0 && i==data.length-1){
                 for(var j=0;j<data.length-(i+1)%col;j++){
                   constomdiv = constomdiv+ '<td></td>';
                 }
                  constomdiv = constomdiv+ '</tr>';
             }
        }
    }
    else if (typeof data == "object")
    {
        for (var row in data)
        {
            tableaddRowInner(frag, row, cellFuncs,col);
        }
    }
     ele.innerHTML=constomdiv;

}

/*
  Description:将表格增加一列
*/
function tableaddRowInner (frag, row, cellFuncs)
{   
   
    for (var j = 0; j < cellFuncs.length; j++)
    {

        var func = cellFuncs[j];
        var td;

        if (typeof func == "string")
        {
            td = document.createElement("td");
            var text = document.createTextNode(func);
            td.appendChild(text);
        }
        else
        {
            var reply = func(row);

            if (DWRUtil._isHTMLElement(reply, "td"))
            {
                td = reply;
            }
            else if (DWRUtil._isHTMLElement(reply))
            {
                td = document.createElement("td");
                td.appendChild(reply);
            }
            else
            {
                td = document.createElement("td");
                td.innerHTML = reply;
            }
             
              
        }
    }
     frag.appendChild(td);
}

/*
点击数限制
*/
function visit_restrict(){
	var v_url = window.location.href;
	var visit_time = GetCookieValue('visit_time');
	if(visit_time==null){
		visit_time = "";
	}
	if(visit_time == ""){
		set_restrictcookie(1);
		return false;
	}
	else{
		var values=unescape(visit_time).split('|');
		if(v_url!=values[1]){
			set_restrictcookie(1);
			return false;
		}
		else{
			var c_time = timestamp();//当前时间戳
			var cookie_time = values[0];//cookie中的时间戳
			if(c_time-cookie_time<15000){
				if(values[2]>50){
					set_restrictcookie(values[2]);
					return true
				}
				else{
					set_restrictcookie(values[2]*1+1);
					return false;
				}
			}
			else{
				set_restrictcookie(1);
				return false;
			}
		}
	}

}


