/**	Code Dependencies:
*	- "jquery-1.7.1.js"
*
*
**	Version:
*	1.0
*
*
**	Usage:
*
*	Attach this plug-in to any clickable calling object. Set the
*	calling object's "data-modal-content-id" attribute 
*	to the id value of the content that you want displayed in the
*	modal.
*
*
**	Options:
*
*	options = {
*		$openModalLinks: jQuery object set [jQuery Object - REQUIRED],
*		see "defaultSettings" below for overridable values
*	}
*
**	Note: Forked from: leanModal v1.0 by Ray Stone - http://finelysliced.com.au
*
*/

(function ($) {
	$.fn.lecynkarModal = function (options) {
		var defaultSettings = {
			$openModalLinks: null, // jQuery object set [jQuery Object - REQUIRED]
			$closeModalLinks: null, // jQuery object set [jQuery Object - OPTIONAL]
			modalOffsetTop: 100, 
			modalOffsetBottom: 100,
			overlayOpacity: 0.5,
			overlayCss: {
				'position': 'fixed',
				'z-index': '100',
				'top': '0px',
				'left': '0px',
				'height': '100%',
				'width': '100%',
				'background': '#000',
				'display': 'none'
			},
			modalCss: {
				'overflow': 'auto',
				'position': 'fixed',
				'opacity': '0',
				'z-index': '11000',
				'left': '50%'
			},
			beforeOpen: null, // Callback function [function() - OPTIONAL]
			afterClose: null // Callback function [function() - OPTIONAL]
		}

		var settings = $.extend(true, {}, defaultSettings, options);

		// Create the "lecynkar" namespace if it does not exist
		(function (lecynkar) { })(window.lecynkar = window.lecynkar || {}, jQuery);

		// Create and/or increment the lecynkar.lecynkarModalCount variable/property
		if (lecynkar.lecynkarModalCount) {
			lecynkar.lecynkarModalCount++;
		} else {
			lecynkar.lecynkarModalCount = 1;
		}

		//this.each(function () {

		// Create the $modal from the data-modal-content-id value on the calling object 
		//var $modal = $("#" + $(this).data('modalContentId'));
		var $modal = this;

		// Build Overlay and attach to Body
		var $overlay = _buildOverlay(settings, $modal);

		// Attach custom event to call to close this Modal and the Overlay
		$modal.on('close', function () {
			$overlay.click();
		});

		// Hook $openModalLinks [REQUIRED]
		if (settings.$openModalLinks != null) {
			settings.$openModalLinks.each(function () {
				$(this).click(function (event) {

					// Prevent this click event bubble
					event.preventDefault();

					// Pre-Open Callback
					if ($.isFunction(settings.beforeOpen)) {
						settings.beforeOpen();
					}

					// Measure Modal
					$modal.css('height', '').css('visibility', 'hidden').show();

					var modalMeasuredSize = {
						outerWidth: null,
						outerHeight: null,
						height: null
					}

					modalMeasuredSize.outerWidth = $modal.outerWidth();
					modalMeasuredSize.outerHeight = $modal.outerHeight();
					modalMeasuredSize.height = $modal.height();

					$modal.hide().css('visibility', '');

					// Show Overlay
					$overlay.css({ 'display': 'block', opacity: 0 });
					$overlay.fadeTo(300, settings.overlayOpacity);

					// Position Modal
					$.extend(true, settings.modalCss, _getModalCssHeight(settings, modalMeasuredSize, $modal), { 'margin-left': -(modalMeasuredSize.outerWidth / 2) + "px", 'top': settings.modalOffsetTop + "px" });

					// Show Modal
					$modal.css(settings.modalCss).fadeTo(300, 1);

				});
			});
		} else {
			alert("Lecynkar Modal Plugin: Alert!!! You must supply a jQuery object for the '$openModalLinks' options property in order to open the modal!");
		}

		// Hook $closeModalLinks if supplied
		if (settings.$closeModalLinks != null) {
			settings.$closeModalLinks.each(function () {
				$(this).on('click', function (event) {
					// Prevent this click event bubble
					event.preventDefault();
					$overlay.click();
				});
			});
		}

		return this; // Return jQuery Object for chaining
	}

	function _buildOverlay(settings, $modal) {
		// Create Overlay
		var $overlay = $("<div id='lecynkarOverlay" + +lecynkar.lecynkarModalCount + "'></div>");

		// Set Overlay css from settings and it's onclick event to close the Modal and Overlay
		$overlay.css(settings.overlayCss).on('click', function (event) {

			// Prevent this click event bubble
			event.preventDefault();

			// Hide Overlay and Post-Close Callback
			if ($.isFunction(settings.afterClose)) {
				$overlay.fadeOut(200, settings.afterClose);
			} else {
				$overlay.fadeOut(200);
			}
			$modal.hide();
		});

		// Add the Overlay after the $modal in the DOM (added after element because of layering not respecting z-index in IE 7)
		$overlay.insertAfter($modal);

		// Return this instance of the Overlay
		return $overlay;
	}

	function _getModalCssHeight(settings, modalMeasuredSize) {
		var css = {};

		var windowHeight = $(window).height();

		if (modalMeasuredSize.outerHeight + settings.modalOffsetTop + settings.modalOffsetBottom > windowHeight) {
			css.height = (windowHeight - settings.modalOffsetTop - settings.modalOffsetBottom) + "px";
		} else {
			css.height = (modalMeasuredSize.height) + "px";
		}

		return css;
	}

})(jQuery);
