//liveTwitter
var liveTwitterObj = {
	lastTwitterID: 0,
	listID: 'related_show_list',
	listItemClassName: 'related_Feed',
	loadUrl: '/livetwitter/ajax_getLiveTwitter/',
	cache: new Array(),	//暂存的Twitter，每秒显示一个，显示完去服务器重新读取
	p: 0,		//下一条要显示推的指针
	max: 30,	//缓存最大长度
	jump: 0,	//连续跳过的次数，如果大于cache的数量，会造成死循环
	checkRepeat: true,	//检测重复用户
	appendTimer: null,	//appendTwitter的setInterval对象
	appendInterval: 5000,	//间隔时间
	loadTimer: null,
	loadInterval: 10000,
	loadTF: false,			//是否请求服务器数据
	appendStop: false,		//是否已停止
	
	
	init: function(lastId, cacheId, appIntv, loadIntv) {
		this.lastTwitterID = lastId;
		
		//从ID为cacheId的DIV中读取缓存
		if(cacheId != null) {
			var cacheList = $('#'+cacheId+' .'+this.listItemClassName);
			$( cacheList.get().reverse() ).each(function(){
				var o = $(this).clone();
				liveTwitterObj.cache.push( o );
			});
		}
		//清除
		$('#'+cacheId).remove();
		//把当前显示的推也放进缓存
		var showList = $('#'+this.listID+' .'+this.listItemClassName);
		$( showList.get().reverse() ).each(function(){
			var o = $(this).clone();
			liveTwitterObj.cache.push( o );
		});
		
		//
		if( appIntv != null ) {
			this.appendInterval = appIntv;
		}
		if( loadIntv != null ) {
			this.loadInterval = loadIntv;
		}
		
	},
	
	start: function() {
		this.appendTimer = setInterval(liveTwitterObj.appendTwitter, this.appendInterval);
		if(this.loadTF){
			this.loadTimer = setInterval(liveTwitterObj.loadTwitterFromServer, this.loadInterval);
		}
		
		if( window.twitterNoteObj != null ) {
			twitterNoteObj.liveTwitterObj = this;
		}
	},
	
	//在页面上显示一条推
	appendTwitter: function() {
		if( liveTwitterObj.appendStop === true ) {
			return false;
		}
		
		if( liveTwitterObj.p >= liveTwitterObj.cache.length || liveTwitterObj.p < 0) {
			liveTwitterObj.p = 0;
		}
		var newTwitter = liveTwitterObj.cache[liveTwitterObj.p].clone();
		//如果当前页面已经有该用户的推就跳过
		if(liveTwitterObj.checkRepeat) {
			var avatarURL = newTwitter.find('.twitter_avatar a').attr('href');
			if( $('#'+liveTwitterObj.listID+' .twitter_avatar a[href="'+avatarURL+'"]').length > 0 ) {
				liveTwitterObj.p++;
				liveTwitterObj.jump++;
				//连续跳过cache.length次就关闭重复检测
				if( liveTwitterObj.jump >= liveTwitterObj.cache.length ) {
					liveTwitterObj.checkRepeat = false;
				}
				liveTwitterObj.appendTwitter();	//显示下一个
				return false;
			}
		}
		liveTwitterObj.jump = 0;
		
		newTwitter.hide().prependTo('#' + liveTwitterObj.listID).slideDown('slow', function(){
		});
		
		//移除最后一个元素
		var showList = $('#' + liveTwitterObj.listID + ' .' + liveTwitterObj.listItemClassName);
		$( showList.get( showList.length-1 ) ).remove();
//		$( showList.get( showList.length-1 ) ).slideUp(function(){ $(this).remove(); });
		//
		liveTwitterObj.p++;
	},
	
	//从服务器读取新推
	loadTwitterFromServer: function() {
		$.get(server_url+ liveTwitterObj.loadUrl + liveTwitterObj.lastTwitterID,
			{},
			function(data){
				if( $.trim(data) == '' )  {
					return;
				}

				var i = liveTwitterObj.cache.length;	//第一个新加推的下标
				var lst = $('<div>'+data+'</div>').find('.'+liveTwitterObj.listItemClassName);

				$(lst.get().reverse()).each(function(){
					var o = $(this);
					liveTwitterObj.cache.push(o);
					//超过最大数限制，从cache中移除第一个
					if(liveTwitterObj.cache.length > liveTwitterObj.max) {
						liveTwitterObj.cache.shift();
						i--;
					}
					var tid = parseInt( o.attr('id').substring(1) );
					liveTwitterObj.lastTwitterID = tid;
				});
				liveTwitterObj.p = i;
			}
		);
	}
}

