/**
 * Created by JetBrains PhpStorm.
 * User: Sean
 * Date: 25/08/11
 * Time: 2:12 PM
 * To change this template use File | Settings | File Templates.
 */
;
(function($) {
	$.fn.RC_Dialog = function(options, callback) {
		if ($.isFunction(options)) {
			callback = options;
			options = null;
		}
		o = $.extend({}, $.fn.RC_Dialog.defaults, options);

		var $container = $(this);

		// Select the auto-format/generated close button, ignore any custom ones
		var $close = $container.find('.closeDialog');

		this.open = function() {
			openPopup();
		};

		this.close = function() {
			$container.bPopup.close();
		};

		return this.each(function() {
			setup();

//			if (o.auto_load)
//				openPopup();
		});

		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// HELP FUNCTIONS - PRIVATE
		////////////////////////////////////////////////////////////////////////////////////////////////////////////
		function setup() {
			var offset = o.close_offset.split(',');

			var horizontal = ($container.width() - ($close.width() / 2)) - parseInt(offset[0]);
			var vertical = (-1 * $close.height() / 2) - parseInt(offset[1]);

			// Ensure the close is above the dialog content/header
			$close.css({'z-index':$container.css('z-index') + 1});
			$close.css(o.close_pos_x, horizontal + 'px');
			$close.css(o.close_pos_y, vertical + 'px');
		}

		function openPopup() {
			var bPopupDefaults = {
				amsl: 50,
				appendTo: "body",
				closeClass: "close",
				content: "ajax",
				contentContainer: null,
				escClose: !0,
				fadeSpeed: 250,
				follow: [!0, !0],
				followSpeed: 500,
				loadUrl: null,
				modal: !0,
				modalClose: 0,
				modalColor: "#000",
				onClose: null,
				onOpen: null,
				opacity: 0.7,
				position: ["auto", "auto"],
				scrollBar: 0,
				zIndex: 9999
			};
			var d = $.extend({}, bPopupDefaults, o);

			$container.bPopup(d);
		}
	};

	$.fn.RC_Dialog.defaults = {
		loadUrl: null,
		zIndex: 9999,

		auto_load: false,
		close_pos_x: 'left',
		close_pos_y: 'top,',
		close_offset: '-20, 20'
	};
})(jQuery);
