
(function($) {
	$.extend($.ui.dialog.prototype, {
		panelHeight: null,
		ie6Height: null,
		addon: false,

		seek4AddOn: function(_opt) {

			this._cont = this.uiDialog.children('.ui-dialog-content');
			var css = this.uiDialog.attr("class");
			var ht = this._cont.html();
			this._cont.html("");
			this._cont.append("<div class='dlg-inner-box' />");
			this._panel = this._cont.children(".dlg-inner-box");
			this._panel.roundbox();
			this._panel.children(".content-wrap").children(".content").html(ht);

			this.addon = true;
		},

		_resizeContent: function() {
			var k = 5;
			this.contentHeight = this._cont.height();
			this.panelHeight = this.contentHeight - k;
			this._panel.height(this.panelHeight);
			this._cont.height(this.contentHeight);
			this._panel.children(".content-wrap").height(this.panelHeight - k);
			this._panel.children(".content-wrap").children(".content").height("100%");
			this._cont.width("auto");
			this._panel.width("auto");
		},

		setTitle: function(title) {
			this.uiDialogTitlebar.children(".ui-dialog-title").html(title);
		},

		copyToForm: function() {
			if ($("form").length < 1) return;
			var inpts = $("#" + this.element[0].id + " input, #" + this.element[0].id + " textarea");
			var l = inpts.length;
			var ht = "<div style='display:none'>";
			for (var i = 0; i < l; i++) {
				switch (inpts[i].type.toLowerCase()) {
					default:
					case "text":
					case "hidden":
						ht += "<input type='hidden' name='" + inpts[i].name + "' value='" + $J(inpts[i]).val() + "' />";
						break;
					case "radio":
					case "checkbox":
						if (inpts[i].checked) ht += "<input type='hidden' name='" + inpts[i].name + "' value='" + inpts[i].value + "' />";
						break;
				}
			}
			var selects = $("#" + this.element[0].id + " select");
			l = selects.length;
			for (var i = 0; i < l; i++) {
				if (selects[i].selectedIndex != -1) {
					ht += "<input type='hidden' name='" + selects[i].name + "' value='" + selects[i].options[selects[i].selectedIndex].value + "' />";
				}
			}
			ht += "</div>";
			$("form").append(ht);
		}

	});
})(jQuery);




