/** 
 * jQuery plugin JUser
 *
 *  http://moda.ru/
 *
 *  jQuery required. Download it at http://jquery.com/
 *
 */

(function(jQuery) {

	jQuery.fn.jdialog = function(options) {
		
		if(this.length == 0) 
			return this;
		
		if(typeof arguments[0] == 'string') {
			if(this.length > 1) {
				var args = arguments;
				return this.each(function() {
					$.fn.jdialog.apply($(this), args);
    			});
			};
			
			$.fn.jdialog[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []);
			
			return this;
		};
		
		var options = $.extend($.fn.jdialog.options, options);
		
		$(this).dialog({
			autoOpen: false,
			draggable: false,
			modal: true,
			resizable: false,
			width: options.width,
			open: function() {
				Cufon.replace('.ui-dialog-title');
				resize(this);
			}
		});
		
		$(window).resize(function() {
			resize(self);
		});
		
		function resize(el) {
			
			var top = ($(window).height() / 2) - ($(el).parent().height() / 2);
			var left = ($(window).width() / 2) - ($(el).parent().width() / 2);
				
			$(el).parent().css({
				top: top,
				left: left
			});
		}
		
		return $(this);
	}
	
	$.fn.jdialog.options = {
		width: 450
	}
	
	$.extend($.fn.jdialog, {
		open: function() {
			$(this).dialog('open');
		},
		close: function() {
			$(this).dialog('close');
		},
		resize: function(params) {
			
			var speed = params.speed || 800;
			
			var old_height = $(this).parent().height();
			var old_width  = $(this).parent().width();
			
			if(old_width == params.width && old_height == params.height)
				return;
			
			var top  = ((params.height - old_height) / 2);
			var left = ((params.width - old_width) / 2);

			$(this).parent().animate({
				'top' : '-=' + top + 'px',
				'left' : '-=' + left + 'px',
				'width' : params.width + 'px',
				'height' : params.height + 'px'
			}, speed, function() {
				if(params.onComplete)
					params.onComplete();
			});
		}
	});
		
})(jQuery);