﻿/*
* campagnerotterdamModal - jQuery Plugin
* http://www.campagne.nl
*
* Copyright (c) 2009 Dennis Kleingeld
* Licensed under the MIT license
*
* $Date: 2009-11-19 (Fri, 27 Nov 2009) $
* $version: 0.0.4
*/

jQuery(function ($) {
	var crModalSettings;

	$.fn.crModal = function (options) {
		if (!this) {
			return false;
		}
		else {
			return this.each(function () {
				/* Fill global settings */
				crModalSettings = $.extend({}, $.fn.crModal.settings, options);

				/* Get localsettings from globalsettings */
				var settings = $.extend({}, {}, crModalSettings || $.fn.crModal.settings);

				/* Set the object onclick event */
				$(this)
				.unbind('click')
				.bind('click', function () {
					/* Write debug message */
					$.crModalDebug("crModal", "Append body with modal HTML");

					/* Append modal HTML to body */
					$("body").append('<div id="' + settings.modalWrapperID + '"><div id="' + settings.modalLoadingID + '"></div><div id="' + settings.modalID + '"><iframe id="' + settings.modalIframeID + '" src="about:blank" width="1" height="1" frameborder="0" allowtransparency="true" border="0" scrolling="no"></iframe></div></div><div id="' + settings.modalBackgroundID + '"></div>');

					/* Write debug message */
					$.crModalDebug("crModal", "Initiate modal background");

					/* Initiate modal background */
					$("#" + settings.modalBackgroundID).fadeTo('fast', 0, function () {
						$(this).css({ "backgroundColor": settings.modalBackgroundColor });
						$(this).fadeTo(settings.modalBackgroundFadeInSpeed, settings.modalBackgroundOpacity);

						if (settings.modalShowLoading) {
							/* Write debug message */
							$.crModalDebug("crModalResize", "Modal Loading Image fadeIn at speed:" + settings.modalLoadingFadeInSpeed);

							$('#' + settings.modalLoadingID).fadeIn(settings.modalLoadingFadeInSpeed);
						}
					});

					/* Write debug message */
					$.crModalDebug("crModal", "Bind click event on background");

					/* Set the background onclick event on to hide the modal */
					$('#' + settings.modalBackgroundID).bind('click', function () {
						$.crModalRemove();
					});

					/* Write debug message */
					$.crModalDebug("crModal", "Show modal");

					/* Show the actual modal */
					$('#' + settings.modalWrapperID).show();

					/* Write debug message */
					$.crModalDebug("crModal", "Fill iframe with source link webpage");

					/* Open the url from the link in the modal Iframe */
					$('#' + settings.modalID).children("iframe#" + settings.modalIframeID).attr("src", $(this).attr("href"));

					/* Return false to stop the browser from navigating to the actual page */
					return false;
				});

				/* Alert if debugging if modal script is loaded */
				if (settings.debug == true) {
					if ($('#' + settings.modalDebugContainer).size() == 0) {
						$("body").append('<div id="' + settings.modalDebugContainer + '"><b>Campagnerotterdam modal debugpanel:</b><ul></ul></div>');
						var debugWidth = $('body').width();
						$('#' + settings.modalDebugContainer).css({ 'width': debugWidth });
					}
				}

				$.crModalDebug("crModal", "Initiated");
			});
		}
	}

	$.crModalManual = function (url, options) {
		if (!this) {
			return false;
		}
		else {
			if (url != "" && url != undefined) {
				$.crModalDebug("crModalManual", "Initiated");

				/* Fill global settings */
				crModalSettings = $.extend({}, $.fn.crModal.settings, options);

				/* Get localsettings from globalsettings */
				var settings = $.extend({}, {}, crModalSettings || $.fn.crModal.settings);

				/* Set the object onclick event */
				/* Write debug message */
				$.crModalDebug("crModal", "Append body with modal HTML");

				/* Append modal HTML to body */
				$("body").append('<div id="' + settings.modalWrapperID + '"><div id="' + settings.modalLoadingID + '"></div><div id="' + settings.modalID + '"><iframe id="' + settings.modalIframeID + '" src="about:blank" width="1" height="1" frameborder="0" allowtransparency="true" border="0" scrolling="no"></iframe></div></div><div id="' + settings.modalBackgroundID + '"></div>');

				/* Write debug message */
				$.crModalDebug("crModal", "Initiate modal background");

				/* Initiate modal background */
				$("#" + settings.modalBackgroundID).fadeTo('fast', 0, function () {
					$(this).css({ "backgroundColor": settings.modalBackgroundColor });
					$(this).fadeTo(settings.modalBackgroundFadeInSpeed, settings.modalBackgroundOpacity);

					if (settings.modalShowLoading) {
						/* Write debug message */
						$.crModalDebug("crModalResize", "Modal Loading Image fadeIn at speed:" + settings.modalLoadingFadeInSpeed);

						$('#' + settings.modalLoadingID).fadeIn(settings.modalLoadingFadeInSpeed);
					}
				});

				/* Write debug message */
				$.crModalDebug("crModal", "Bind click event on background");

				/* Set the background onclick event on to hide the modal */
				$('#' + settings.modalBackgroundID).bind('click', function () {
					$.crModalRemove();
				});

				/* Write debug message */
				$.crModalDebug("crModal", "Show modal");

				/* Show the actual modal */
				$('#' + settings.modalWrapperID).show();

				/* Write debug message */
				$.crModalDebug("crModal", "Fill iframe with source link webpage");

				/* Open the url from the link in the modal Iframe */
				$('#' + settings.modalID).children("iframe#" + settings.modalIframeID).attr("src", url);

				/* Return false to stop the browser from navigating to the actual page */
			}
			return false;
		}
	}

	/* Bind Esc key to remove modal */
	$.crModalCheckKeyPress = function (e) {
		/* Get localsettings from globalsettings */
		var settings = $.extend({}, {}, crModalSettings || $.fn.crModal.settings);
		
		if (settings.enableEscapeRemove) {
			var code;
			if (!e) var e = window.event;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;
			if (code == 27 && $('#' + settings.modalID).size() > 0) {
				$.crModalDebug("crModalKeyPress", "Remove Initiated");
				$.crModalRemove();
			}
		}
	};

	/* Default settings modal */
	$.fn.crModal.settings = {
		modalWrapperID: "crModalWrapper",
		modalShowLoading: true,
		modalLoadingID: "crModalLoading",
		modalLoadingFadeInSpeed: 'fast',
		modalLoadingFadeOutSpeed: 'fast',
		modalID: "crModal",
		modalIframeID: "crModalIframe",
		modalContentHiderID: 'modalhider',
		modalBackgroundID: "crModalBackground",
		modalDebugContainer: 'crModalDebug',
		modalBackgroundColor: "#FFFFFF",
		modalBackgroundOpacity: 0.8,
		modalBackgroundFadeInSpeed: 'medium',
		modalBackgroundFadeOutSpeed: 'medium',
		modalResizeSpeed: 'slow',
		modalHiderFadeInSpeed: 'fast',
		modalHiderFadeOutSpeed: 'fast',
		modalDefaultSelector: '.crModal',
		enableEscapeRemove: true,
		debug: false
	}

	/* Function to remove the modal from the body */
	$.crModalRemove = function () {
		/* Get localsettings from globalsettings */
		var settings = $.extend({}, {}, crModalSettings || $.fn.crModal.settings);

		if (settings.modalShowLoading) {
			/* Write debug message */
			$.crModalDebug("crModalResize", "Modal Loading Image hidden if visible");

			$('#' + settings.modalLoadingID).hide();
		}

		/* Write debug message */
		$.crModalDebug("crModalResize", "Content hider fadeIn");

		try {
			/* Fade in the content hider to hide the content from view when resizing */
			$('iframe#' + settings.modalIframeID).contents().find("#" + settings.modalContentHiderID).fadeIn(settings.modalHiderFadeInSpeed, function () {
				/* Write debug message */
				$.crModalDebug("crModalRemove", "Initiated");

				/* Initiate the resizing of the modal */
				$.crModalResize(2, 2, true);
			});

			if ($('iframe#' + settings.modalIframeID).contents().find("#" + settings.modalContentHiderID).size() == 0) {
				/* Write debug message */
				$.crModalDebug("crModalRemove", "Initiated modal remove because no contentHider found but no error occured");

				/* Initiate the resizing of the modal */
				$.crModalResize(2, 2, true);
			}
		}
		catch (Err) {
			/* Write debug message */
			$.crModalDebug("crModalRemove", "Initiated modal remove because no contentHider found");

			/* Initiate the resizing of the modal */
			$.crModalResize(2, 2, true);
		}
	}

	$.crModalResize = function (newHeight, newWidth, hide) {
		/* Get localsettings from globalsettings */
		var settings = $.extend({}, {}, crModalSettings || $.fn.crModal.settings);

		/* Write debug message */
		$.crModalDebug("crModalResize", "Initiated. width: " + newWidth + ", height: " + newHeight + ", removeAfter: " + hide);

		/* Get the marginTop and marginLeft to position the modal */
		var marginTop = -Math.round(newHeight / 2);
		var marginLeft = -Math.round(newWidth / 2);

		/* If $.crModalRemove is initiating this function */
		if (hide == true) {

			/* Write debug message */
			$.crModalDebug("crModalResize", "Background fadeOut. speed: " + settings.modalBackgroundFadeOutSpeed);

			/* Fade background out */
			$('#' + settings.modalBackgroundID).fadeTo(settings.modalBackgroundFadeOutSpeed, 0, function () {
				/* Remove the modal background */
				$(this).remove();

				/* Write debug message */
				$.crModalDebug("crModalResize", "Background removed");
			});
		}
		else {
			if (settings.modalShowLoading) {
				/* Write debug message */
				$.crModalDebug("crModalResize", "Modal Loading Image fadeOut at speed:" + settings.modalLoadingFadeOutSpeed);

				$('#' + settings.modalLoadingID).fadeOut(settings.modalLoadingFadeOutSpeed);
			}
		}

		/* Write debug message */
		$.crModalDebug("crModalResize", "Modal resize. speed: " + settings.modalResizeSpeed);

		/* The actual resizing of the modal to the newHeight and newWidth and reposition it in the right position */
		$('#' + settings.modalID).animate({ "marginLeft": marginLeft, "marginTop": marginTop }, settings.modalResizeSpeed).children('iframe#' + settings.modalIframeID).animate({ "width": newWidth, "height": newHeight }, settings.modalResizeSpeed, function () {
			/* If $.crModalRemove is initiating this function */
			if (hide == true) {
				/* Remove the modal from the body */
				$('#' + settings.modalWrapperID).hide().remove();

				/* Write debug message */
				$.crModalDebug("crModalResize", "Modal removed");
			}
			else {
				/* Write debug message */
				$.crModalDebug("crModalResize", "Content hider fadeOut");

				/* Fade out the content hider to show the content of the modal to user */
				$('iframe#' + settings.modalIframeID).contents().find("#" + settings.modalContentHiderID).fadeOut(settings.modalHiderFadeOutSpeed);
			}
		});
	}

	$.crModalDebug = function (func, msg) {
		/* Get localsettings from globalsettings */
		var settings = $.extend({}, {}, crModalSettings || $.fn.crModal.settings);
		if (settings.debug == true) {
			var d = new Date();
			$('#' + settings.modalDebugContainer + ' ul').prepend('<li><label>' + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "&nbsp;&nbsp;&nbsp;" + func + '</label><span>' + msg + '</span></li>');
		}
	}

	/* Default initiation of campagnerotterdam Modal */
	$($.fn.crModal.settings.modalDefaultSelector).crModal();
	$(document).keypress(function (e) {
		$.crModalCheckKeyPress(e);
	});
});