/**
 * Plug-in for easy showing pop-up dialogs with jqModal.
 *
 * @author Michal Kandr
 **/
(function($){
	var dialogId = 'jqm_dialog_wrapper';
	var dialogClass = 'jqm_dialog';

	/** @var integer number of actually shown dialogs */
	var shownDialogs = 0;

	function jqmOnHide(hash){
		-- shownDialogs;
		hash.w.remove();
		hash.o.remove();
	}

	/**
	 * Show dialog and fill up dialog content
	 * @param elem object dialog
	 * @param options object jqModal options
	 * @param text string|jQuery dialog content text
	 * @param textSelect string|isJQuery element, which html is used as dialog text
	 * @return void
	 **/
	function showDialog(elem, options, text, textSelect){
		if($.isObject(text) && $.isJQuery(text)){
			$(elem).find(textSelect).html('').append($(text).clone());
		} else {
			$(elem).find(textSelect).html( text ? text : '' );
		}
		if($.isFunction(options.onHide)){
			var oldFunc = options.onHide;
			options.onHide = function(hash){
				oldFunc(hash);
				jqmOnHide(hash);
			};
		} else {
			options.onHide = jqmOnHide;
		}
		$(elem).appendTo('body')
			.jqm( options ).jqmShow()
			.css('z-index', parseInt($(elem).css('z-index'), 10) + ((shownDialogs-1)*2));

		if($.isSet(options.boxClass)){
			$(elem).addClass(options.boxClass);
		}

		var overlayClass = options.overlayClass;
		if($.isUndefined(overlayClass)){
			overlayClass = $.jqm.params.overlayClass;
		}
		if($.isSet(overlayClass)){
			var overlay = $('.' + overlayClass + ':first');
			if(overlay.length == 1){
				$(overlay).css('z-index', parseInt($(elem).css('z-index'), 10) - 1);
			}
		}

		$('.jqm_close', elem).click(function(){
			$(elem).elDialogHide();
		});
	}


	$.extend({
		/**
		* Show simple dialog with text (html).
		* @param title string dialog title, optional
		* @param text string|array|jQuery dialog text, array of texts or element with text
		* @param options object options for jqModal, optional
		* @return object dialog element
		*/
		elDialog: function(title, text, options){
			// title not set and options is present as second argument
			if((options === null || options === undefined) && $.isObject(text) && ! $.isJQuery(text)){
				options = text;
				text = title;
				title = null;
			}
			// title and options not set
			if((options === null || options === undefined) && (text === null || text === undefined)){
				text = title;
				title = null;
			}
			// options not set
			if(options === null || options === undefined){
				options = {};
			}

			++ shownDialogs;
			var id = dialogId + shownDialogs;

			var elem = $('<div id="' + id + '" class="' + dialogClass + ((options.dialogClass === undefined) ? '' : ' '+options.dialogClass) + '">'
				+ '<div class="jqmd_tl">'
					+ '<div class="jqmd_tr">'
						+ '<div class="jqmd_tc jq_drag"></div>'
					+ '</div>'
				+ '</div>'
				+ '<div class="jqmd_bl">'
					+ '<div class="jqmd_br">'
						+ '<div class="jqmd_bc">'
							+ '<div class="jqmd_msg"></div>'
						+ '</div>'
					+ '</div>'
				+ '</div>'
				+ '<span class="jqmd_x jqm_close"></span></div>');
			showDialog(elem, options, text, '.jqmd_msg');
			$('body > .' + id).remove();
			if(title !== null){
				$('.jqmd_tc', elem).html(title);
			} else {
				$('.jqmd_tl', elem).remove();
			}

			return elem;
		},
		
		/**
		* Show dialog with content loaded by AJAX.
		* @param url string address for AJAX request
		* @param object options options for jqModal, optional
		* @return object dialog element
		*/
		elDialogAjax: function(title, url, options){
			// title is omitted and options are present as second argument
			if((options === null || options === undefined) && $.isObject(url)){
				options = url;
				url = title;
				title = null;
			}
			// title and options are omitted
			if((options === null || options === undefined) && url === null){
				url = title;
				title = null;
			}

			if(options === null || options === undefined){
				options = {};
			}

			options.ajax = url;
			options.target = '.jqmd_msg';

			if($.isFunction($.loaderGetElem)){
				options.ajaxText = $.loaderGetElem();
			}

			return $.elDialog(title, '', options);
		}
	});

	$.fn.elDialogHide = function(){
		this.each(function(){
			$(this).jqmHide();
		});
	};
})(jQuery);

