var SnsShare = {
	buttons : {},
	protocols : {
		tsina: 'http://v.t.sina.com.cn/share/share.php?title=%s&url=%s&pic=%s&source=bookmark&appkey=%s&ralateUid=%s',
		tqq : 'http://share.v.t.qq.com/index.php?c=share&a=index&title=%s&url=%s&pic=%s&appkey=%s&site=lolbox.duowan.com',
		qzone : 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?title=%s&url=%s&pics=%s&summary=%s'
	},
	init : function() {
		for (var k in SnsShare.protocols) {
			var buttons = SnsShare.getElementsByClassName(null, 'a', 'button_' + k);
			if (buttons.length > 0) {
				var buttonsToAdd = [];
				for (var i = 0, j = buttons.length; i < j; i++) {
					if (buttons[i].onclick) continue;
					buttons[i].onclick = function() {
						SnsShare.share(this);
						return false;
					};
					buttons[i].style.cursor = 'pointer';
					buttonsToAdd.push(buttons[i]);
				};
				SnsShare.buttons[k] = buttonsToAdd;
			}
		}
	},
	share : function(button) {
		var key = button.className.substr(7);
		if (!SnsShare[key]) return;
		var appkey = jiathis_config.appkey[key];
		var protocol = SnsShare.protocols[key];
		var url = SnsShare[key].call(null, appkey, protocol);
		window.open(url);
	},
	tsina : function(appkey, protocol) {
		var url = jiathis_config.url || window.location.href;
		var pic = jiathis_config.pic || '';
		return SnsShare.sprintf(protocol, encodeURIComponent(jiathis_config.title), encodeURIComponent(url),
									encodeURIComponent(pic), appkey, jiathis_config.ralateuid['tsina']);
	},
	tqq : function(appkey, protocol) {
		var url = jiathis_config.url || window.location.href;
		var pic = jiathis_config.pic || '';
		return SnsShare.sprintf(protocol, encodeURIComponent(jiathis_config.title), encodeURIComponent(url),
									encodeURIComponent(pic), appkey);
	},
	qzone : function(appkey, protocol) {
		var url = jiathis_config.url || window.location.href;
		var pic = jiathis_config.pic || '';
		return SnsShare.sprintf(protocol, encodeURIComponent(jiathis_config.title), encodeURIComponent(url),
									encodeURIComponent(pic), encodeURIComponent(jiathis_config.summary));
	},
	getElementsByClassName : function(fatherId, tagName, className) {
		var node = fatherId && document.getElementById(fatherId) || document;
		tagName = tagName || "*";
		className = className.split(" ");
		var classNameLength = className.length;
		for (var i = 0, j = classNameLength; i < j; i++) {
			//创建匹配类名的正则 
			className[i] = new RegExp("(^|\\s)" + className[i].replace(/\-/g, "\\-") + "(\\s|$)");
		}
		var elements = node.getElementsByTagName(tagName);
		var result = [];
		for (var i = 0, j = elements.length, k = 0; i < j; i++) { //缓存length属性 
			var element = elements[i];
			while (className[k++].test(element.className)) { //优化循环 
				if (k === classNameLength) {
					result[result.length] = element;
					break;
				}
			}
			k = 0;
		}
		return result;
	},
	sprintf : function() {
		var i = 0,
			a, f = arguments[i++],
			o = [],
			m, p, c, x, s = '';
		while (f) {
			if (m = /^[^%]+/.exec(f)) {
				o.push(m[0]);
			} else if (m = /^%{2}/.exec(f)) {
				o.push('%');
			} else if (m = /^%(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
				if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
					throw ('Too few arguments.');
				}
				if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) {
					throw ('Expecting number but found ' + typeof(a));
				}
				switch (m[7]) {
				case 'b':
					a = a.toString(2);
					break;
				case 'c':
					a = String.fromCharCode(a);
					break;
				case 'd':
					a = parseInt(a);
					break;
				case 'e':
					a = m[6] ? a.toExponential(m[6]) : a.toExponential();
					break;
				case 'f':
					a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a);
					break;
				case 'o':
					a = a.toString(8);
					break;
				case 's':
					a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a);
					break;
				case 'u':
					a = Math.abs(a);
					break;
				case 'x':
					a = a.toString(16);
					break;
				case 'X':
					a = a.toString(16).toUpperCase();
					break;
				}
				a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+' + a : a);
				c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
				x = m[5] - String(a).length - s.length;
				p = m[5] ? str_repeat(c, x) : '';
				o.push(s + (m[4] ? a + p : p + a));
			} else {
				throw ('Huh ?!');
			}
			f = f.substring(m[0].length);
		}
		return o.join('');
	}
};