jQuery(function($){
	JS_NETLASH.shareLinks.init();
});

if (!JS_NETLASH) { var JS_NETLASH = new Object(); }

JS_NETLASH.shareLinks = {
	
	parent: null,
	link: null,
	list: null,
	selected: null,
		
	init: function(parent, link, list, selected) {
	
		// set variables
		JS_NETLASH.shareLinks.parent = '.' + (parent || 'share');
		JS_NETLASH.shareLinks.link = '.' + (link || 'shareLink');
		JS_NETLASH.shareLinks.list = '.' + (list || 'shareList');
		JS_NETLASH.shareLinks.selected = selected || 'shareSelected';
		
		// hide all dropdowns (should already be done in css)
		$(JS_NETLASH.shareLinks.list).hide();
		
		// links open in new window
		$(JS_NETLASH.shareLinks.list + ' > li > a').each(function() { $(this).attr('target', '_blank'); });
		
		// click handler: prevent default behaviour (following the "#"-link, bubbling through 'document' and thus closing the "dropdown")
		$(JS_NETLASH.shareLinks.link).click(function(evt) {
			JS_NETLASH.shareLinks.preventDefaultAction(evt);	
		})	
		// mousedown handler: open the "dropdown" when our mouse is down (this event is called before focus)
		.mousedown(function(evt) {
			JS_NETLASH.shareLinks.changeShareDropDown.call(this.parentNode);
		})
		// focus handler: open "dropdown" on focus
		.focus(function(evt) {
			JS_NETLASH.shareLinks.changeShareDropDown.call(this.parentNode, true);
		});	
		
		// handle focusses: close "dropdown" when focus is no longer there
		$('*').focus(function(evt) {
			if ($(this).parents(JS_NETLASH.shareLinks.parent).length <= 0 && $(this).siblings(JS_NETLASH.shareLinks.list).length <= 0) JS_NETLASH.shareLinks.changeShareDropDown.call(null);
		});
		
		// click handler for 'anywhere in the document': close "dropdown" (but not if it's a rightclick)
		$(document).click(function(evt) {
			var rightclick = false;
			if (evt.which) rightclick = (evt.which == 3);
			else if (evt.button) rightclick = (evt.button == 2);
			if (!rightclick) JS_NETLASH.shareLinks.changeShareDropDown.call(null);
		});		

	},
	
	// cancel default behaviour (don't bubble, don't propagate default action)
	preventDefaultAction: function(evt) {
			evt.stopPropagation();
			evt.preventDefault();
	},
	
	// collapse/expand the "dropdown"
	changeShareDropDown: function(show) {
		
		if (typeof(show) == 'undefined') show = false;
		if ($(this) != null && $(this).find(JS_NETLASH.shareLinks.link).attr('id') == '') var show = true;
		$(JS_NETLASH.shareLinks.link).attr('id', '');
		$(JS_NETLASH.shareLinks.list).hide();
		
		if (show) {
			
			$(this).find(JS_NETLASH.shareLinks.link).attr('id', JS_NETLASH.shareLinks.selected)
			$(this).find(JS_NETLASH.shareLinks.list).show();
		}
	},	
	
	_eoo: true
}
