/**
 * clearFusionCMS script to create an overlay window.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2013 fusionLib. All rights reserved.
 * @link http://clearFusionCMS.com
 */

(function() {

	// Look through style sheets for overlay CSS rules
	var hasCSS = false,
		overlayOpen = false;

	$fl(document).ready(function() {
		r = new RegExp('\\#cfOverlay\\b','i');
		var sheets = document.styleSheets;
		for(var i=0;i<sheets.length && !hasCSS;i++) {
			try {
				var rules = sheets[i].rules ? sheets[i].rules : sheets[i].cssRules;
				if(rules) {
					for(var n=0;n<rules.length && !hasCSS;n++) {
						var s = rules[n].selectorText ? rules[n].selectorText : rules[n].cssText;
						if(r.test(s))
							hasCSS = true;
					}
				}
			} catch(e) {}
		}

		// If no CSS then load our default CSS
		if(!hasCSS) {
			var link = document.createElement("link");
			link.rel = "stylesheet";
			link.href = "system/app/js/cfOverlay.css";
			document.getElementsByTagName("head")[0].appendChild(link);
		}

		// Add HTML to the page
		$fl('body').append('<div id="cfOverlay"><div id="cfOverlayDialog" role="dialog" aria-labelledby="cfOverlayCaptionCtnt"><div id="cfOverlayDialogInside"><div id="cfOverlayCaption"></div><div id="cfDialogClose"></div><div id="cfOverlayContent"></div></div></div></div>');
		$fl('body').append('<div id="cfOverlayBusy"><div id="cfOverlayBusyCtnt"></div></div>');

		// Handle clicks off the dialog
		$fl('#cfOverlay').bind('click', function(evt) {
			if(evt.target.id == 'cfOverlay') {
				$fl('#cfOverlay').css('display', 'none');
				overlayOpen = false;
				evt.preventDefault();
				evt.stopPropagation();
				return false;
			}
		});

		// Handle clicks on close
		$fl('#cfDialogClose').bind('click', function(evt) {
			$fl('#cfOverlay').css('display', 'none');
			overlayOpen = false;
			evt.preventDefault();
			evt.stopPropagation();
			return false;
		});

		// Escape to close
		$fl(window).bind('keyup', function(evt) {
			if(overlayOpen && evt.keyCode == 27) {
				$fl('#cfOverlay').css('display', 'none');
				overlayOpen = false;
				evt.preventDefault();
				evt.stopPropagation();
				return false;
			}
		});
	});

	fusionLib.fn.extend({
		/**
		 * Show the overlay window.
		 *
		 * @param width int The width of the window or 0 to auto size.
		 * @param height int The eight of the window or 0 to auto size.
		 * @param caption string The dialog cation.
		 * @param content string The HTML content.
		 */
		showOverlay: function(width, height, caption, content) {
			overlayOpen = true;

			if(width == 0)
				width = $fl(window).width() * .75;
			if(height == 0)
				height = $fl(window).height() * .75;

			$fl('#cfOverlayDialog').css('display', 'block');
			$fl('#cfOverlay').css('display', 'block');

			if(caption.length)
				$fl('#cfOverlayCaption').html(caption).css('display', 'block');
			else
				$fl('#cfOverlayCaption').css('display', 'none');
			$fl('#cfOverlayContent').html(content);

			// Limit max width
			if($fl('#cfOverlayDialog').css('max-width') != 'none')
				width = Math.min(parseInt($fl('#cfOverlayDialog').css('max-width')), width);

			// Set size of container
			$fl('#cfOverlayDialog')
				.width(width).height(height)
				.css('margin-left', -(width / 2) + 'px')
				.css('margin-top', -(height / 2) + 'px')
				.css('display', 'block');
		},

		/**
		 * Show the busy overlay window.
		 */
		showBusyOverlay: function() {
			overlayOpen = true;
			$fl('#cfOverlayBusy').css('display', 'block');
		},

		/**
		 * Hide the overlay.
		 */
		hideOverlay: function() {
			overlayOpen = false;
			$fl('#cfOverlay').css('display', 'none');
			$fl('#cfOverlayBusy').css('display', 'none');
		}
	});

	fusionLib.showOverlay = fusionLib.fn.showOverlay;
	fusionLib.showBusyOverlay = fusionLib.fn.showBusyOverlay;
	fusionLib.hideOverlay = fusionLib.fn.hideOverlay;
})();
