// jQuery set to Safe Mode
var $j = jQuery.noConflict();

// namespace:
if (!COVERWIDGET) {
	var COVERWIDGET = {};
}

COVERWIDGET.lib = {

	lib : $j(function() {
		// create container and overlay divs ready
		$j('body').append('<div id="overlay-fade"></div><div id="overlay-container"></div>');
		$j('#overlay-fade, #overlay-container').fadeOut(0);
		
		// hijack the "share" link on a cover widget
		$j("a.widget-share").click(function(e) {
			e.preventDefault();
			COVERWIDGET.lib.showOverlay();
			// load the href into #overlay-container IF it's not already there:
			if ($j('#share-overlay').length == 0) {
				var loadStr = $j(this).attr('href') + ' #share-overlay';
				$j('#overlay-container').load(loadStr, function() {
					// attach close actions to loaded button
					$j('#share-overlay-close').click(function(e) {
						e.preventDefault();
						COVERWIDGET.lib.hideOverlay();
					});
					// attach "select" actions to inputs
					$j('#widget-instructions input').click(function() {
						this.select();
					});
				});
			}
		});
	}),
	
	showOverlay : function() {
		// ensure positioning is correct
		var clientW = $j(window).width();
		var clientH = $j(window).height();
		var scrollV = $j(window).scrollTop();
		var panelW = ($j('#overlay-container').width())/2;
		var panelH = ($j('#overlay-container').height())/2;
		var lPos = (clientW/2) - panelW;
		var tPos = ((clientH/2) - panelH) + scrollV;
		$j('#overlay-container').css({
			'left' : lPos + 'px',
			'top' : tPos + 'px'
		});
		// ensure overlay is sized, then kick off
		$j('#overlay-fade')
			.css({
				'width' : clientW + 'px',
				'height' : $j(document).height() + 'px',
				'display' : 'block',
				'opacity' : '0'
			})
			.click(function() {
				COVERWIDGET.lib.hideOverlay();
			})
			.fadeTo('fast', 0.75, function() {
				$j('#overlay-container').fadeIn('normal');
			});
	},

	hideOverlay : function() {
		$j('#overlay-fade, #overlay-container').fadeOut('normal');
	}

}
