/**
 * A function that automatically creates a branding overlay for RGN websites. 
 * 
 * @author Sybren Dotinga, JDI internet professionals
 * @version 0.1
 */
(function($) {
	$.fn.Overlay = function(options) {		
		options = $.extend( {
			
			defaults: {
				log: false,
				logType:{ 	 
							log: 1,
							info: 2,
							error: 3
						},
				wrapper: $(this)
			},	
			Settings : {
				RGNName: 'Default user', //Default name which will be visable in the layer
				Timeout: 3000 //Set false to disable auto-hide
			}
			
		}, options );
			
		init();
		
		function init() {
			
			if (typeof(window["console"]) == "undefined") {
				options.defaults.log = false;
			}
			create();
			check();
		};
		
		/**
		 * Check to see if everything is present
		 */
		function check(){
			//Check to see if all needed layers where succesfully created
			if ( $('#RGNOverlay').length && $('#RGNContent').length ) {
				//Resize all layers
				resize();
				//Position content layer
				positionContent();
				
				//Set functions when resize event is triggered, to prevent the overlay getting ugly
				$(window).bind('resize', function () {
					resize();
					positionContent();	
				});
				
				//All ready to go
				show();
			} else {
				debug( 'Failed to initialize layers', 3 );
			}
		};
		
		/**
		 * Function to position the content of the layer, so it will always show in the middle of the window
		 */
		function positionContent() {
			var _Height = $(window).height();
			var _ContentHeight = $('#RGNContent').height();
			var _Top = ( _Height / 2 ) - ( _ContentHeight / 2);
			$('#RGNContent').css( 'top', _Top );
			$('#RGNContent').css( 'marginTop', 0 );
		};
		
		/**
		 * Create all needed layers
		 */
		function create() {
			$( options.defaults.wrapper ).append('<div id="RGNOverlay"></div>');
			$('#RGNOverlay').append('<div id="RGNDarklayer"></div>');
			$('#RGNOverlay').css( 'opacity', 0 );
			$('#RGNDarklayer').css( 'opacity', 0.9 );
			$('#RGNOverlay').append('<div id="RGNContent"></div>');
			$('#RGNContent').append('<a href="#" id="RGNClose"></a>');
			$('#RGNClose, #RGNDarklayer').click(function(ev){
				ev.preventDefault();
				close();
			});
			$('#RGNContent').append('<div id="RGNNameHolder"><span id="RGNName">Default user</span> is aangesloten bij ReconditioneringsGroep Nederland</div>');
			$('#RGNName').html( options.Settings.RGNName );
			$('#RGNContent').append( '<a id="RGNLink" href="http://www.rgn.nu/" target="_blank"></a>' );
			
			debug( 'All layers created', 2 );
		};
		
		function show() {
			 $('#RGNOverlay').animate({
				 opacity: 1
			 }, 500, function() {
				//Set the timeout to hide the overlay
				timeOut();
				//A fix for opacity problem in IE
				jQuery.each( jQuery.browser, function(i) {
			        if($.browser.msie){
			            $('#RGNOverlay').css({opacity:''});
			            $('#RGNOverlay').removeAttr('filter');
			        }
			    });
			 });
		};
		
		function close() {
			 $('#RGNOverlay').animate({
				 opacity: 0
			 }, 500, function() {
				 $('#RGNOverlay').css( 'display', 'none' );
			 });
		};
		
		function timeOut() {
			//If the value is a number, and not false, the timeout will be set to hide the layers
			if ( typeof options.Settings.TimeOut === 'number' && options.Settings.TimeOut !== false ) {
				setTimeout( function(){ close(); }, options.Settings.TimeOut );
			}
		};
		
		function resize() {
			var _Height = Math.max( $('body').height(), $(window).height() );
			var _Width = Math.max( $('body').width(), $(window).width() );
			$('#RGNOverlay').css( 'height', _Height );
			$('#RGNOverlay').css( 'width', _Width );
			$('#RGNDarklayer').css( 'height', _Height );
			$('#RGNDarklayer').css( 'width', _Width );
		};
		
		function debug( mData, iType ) {
			if(options.defaults.log === true) {
				if ( typeof( console ) == 'undefined' ) {
						alert(mData);
				} else {
					switch(iType) {
						case options.defaults.logType.log:
							console.log(mData);
						break;
						case options.defaults.logType.info:
							console.info(mData);
						break;
						case options.defaults.logType.error:
							console.error(mData);
						break;
						default:
							console.info(mData);
					}
				}
			} else {
				//Debug off
			}	
		};
	};
})(jQuery);
