if (typeof jext != "object") var jext = {};

jext.cycle = function () {

	if ($(arguments[0]).attr("tagName") == undefined) {
		throw "[jext.cycle] El elemento "+ arguments[0] +" no existe.";
		return false;
	}

	var element = arguments[0];
	var options = jext.cycle.agent.options;
	options.next = element;
	
	if (arguments.length == 2) $.extend(options, arguments[1]);

	jext.cycle.agent.add(element, options);
	jext.cycle.agent.load();

}

jext.cycle.agent = {
	slide: function() {
		this.element;
		this.options;
		this.isLoaded;
	},

	elements: new Array(),

	add: function(element, options) {
		var slide = new jext.cycle.agent.slide();
		slide.element = element;
		slide.options = options;
		
		this.elements[this.elements.length+1] = slide;
	},

	getScripts: function(func) {

		if (jext.cycle.agent.isLoaded === true) return true;
		else if (jext.cycle.agent.isLoaded === false) {
			jext.cycle.agent.isLoaded = null;

			var fileName = "jext.cycle.js";
			var filePath = $("script[src$="+ fileName +"]").attr("src");
			filePath = filePath.substr(0, (filePath.length - fileName.length));

			$.getScript(filePath + "jquery.cycle.all.min.js", function() {
				jext.cycle.agent.isLoaded = true;
				jext.cycle.agent.show();
			});

			return true;
		}
		else return null;
	},

	show: function() {
		$.each(jext.cycle.agent.elements, function() {
			if (this instanceof jext.cycle.agent.slide && this.isLoaded != true){
				$(this.element).cycle(this.options);

				this.isLoaded = true;
			}
		});
	},

	load: function() {

		if (jext.cycle.agent.isLoaded === true) {
			jext.cycle.agent.show();
		} else {
			this.getScripts();
		}

	},

	isLoaded: false,

	options: {
		fx:     'fade',
		speed:   1000,
		timeout: 3500,
		pause:   1
	}
}