var ui_frameControl = new function() {

	var self = this;

	$
			.extend(
					true,
					self,
					{

						is_frameCreate : false,

						/*
						 * Konstruktor
						 */
						init : function() {

							$('.ui-frameControl').click(
									function() {

										if (self.is_frameCreate) {

											self.editFrame(null, null, $(this)
													.attr("allocation"), true);

											self.abortContainerSelect();
											self.is_frameCreate = false;

										}

									});

							$('.ui-frameControl').click(
									function() {

										if (self.is_frameCreate) {
											self.editFrame(null, null, $(this)
													.attr("allocation"), true);

											self.abortContainerSelect();
											self.is_frameCreate = false;

										}

									});

							/*
							 * Verschieben vorbereiten
							 */

							$('.ui-frameControl')
									.sortable(
											{
												opacity : 0.6,
												disabled : true,
												cursor : 'move',
												placeholder : 'ui-frameControl-frame-placeholder',
												connectWith : '.ui-frameControl',
												cancel : ".ui-frameControl-frame-select-disabled",
												dropOnEmpty : false,
												start : function(event, ui) {
													self.prepareEmptyList();
												},
												over : function(event, ui) {
												},
												stop : function(event, ui) {
													self.prepareEmptyList();
												},
												update : function(event, ui) {

													self.prepareEmptyList();

													var frame = $(ui.item)
															.children(
																	'.ui-frame');

													var frameID = $(ui.item)
															.attr("frameID");
													var allocation = $(ui.item)
															.attr("allocation");

													var parentAllocation = ui.item
															.parent(
																	".ui-frameControl")
															.attr("allocation");

													$
															.post(
																	"",
																	{
																		"ui-sortFrames" : "ui-sortFrames",
																		clearPage : true,
																		"frameID" : frameID,
																		"allocation" : allocation,
																		"parentAllocation" : parentAllocation,
																		"noPageContent" : true,
																		"sortList" : $(
																				this)
																				.sortable(
																						"toArray")
																	},
																	function(
																			html) {

																		if (false)
																			alert(html);

																	});

												}
											});

							$('.ui-frameControl').bind('mousedown',
									function(e) {
										if ($.browser.msie)
											e.stopPropagation();
									});

						},

						// Bearbeiten vom Frame
						editFrame : function(sender, filePath, allocation,
								create) {

							if (create == null)
								create = false;

							data = {
								clearPage : true,
								externalEdit : true,
								createFrame : create
							};

							if (filePath != null)
								data.filePath = filePath;

							if (allocation != null)
								data.allocation = allocation;

							if ($(sender).attr('frameID'))
								data.frameID = $(sender).attr('frameID');

							$
									.mbox(
											{

												key : 'ui-frameControl-editFrame',
												dialogType : 'saveAbort',
												closeAble : true,
												resizeAble : false,
												style : {

												},
												request : {
													url : PAGE['rootPath']
															+ 'ajax/protected/page/settings-frame,edit.html',
													data : data
												},

												events : {

													dialogResult : {

														save : function() {

															var mbox = $
																	.mbox('ui-frameControl-editFrame');
															var form = mbox.control
																	.find('form[name="ui-frameControl-editFrame-editFrameForm"]');

															var id = form
																	.find(
																			'[name="frameID"]')
																	.val();
															var filePath = form
																	.find(
																			'[name="ui-frameControl-editFrame-filePath"]')
																	.val();

															var content = "";

															content = $(
																	'#ui-frameControl-editFrame-editContent')
																	.html();

															var pageLinks = [];
															$
																	.each(
																			form
																					.find('select[name="pageLinkSelect"] option:selected'),
																			function(
																					i,
																					v) {

																				if ($(
																						v)
																						.val() != "")
																					pageLinks
																							.push($(
																									v)
																									.val());

																			});

															var allocation = form
																	.find(
																			'[name="frameAllocationSelect"]')
																	.val();

															var postData = {
																saveFrame : true,
																type : form
																		.find(
																				'[name="type"]')
																		.val(),
																nameText : form
																		.find(
																				'[name="nameText"]')
																		.val(),
																pageLinkSelect : pageLinks,
																frameStyleSelect : form
																		.find(
																				'[name="frameStyleSelect"]')
																		.val(),
																"ui-frameControl-editFrame-saveFrameButton" : true,
																"allocation" : allocation
															};

															if (allocation != null) {

																postData.frameContent = content;
																postData.id = id;
																postData.filePath = filePath;

																if (form
																		.find(
																				'[name="frameLinkID"]')
																		.val() != null
																		&& form
																				.find(
																						'[name="frameLinkID"]')
																				.val() != "")
																	postData.link = form
																			.find(
																					'[name="frameLinkID"]')
																			.val();

																$
																		.post(
																				PAGE['rootPath']
																						+ 'ajax/protected/page/settings-frame,edit.html?id='
																						+ id,
																				postData,
																				function(
																						data) {

																					if (data.complete == true)

																						if (data.complete == true) {

																							/*
																							 * Datei
																							 * wurde
																							 * gespeichert *
																							 */
																							if (create) {

																								if ($('.ui-frameControl[allocation="'
																										+ form
																												.find(
																														'[name="frameAllocationSelect"]')
																												.val()
																										+ '"]').length > 0) {
																									$(
																											'.ui-frameControl[allocation="'
																													+ form
																															.find(
																																	'[name="frameAllocationSelect"]')
																															.val()
																													+ '"]')
																											.prepend(
																													$(
																															'<div/>',
																															{
																																id : 'ui-frameControl-frame_'
																																		+ id,
																																frameID : id,
																																class : 'ui-frameControl-frame',
																																style : 'display: none;'
																															}));

																									$(
																											'.ui-frameControl-frame[frameID="'
																													+ id
																													+ '"]')
																											.fadeIn(
																													'fast',
																													function() {
																														self
																																.refreshFrame(id);
																													});
																								}

																							} else {

																								self
																										.refreshFrame(id);

																							}

																							if ($(
																									sender)
																									.attr(
																											'staticEdit') == 'true')
																								location = PAGE['rootPath']
																										+ 'protected/page/settings-frame,list.html';

																						} else {

																							/*
																							 * Datei
																							 * konnte
																							 * nicht
																							 * gespeichert
																							 * werden
																							 */
																							alert("File save error...\n\n"
																									+ data);

																						}

																				},
																				'JSON');

																$
																		.mbox(
																				'ui-frameControl-editFrame')
																		.close();

																initUI();

															} else {
																alert('Missing allocation!');
															}

														},
														abort : function() {

														}
													}
												}

											}).show();

						},

						// Aktualisieren vom Frame
						refreshFrame : function(id) {

							var frame = $('.ui-frameControl-frame[frameID="'
									+ id + '"]');

							if (frame.length < 1)
								return false;
							

							frame.load(PAGE['rootPath'] + PAGE['currentURL'], {
								clearPage : true,
								type : 'advanced',
								framePreview : id
							}, function() {
							});

							return true;

						},

						// Löschen des Frames
						deleteFrame : function(id) {

							var frame = $('.ui-frameControl-frame[frameID="'
									+ id + '"]');

							if (frame.length < 1)
								return false;

							if (frame.attr("frameID") != null) {

								$.post("", {
									"ui-deleteFrame" : "ui-deleteFrame",
									clearPage : true,
									"noPageContent" : true,
									"frameID" : id
								}, function(html) {

									if (html != "0") {
										frame.fadeOut('fast', function() {

											frame.remove();
											self.prepareEmptyList();
											return true;

										});

									}

								});

							}

							return false;
						},

						// Bereitet das Makieren der Spalten vor
						prepareContainerSelect : function() {

							$('.ui-frameControl').addClass(
									'ui-frameControl-select');
							$('.ui-frameControl').children(
									'.ui-frameControl-frame').fadeOut('fast',
									function() {

									});

							$('a#ui-frameControl-createFrameButton').attr(
									'open', true);

						},
						abortContainerSelect : function() {

							$('.ui-frameControl').removeClass(
									'ui-frameControl-select');
							$('.ui-frameControl').children(
									'.ui-frameControl-frame').fadeIn('fast',
									function() {

									});

							$('a#ui-frameControl-createFrameButton').attr(
									'open', null);

						},

						prepareEmptyList : function() {

							$('.ui-frameControl')
									.each(
											function(i, v) {

												list = $(v);
												if ((list
														.children(".ui-frameControl-frame").length - list
														.children(".ui-frameControl-frame-noEntries").length) > 0) {

													list
															.children(
																	".ui-frameControl-frame-noEntries")
															.hide(
																	function() {

																		list
																				.children(
																						".ui-frameControl-frame-noEntries")
																				.remove();
																	});

												} else {

													if (list
															.children(".ui-frameControl-frame-noEntries").length < 1) {
														$(
																'<div/>',
																{
																	'class' : 'ui-frameControl-frame ui-frameControl-frame-noEntries ui-frameControl-frame-select-disabled',
																	'html' : '<div class="ui-frame"><div class="ui-frame-content"></div><div>'
																}).appendTo(
																list);
														list
																.children(
																		"div.ui-frameControl-frame-noEntries")
																.show();
													}
												}

											});

						}

					});

	return self;
};
