/**
 * jQuery overlay plugin v1.0 by Ferry Brouwer
 * Example: 
 *			//generate overlay settings by creating a new instance of the Overlay Class
 *			var overlay_settings = new Overlay({
 *				fadeInDuration 		: 1000,			// fade in duration
 *				fadeOutDuration		: 800,			// fade out duration
 *				backgroundColor		: '#000000',	// background color of overlay
 *				backgroundOpacity	: .5,			// background opacity of overlay
 *				alignCenter			: true			// align content to the center of screen
 *			});
 *	
 *			//show the overlay
 *			$('#example').overlay(OVERLAY_SHOW, overlay_settings, function(){ 
 *				alert('on complete overlay show');
 *			});
 */


/**
 * static const vars
 * OVERLAY_SHOW : show overlay event
 * OVERLAY_HIDE : hide overlay event
 */
var OVERLAY_SHOW = 'overlayShow';
var OVERLAY_HIDE = 'overlayHide';
var default_overlay = new Overlay();

/**
 * Settings Class for initializing the overlay properties
 * @param settings : {	fadeInDuration 		[int milliseconds],
 * 						fadeOutDuration		[int milliseconds],
 *						backgroundColor		[string hexcode],
 *						backgroundOpacity 	[float range 0 - 1]},
 *						alignCenter			[boolean]
 */
function Overlay(settings){
	settings = jQuery.extend({
		fadeInDuration 		:	1000,
		fadeOutDuration		:	1000,
		backgroundColor		:	'#000000',
		backgroundOpacity	:	.5,
		alignCenter			: 	false
	}, settings);
	return settings;
}

jQuery.fn.extend({
	/**
	 * Show overlay
	 * @param type				: String 		Hide or show
	 * @param overlay_object	: Object 		{fadeInDuration, fadeOutDuration, backgroundColor, backgroundOpacity}, default = default_overlay
	 * @param oncomplete 		: Function		Triggers when overlay animated succesfully [optional]
	 */
	overlay: function(type, overlay_object, oncomplete){
		overlay_object = overlay_object ? overlay_object : default_overlay;

		var bg = jQuery('#jQuery-overlay');
		if (!bg.length){
			bg = jQuery(document.createElement('div'));
			bg.attr('id', 'jQuery-overlay');
			bg.css({
				'backgroundColor'	:	'#' + overlay_object.backgroundColor.replace(/^#/gi, ''),
				'position'			: 	'absolute',
				'top'				:	0,
				'left'				: 	0,
				'z-index'			: 	99
			});
			setOpacity(bg, 0);
			jQuery('body').prepend(bg);
		}
		
		var content = this;
		
		//set boundings background
		var bg_height = (jQuery('html').height() > jQuery(window).height()) ? jQuery('html').height() : jQuery(window).height();
		bg.height(bg_height);			
		bg.width( jQuery(window).width() );
		
		switch(type){
			case OVERLAY_SHOW:
				if ( content.is(':hidden') ){
					//IE 7 fix reposition
					if (jQuery.browser.msie && Math.floor(jQuery.browser.version) == '7')
						repositionForIE7(content);
					
					//align content to center
					if (overlay_object.alignCenter)
						alignToCenter(content, {width:bg.width(), height:bg.height()});
						
					setOpacity(content, 0);
					content.css({'position':'absolute', 'z-index':999});
					content.show();
				}
				
				//animate
				bg.stop().fadeTo(overlay_object.fadeInDuration, overlay_object.backgroundOpacity);
				content.stop().fadeTo(overlay_object.fadeInDuration, 1, function(){
					if (oncomplete) oncomplete();
				});
			break;
			case OVERLAY_HIDE:
				//animate
				bg.stop().fadeTo(overlay_object.fadeOutDuration, 0, function(){		bg.remove();	});
				content.stop().fadeTo(overlay_object.fadeOutDuration, 0, function(){
					content.hide();					
					if (oncomplete) oncomplete();
				});
			break;
		}
	}
});

/**
 * Reposition content element when element is wrapped by a relative div
 * @param content : content element to reposition
 */
function repositionForIE7(content){
	var elm=content, offset_top=0, offset_left=0;
	while (elm[0].tagName.toLowerCase() != 'body'){
		if (elm.css('position') == 'relative' || elm.parent().css('position') == 'relative'){
			offset_top 	+= parseInt(elm.position().top);
			offset_left += parseInt(elm.position().left);
		}
		elm = elm.parent();
	}

	var first_block_element;
	jQuery.each( jQuery('body').children('div'), function(i, div){
		if (jQuery(div).css('position') !== 'absolute' && jQuery(div).css('position') !== 'relative'){
			first_block_element = jQuery(div);
			return false;
		}
	});
	
	if (offset_top != 0)
		content.css({'position':'absolute', 'top':Math.abs(offset_top), 'margin-top':0});
		
	if (offset_left != 0 && first_block_element)
		content.css({'left':first_block_element.offset().left + Math.abs(offset_left), 'margin-left':0});
	
	jQuery('body').append(content);
}

/**
 * Align content element to center
 * @param content 		: Element	content element align to center
 * @param bg_bounding	: Object 	background bounding data
 */
function alignToCenter(content, bg_bounding){
	jQuery('body').append(content);
	content.css({
		top	: (bg_bounding.height - content.height())/2,
		left: (bg_bounding.width - content.width())/2
	});
	
}

/**
 * Set opacity of an element
 * @param elm	: element
 * @param value	: float range 0-1
 */
function setOpacity(elm, value){
	elm.css({
		'-moz-opacity'	: value,
		'filter'		: 'alpha(opacity=' + value*100 + ')',
		'opacity'		: value
	});
}

/**
* This override function will remove the filter attribute used in MSIE, 
* this will anti-aliase the text again after the fade
*/ 
jQuery.fn.fadeTo = function(speed,to,callback) { 
	return this.animate({opacity: to}, speed, function() { 
		if (to == 1 && jQuery.browser.msie) this.style.removeAttribute('filter');  
		if (jQuery.isFunction(callback)) callback();  
	}); 
};
