var Utils = {
	black_bg : function(action) {
		if (jQuery('#black-bg')[0]) {
			if (action == 'remove') {
				jQuery(window).unbind( 'resize', Utils.resize_black_bg );
				jQuery('#black-bg').fadeOut('normal', function() {
					jQuery('#black-bg').remove();
				});
			}
			return;
		}
		var bg = jQuery('<div />').attr({id : 'black-bg'}).css({
			width : jQuery(document).width(),
			height : jQuery(document).height(),
			display : 'none',
			opacity : 0.85
		}).appendTo('body').fadeIn();
		jQuery(window).bind( 'resize', Utils.resize_black_bg );
	},
	resize_black_bg : function() {
		jQuery('#black-bg').css({
			width : jQuery(document).width(),
			height : jQuery(document).height()
		});
	},
	popup : function( msg, action, opts ) {
		if ( !opts )
			opts = {};
		if (!msg) {
			jQuery('#popup, #black-bg').fadeOut('normal', function() {
				jQuery(this).remove();
			});
			return;
		}

		Utils.black_bg();
		if (jQuery('#popup').length)
			jQuery('#popup').remove();

		var popup = jQuery('<div />');
		popup.attr({
			id : 'popup'
		});
		popup.addClass('popup');
		popup.html(msg).appendTo('body');
		popup.css({
			display : 'none',
			zIndex : 10001
		});
		popup.focus();
		Utils.center_div(popup);
		popup.fadeIn();

		switch (action) {
			case 'fade':
				setTimeout(function() {
					jQuery('#popup, #black-bg').fadeOut('normal', function() {
						jQuery(this).remove();
					})
				}, 2000);
				break;
			case 'alert':
				var click_callback;
				if ( !opts.click_callback )
					click_callback = function() {
						jQuery('#popup, #black-bg')
						.unbind('click')
						.css( {
							cursor : 'default'
						} ).fadeOut( 'normal', function() {
							if ( opts.fade_callback )
								opts.fade_callback();
							jQuery(this).remove();
						} );
					};
				else
					click_callback = opts.click_callback;
				
				jQuery('#popup, #black-bg').click( click_callback )
				.css({
					cursor : 'pointer'
				});
				break;
		}
	},
	center_div : function(ele) {
		ele.css({
			left : (jQuery(window).width() - ele.width()) / 2,
			top : (jQuery(window).height() - ele.height()) / 2
		});
		return ele;
	},
	email_verify : function(email) {
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		return filter.test(email);
	}
};
