
App.DialogBox = {
		
	dimensions : { width:200, height: 200 },
	hasInit : false,
	notify : null,
	onclose : null,
	id : null,
	notesize : 200,
	winoffset : 50,
	windowopen : false,
	oArgs : null,
	overlayClass : null,
	dialogClass : null,
	
	closehandler : function() { CreativeUrbans.DialogBox.close(); },	
	
	__preinit : function() {
		
		if (App.DialogBox.hasInit) return;
		App.DialogBox.hasInit = true;
		
		this.__create();
		
		$('#dialogbox_close').live('click',  App.DialogBox.closehandler );
		
		$(window).resize(function() {
			if (App.DialogBox.windowopen) {
				App.DialogBox.__relocate();
			}
			return true;
		});
		
		this.init();
		
	},
	
	init : function() {
		// does nothing really ;)
	},

	open : function(oEvent, oArgs) {
		if(oEvent == null) oEvent = '';

		if (this.windowopen) {
			// clear content just in case...
			$('#dialogbox_content').html('');		
		}
		
		this.overlayClass = null;
		this.dialogClass = null;
		this.onclose = null;
		this.oArgs = oArgs;
		this.notify = oArgs.notify;
		this.id = oArgs.id;	
		
		if (oArgs.params != null) {
			
			if (oArgs.params.overlay != null) this.overlayClass = oArgs.params.overlay;
			if (oArgs.params.dialog != null) this.dialogClass = oArgs.params.dialog;
			
			this.resize({ width: oArgs.params.width, height: oArgs.params.height });
			if (oArgs.params.onclose != null) {
				this.onclose = oArgs.params.onclose;
			}
		}
		
		if (oArgs.url != null) {
			
			var oData = {};
			
			$.ajax({
				type: "get",
				error: function(data) {
					App.DialogBox.__recoverfailure(data);
				},
				success: function(data) {
					$('#dialogbox_content').html(data);
					App.DialogBox.__loadcomplete(data);
				},				
				url: oArgs.url,
				data:oData
			});				
			
		} else {
			if (!this.windowopen) {
				this.__relocate(true);
			} 
		}
	},
	
	reload : function(url) {
		if (this.oArgs != null) {
			url = ((url != 'undefined') && (url != null)) ? url : oArgs.url;			
		} else {
			url = ((url != 'undefined') && (url != null)) ? url : '';
		}
		if (this.windowopen && (url != '')) {
			var oData = {};
			$.ajax({
				type: "get",
				error: function(data) {
					App.DialogBox.__recoverfailure(data);
				},
				success: function(data) {
					$('#dialogbox_content').html(data);
					App.DialogBox.__loadcomplete(data);					
				},				
				url: url,
				data:oData
			});					
		}		
	},
	
	close : function(onclose) {
		if (this.windowopen) {
			this.__closeWindow(onclose);		
		}
	},
	
	resize : function(params) {
		if (params == null) return;
		this.dimensions = { width: params.width, height: params.height };
		this.__relocate();
	},
	
	__recoverfailure : function(data) {
		this.__relocate(true);
	},	
	
	__loadcomplete : function(data) {	
		if (this.notify != null) {
			this.notify.call(this, this.id);
		}
		if (this.oArgs != null) {
			//if(this.oArgs.params.validate != null) App.Forms.doValidation(this.oArgs.params.validate.fid);
			//if(this.oArgs.tiny != null) App.UI.Tiny.loadTiny({'id':this.oArgs.tiny.id, 'theme': this.oArgs.tiny.theme});
		}
		this.__relocate(true);
		$('*').triggerHandler('page-refresh', '#dialogbox_inner');		
	},
	
	__relocate : function(force) {
		if (this.windowopen || force) {
			position = null;			
			metrics = this.__getWindowMetrics();
			if (metrics == null) {
				// oops
			} else {
				position = { position:'fixed', 
								width: ''+this.dimensions.width+'px', height: ''+this.dimensions.height+'px', 
								left: ''+Math.round((metrics.width - this.dimensions.width) / 2)+'px', 
								top: ''+(Math.round((metrics.height - this.dimensions.height) / 2)-this.winoffset)+'px' 
							};
				this.__openWindow(position);	
			}
		}
 	},
	
	__getWindowMetrics : function() {
		if ($(window) == null) return null;
		return { width: $(window).width(), height: $(window).height() };
	},
	
	__closeWindow : function(onclose) {
		onclose = ((onclose == 'undefined') || (onclose == null) || (onclose == '')) ? this.onclose : onclose; 
		if (!this.windowopen) return; // already closed

		$('#dialogbox_overlay').fadeOut('fast', function() {
			if (App.DialogBox.overlayClass != null) $('#dialogbox_overlay').removeClass(App.DialogBox.overlayClass);			
			App.DialogBox.overlayClass = null;
		});
		
		$('#dialogbox').removeAttr('style');
		
		$('#dialogbox').fadeOut('fast', function() {
			if (Jobbawok.DialogBox.dialogClass != null) $('#dialogbox').removeClass(App.DialogBox.dialogClass);			
			App.DialogBox.dialogClass = null;			
		});
		
		$('#dialogbox_content').html('');
		this.windowopen = false;		
		if (onclose != null) {
			App.Core.Nav.changeMenu(onclose);
		}
	},
 	
 	__openWindow : function(params) {
		if (!this.windowopen) {
			if ($('#dialogbox').attr('style') != null) {
				$('#dialogbox').removeAttr('style');
			}
		}
		//$('#dialogbox_overlay').show();
		
		if (this.overlayClass != null) $('#dialogbox_overlay').addClass(this.overlayClass);
		if (this.dialogClass != null) $('#dialogbox').addClass(this.dialogClass);
		
		$('#dialogbox').css({ 'position': params.position, 'width': params.width, 'min-height': params.height, 'left': params.left, 'top': params.top });
		$('#dialogbox_inner').css({'height': params.height});
		if (!this.windowopen) {
			$('#dialogbox_overlay').fadeIn('fast');
			$('#dialogbox').fadeIn('fast');
		} else {
			$('#dialogbox').animate({'position': params.position, 'width': params.width, 'min-height': params.height, 'left': params.left, 'top': params.top},'1500');
		}
		//$('dialogbox').animate({ 'width': oArgs.elWidth, 'min-height': oArgs.elHeight, 'left': oArgs.posLeft, 'top': oArgs.posTop }, oArgs.animTime);
		this.windowopen = true;
	},
		
	__create : function() {		
		var dialog = '<div id="dialogbox_overlay"></div><div id="dialogbox">'+
			'<div id="dialogbox_inner"><div id="dialogbox_close"></div>'+
			'<div id="dialogbox_content"></div>'+
			'</div></div>';		
		$('body').append(dialog);
	}
	
};

$(document).ready(function() {
	
	App.DialogBox.__preinit();
	
});