(function($) {

	seek4.alert = function(_message, _title, _width, _height) {
		if (this.dlg == null) {
			this.dlg = new seek4.Seek4BoxInstance(_message, _title, _width, _height);
			var ht = "<div class='ui-dialog-buttonpane'>";
			ht += "<span class='btn-wrapper'><span class='btn-left'>&nbsp;</span><input type='button' class='btn-middle' onclick=\"jQuery('" + this.dlg.box + "').dialog('close');\" value='Ok' /><span class='btn-right'>&nbsp;</span></span>";
			ht += "</div>";
			$(this.dlg.textbox).parent().append(ht);

		}
		else {
			if (_title != this.dlg.title) this.dlg.setTitle(_title);
			if (_message != this.dlg.message) this.dlg.setText(_message);
			if (_width != this.dlg.width) this.dlg.setWidth(_width);
			if (_height != this.dlg.height) this.dlg.setHeight(_height);
		}
		$(".ads").css("visibility", "hidden");
		this.dlg.show();
	}

	seek4.confirm = function(_message, _callback, _o) {
		var self = this;
		_o = _o ? _o : {};
		if (this.confirmdlg == null) {
			this.confirmdlg = new seek4.Seek4BoxInstance(_message, _o.title, _o.width, _o.height);
			this.confirmdlg.setCallbackData(_o.data);
			this.confirmdlg.setCallback(_callback);
			this.confirmdlg.setButtons(_o.buttons);
		}
		else {
			if (_o.title != this.confirmdlg.title) this.confirmdlg.setTitle(_o.title);
			if (_message != this.confirmdlg.message) this.confirmdlg.setText(_message);
			if (_o.data != this.confirmdlg.callbackData) this.confirmdlg.setCallbackData(_o.data);
			if (_callback != this.confirmdlg.callback) this.confirmdlg.setCallback(_callback);
			if (_o.width != this.confirmdlg.width) this.confirmdlg.setWidth(_o.width);
			if (_o.height != this.confirmdlg.height) this.confirmdlg.setHeight(_o.height);
			if (_o.buttons != this.confirmdlg.buttons) this.confirmdlg.setButtons(_o.buttons);
			this.confirmdlg.setCallback(_callback);
		}
		$(".ads").css("visibility", "hidden");
		this.confirmdlg.show();
	}

	seek4.dialogbox = function(_contentObjectId, _callback, _o) {

		var self = this;
		_o = _o ? _o : {};
		if (typeof (_o.buttons) == "undefined" || _o.buttons == null || _o.buttons.length == 0) {
			_o.buttons = ["Save", "Cancel"];
		}
		if (typeof (this.roster) == "undefined") {
			this.roster = [];
			this.register = function(_dlg) {
				this.roster[_dlg.id] = _dlg;
			}
		}
		if (typeof (this.roster[_contentObjectId]) == "undefined" || this.roster[_contentObjectId] == null) {
			this.register(new seek4.Seek4BoxInstance(null, _o.title, _o.width, _o.height, _contentObjectId));
			this.roster[_contentObjectId].setCallbackData(_o.data);
			this.roster[_contentObjectId].setCallback(_callback);
			this.roster[_contentObjectId].setContent(_contentObjectId);
			this.roster[_contentObjectId].setButtons(_o.buttons);
			if (typeof (_o.onclose) != "undefined" && _o.onclose != null) this.roster[_contentObjectId].setOnClose(_o.onclose);
		}
		else {
			if (_o.title != this.roster[_contentObjectId].title) this.roster[_contentObjectId].setTitle(_o.title);
			if (_o.data != this.roster[_contentObjectId].callbackData) this.roster[_contentObjectId].setCallbackData(_o.data);
			if (_callback != this.roster[_contentObjectId].callback) this.roster[_contentObjectId].setCallback(_callback);
			if (_o.width != this.roster[_contentObjectId].width) this.roster[_contentObjectId].setWidth(_o.width);
			if (_o.height != this.roster[_contentObjectId].height) this.roster[_contentObjectId].setHeight(_o.height);
			if (_o.buttons != this.roster[_contentObjectId].buttons) this.roster[_contentObjectId].setButtons(_o.buttons);
			if (_o.onclose != this.roster[_contentObjectId].onclose) this.roster[_contentObjectId].setOnClose(_o.onclose);
			this.roster[_contentObjectId].setCallback(_callback);
		}
		$(".ads").css("visibility", "hidden");
		$(this.roster[_contentObjectId].box).bind("open", function(e) { $(".ads").css("visibility", "hidden"); });
		this.roster[_contentObjectId].show();
		return this.roster[_contentObjectId];
	}

	seek4.dialogbox.prototype = {
		close: function(_contentObjectId) {
			this.roster[_contentObjectId].close();
		}
	}


	seek4.Seek4BoxInstance = function(_message, _title, _width, _height, _contentObjectId) {
		this.message = _message;
		this.contentObjectId = _contentObjectId;
		this.title = _title ? _title : this.defTitle;
		this.width = _width ? _width : this.defWidth;
		this.height = _height ? _height : this.defHeight;
		this._createBox();
		this.id = _contentObjectId;
		return this;
	}

	seek4.Seek4BoxInstance.prototype = {
		box: null,
		ID: null,
		textbox: null,
		defWidth: 270,
		defHeight: 160,
		defTitle: "Seek4Fitness",
		defButtons: ["Ok", "Cancel"],
		buttons: 0,
		callback: null,
		callbackData: null,
		content: null,
		contentObjectId: null,
		onclose: null,

		show: function() {
			$(this.box).dialog("open");
		},
		_createBox: function() {
			this.box = "seek4_alertbox_" + Math.round(Math.random() * 100);
			this.textbox = this.box + "_text";
			$("body").append("<div id='" + this.box + "'><div id='" + this.textbox + "'></div></div>")
			this.box = "#" + this.box;
			this.textbox = "#" + this.textbox;
			var self = this;
			$(this.box).dialog({
				modal: true,
				dialogClass: "dlg-green",
				overlay: { backgroundColor: 'black', opacity: 0.7 },
				autoOpen: false,
				resizable: false,
				height: self.height,
				width: self.width,
				title: self.title
			});

			$(this.box).dialog("seek4AddOn", null);
			if (this.content != null) {
				this.setContent(this.contentObjectId);
			} else {
				this.setText(this.message);
			}
			this.setOnClose(null);
		},

		setOnClose: function(_onClose) {
			this.onclose = _onClose;
			var self = this;
			jQuery(this.box).bind("dialogclose", function(evnt, ui) { $(".ads").css("visibility", "visible"); if (typeof (self.onclose) != "undefined" && self.onclose != null) self.onclose() });
		},

		setContent: function(_contentId) {
			$(this.textbox).html("");
			this.contentObjectId = _contentId;
			this.content = $("#" + _contentId);
			this.content.show();
			$(this.textbox).append(this.content[0]);
			//this.content.parent().remove("#"+_contentId);
		},

		setText: function(_text) {
			this.message = _text;
			$(this.textbox).html(_text);
		},

		setTitle: function(_title) {
			this.title = _title ? _title : this.defTitle;
			$(this.box).dialog("option", "title", this.title);
		},

		setWidth: function(_width) {
			this.width = _width ? _width : this.defWidth;
			$(this.box).dialog("option", "width", this.width);
		},
		setHeight: function(_height) {
			this.height = _height ? _height : this.defHeight;
			$(this.box).dialog("option", "height", this.height);
		},

		setButtons: function(_btns) {
			this.buttons = _btns ? _btns : this.defButtons;
			if (this.buttons == "none") return;
			$(this.textbox).parent().children(".ui-dialog-buttonpane").remove();
			ht = "<div class='ui-dialog-buttonpane'>";
			for (var i = 0; i < this.buttons.length; i++) {
				window["__callbackData"] = this.callbackData;
				ht += "<span class='btn-wrapper'><span class='btn-left'>&nbsp;</span><input type='button' class='btn-middle' onclick=\"jQuery('" + this.box + "').dialog('copyToForm'); if (" + this.callback + "({value:'" + this.buttons[i] + "',index:" + i + ",button:" + i + " ,data:__callbackData}) !== false) jQuery('" + this.box + "').dialog('close');\" value='" + this.buttons[i] + "' /><span class='btn-right'>&nbsp;</span></span>";
			}
			ht += "</div>";
			$(this.textbox).parent().append(ht);
		},

		setCallback: function(_call) {
			this.callback = _call;
		},

		setCallbackData: function(_data) {
			this.callbackData = _data;
		},

		close: function() {
			jQuery(this.box).dialog('close');
		}
	}
})(jQuery);

$.Seek4Confirm = function(_message,_callback, _o){return seek4.confirm(_message,_callback, _o)};
$.Seek4Alert = function(_message,_title, _width, _height){return seek4.alert(_message,_title, _width, _height)};
$.Seek4Dialog = function(_contentObjectId, _callback, _o){ return seek4.dialogbox(_contentObjectId, _callback, _o)};