var weiboData;

var weibo = {
	name			: false,
	sign			: false,
	loaderTimer		: false,
	loaderTimeOut	: 500,
	getTime			: function (time){
		var nowDate = new Date(time*1000);
		var _format = function(n) { return n = n < 10 ? '0' + n : n; }
		var _lformat = function(n) { return n = (n > 60 && n < 1900) ? n + 1900 : n; }
		var diff = parseInt(nowDate.getTime()/1000) - parseInt(time);
		if(diff < 60*24*30*12){
			return (nowDate.getMonth() + 1)+'月'+_format(nowDate.getDate())+'日' + ' ' +_format(nowDate.getHours())+':'+_format(nowDate.getMinutes());
		} else {
			return _lformat(nowDate.getYear())+'年'+(nowDate.getMonth() + 1)+'月'+_format(nowDate.getDate())+'日' + ' ' +_format(nowDate.getHours())+':'+_format(nowDate.getMinutes());
		}
	},

	getType			: function (type){
		switch(type){
		case 1:
			return '广播';
		case 2:
			return '转播';
		case 4:
			return '对话';
		default:
			return '';
		}
	},

	getText			: function (text){
		text = text.replace(/&#160;/g, '&nbsp;');
		text = text.replace(/(http:\/\/([^\b]+))/g, '<a href="$1" target="_blank">$1</a>');
		text = text.replace(/@([\w\-_]+)/g, '<a href="http://t.qq.com/$1" target="_blank">@$1</a>');
		text = text.replace(/#([^#]+)#/g, '<a href="http://t.qq.com/k/$1" target="_blank">#$1#</a>');
		return text;
	},

	getData			: function (data){
		switch(data.type){
		case 1:
			return '<li><div><span class="content">'+ this.getType(data.type) +': '+ this.getText(data.content) + '</span>' +
					'<span class="from">来自: <a href="http://t.qq.com/'+ data.name +'" target="_blank">'+ data.nick + '</a> | '+ this.getTime(data.timestamp) +'</span>' +
					'</div><div class="clear"></div></li>';
		case 2:
		case 4:
			return '<li><div><span class="content">'+ this.getType(data.type) +': '+ this.getText(data.content) + '</span>' +
					'<span class="from">来自: <a href="http://t.qq.com/'+ data.name +'" target="_blank">'+ data.nick + '</a> | '+ this.getTime(data.timestamp) +'</span>' +
					'</div><div class="clear"></div><div class="reply"><div>' +
					'<span class="content">'+ this.getType(data.source.type) +': '+ this.getText(data.source.content) + '</span>' +
					'<span class="from">来自: <a href="http://t.qq.com/'+ data.source.name +'" target="_blank">'+ data.source.nick + '</a> | '+ this.getTime(data.source.timestamp) +'</span>' +
					'</div><div class="clear"></div></div></li>';
		default:
			return false;
		}
	},

	show 			: function (d){
		// 循环插入条目
		for(i in d){
			var r = this.getData(d[i]);
			if (r) $('#notice ul').prepend(r);
		}
		// 移动最新条目到顶端
		$('#notice ul').prepend($('#notice ul li:last'));
	},

	bind			:function (){
		$('#notice ul').bind('contextmenu', function(){return false});
		$('#notice ul').bind('mousedown', function(e){
			$(this).stop();
			switch(e.which){
			case 1:
				$('#notice ul').append($('#notice ul li:first'));
				break;
			case 2:
				$('#notice ul').click();
				break;
			case 3:
				$('#notice ul').prepend($('#notice ul li:last'));
				break;
			}
			return false;
		});
		$('#notice ul').bind('mouseover', function(){
			$(this).stop();
			$(this).animate({
				height: $(this).find('li:first').height() + 'px'
			}, 300);
		});
		$('#notice ul').bind('mouseout', function(){
			$(this).stop();
			$(this).animate({
				height: '18px'
			}, 200);
		});
	},

	data			: function (d){
		$('#notice ul').css({height:'18px'});
		clearInterval(this.loaderTimer);
		if (d.ret == 0){
			this.show(d.data);
			this.bind();
		} else {
			$('#notice').append('<ul class="weibo"><li>微博加载失败: '+ d.msg +'</li></ul>');
			console.log(d);
		}
		$('#notice ul li[class=first]').remove();
	},

	load			: function (){
		$.getScript('http://v.t.qq.com/output/json.php?type=1&name='+ this.name +'&sign='+ this.sign);
	},

	init			: function (){
		var cssPath	= document.location.href.match(/http:\/\/([^\/]+)/)[0] + '/usr/plugins/TencentWB';
		$('#notice').html('<ul class="weibo"><li class="first">正在载入腾讯微博 ....</li></ul>');
		$('head').append('<link rel="stylesheet" type="text/css" media="all" href="'+ cssPath +'/Style.css"/>');
		this.loaderTimer = setInterval('weibo.load()', this.loaderTimeOut);
	}
};

jQuery(document).ready(function($){
	weiboData = function(v){weibo.data(v)};
	weibo.name = 'tinystrong';
	weibo.sign = '8efce5b1f39e7a2f9a833387f82d02c1586ac456';
	weibo.init();
});
