var timeline = {
    setup: function(b, refresh_key){
		//alert(b.place.region)
		/*txmap_1*/
		/** 初始化参数 */
		this._config = {
			"place":'{}',//地址参数
			"url":'',//发起的url
			"currentPage":1,
			"page_size":0,
			"urlUpdate":'',//update的链接
			"autoUpdate":true,
			"type":1,
			"el":"txweblist",
			"count":30,
			"intervalTime":30000,
			"emptyFun":null,
			"afterUpdateFun":null,
			"sortType":1,
			"nextPageTimer":0,
			"clickNextPage":false,
			"loadingIco":$("#txweblistLoading"),
			"maxId":0,
			"timeToken":0,
			'ajax_load':1,//是否开启ajax获取数据
			f_text:1,  //是否包含文字
			f_photo:1, //是否包含图片
			f_video:1  //是否包含视频
		};
		this._refresh_key = ['f_text', 'f_photo', 'f_video'];
		var k;
		//充实初始化参数
		for (k in this._config){
			if (typeof b[k]=='undefined'){
				b[k] = this._config[k];
			}
		}
		//转移到this里
		for (k in b) {
			if (typeof b[k]!='undefined') {
				//特殊化处理
				if ('place' == k && typeof b.place == 'object') {
					this.place='{"region":"'+b.place.region+'","bodys":"'+b.place.bodys+'","thing":['+b.place.thing[0]+','+b.place.thing[1]+','+b.place.thing[2]+'], "lat":'+b.place.lat+', "lng":'+b.place.lng+', "place_id":'+b.place.place_id+', "radius":'+b.place.radius+', "t":"'+b.place.t+'"}'
				}else{
					this[k] = b[k];
				}
			}
		}
		
		if (typeof refresh_key !='undefined'){
			this._refresh_key = refresh_key;
		}
		
        $.ajaxSetup({cache: false});
		
        var a = document.getElementById(this.el);
        if (a != null && a.tagName == "UL") {
            this.timelineBox = a;
			var ap=Number(a.getAttribute('nextPageStyle'));
			if(ap){
				this.nextPageStyle=ap;
				//nextPageStyle='2' 分页样式为更多，无限级展开,不设置该属性是则为普通的上一页，下一页样式
				//变为“更多”样式只需在，HTML中ID为txweblist的UL加上nextPageStyle='2'自定义属性
			}
        } else {
            return
        }
        var c = window.location.hash ? window.location.hash : "#p1";
    },
    init: function(){
    	if (this.ajax_load){
    		this.loadData();
    	}else{//这个时候刷新本页
    		var refresh = this.url;
    		var k, t;
    		for(k in this._refresh_key){
    			t = this._refresh_key[k];
    			refresh = refresh + "&"+t+"="+this[t];
    		}
    		window.location.href = refresh;
    	}    	
    },
    nextPage: function(){
		this.clickNextPage=true;
		if(this.nextPageStyleIs2()){
			this.nextPageTimer=$('#'+this.timelineBox.id+' li:last').attr('time');
			//this.autoUpdate=false;
			$('#'+this.timelineBox.id+' li:last').addClass('pageLastLi')
			$('.nextPageStyleIs2').hide();
			$('.nextPageStyleIs2_loadding').show();
		}
        this.currentPage++;
        this.loadData();
		this.nextPageTimer=0;
    },
    previousPage: function(){
        if (this.currentPage > 1) {
            this.currentPage--;
            this.loadData()
        }
        else {
            ye_msg.open("\u73b0\u5728\u662f\u7b2c\u4e00\u9875\uff0c\u6ca1\u6709\u4e0a\u4e00\u9875\u4e86", 1, 2)
        }
    },
	nextPageStyleIs2:function(){
		/*if(this.firstLodding==true){
			return true;
		}*/
		if(this.nextPageStyle==2 && this.clickNextPage==true){
			return true;
		}else{
			return false;
		}
		
	},
    //单纯的做获得数据的操作,在地图模块使用
    justLoadData:function(callback){
    	$.getJSON(this.url, {
            pid: this.currentPage,
            type: this.type,
            sortType: this.sortType,
            page_size :this.page_size,
			/*txmap_2*/
			place:this.place			
        }, function(b){
            if (b.result == "noLogin") {
                ye_msg.open("\u8bf7\u5148\u767b\u5f55\uff01", 1, 2);
                window.setTimeout("window.location='/login.php?aid=login'", 1500);
                return
            }
            else {
                if (b.result == "noUserId") {
                    ye_msg.open("\u7f51\u7edc\u5f02\u5e38\uff0c\u83b7\u53d6\u53c2\u6570\u9519\u8bef\uff0c\u8bf7\u91cd\u8bd5\uff01", 1, 2);
                    window.setTimeout("window.location.reload()", 1500);
                    return
                }
            }
            if (typeof callback=='function') {
            	callback(b[0]);
            }
        });
    },
    /**
     * 获得查询传递的条件数据
     */
    getQueryData:function(){
    	var data={};
    	data.pid = this.currentPage;
        data.type = this.type;
        data.sortType = this.sortType;
        data.place = this.place;
        data.nextPageTimer = this.nextPageTimer;/*翻页时记录从哪一条开始翻*/
        //过滤类型，纯文字，含图片，含视频
        if (this.f_text){
        	data.f_text = this.f_text;
        }
        if (this.f_photo){
        	data.f_photo = this.f_photo;
        }
        if (this.f_video){
        	data.f_video = this.f_video;
        }
        return data;
    },
    loadData: function(){
		//alert(this.afterUpdateFun)
        var a = this;
		if(!a.nextPageStyleIs2()){
			this.loadingIco.show();
		}
        this.maxId = 0;
        this.timeToken = 0;
        
        var data = this.getQueryData(); //ajax传递的数据        
        
        $.getJSON(this.url, data, function(b){
            if (b.result == "noLogin") {
                ye_msg.open("\u8bf7\u5148\u767b\u5f55\uff01", 1, 2);
                window.setTimeout("window.location='/login.php?aid=login'", 1500);
                return
            }
            else {
                if (b.result == "noUserId") {
                    ye_msg.open("\u7f51\u7edc\u5f02\u5e38\uff0c\u83b7\u53d6\u53c2\u6570\u9519\u8bef\uff0c\u8bf7\u91cd\u8bd5\uff01", 1, 2);
                    window.setTimeout("window.location.reload()", 1500);
                    return
                }
            }
			if(!a.nextPageStyleIs2()){
            	$(a.timelineBox).html("");
			}
			
            a.process(b[0]);
			
            if (typeof  update_markers != "undefined") {
        		update_markers(b[0]);
        	}
            if (a.currentPage == 1 && b[0].items.length < 1 && a.emptyFun) {
                a.emptyFun()
            }
            if (b[0].items.length > 20) {
                $("#toTop").show()
            }
            a.pageSign = b[0].page;
            a.createPageNav()
			a.clickNextPage=false;
        });
        if (this.autoUpdate && this.currentPage == 1) {
            this.createAutoUpdate()
        }
        else {
            this.destroyAutoUpdate()
        }
		
    },
	
    createAutoUpdate: function(){
        if (!this.timer) {
            this.timer = window.setInterval("timeline.update()", this.intervalTime)
        }
    },
    destroyAutoUpdate: function(){
        if (this.timer) {
            window.clearInterval(this.timer)
        }
    },
	/*resetPlace:function(b){
		if(b && typeof b==='object'){
			if(b.place){
				this.place.place=b.place;
			}
			if(b.place.region){
				this.region=b.region;
			}
			if(b.place.bodys){
				this.bodys=b.bodys;
			}
			if(b.place.thing){
				this.thing=b.thing;
			}
		}
	},*/
    update: function(repear,fd){//当转发时repear=='repear',fd={place:{},//地点region:'100Km',//范围bodys:'everybody',//人thing:['checkins','notes','photos'],//消息timeline://thimline参数}
	//this.resetPlace(fd);
	//发送消息时是否选择了地点
	//if(fd && fd.account=='post'){
		//alert(txmap.pick_place.name)
	//}
	//alert(this.urlUpdate)
	if(repear=='repear'&& !this.urlUpdate){//转发但不需要重新装载内容，也就是装载地址不存在时
		setTimeout(function(){ye_msg.open('\u8f6c\u53d1\u6210\u529f\uff01', 1, 1)},100);
		setTimeout(function(){ye_msg_2.closer(1)},100);
	}else{
        var a = this;
        $.getJSON(this.urlUpdate, {
            type: this.type,
            maxTime: this.timeToken,
            sortType: this.sortType,
			place:this.place
        }, function(b){
            if (b.result == "noLogin") {
                ye_msg.open("\u8bf7\u5148\u767b\u5f55\uff01", 1, 2);
                window.setTimeout("window.location='/login.php?aid=login'", 1500);
                return
            }
            else {
                if (b.result == "noUserId") {
                    ye_msg.open("\u7f51\u7edc\u5f02\u5e38\uff0c\u83b7\u53d6\u53c2\u6570\u9519\u8bef\uff0c\u8bf7\u91cd\u8bd5\uff01", 1, 2);
                    window.setTimeout("window.location.reload()", 1500);
                    return
                }
            }
			/*repear=='repear'转发b[0].items.length>=1有转发内容,关非取消转发后再转发*/
			if(repear=='repear'&& b[0].items.length>=1){//updat应该是用于单条信息，如发表，转发等，所以都取第一条做处理
				b[0].items.length=1;
				if(repear=='repear'){
				setTimeout(function(){ye_msg.open('\u8f6c\u53d1\u6210\u529f\uff01', 1, 1)},100);
				setTimeout(function(){ye_msg_2.closer(1)},100);
				}
			}
            a.process(b[0]);            
        })
	}
    },
    createItem: function(f){
        var e = f.status;
        var d = f.userInfo;
        var b = f.relationInfo; 
        var a = f.replyInfo;
        var i = b.favorite_flag ? "\u53d6\u6d88\u8f6c\u53d1" : "\u8f6c\u53d1";
        var c = "";
        //if (e.replyId > 0) {
        if(e.replyId !=''){
            //c = ' <a target="_blank" href="/k/topic.php?status_id=' + e.id + '"			title="\u67e5\u770b\u5bf9\u8bdd">\u67e5\u770b\u5bf9\u8bdd</a> | '
        }
        var vip_icon = "";
        switch(d.vip){
        	case '1':
        		vip_icon = 'px.gif';
        		break;//名人
        	case '2':
        		vip_icon = 'bx.gif';
        		break;//用户
        	case '3':
        		vip_icon = 'ox.gif';
        		break;//商家
        }
        var h = '<li id="lit' + e.id + '" time="'+b.time+'"';
		if(e.created_source == 5 || e.created_source == 9 || e.created_source == 10 || e.created_source == 11){
			h +='class="follow"';
		}
		h += '><a class="userphoto" href="' + d.mid + '" target="_blank"><img alt="' + d.name + '" class="photo_l" src="' + d.image + '"/></a>';
        h += '<div class="status_reply_list_wrap"><p class="statusContent"><a class="username" href="' + d.mid + '" target="_blank">' + d.name + "</a>" + (d.vip > 0?'<img border="0" align="absmiddle" src="http://site.tongxueimg.com/t/images/confirm/' + vip_icon + '" />':'') + b.privateLabel + " " + e.content + "</p>";
		if(e.swf){
			//h += '<div onmouseout="$(this).find(\'.showSwf_resize\').hide();" onmouseover="$(this).find(\'.showSwf_resize\').show();" class="showSwf"><embed height="200" width="225" lk_media="yes" id="status_media_'+e.id+'" src="'+e.swf+'" type="application/x-shockwave-flash" allow="" script="" access="always" allowfullscreen="true" wmode="transparent"/><a onfocus="this.blur()" onclick="showSwf_resize(this)" resize="yes" class="showSwf_resize" href="javascript:void(0)" style="display: none;"/></div>'
			h += e.swf;
		}else{
			h += this.imgArr(e.picName, b.picHref);
		}
		
		if(a.id){
			var reply_status_class = 'status_reply';
			if(e.created_source == 2){
				reply_status_class += ' zhuan';
			}
			h += '<div class="' + reply_status_class + '"><div class="top"><div class="c"></div></div><div class="cont"><div class="c"><div class="innercont"><p><a class="username" href="/' + a.mid + '" target="_blank">' + a.name + '</a>\uff1a<span>' + a.content + '</span><span class="option2">     ';
			h += '<a name="_comment_count_miniblog" href="/detail/' + a.id + '">\u539f\u6587\u56de\u590d';
			if(a.reply_num != 0){
				h += '(' + a.reply_num + ')';
			}
			h += '</a></span></p>';
			//h += '<p><a href="/place/'+a.place.id+'">@'+a.place.name+'</a></p>'
			if(a.swf){
				h += a.swf;
			}else{
				h += this.imgArr(a.picName, a.picHref);
			}
			h += '</div></div></div><div class="bottom"><div class="c"></div></div></div>';
		}
        h += '<span class="conTinge meta">@<a href="/place/'+e.place.id+'" title="'+e.place.title+'">'+e.place.name+'</a> - <a target="_blank" title="' + b.addTime + '" href="/detail/' + e.id + '">' + e.sendTime + "</a> - " + e.from;
        /*if (e.created_source != 5) {*/
            h += '<div class="action">' + c;
        	if (d.myself == true && !b.favorite_flag) {
                h += '<a href="#" onclick="return delStatus(\'' + e.id + "\',$('#lit" + e.id + '\'),1);" title="\u5220\u9664\u6b64\u6d88\u606f">\u5220\u9664</a>';
            }
        	if (e.created_source != 5 && e.created_source != 9 && e.created_source != 10 && e.created_source != 11) {
        		if (d.myself == true && !b.favorite_flag) {h+='&nbsp;|&nbsp;';}
            h += '<a href="#" title="' + i + '" id="fav' + e.id + '" onclick="return favoriteStatus.process(\'fav' + e.id + "',\'" + e.id + '\');">' + i + '</a>';
            if (e.zhuan_num != 0 ){
            	h += '(' + e.zhuan_num + ')';
            }
            h += "&nbsp;|&nbsp;";
            h += '<a href="####" sid="' + e.id + '" onclick="ajaxReply.init(this)" title="\u56de\u590d\u6b64\u6d88\u606f">\u56de\u590d';
            if (e.reply_num != 0 ){
            	h += '(' + e.reply_num + ')';
            }
            h += '</a>';
            if (d.isAdmin) {
                h += '&nbsp;|&nbsp;<a href="#" onclick="return delStatus(\'' + e.id + "\',$('#lit" + e.id + '\'),2);" title="\u5c4f\u853d\u5728\u5927\u5385\u663e\u793a">\u5c4f\u853d</a>'
            }
        	}
            h += "</div>"
        /*}*/
        h += "</span></div>";
        h += '<div id="boxid_'+ e.id + '" style="display:none;"></div>';
        h += "</li>";
        var g = b.time - 0;
        if (g >= this.timeToken) {
            this.timeToken = g;
            this.maxId = e.id
        }
        return h
    },
    imgArr: function(a, b){    	
        if (a == "") {
            return ""
        }
        a = a + ";"
        var d = a.split(";");
        var e = "";        
        if (d.length > 0) {
            e = "<p>";
            for (var c = 0; c < d.length - 1; c++) {
            	var ori_src = d[c].replace("tb", "n");
            	ori_src = ori_src.replace(/\/s([a-zA-Z0-9_]*?\.jpg)/g, "/n$1");
                if (b != null && b != "") {     
					if(d[c] != ""){
                    //e += '<a href="' + b + '" ><img src="' + d[c] + '" class="h_postimg" /></a> '
					e += '<a href="' + ori_src + '" target="_blank" onclick="return hs.expand(this)"><img src="' + d[c] + '" class="h_postimg" /></a> ';
					}
                }
                else {
					if(d[c] != ""){
					e += '<a href="' + ori_src + '" target="_blank" onclick="return hs.expand(this)"><img src="' + d[c] + '" class="h_postimg" /></a> '
					}
                }
            }
            e += "</p>"
        }
        
        return e
    },
    process: function(e){
		this.loadingIco.hide();
        var b = e.items;
        var a = b.length - 0;
        if (a > 0) {
            var c = [];
            var f = "";
			
            for (var d = a - 1; d >= 0; d--) {
                if ((b[d].relationInfo.time - 0) >= this.timeToken && b[d].status.id != this.maxId) {
                    c.unshift(this.createItem(b[d]))//最后的再最前面，因为是从后面开始，仍是正序
                }
            }
            f = c.join("");//把所有元素放存为一个字符串
			if(this.clickNextPage==true){
				$(this.timelineBox).append(f);
				
			}else{
				$(this.timelineBox).prepend(f);
			}
			
            //this.onHoverLi();
            if (this.afterUpdateFun) {
                this.afterUpdateFun()
            }
        }
        
		if(!this.nextPageStyleIs2()){
			this.clear()
		}
    },
    clear: function(){
        var b = this.timelineBox.getElementsByTagName("LI").length;
        if (b > this.count) {
            var a = b - this.count;
            for (var c = 0; c < a; c++) {
                $(this.timelineBox).find("li:last").remove()
            }
        }
    },
    onHoverLi: function(){
        $(this.timelineBox).find("li").hover(function(){
            $(this).addClass("over")
        }, function(){
            $(this).removeClass("over")
        })
    },
    /*createPageNav: function(){
        var a = document.getElementById("pageNumber");
        if (a == null) {
            return
        }
        var b = "";
        switch (this.pageSign) {
            case 0:
                $(a).hide();
                return;case 1:
				if(this.nextPageStyleIs2()){
                	b = '<a  href="####"   class="nextPageStyleIs2"   onclick="return timeline.nextPage()">\u67e5\u770b\u66f4\u591a</a><a style="display:none" class="nextPageStyleIs2_loadding"></a>'
				}else{
                	b = '<a  href="#container"  onclick="return timeline.nextPage()">\u4e0b\u4e00\u9875</a>'
				}
                break;
            case 2:
				if(this.nextPageStyleIs2()){
					b+='';
				}else{
                	b = '<a  href="#container"  onclick="return timeline.previousPage()">\u4e0a\u4e00\u9875</a>';
				}
				b+='';
                break;
            case 3:
                if(this.nextPageStyleIs2()){
                	b = '<a href="####"    class="nextPageStyleIs2"   onclick="return timeline.nextPage()">\u67e5\u770b\u66f4\u591a</a><a style="display:none" class="nextPageStyleIs2_loadding"></a>';
				}else{
               		b = '<a href="#container"  onclick="return timeline.previousPage()">\u4e0a\u4e00\u9875</a> <a  href="#container"  onclick="return timeline.nextPage()">\u4e0b\u4e00\u9875</a>';
				}
				break
        }
        $(a).html(b).show()
    }*/
	createPageNav: function(){
        var a = document.getElementById("pageNumber");
        if (a == null) {
            return
        }
        var b = "";
        switch (this.pageSign) {
            case 0:
                $(a).hide();
                return;case 1:
                	b = '<a  href="####"   class="nextPageStyleIs2"  style="font-size:14px;"  onclick="return timeline.nextPage()">\u67e5\u770b\u66f4\u591a</a><a style="display:none" class="nextPageStyleIs2_loadding"></a>'
                break;
            case 2:
					b+='';
				b+='';
                break;
            case 3:
                	b = '<a href="####"    class="nextPageStyleIs2"   style="font-size:14px;" onclick="return timeline.nextPage()">\u67e5\u770b\u66f4\u591a</a><a style="display:none" class="nextPageStyleIs2_loadding"></a>';
				break
        }
        $(a).html(b).show()
    }
};
