/***
**  JQUERY MINIDIALOG PLUGIN
**	WRITTEN BY TOM ROMBAUT
**  Version 0.1
**  11/2009
**  DEPENDENCIES : JQUERY 1.3.1+ , JQUERY UI 1.7.2+
***/

var MiniDialog = function(element, options) {
	var conf = $.extend({}, $.fn.minidialog.defaults, options);
	var dialog;
	if(conf.clone){
		dialog = $(element).clone().prependTo('body');
	}else{
		dialog = $(element);
	}
	var initialize = function() {
		var disp = "block";
		if(!conf.show)
			disp = "none";
			
		/*move dialog to body*/
		if(conf.clone){
			$(element).remove();
		}
					
		dialog.prepend($("<div class='md-title'>"+conf.title+"</div>"));
		dialog.prepend($("<div class='md-btnholder'><div class='md-dialog-btn md-close'></div></div>"));
		dialog.addClass("mini-dialog");
		
		dialog.css({
				width:	conf.width=="auto"?conf.width:conf.width+"px",
		  		height:	conf.height=="auto"?conf.height:conf.height+"px",
		  		display: disp
		});	
		
		if($(".footer",dialog).length > 0){
			$(".footer",dialog).addClass("md-footer");
		}else if(conf.footer != false)
			dialog.append("<div class='md-footer'></div>");
			
		$(".md-close",dialog).hover(function(){
								$(this).addClass("md-hover");
			  			   	},function(){
								$(this).removeClass("md-hover");
						   	})
							.mousedown(function(){
							$(this).addClass("md-down");
							})
							.mouseup(function(){
								$(this).removeClass("md-down");
							})
							.click(function(){
								dialog.close();
							});
		if(conf.draggable){
			dialog.draggable({
				handle: ".md-title",
				start:	function(){
					$(".md-body",dialog).css("visibility","hidden");
				},
				stop: function(){
					$(".md-body",dialog).css("visibility","visible");
				}
			});
		}
		if(conf.resizable){
			dialog.resizable();
		}
		if(conf.trigger){
			$(conf.trigger).click(function(){
				dialog.open();
				return false;
			});
		}
	};
	dialog.setTitle = function(title){
		$(".md-title",dialog).html(title);
	};
	dialog.open = function(){
		if($.isFunction(conf.onopen))
			conf.onopen(dialog);
		
		if(conf.overlay){
			$("body").append("<div class='md-overlay'></div>");
			$(".md-overlay").css({
						"opacity"	:	conf.overlay_op,
						"width"		:	$(document).width(),
						"height"	:	$(document).height()
					});
		}
			
		
		/** positionelement **/
		var left,top = 0;
		if(!conf.left){
			var windowwidth  = $(window).width();
			var width = dialog.width();
			left = (windowwidth/2) - (width/2);
		}else
			left = conf.left;
	
		if(!conf.top){
			var windowheight = $(window).height();
			var height = dialog.height();
			top = ((windowheight/2) - (height/2)) + $(window).scrollTop();
		}else
			top = conf.top;
			
		dialog.css({left:left,top:top});
		dialog.show();
		
		
		/** IE BUG **/
		
		if($.isFunction(conf.onopen_end))
			conf.onopen_end(dialog);
		
		/*
		$(".md-title",dialog).css("width",$(dialog).width()-($(".md-title",dialog).css("paddingLeft").replace("px","")*1)+2);
		*/
		
	};
	dialog.close = function(){
		if($.isFunction(conf.onclose)){
			if(conf.onclose()){
				if(conf.overlay){
					$(".md-overlay").remove();
				}
				dialog.hide();
			}
		}else{
			if(conf.overlay){
				$(".md-overlay").remove();
			}
			dialog.hide();
		}
	};
	initialize();
	return dialog;
};

/**
**	JQUERY PLUGIN
**/
(function($) {	
	$.fn.minidialog = function(options) {
		return this.each(function() {
			var element = $(this);
			// Return early if this element already has a plugin instance
			if (element.data('minidialog')) return;
			// pass options to plugin constructor
			var w = new MiniDialog(this, options);
			// Store plugin object in this element's data
			element.data('minidialog', w);
		});
	};
})(jQuery);

$.fn.minidialog.defaults = {
	width		:	"auto",
	height		:	"auto",
	overlay		:	false,
	overlay_op	:	0.5,
	clone		:	false,
	left		:	false,
	top			:	false,
	title		:  	"",
	footer		: 	false,
	show		: 	false,
	trigger		:	false,
	draggable	: 	true,
	resizable	: 	false,
	onopen		: 	false,
	onopen_end	:	false,
	onclose		:	false
};