
LightBox = function($box, options)
{
	this.OK = 1;
	this.ERROR = 2;
	this.REQUIRE_LOGIN = 3;
	this.CLOSE = 4;
	this.CLOSE_AFTER = 5;
	this.SHAKE = 6;
	
	this.settings = $.extend(true, {
		loading: true,
		close_delay: 3000,
		load: {
			type: 'post'
		},
		save: {
			type: 'post',
			closeDelay: -1
		},
		_save_button: '.lb_save',
		_close_button: '.lb_close',
		_cache: '#lightbox_cache'
	}, options);
	this.$box = $box;
	this.$cache = $(this.settings._cache);
	var self = this;
	this.$cache.click(function()
	{
		self.close();
	});
}



LightBox.createMain = function()
{
	if ($('#lightbox_main').length == 0) 
		$('body').append('<div id="lightbox_main" class="lightbox"><div class="lb_content"></div></div><div id="lightbox_cache"></div>');
};

LightBox.prototype = {
	opened: false,
	
	load: function(load_settings)
	{
		var me = this;
		if (this.settings.onBeforeLoad) 
			this.settings.onBeforeLoad.call(this);
		if (this.settings.loading) 
			this.setLoading();
		this.open();
		$.ajax($.extend(this.settings.load, load_settings, {
			success: function(resp)
			{
				me.onLoadSuccess(me.evalResp(resp));
			}
		}));
	},
	
	onLoadSuccess: function(resp)
	{
		this.onSuccess(resp, this.settings.onContentLoaded);
		if (this.settings.onLoadSuccess) 
			this.settings.onLoadSuccess.call(this, resp);
	},
	
	onSaveSuccess: function(resp)
	{
		this.onSuccess(resp, this.settings.onContentLoaded);
		if (this.settings.onSaveSuccess) 
			this.settings.onSaveSuccess.call(this, resp);
	},
	
	evalResp: function(resp)
	{
		var parts = resp.split('|');
		var flag = parseInt(parts[0]);
		switch (flag)
		{
			case this.OK:
				parts[0] = '';
				return {
					code: true,
					flag: flag,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.ERROR:
				parts[0] = '';
				return {
					code: false,
					flag: flag,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.REQUIRE_LOGIN:
				redirect(R);
				break;
			case this.CLOSE:
				this.close();
				parts[0] = '';
				return {
					code: true,
					flag: flag,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.CLOSE_AFTER:
				var self = this;
				this.close(this.settings.close_delay);
				parts[0] = '';
				return {
					code: true,
					flag: flag,
					resp: parts.join('|').substring(1)
				};
				break;
			case this.SHAKE:
				this.shake();
				parts[0] = '';
				return {
					code: true,
					flag: flag,
					resp: parts.join('|').substring(1)
				};
				break;
			default:
				alert("Ajax response needs to be defined in file\nResp: " + resp);
				return {
					code: true,
					flag: flag,
					resp: resp
				};
				break;
		}
	},
	
	onSuccess: function(resp, callback)
	{
		this.unsetLoading();
		if (typeof resp.resp == 'string') 
			this.setContent(resp.resp, callback);
		else if (callback) 
			callback.call(this, resp);
		if (this.settings.onSuccess) 
			this.settings.onSuccess.call(this, resp);
	},
	
	setLoading: function()
	{
		this.setContent('<h2>Chargement ...</h2>', true);
		this.$box.addClass('loading');
	},
	
	unsetLoading: function()
	{
		this.$box.removeClass('loading');
	},
	
	open: function()
	{
		var self = this;
		this.$cache.show();
		this.$cache.height($(window).height());
		if (this.settings.content) 
			this.setContent(this.settings.content);
		
		this.$box.show();
		
		
		this.opened = true;
		this.center();
		if (this.settings.onOpen) 
			this.settings.onOpen.call(this);		
	},
	
	close: function(delay)
	{
		if (delay) 
		{
			var me = this;
			setTimeout(function()
			{
				me.close();
			}, delay);
			return;
		}
		this.$cache.hide();
		this.$box.hide();
		if (this.settings.onClose) 
			this.settings.onClose.call(this);
		this.opened = false;
	},
	
	save: function(save_settings)
	{
		if (this.settings.onBeforeSave) 
			this.settings.onBeforeSave.call(this);
		var me = this;
		var _data = (this.settings.save.data ? this.settings.save.data + '&' : '') + (this.$saveButton.attr('type') == 'submit' ? this.$box.find('form').serialize() : '');
		var opt = $.extend(true, {}, this.settings.load, this.settings.save, {
			data: _data
		}, save_settings, {
			success: function(resp)
			{
				me.onSaveSuccess(me.evalResp(resp));
			}
		});
		$.ajax(opt);
	},
	
	setContent: function(content, no_anim)
	{
		this.$box.removeClass('loading');
		var self = this;
		this.$box.css('overflow', 'hidden');
		if (this.settings.parseContent) 
			this.settings.parseContent.call(this, $preview);
		var $content = self.$box.find('.lb_content');
		$content.html(content);
		this.onContentLoaded();
		this.center();
	},
	
	onContentLoaded: function()
	{
		this.$saveButton = this.$box.find(this.settings._save_button);
		this.$closeButton = this.$box.find(this.settings._close_button);
		var self = this;
		this.$closeButton.click(function()
		{
			self.close();
		});
		this.$saveButton.click(function()
		{
			self.save();
		});
		if (this.$saveButton.attr('type') == 'submit') 
		{
			this.$box.find('form').submit(function()
			{
				self.save();
				return false;
			}).find('input:eq(0)').focus();
		}
	},
	
	center: function()
	{
		if (!this.opened) 
			return;
		
		var $content = this.$box.find('.lb_content');
		$content.css({width: 'auto', height: 'auto'});
		w = $content.width();
		h = $content.height();
		var self = this;
		var top = Math.round($(window).scrollTop() + ($(window).height() - h) / 2);
		var left = Math.round(($(window).width() - w) / 2);
		top = top < 0 ? 0 : top;
		left = left < 0 ? 0 : left;
		
		this.$box.css({
			top: top + 'px',
			left: left + 'px'
		});
		this.$box.find('.lb_content').css({
			width: w + 'px',
			height: h + 'px'
		});
	}
}
