/*
 分享插件
 作者：林庭
 整理：zinn
*/

$.include(jextpath+'org/qrcode.min.js', 1);

JEXTFN['JQSHARE']=function(){
	jx('[jqshare]').click(function(){
		var a=$(this);
		var d={};
		if(a.attr('s-url')) d.url=a.attr('s-url');
		if(a.attr('s-title')) d.title=a.attr('s-title');
		if(a.attr('s-brief')) d.brief=a.attr('s-brief');
		if(a.attr('s-img')) d.img=a.attr('s-img');
		if(a.attr('s-from')) d.from=a.attr('s-from');
		jqNativeShare(a.attr('jqshare'), d);
	});
};

var jqNativeShare = function (to_api, config){
	var url = config.url || document.location.href || '',
		title = config.title || document.title || '',
		brief = config.brief || document.title || '',
		img = config.img || document.getElementsByTagName('img').length > 0 && document.getElementsByTagName('img')[0].src || '',
		img_title = config.img_title || document.title || '',
		from = config.from || window.location.host || '';

	this.urlEncode = function(param, key, encode){
		var _result = [];
		for (var key in param){
			var value = param[key];
			if (value.constructor == Array){
				value.forEach(function(_value){
					_result.push(key + "=" + encodeURIComponent(_value));
				});
			}else{
				_result.push(key + '=' + encodeURIComponent(value));
			}
		}
		return _result.join('&');
	};
	if (to_api == 'sina') {//新浪微博
		var apiUrl = "http://v.t.sina.com.cn/share/share.php";
		var v = {
			appkey:"",
			title: title,
			url: url,
			pic: img
		}
	} else if (to_api == 'facebook') {//脸谱
		var apiUrl = "http://www.facebook.com/sharer.php";
		var v = {
			u: url,
			t: title
		}
	} else if (to_api == 'twitter') {//推特
		var apiUrl = "https://twitter.com/intent/tweet";
		var v = {
			text:title+" "+url
		}
	} else if (to_api == 'douban') {//豆瓣
		var apiUrl = "https://www.douban.com/share/service";
		var v = {
			href: url,
			name: title,
			text: brief,
			image: img
		}
	} else if (to_api == 'linkedin') {//领英
		var apiUrl = "http://www.linkedin.com/shareArticle";
		var v = {
			mini: 'true',
			url: url
		}
	} else if (to_api == 'qzone') {//QQ空间
		var apiUrl = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey";
		var v = {
			url: url,
			title: title,
			pics: img
		}
	} else if (to_api == 'pinterest') {//QQ好友
		var apiUrl = 'https://www.pinterest.com/pin/create/button/';
		var v = {
			url: url,
			description: title,
			media: img
		}
	} else if (to_api == 'tieba') {//贴吧
		var apiUrl = 'http://tieba.baidu.com/f/commit/share/openShareApi';
		var v = {
			url: url,
			title: title,
			desc: brief,
			pic:img
		}
	} else if (to_api == 'amazon') {//亚马逊
		var apiUrl = 'https://www.amazon.com/gp/wishlist/static-add';
		var v = {
			u: url,
			t: title
		}
	} else if (to_api == 'google') {
		var apiUrl = 'https://www.google.com/bookmarks/mark';
		var v = {
			op: 'add',
			bkmk: url,
			title: title,
			annotation: brief
		}
	} else if (to_api == 'qq') {//QQ好友
		var apiUrl = "https://connect.qq.com/widget/shareqq/index.html";
		var v = {
			url: url,
			title: title,
			pics: img
		}
	} else if (to_api == 'wechat') {//微信
		var top = ($(window).scrollTop()+(342*0.5));
		var html = '<div class="lyshare_weixin_qrcode_dialog" style="background:#fff; border-radius:5px;">';
			html += '<div style="height:48px; border-bottom:1px solid #ddd;">';
			html += '<div style="float:right; position:relative; line-height:48px; font-size:28px; color:#999; margin:0 15px 0 0; cursor:pointer; font-family:Arial; opacity:1;" class="close" close>×</div>';
			html += '<div move style="font-size:14px; color:#000; line-height:48px; margin:0 0 0 15px;">'+jLang.share.wx_title+'</div>';
			html += '<div style="clear:both;"></div>';
			html += '</div>';
			html += '<div style="width:190px; height:190px; margin:0 auto; padding:20px 0; box-sizing:initial;" id="qrcode"></div>';
			html += '<div style="font-size:12px; color:#000; padding:10px; border-top:1px solid #ddd; box-sizing:initial;">'+jLang.share.wx_tip+'</div>';
			html += '</div>';
			html += '</div>';
		$.alert(html,{
			type: 'null',
			wh: [360,380],
			init: function(a){
				a.move.remove();
				new QRCode(document.getElementById("qrcode"), {width:190,height:190,text:url});
			}
		});
	}
	if (apiUrl) {
		window.open(apiUrl + '?' + this.urlEncode(v), '_blank');
	}
};
