(function($) {
	
	$.aycoalert = {
		
		// These properties can be read/written by accessing $.aycoalert.propertyName from your scripts at any time
		
		verticalOffset: -50,                // vertical offset of the dialog from center screen, in pixels
		horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
		repositionOnResize: true,           // re-centers the dialog on window resize
		overlayOpacity: .1,                // transparency level of overlay
		overlayColor: '#FFF',               // base color of overlay
		draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
		showClose: false,                    // make the dialogs draggable (requires UI Draggables plugin)
		
		// Public methods
		
		mostrar: function(message) {
			$.aycoalert._show(message);
		},

		ocultar: function() {
			$.aycoalert._hide();
		},

		// Private methods
		
		_show: function(msg) {
			
			$.aycoalert._hide();
			$.aycoalert._overlay('show');
			
			$("body").append(
			  '<div id="aycoalert_container">' +
					'<div id="aycoalert_close"></div>' +
			    '<div id="aycoalert_content">' +
			      '<div id="aycoalert_message"></div>' +
			      '<div id="aycoalert_pie"></div>' +
					'</div>' +
			  '</div>');
			
			if( $.aycoalert.dialogClass ) $("#aycoalert_container").addClass($.aycoalert.dialogClass);
			if( !$.aycoalert.showClose ) $("#aycoalert_close").hide();
			
			// IE6 Fix
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
			
			$("#aycoalert_container").css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			
			$("#aycoalert_message").text(msg);
			$("#aycoalert_message").html( $("#aycoalert_message").text().replace(/\n/g, '<br />') );
			
			$("#aycoalert_container").css({
				minWidth: $("#aycoalert_container").outerWidth(),
				maxWidth: $("#aycoalert_container").outerWidth()
			});
			
			$.aycoalert._reposition();
			$.aycoalert._maintainPosition(true);

			$("#aycoalert_close").click( function() {
				$.aycoalert._hide();
			});

			// Make draggable
			if( $.aycoalert.draggable ) {
				try {
					$("#aycoalert_container").draggable({ handle: $("#aycoalert_title") });
					$("#aycoalert_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
		},
		
		_hide: function() {
			$("#aycoalert_container").remove();
			$.aycoalert._overlay('hide');
			$.aycoalert._maintainPosition(false);
		},
		
		_overlay: function(status) {
			switch( status ) {
				case 'show':
					$.aycoalert._overlay('hide');
					$("body").append('<div id="aycoalert_overlay"></div>');
					$("#aycoalert_overlay").css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: $.aycoalert.overlayColor,
						opacity: $.aycoalert.overlayOpacity
					});
				break;
				case 'hide':
					$("#aycoalert_overlay").remove();
				break;
			}
		},
		
		_reposition: function() {
			var top = (($(window).height() / 2) - ($("#aycoalert_container").outerHeight() / 2)) + $.aycoalert.verticalOffset;
			var left = (($(window).width() / 2) - ($("#aycoalert_container").outerWidth() / 2)) + $.aycoalert.horizontalOffset;
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
			
			$("#aycoalert_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#aycoalert_overlay").height( $(document).height() );
		},
		
		_maintainPosition: function(status) {
			if( $.aycoalert.repositionOnResize ) {
				switch(status) {
					case true:
						$(window).bind('resize', $.aycoalert._reposition);
					break;
					case false:
						$(window).unbind('resize', $.aycoalert._reposition);
					break;
				}
			}
		}
		
	}
	
	// Shortuct functions
	ajaxShowLoader = function(message) {
		$.aycoalert.mostrar(message);
	}

	ajaxHideLoader = function() {
		$.aycoalert.ocultar();
	}

})(jQuery);