/*
 *
 * ThickDialogBox 1.0 - Client-side table sorting with ease!
 * Version 1.0
 * @requires jQuery v1.3.2
 *
 * Copyright (c) 2010 Arnaud Georgin
 *
 */

/**
 *
 * @description Create a modalbox based on the thickbox philosophy
 *
 * @example $('#myContainer').tdb_init({'id':'mymodalbox'});
 * @desc Prepare the dialogBox by adding a dialogbox with id 'mymodalbox' into the container of id 'myContainer'
 *
 * @example $('#mymodalbox').tdb_show({'id':'mymodalbox','iframe':true,'url':'the_url','width':883,'height':400,'modal':true});
 * @desc show the dialogBox of id 'mymodalbox' with the content of the url option in iframe mode
 *
 * @example $('#mymodalbox').tdb_show({'id':'mymodalbox','ajax':true,'url':'the_url','width':883,'height':400,'modal':true});
 * @desc show the dialogBox of id 'mymodalbox' with the content of the url option in ajax mode
 */
(function($){
     $.fn.extend({
         //plugin name - tdb_init
         tdb_init: function(options) {
	    	 var defaults = {
		        id:'modalbox'
	          	};

	    	 var options = $.extend(defaults, options);

	    	 return this.each(function(){
	    		 $(this).append('<div id="'+options.id+'"></div>');
	    		 $('#'+options.id).css({'display':'none'})
	    	 });
         }
     });

     $.fn.extend({
         //plugin name - tdb_show
         tdb_show: function(options) {
	    	 var defaults = {
		        id:'modalbox',
		        iframe:false,
		        url:'',
		        content:'',
		        ajax:false,
		        height:600,
		        width:800,
		        modal:false,
		        resizable:true,
		        title:''
	          	};

	    	 var options = $.extend(defaults, options);

	    	 return this.each(function(){

	    		if(options.url != ''){
		    		if(options.iframe && !options.ajax){
			    		// on paramètre le contenu de la fenetre modale
		    			var iframe = '<iframe src="'+options.url+'" width="'+options.width+'" height="'+options.height+'" scrolling="no" frameborder="0"></iframe>';
		    			$('#'+this.id).html(iframe);
		    			$('#'+this.id).dialog({
		    				height: options.height,
		    				width:options.width,
		    				modal: options.modal,
		    				resizable: options.resizable,
		    				title: options.title
		    			});
		    			$('#'+this.id).css({'padding':'0', 'overflow':'hidden'});

		    			// on force l'ouverture
		    			$('#'+this.id).dialog('open');
		    		}else if(options.ajax && !options.iframe){
		    			$.ajax({
		    				type:"GET",
		    				url:options.url,
		    				success:function(msg){
		    					$('#'+this.id).html(msg);
		    					$('#'+this.id).dialog({
		    						height: options.height,
		    						width:options.width,
		    						modal: options.modal,
		    						resizable: options.resizable,
				    				title: options.title
		    					});

		    					// on force l'ouverture
		    	    			$('#'+this.id).dialog('open');
		    				}
		    			});
		    		}
	    		}else if(options.url == '' && options.content != ''){
	    			$('#'+this.id).html(options.content);
					$('#'+this.id).dialog({
						height: options.height,
						width:options.width,
						modal: options.modal,
						resizable: options.resizable,
	    				title: options.title
					});

					// on force l'ouverture
	    			$('#'+this.id).dialog('open');
	    		}
	    	 });
         }
     });

     $.fn.extend({
         //plugin name - tdb_close
         tdb_close: function(options) {
	    	 var defaults = {
		        id:'modalbox'
	          	};

	    	 var options = $.extend(defaults, options);

	    	 return this.each(function(){
	    		 $(this).dialog('close');
	    	 });
         }
     });
 })(jQuery);
