if (typeof EFX == 'undefined') {
    var EFX = {};
}

EFX.namespace = function(){
    var a = arguments;
    var names, obj, i, n;
    for (i = 0; i < arguments.length; ++i) {
        names = arguments[i].split(".");
        obj = EFX;
        for (n = (names[0] == 'EFX') ? 1 : 0; n < names.length; ++n) {
            obj[names[n]] = obj[names[n]] || {};
            obj = obj[names[n]];
        }
    }
    return obj;
}

EFX.namespace('core');
EFX.core.initialize = {
	init : function(context) {
		if ($('.lightboxLink', context).length != 0) {
			this.setLightboxLink(context);
		}
		if ($('.lightboxForm', context).length != 0) {
			this.setLightboxForm(context);
		}
	},

	setLightboxLink : function(context) {
		$('.lightboxLink', context).click(function() {
			EFX.utilities.ajax.send(this, function(data) {
				if (data.substr(0, 19) == '<div id="redirect">') {
					$(location).attr('href', $(data).html());
				} else {
					lightbox = new EFX.widgets.modal(data);
					EFX.core.initialize.init('#lightbox');
				}
			});
			return false;
		});
	},

	setLightboxForm : function(context) {
		$('.lightboxForm input[type="submit"]', context).click(function() {
			$('#submitAction', context).attr('value', $(this).attr('name'));
			$(this).parents('form').submit();
			return false;
		});
		$('.lightboxForm', context).submit(function() {
			EFX.utilities.ajax.submit(this, function(data) {
				if (data.substr(0, 19) == '<div id="redirect">') {
					$(location).attr('href', $(data).html());
				} else {
					lightbox = new EFX.widgets.modal(data);
					EFX.core.initialize.init('#lightbox');
				}
			});
			return false;
		});
	}
};

EFX.namespace('widgets');
EFX.widgets.modal = function(data, config) {
	this.init(data, config);
	return this;
}

EFX.widgets.modal.prototype = {
	title : null,
	content : null,
	config : {
		autoOpen : false,
		draggable : false,
		hide : {
			effect : 'blind',
			duration: 500
		}, 
		modal : true,
		resizable : false,
		show : {
			effect : 'blind',
			duration: 500
		},
		title : 'efx design',
		width : 600
	},

	init : function(data, config) {
		this.reset();
		this.close();
		$('#lightboxWrapper').html(data);
		this.setTitle();
		this.setContent();
		this.setConfig(config);
		this.create();
		this.open();
	},

	reset : function() {
		this.title = null;
		this.content = null;
		this.config.autoOpen = false;
		this.config.draggable = false;
		this.config.hide.effect = 'blind';
		this.config.hide.duration = 500;
		this.config.modal = true;
		this.config.resizable = false;
		this.config.show.effect = 'blind';
		this.config.show.duration = 500;
		this.config.title = 'efx design';
		this.config.width = 600;
	},

	setTitle : function() {
		this.title = $('#lightbox h1').html();
	},

	setContent : function() {
		this.content = $('#lightbox').html();
	},

	setConfig : function(config) {
		this.config.title += ' - ' + this.title;
		if (config != null) {
			if (config.autoOpen != null) { this.config.autoOpen = config.autoOpen; }
			if (config.draggable != null) { this.config.draggable = config.draggable; }
			if (config.hide != null) { this.config.show = config.hide; }
			if (config.modal != null) { this.config.modal = config.modal; }
			if (config.resizable != null) { this.config.resizable = config.resizable; }
			if (config.show != null) { this.config.show = config.show; }
			if (config.width != null) { this.config.width = config.width; }
		}
	},

	create : function() {
		$('#lightbox').dialog(this.config);
	},

	open : function() {
		$('#lightbox').dialog('open');
	},

	close : function() {
		$('#lightbox').parent().remove();
		$('#lightbox').remove();
	}
};

EFX.namespace('utilities');

EFX.utilities.ajax = {
	xhr : null,

	send : function(link, callback) {
		url = $(link).attr('href')
		if (url.split('?').length == 2) {
			this.xhr = $.get($(link).attr('href') + '&display=mini');
		} else {
			this.xhr = $.get($(link).attr('href') + '?display=mini');
		}
		xhr = this.xhr;
		this.xhr.done( function() {
			callback(xhr.responseText);
		});
	},

	submit : function(form, callback) {
		url = $(form).attr('action')
		if (url.split('?').length == 2) {
			this.xhr = $.post($(form).attr('action') + '&display=mini', $(form).serialize());
		} else {
			this.xhr = $.post($(form).attr('action') + '?display=mini', $(form).serialize());
		}
		xhr = this.xhr;
		this.xhr.done( function() {
			callback(xhr.responseText);
		});
	}
};

$(document).ready(function() {
	EFX.core.initialize.init('body');
});



















