jQuery(function($){
	// change visibility of dropdown
	var changeShareDropDown = function() {
		var show = false;
		if ($(this) != null && $(this).find('.toggleShareList').attr('id') == '') {
			var show = true;
		}
		// hide all
		$('.toggleShareList').attr('id', '');
		$('.shareList').hide();
		// show the one that needs to be shown
		if (show) {
			$(this).find('.toggleShareList').attr('id', 'shareSelected');
			$(this).siblings('.shareList').show();
		}
	}

	// hide share dropdown by default
	$('.shareList').hide();

	// prevent default action (= browsing to the link)
	var preventDefaultAction = function(e) {
		// IE
		e.cancelBubble = true;
		e.returnValue = false;
		// others
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}

	// open share link in new window
	$('.shareList > li > a').each(function() {
		$(this).attr('target', '_blank');
	});

	// show dropdown when clicked on share button
	$('.share').click(function(e) {
		if(!e) var e = window.event;
		preventDefaultAction(e);

		// unbind event listeners on dropdown (in case visitor clicks share button more then once)
		changeShareDropDown.call(this);
	});

	$(document).click(function(e) {
		changeShareDropDown.call(null);
	});
});
