/*
 * 
 * FrameContainer
 * 
 * */

(function($) {

	// Implementation
	$.fn.fcontainer = function(config) {
		var interfaces, id;

		if (config == null) {

			for ( var i = 0; i < $.fn.fcontainer.interfaces.length; i++)
				if (i == $(this).attr("containerID")) {

					return $.fn.fcontainer.interfaces[i];

				}

			return null;

		} else {

			id = $(this).attr("containerID");

			if (id == null) {

				id = $.fn.fcontainer.interfaces.length;
				for (i = 0; i < id; i++) {
					if (typeof $.fn.fcontainer.interfaces[i] == 'undefined') {
						id = i;
						break;
					}
				}

				$(this).attr("containerID", id);

			}

			id = $.fn.fcontainer.interfaces.length;
			for (i = 0; i < id; i++) {
				if (typeof $.fn.fcontainer.interfaces[i] == 'undefined') {
					id = i;
					break;
				}
			}

			// Erzeugen der ID

			obj = new fContainer($(this), $.extend(true, {},
					$.fn.fcontainer.defaults, config), id);
			$.fn.fcontainer.interfaces[id] = obj;

			obj.show();

		}

	}

	function fContainer(target, config, id) {

		
		var self = this;

		$
				.extend(
						true,
						self,
						config,
						{
							
							target : target,
							id : id,

							load : function(url, data) {

								data = $.extend(true, {}, data, self["data"]);
								
								if (self.config.enabled) {

									if (url == null)
										url = self.url;

									if (url != null) {

										if (self.loadIcon) {

											// Einbinden des Lade-Container
											container = "<div class=\"ui-loading-container\">"
													+ "<div class=\"ui-loading\"></div>"
													+ "</div>";

											target.html(container);

										}

										self.loadStart();
										
										$.post(url, data).success(
												self.loadSuccess).error(
												self.loadError).complete(
												self.loadComplete);

									}

								}

							},

							post : function(url, data, success, error, complete) {

								if(success == null)
									success = [];
								if(error == null)
									error = [];
								if(complete == null)
									complete = [];
								
								if (url == null)
									url = self.config.url;

								var post = $.post(url, data);

								post.success($
										.extend([ success ], self.success));
								post.error($.extend([ error ], self.error));
								post.complete($
										.extend([ complete ], self.error));
								
								/*
								 * $.ajax({ type : 'POST', url : url, data :
								 * data, success : $.extend([ success ],
								 * self.success), error : $.extend([ error ],
								 * self.error) });
								 */

								return true;
							},

							tick : function() {

							},

							show : function() {

								// self.config.events.load.complete.

								if (self.config.enabled) {
								
									// Laden des Inhalts
									self.load();
									
								}

							},

							// Aktualisiert alles
							refresh : function() {

								self.show();
								
								$(self.config.events.load.refresh).each(
										function() {

											if (this != null)
												this();

										});

							},

							// Tritt ein, wenn der Download beginnt
							loadStart : function(html) {

								$(self.config.events.load.start).each(
										function() {

											this(html);

										});
							},
							
							// Tritt ein, wenn der Download komplett
							loadComplete : function() {

								self.config.requestComplete = true;

								if (self.config.loadIcon) {

									/*
									 * target.children(".ui-loading-container")
									 * .children(".ui-loading-content")
									 * .css("display", "block");
									 * 
									 * target .children(".ui-loading-container")
									 * .addClass(
									 * "ui-loading-container-disabled");
									 */
									/*
									 * 
									 * 
									 * target.children(".loadingBack").children(
									 * ".loading").children(".label")
									 * .html("Complete!!!");
									 * 
									 * temp = $(
									 * "#frameContent_TempRequestContent_" +
									 * self.id).html();
									 */
									// target.html(temp);
								}

								$(self.config.events.load.complete).each(
										function() {

											if (typeof this == "function")
												this();

										});
							},

							// Tritt ein, wenn der Download �bertr�gt
							loadSuccess : function(html) {

								if (false)
									target.children(".ui-loading-container")
											.children(".ui-loading-content")
											.html(html);
								else
									target.html(html);

								$(self.config.events.load.success).each(
										function() {

											this(html);

										});
							},

							// Tritt ein, wenn der Download fehlschl�gt
							loadError : function() {

								self.config.requestComplete = false;

								$(self.config.events.load.error).each(
										function() {

											this();

										});
							},

							config : config
							

						});

	}

	$.fn.fcontainer.interfaces = [];
	
	$.fn.fcontainer.defaults = {

		enabled : true,
		parent : null,
		type : null,
		data : null,

		loadIcon : false,
		requestComplete : false,

		url : null,

		events : {

			refresh : [],

			load : {
				start : [],
				complete : [],
				success : [],
				error : []
			},
			post : {
				complete : [],
				success : [],
				error : []
			}

		}

	};

})(jQuery);
