// This is different from netlash.com in the sense that everything is in an element with a class 'share'
// On netlash.com the shareList ul is outside of that element.
// $(this).siblings('.shareList').show(); -> $(this).children('.shareList').show();

/*
<div class="share">
	<a class="shareAltButton toggleShareList" href="#"><b></b><span>{$lblShare|ucfirst}</span><i></i></a>
	
	<ul class="shareList">
		<li class="netlog"><a href="http://www.netlog.com/go/manage/links/view=save&amp;origin=external&amp;url={$SITE_URL}{$entryLink}&amp;title={$entryTitle}"><strong>&bull;</strong> <small>Netlog</small></a></li>
		<li class="linkedin"><a href="http://www.linkedin.com/shareArticle?mini=true&amp;url={$SITE_URL}{$entryLink}&amp;title={$entryLink}"><strong>&bull;</strong> <small>LinkedIn</small></a></li>
		<li class="twitter"><a href="http://twitter.com/home?status={$SITE_URL}{$entryLink}"><strong>&bull;</strong> <small>Twitter</small></a></li>
		<li class="facebook"><a href="http://www.facebook.com/sharer.php?u={$SITE_URL}{$entryLink}&amp;t={$entryTitle}"><strong>&bull;</strong> <small>Facebook</small></a></li>
	</ul>
</div>
*/

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

	// hide all dropdowns (should already be done in css already however)
	$('.shareList').hide();

	// collapse/expand the "dropdown"
	var changeShareDropDown = function(show) {
		if(typeof(show) == 'undefined') show = false;
		if($(this) != null && $(this).find('.toggleShareList').attr('id') == '') var show = true;
		$('.toggleShareList').attr('id', '');
		$('.shareList').hide();
		if (show) {
			$(this).find('.toggleShareList').attr('id', 'shareSelected');
			$(this).children('.shareList').show();
		}
	}

	// cancel default behaviour (don't bubble, don't propagate default action)
	var preventDefaultAction = function(e) {
		e.cancelBubble = true;
		e.returnValue = false;
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}

	// click handler: prevent default behaviour (following the "#"-link, bubbling through 'document' and thus closing the "dropdown")
	$('.share a.shareAltButton').click(function(e) {
		if(!e) var e = window.event;
		changeShareDropDown.call(this.parentNode);
		preventDefaultAction(e);
	});

	// mousedown handler: open the "dropdown" when our mouse is down (this event is called before focus)
	$('.share a.shareAltButton').mousedown(function(e) {
		// changeShareDropDown.call(this.parentNode);
	});

	// focus handler: open "dropdown" on focus
	// $('.share a.shareAltButton').focus(function(e) {
	// 	changeShareDropDown.call(this.parentNode, true);
	// });

	// handle focusses: close "dropdown" when focus is no longer there
	$('*').focus(function(e) {
		if ($(this).parents('.share').length <= 0 && $(this).parents('.shareList').length <= 0) changeShareDropDown.call(null);
	});

	// click handler for 'anywhere in the document': close "dropdown" (but not if it's a rightclick)
	$(document).click(function(e) {
		if (!e) var e = window.event;
		var rightclick = false;
		if (e.which) rightclick = (e.which == 3);
		else if (e.button) rightclick = (e.button == 2);
		if (!rightclick) changeShareDropDown.call(null);
	});
});
