1 (function($){
  2 	if ( typeof(window.xataface) == 'undefined' ){
  3 		window.xataface = {};
  4 	}
  5 	
  6 	window.xataface.RecordDialog = RecordDialog;
  7 
  8 	function RecordDialog(o){
  9 		this.el = document.createElement('div');
 10 		this.recordid = null;
 11 		this.table = null;
 12 		this.baseURL = DATAFACE_URL+'/js/RecordDialog';
 13 		this.formChanged = false;
 14 		for ( var i in o ) this[i] = o[i];
 15 	};
 16 	
 17 	RecordDialog.prototype = {
 18 	
 19 		display: function(){
 20 			var dialog = this;
 21 			$(this.el).load(this.baseURL+'/templates/dialog.html', function(){
 22 				var frame = $(this).find('.xf-RecordDialog-iframe')
 23 					.css({
 24 						'width': '100%',
 25 						'height': '96%',
 26 						'padding':0,
 27 						'margin':0,
 28 						'border': 'none'
 29 					})
 30 					.attr('src', dialog.getURL());
 31 					
 32 				
 33 				$(frame).hide();
 34 				//alert(frame.attr('width'));
 35 				frame.load(function(){
 36 					dialog.formChanged = false;
 37 					var iframe = $(this).contents();
 38 					try {
 39 						var parsed  = null;
 40 						
 41 						eval('parsed = '+iframe.text()+';');
 42 						if ( parsed['response_code'] == 200 ){
 43 							
 44 							// We saved it successfully
 45 							// so we can close our window
 46 							if ( dialog.callback ){
 47 								dialog.callback(parsed['record_data']);
 48 							}
 49 							
 50 							$(dialog.el).dialog('close');
 51 							if ( parsed['response_message'] ){
 52 								alert(parsed['response_message']);
 53 							}
 54 							return;
 55 						
 56 						}
 57 					} catch (err){
 58 						//alert(err);
 59 					
 60 					}
 61 					
 62 					var dc =iframe.find('.documentContent');
 63 					if ( dc.length == 0 ) dc = iframe.find('#main_section');
 64 					if ( dc.length == 0 ) dc = iframe.find('#main_column');
 65 					
 66 					dc.remove();
 67 					
 68 					var ibody = iframe.find('body');
 69 					var hidden = $(':hidden', ibody);
 70 					
 71 					iframe.find('body').empty();
 72 					$('script', dc).remove();	// So script tags don't get run twice.
 73 					dc.appendTo(ibody);
 74 					hidden.each(function(){
 75 						if ( this.tagName == 'SCRIPT'  ){
 76 							return;
 77 						}
 78 						//alert('About to append tag: '+this.tagName+' '+ $(this).text());
 79 						$(this).appendTo(ibody);
 80 						$(this).hide();
 81 						
 82 					});
 83 					//hidden.appendTo(ibody);
 84 					//hidden.hide();
 85 					$('#details-controller, .contentViews, .contentActions', ibody).hide();
 86 					$(ibody).css('background-color','transparent');
 87 					$('.documentContent', ibody).css({
 88 						'border':'none',
 89 						'background-color': 'transparent'
 90 					});
 91 					$(frame).fadeIn();
 92 					
 93 					
 94 					$('input, textarea, select', ibody).change(function(){
 95 						dialog.formChanged = true;
 96 					});
 97 					
 98 						
 99 				});
100 					
101 				
102 			});
103 			$(this.el).appendTo('body');
104 			
105 			
106 			$(this.el).dialog({
107 				beforeClose: function(){
108 					if ( dialog.formChanged ){
109 						return confirm('You have unsaved changes.  Clicking "OK" will discard these changes.  Do you wish to proceed?');
110 						
111 					}
112 				},
113 				buttons: {
114 					OK : function(){
115 						
116 						if ( dialog.callback ){
117 							dialog.callback();
118 						}
119 						$(this).dialog('close');
120 					}
121 					
122 				},
123 				height: $(window).height()-25,
124 				width: $(window).width()-25,
125 				title: (this.recordid?'Edit '+this.table+' Record':'Create New '+this.table+' Record'),
126 				modal: true
127 			});
128 			
129 		},
130 		
131 		getURL: function(){
132 			var action;
133 			if ( !this.recordid ){
134 				action='new';
135 			} else {
136 				action='edit';
137 			}
138 			return DATAFACE_SITE_HREF+'?-table='+encodeURIComponent(this.table)+(this.recordid?'&-recordid='+encodeURIComponent(this.recordid):'')+'&-action='+encodeURIComponent(action)+'&-response=json';
139 		}
140 	};
141 	
142 	RecordDialog.constructor = RecordDialog;
143 	
144 	
145 	
146 	$.fn.RecordDialog = function(options){
147 		return this.each(function(){
148 		
149 			$(this).click(function(){
150 				var d = new RecordDialog(options);
151 				d.display();
152 			});
153 		});
154 	};
155 	
156 	
157 	
158 })(jQuery);