

LightBox = function(options)
{
	this.OK = 1;
	this.ERROR = 2;
	this.REQUIRE_LOGIN = 3;
	this.CLOSE = 4;
	this.CLOSE_AFTER = 5;
	this.SHAKE = 6;
	this.REFRESH = 9;
	
	this.settings = $.extend(true, {
		_save_button: '.lb_save',
		_close_button: '.lb_close',
		load: {},
		save: {}
	}, options);
}

LightBox.prototype = {
	opened: false,
	
	load: function(load_settings)
	{
		var self = this;
		$.fancybox({
			content: "Chargement ...",
			changeFade: 0,
			onComplete: function()
			{
				if (self.settings.onBeforeLoad) 
					self.settings.onBeforeLoad.call(self);
				$.ajax($.extend(self.settings.load, load_settings, {
					success: function(resp)
					{
						self.onLoadSuccess(self.evalResp(resp));
					}
				}));
			}
		});
	},
	
	onLoadSuccess: function(resp)
	{
		this.onSuccess(resp);
		if (this.settings.onLoadSuccess) 
			this.settings.onLoadSuccess.call(this, resp);
	},
	
	onSaveSuccess: function(resp)
	{
		this.onSuccess(resp);
		if (this.settings.onSaveSuccess) 
			this.settings.onSaveSuccess.call(this, resp);
	},
	
	evalResp: function(resp)
	{
		var parts = resp.split('|');
		switch (parseInt(parts[0]))
		{
			case this.OK:
				parts[0] = '';
				return {
					code: true,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.ERROR:
				parts[0] = '';
				return {
					code: false,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.REQUIRE_LOGIN:
				redirect(R);
				break;
			case this.CLOSE:
				this.close();
				parts[0] = '';
				return {
					code: true,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.CLOSE_AFTER:
				var self = this;
				this.close(this.settings.close_delay);
				parts[0] = '';
				return {
					code: true,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.SHAKE:
				this.shake();
				parts[0] = '';
				return {
					code: true,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.REFRESH:
				document.location.reload(true);
				return {
					code: true,
					resp: "Chargement ..."
				};
				break;
			default:
				alert("Ajax response needs to be defined in file\nResp: " + resp);
				return {
					code: true,
					resp: resp
				};
				break;
		}
	},
	
	onSuccess: function(resp)
	{
		if (typeof resp.resp == 'string') 
		{
			this.setContent(resp.resp);
		}
		if (this.settings.onSuccess) 
			this.settings.onSuccess.call(this, resp);
	},
	
	close: function()
	{
		$.fancybox.close();
	},
	
	save: function(save_settings)
	{
		if (this.settings.onBeforeSave) 
			this.settings.onBeforeSave.call(this);
		var self = this;
		var _data = (this.settings.save && this.settings.save.data ? this.settings.save.data + '&' : '') + (this.$saveButton.attr('type') == 'submit' ? this.$lightbox_content.find('form').serialize() : '');
		var opt = $.extend(true, {
			type: 'post'
		}, this.settings.load, this.settings.save, {
			data: _data
		}, save_settings, {
			success: function(resp)
			{
				self.onSaveSuccess(self.evalResp(resp));
			}
		});
		$.ajax(opt);
	},
	
	setContent: function(content)
	{
		var self = this;
		if (this.settings.parseContent) 
			content = this.settings.parseContent.call(this, content);
		$.fancybox({
			content: content,
			onComplete: function()
			{
				self.$lightbox_content = $('#fancybox-inner');
				self.onContentLoaded();
			},
			changeFade: 0
		});
	},
	
	onContentLoaded: function()
	{
		this.$saveButton = this.$lightbox_content.find(this.settings._save_button);
		this.$closeButton = this.$lightbox_content.find(this.settings._close_button);
		var self = this;
		this.$closeButton.click(function()
		{
			self.close();
		});
		$.fancybox.resize();
		$.fancybox.center();
		if (this.settings.onContentLoaded) 
			this.settings.onContentLoaded.call(this);
		if (this.$saveButton.attr('type') == 'submit') 
		{
			this.$lightbox_content.find('form').submit(function()
			{
				self.save();
				return false;
			});
		}
		else 
		{
			this.$saveButton.click(function()
			{
				self.save();
			});
		}
	}
}
