(function($){

function openShreDialog(evt) {
	evt.preventDefault();

	var linkClass = $(this).parent('li').attr('class').match(/(\b.*)-share/i);

	if( linkClass.length < 1 ) {
		return false;
	}

	linkClass = linkClass.shift();

	switch(linkClass) {
		case 'facebook-share':
			FB.ui({
				method: 'share',
				href: window.location.href,
			}, function(response){});
			break;
		case 'twitter-share':
			var twUrl = window.location.href,
			twText = $('meta[name="twitter:description"]').attr('content');
			window.open(
				'http://twitter.com/share?url=' + twUrl + '&text=' + twText + '&',
				'twitterwindow',
				'height=450,\
					width=550,\
					top='+($(window).height()/2 - 225) +',\
					left='+$(window).width()/2 +',\
					toolbar=0,\
					location=0,\
					menubar=0,\
					directories=0,\
					scrollbars=0'
			);
			break;
		case 'linkedin-share':
			var inUrl = window.location.href,
			inText = $('meta[property="og:description"]').attr('content'),
			inTitle = $('meta[property="og:title"]').attr('content');

			window.open(
				'https://www.linkedin.com/shareArticle?mini=true&url='+inUrl+'&title='+inTitle+'&summary='+inText,
				'linkedinwindow',
				'height=450,\
					width=550,\
					top='+($(window).height()/2 - 225) +',\
					left='+$(window).width()/2 +',\
					toolbar=0,\
					location=0,\
					menubar=0,\
					directories=0,\
					scrollbars=0'
			);
			break;
		case 'pinterest-share':
			var piUrl = window.location.href,
			piText = $('meta[name="twitter:description"]').attr('content'),
			piImg = $('meta[name="twitter:image:src"]').attr('content');

			window.open(
				'https://pinterest.com/pin/create/button/?url='+piUrl+'&media='+piImg+'&description='+piText,
				'linkedinwindow',
				'height=450,\
					width=550,\
					top='+($(window).height()/2 - 225) +',\
					left='+$(window).width()/2 +',\
					toolbar=0,\
					location=0,\
					menubar=0,\
					directories=0,\
					scrollbars=0'
			);
			break;
	}
}


function openShareMenu(evt) {
	$(this).toggleClass('open');
}

function init() {
	// $(window).on('resize', onPageResize);
	$('#shareMenu')
		.on('click', openShareMenu)
		.find('a')
		.on('click', openShreDialog);
}

$(document).ready(init);
})(jQuery);

window.fbAsyncInit = function() {
	FB.init({
		appId	: '882645945134036',
		xfbml	: true,
		version	: 'v2.4'
	});
};

(function(d, s, id){
	 var js, fjs = d.getElementsByTagName(s)[0];
	 if (d.getElementById(id)) {return;}
	 js = d.createElement(s); js.id = id;
	 js.src = "//connect.facebook.net/en_US/sdk.js";
	 fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));
