/*
 * jQuery Modal Dialog plugin 1.0
 * Released: July 14, 2008
 * 
 * Copyright (c) 2008 Chris Winberry
 * Email: transistech@gmail.com
 * 
 * Original Design: Michael Leigeber
 * http://www.leigeber.com/2008/04/custom-javascript-dialog-boxes/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * @license http://www.opensource.org/licenses/mit-license.php
 * @license http://www.gnu.org/licenses/gpl.html
 * @project jquery.modaldialog
 * 
 * 
 * This is MODIFIED jquery.modaldialog, NOT orginal.
 * Changes by Domagoj Trsan (domagoj.trsan@calyx.hr)
 * Released: December 14, 2011
 * 
 */
(function($) {
	var modaldialog = { };

	// Creates and shows the modal dialog
	function showDialog (msg, options) {

		// Merge default title (per type), default settings, and user defined settings
		var settings = modaldialog.defaults;

		// If there's no timeout, make sure the close button is show (or the dialog can't close)
		settings.timeout = (typeof(settings.timeout) == "undefined") ? 0 : settings.timeout;
		settings.showClose = ((typeof(settings.showClose) == "undefined") | !settings.timeout) ? true : !!settings.showClose;

		dialog = document.createElement('div');
		dialog.id = 'dialog';
			
		if(options.type == "progress") {
			$(dialog).html(
					"<div id='dialog-content'>" +
						"<div id='dialog-content-inner' />" +
						"<div id='dialog-progress-container'><div id='dialog-progress'/><div>" +
					"</div>"
					);
		} else if(options.type == "subscribe") {
			$(dialog).addClass('subscribe');
			$(dialog).html(
					"<div id='dialog-content' class='subscribe'>" +
					"<div id='dialog-content-inner' />" +
					"<div id='dialog-button-container'>" +
						"<label>E-mail</label><input type='text' id='notify-email-address' value='' /><input type='button' id='notify-me-button' class='button green-small' value='Notify me'>"+
						"<br/><br/><input type='button' id='dialog-button' class='button green-big' value='Close'>" +
					"</div>" +
				"</div>"
					);
		} else {
			$(dialog).html(
				"<div id='dialog-content'>" +
					"<div id='dialog-content-inner' />" +
					"<div id='dialog-button-container'>" +
						"<input type='button' id='dialog-button' class='button green-big' value='OK'>" +
					"</div>" +
				"</div>"
				);
		}
		
		if (!document.getElementById('dialog')) {
			dialogmask = document.createElement('div');
			dialogmask.id = 'dialog-mask';
		
			$(dialogmask).hide();
			$(dialog).hide();
		
			document.body.appendChild(dialogmask);
		} else {
			$('#dialog').remove();
		}
		
		document.body.appendChild(dialog);
			// Set the click event for the "x" and "Close" buttons			
		$("#dialog-close").click(modaldialog.hide);
		$("#dialog-button").click(modaldialog.hide);
		$("#notify-me-button").click(modaldialog.notify);
		$("#notify-me-button").data('url', settings.url);

		var dl = $('#dialog');
		var dlh = $('#dialog-header');
		var dlc = $('#dialog-content');
		var dlb = $('#dialog-button');

		$('#dialog-content-inner').html(msg);

		// Center the dialog in the window but make sure it's at least 25 pixels from the top
		// Without that check, dialogs that are taller than the visible window risk
		// having the close buttons off-screen, rendering the dialog unclosable 
		dl.css('width', settings.width);
		var dialogTop = Math.abs($(window).height() - dl.height()) / 2;
		dl.css('left', ($(window).width() - dl.width()) / 2);
		dl.css('top', (dialogTop >= 25) ? dialogTop : 25);

		if (!settings.showClose) {
			$('#dialog-close').hide();
			$('#dialog-button-container').hide();
		} else {
			$('#dialog-close').show();
			$('#dialog-button-container').show();
		}

		if (settings.timeout) {
			modaldialog.timeout = window.setTimeout("$('#dialog').fadeOut('normal', 0); $('#dialog-mask').fadeOut('fast', 0);", (settings.timeout * 1000));
		}
		
		dl.fadeIn("slow");
		$('#dialog-mask').fadeIn("normal");
	};

	modaldialog.show = function $$modaldialog$error (msg) {
		var options = {type: "normal"};
		return(showDialog(msg, options));
	};
	
	modaldialog.progress = function $$modaldialog$error (msg) {
		var options = {type: "progress"};
		return(showDialog(msg, options));
	};
	
	modaldialog.subscribe = function $$modaldialog$error (msg) {
		var options = {type: "subscribe"};
		return(showDialog(msg, options));
	};

	modaldialog.hide = function $$modaldialog$hide () {
		$('#dialog').fadeOut("slow", function () { $(this).remove(); });
		$('#dialog-mask').fadeOut("normal", function () { $(this).remove(); });
	};
	
	modaldialog.notify = function $$modaldialog$notify () {
		$('#notify-email-address').removeClass('error');
		var params = {
				'email': $('#notify-email-address').val()
		};
		$.get($("#notify-me-button").data('url'), params)
		.success(function(data) {

			if(!data['error']) {
				$('#dialog').fadeOut("slow", function () { $(this).remove(); });
				$('#dialog-mask').fadeOut("normal", function () { $(this).remove(); });
			} else {
				$('#notify-email-address').addClass('error');
			}
		}).error(function() {
			$('#notify-email-address').addClass('error');
		});
	};

	modaldialog.defaults = {
		timeout: 0
		, url: ''
		, showClose: true
	};

	$.extend({ modaldialog: modaldialog });
})(jQuery);