//liveBrand
var liveBrandObj = {
	listID: 'brand_list',
	nodeClassName: 'brand_list_feed',
	listBtnID: 'brand_list_btn',
	btnCurClass: 'cur',
	pageNo: 0,
	pageCur: 1,
	type: 'cache',
	switchTimer: null,
	switchInterval: 10000,
	
	init: function(json,type) {
		if(type == 'json'){
			this.create(json);
		}
		this.pageNo = $('#'+this.listID+' > .'+this.nodeClassName).size();
		if(this.pageNo > 0){
			this.createBtn();
			this.bindContentEvents();
			if(this.pageNo > 1){
				this.bindBtnEvents();
			}
		}
	},
	start: function() {
		if(this.pageNo > 1){
			this.switchTimer = setInterval(liveBrandObj.switchBrand, this.switchInterval);
		}
	},
	switchBrand: function() {
		if(liveBrandObj.pageCur >= liveBrandObj.pageNo){liveBrandObj.pageCur = 0;}
		$('#'+liveBrandObj.listID+' > .'+liveBrandObj.nodeClassName).hide();
		$('#'+liveBrandObj.listBtnID+' li').removeClass(liveBrandObj.btnCurClass);
		$('#'+liveBrandObj.listID+' > .'+liveBrandObj.nodeClassName).eq(liveBrandObj.pageCur).fadeIn("slow");
		$('#'+liveBrandObj.listBtnID+' li').eq(liveBrandObj.pageCur).addClass(liveBrandObj.btnCurClass);
		liveBrandObj.pageCur++;
	},
	bindBtnEvents:function() {
		$('#'+liveBrandObj.listBtnID+' li').each(function(i){
			$(this).click(function(){
				$('#'+liveBrandObj.listID+' > .'+liveBrandObj.nodeClassName).hide();
				$('#'+liveBrandObj.listBtnID+' li').removeClass(liveBrandObj.btnCurClass);
				$('#'+liveBrandObj.listID+' > .'+liveBrandObj.nodeClassName).eq(i).fadeIn("slow");
				$(this).addClass(liveBrandObj.btnCurClass);
				liveBrandObj.pageCur = i+1;
				clearInterval(liveBrandObj.switchTimer);
				liveBrandObj.switchTimer = setInterval(liveBrandObj.switchBrand, liveBrandObj.switchInterval);
			});
		 });
	},
	bindContentEvents:function() {
		$('#'+liveBrandObj.listID+' > .'+liveBrandObj.nodeClassName+' li').hover(
		  function () {
		    $(this).children('.brand_logo').hide();
		    $(this).children('.brand_ciscount').show();
		    if(liveBrandObj.switchTimer != null){
		    	clearInterval(liveBrandObj.switchTimer);
		    }
		  },
		  function () {
		  	$(this).children('.brand_ciscount').hide();
		  	$(this).children('.brand_logo').show();
		  	if(liveBrandObj.pageNo > 1){
		  		liveBrandObj.switchTimer = setInterval(liveBrandObj.switchBrand, liveBrandObj.switchInterval);
		  	}
		  }
		);
	},
	createContent: function(json) {
		
	},
	createBtn: function() {
		if(liveBrandObj.pageNo > 1){
			var btnHtml = '';
			for (var i=0; i < liveBrandObj.pageNo-1; i++) {
				$('#'+liveBrandObj.listBtnID+'>ul').append('<li></li>');
			}
		}
	}
}

function welcome_close() {
	$("#welcome_bg").remove();
	$("#welcome").remove();
	var tip_timer = null;
	var ie6fix = 210;

	$('body').append('<div id="zhe_tips" class="mogu_tips"></div>');
  	var obj=$("#zhe_tips");
	var html = ''
	
    obj.html(html);
    obj.show("slow");
    if($.browser.msie && $.browser.version=='6.0'){
    	obj.css("top",$(window).height()-ie6fix+"px");
    }

    $(window).scroll(function(){ 
        if($.browser.msie && $.browser.version=='6.0'){
            obj.css('top',$(window).height()+$(window).scrollTop()-ie6fix+"px");
        }
    });
    
    $(window).resize(function(){
        if($.browser.msie && $.browser.version=='6.0'){
            obj.css('top',$(window).height()+$(window).scrollTop()-ie6fix+"px");
        }
    });
    $("#tip_close").click(function() {
    	clearTimeout(tip_timer);
    	obj.hide("slow");
    })
    tip_timer = setTimeout(function(){obj.hide("slow");},10000);
}
$(function(){
	if (getCookie("welcome") != "isee") {	
		var pw = pageWidth();
		$("#welcome").css("left",(pw-565)/2+"px");
		$("#welcome_bg").show();
		$("#welcome").show();
		$("#welcome .close").click(function() {
			//welcome_close();
			$("#welcome_bg").remove();
			$("#welcome").remove();
		});
		$("#welcome .isee").click(function() {
			setCookie("welcome","isee","30");
			welcome_close();
			//$("#welcome_bg").remove();
			//$("#welcome").remove();
		});
		// $("#welcome_bg").click(function() {
		// 			//welcome_close();
		// 			$("#welcome_bg").remove();
		// 			$("#welcome").remove();
		// 		});
	} else {
		welcome_close();
	}
});

