/// <reference path="../jquery-1.3.2-vsdoc.js" />
//pop up message box
//content and buttons need to valid html, so setting content to 'I am content' will not work
// it needs to be '<p>I am content</p>'
//you can change the caption, content and buttons by using the returned api.

// (C) Lee Davies www.fruitbatscode.com
//
// jQuery formbuilder 
// version: 0.1.alpha
// License
// This is licensed under the Creative Commons Attribution-Share Alike (http://creativecommons.org/licenses/by-sa/3.0/) license. 
// Use/abuse it as needed, within the scope of that license.

(function($) {
	$.fn.fbisGui.msgBox = function(content, buttons, options) {
		var opts = $.extend({}, $.fn.fbisGui.msgBoxDefaults, options);

		var data = null; //data passed to opts.buttonClick
		//console.log(opts.contentWrap, opts.title, opts.buttonRowWrap);

		var $$box = $(opts.markup);
		//if set to hidden hide inituilly
		if (opts.hidden == true) $$box.hide();
		if (opts.cssClass != '') $$box.addClass(opts.cssClass);
		opts.parent.append($$box);

		$$box.find('span.caption').prepend(opts.caption); //set the caption
		//get the objects
		var $$content = $$box.find('.mbcontent');
		var $$titlebar = $$box.find('.mbtbar');
		var $$buttons = $$box.find('ul.buttons');
		//add the passed in buttons
		$$buttons.append(buttons);

		//append the passed in content
		if (content != null) {
			$$content.append(content);
		}
		//if we have an ajax url then load it in
		if (opts.ajaxLoadUrl != null) {
			//load content from ajax
			$$ajax = $('<div></div>');
			$().fbisGui.ajaxInsert($$ajax, opts.ajaxLoadUrl);
			$$content.append($$ajax);
		}

		//attach click
		$$buttons.find('li').live('click', function(e) {
			//if we've got a buttonclick
			if (opts.buttonClick != undefined) {
				opts.buttonClick(e, data);
				if (opts.persist != true) {
					$$box.remove();
					$$box = null;
					api = null;
				} else { $$box.hide(); }
			} else {
				//else just remove the box
				if ($$box != null) {
					$$box.remove();
					$$box = null;
					api = null;
				}
			}
		});
		$$box.find('span.close').click(function(e) {
			if (opts.persist != true) {
				$$box.remove();
				$$box = null;
				api = null;
			} else { $$box.hide(); }
		});
		if ($().draggable) {
			$().fbisGui.center($$box).draggable({ handle: '.mbtbar' }); 					//center and make draggable
		} else {
			$('body').append($$box);
			$().fbisGui.center($$box)
		}

		//make the content height match the box
		var innerheight = $$box.outerHeight() - ($$titlebar.outerHeight() + $$buttons.outerHeight());
		$$content.css({ 'height': innerheight, 'max-height': innerheight });

		//creating a chainable api here so the user can call multiple functions.
		//it works by creating an object and passing the object itself back in all method calls
		//avoid using in an href="javascript:" as the page will redirect to the api object.
		var api = function() {
			this.setCaption = function(caption) {
				$$box.find('span.caption').empty().append(caption);
				return this;
			};
			this.setContent = function(content) {
				$$content.empty().append(content);
				return this;
			};
			this.replaceText = function(selector, value) {
				$$content.find(selector).text(value);
			}
			this.show = function(speed, passData) {
				data = passData;
				$$box.show(speed);
				return this;
			};
			this.hide = function(speed) {
				$$box.hide(speed);
				return this;
			};
			this.setButtons = function(buttons) {
				$$buttons.empty().append(buttons);
				return this;
			};
			if (this instanceof api) {
				return this.api;
			} else {
				return new api();
			};
		};

		return new api();
	};
	//markup is serached for the following
	//.mbcontent = Passed in content in put here
	//.buttons = buttons put here
	//.mbtbar = title wrapper (caption and icons)
	//.mbtbar .caption = Replaced with caption
	$.fn.fbisGui.msgBoxDefaults = {
		markup: '<div class="msgbox"><div class="mbtbar"><span class="caption"><span class="icon close"></span></span></div><div class="mbcontent"></div><ul class="buttons"></ul></div>',
		cssClass: '',
		caption: 'Alert',
		buttonWrap: '<li></li>',
		parent: $('body'),
		ajaxLoadUrl: null,
		hidden: false, //whether the msgbox is added to the dom and hidden or immediately displayed
		persist: true //if true the object isn't destroyed after display so you can re-use it
	};
})(jQuery);