var timeline = {
    setup: function(b){
        $.ajaxSetup({
            cache: false
        });
        this.url = b.url;
        this.urlupdate = b.urlUpdate;
        if (b.currentPage) {
            this.currentPage = b.currentPage
        }
        else {
            this.currentPage = 1
        }
        if (b.autoUpdate != undefined) {
            this.autoUpdate = b.autoUpdate
        }
        else {
            this.autoUpdate = true
        }
        if (b.type) {
            this.type = b.type
        }
        else {
            this.type = 1
        }
        if (b.el) {
            this.el = b.el
        }
        else {
            this.el = "txweblist"
        }
        if (b.count) {
            this.count = b.count
        }
        else {
            this.count = 30
        }
        if (b.intervalTime) {
            this.intervalTime = b.intervalTime
        }
        else {
            this.intervalTime = 30000
        }
        if (b.emptyFun && $.isFunction(b.emptyFun)) {
            this.emptyFun = b.emptyFun
        }
        if ($.isFunction(b.afterUpdateFun)) {
            this.afterUpdateFun = b.afterUpdateFun
        }
        this.loadingIco = $("#txweblistLoading");
        this.maxId = 0;
        this.timeToken = 0;
        var a = document.getElementById(this.el);
        if (a != null && a.tagName == "UL") {
            this.timelineBox = a
        }
        else {
            return
        }
        var c = window.location.hash ? window.location.hash : "#p1";
        if (b.sortType) {
            this.sortType = b.sortType
        }
        else {
            this.sortType = 1
        }
    },
    init: function(){
        this.loadData()
    },
    nextPage: function(){
        this.currentPage++;
        this.loadData()
    },
    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)
        }
    },
    loadData: function(){
        var a = this;
        this.loadingIco.show();
        this.maxId = 0;
        this.timeToken = 0;
        $.getJSON(this.url, {
            pid: this.currentPage,
            type: this.type,
            sortType: this.sortType
        }, 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
                }
            }
            $(a.timelineBox).html("");
            a.process(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()
			
        });
        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)
        }
    },
    update: function(repear){//当转发时repear=='repear'
	//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
        }, 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){
				b[0].items.length=1;
				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 h = '<li id="lit' + e.id + '" time="'+b.time+'">					<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/v4/k/images/' + (d.vip == 2?"auth":"vip") + '_icon.gif"/>':"") +" " + 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="/k/detail.php?status_id=' + a.id + '">\u539f\u6587\u56de\u590d';
			if(a.reply_num != 0){
				h += '(' + a.reply_num + ')';
			}
			h += '</a></span></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 target="_blank" title="' + b.addTime + '" href="/k/detail.php?status_id=' + 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>&nbsp;|&nbsp;';
            }
            h += '<a href="#" title="' + i + '" id="fav' + e.id + '" onclick="return favoriteStatus.process(\'fav' + e.id + "',\'" + e.id + '\');">' + i + "</a>&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++) {            
                if (b != null && b != "") {            
					if(d[c] != ""){
                    e += '<a href="' + b + '" ><img src="' + d[c] + '" class="h_postimg" /></a> '
					}
                }
                else {
					if(d[c] != ""){
                    e += '<a href="' + d[c].replace("tb", "n") + '" target="_blank" onclick="return hs.expand(this)"><img src="' + d[c] + '" class="h_postimg" /></a> '
					}
                }
            }
            e += "</p>"
        }
        
        return e
    },
    process: function(e){
        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("");
            $(this.timelineBox).prepend(f);
            //this.onHoverLi();
            if (this.afterUpdateFun) {
                this.afterUpdateFun()
            }
        }
        this.loadingIco.hide();
        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:
                b = '<a href="#container" onclick="return timeline.nextPage()">\u4e0b\u4e00\u9875</a>';
                break;
            case 2:
                b = '<a href="#container" onclick="return timeline.previousPage()">\u4e0a\u4e00\u9875</a>';
                break;
            case 3:
                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()
    }
};
