/*
 * Copyright (c) 2011 Arron Bailiss <arron@arronbailiss.com>
 * Based on original code from Steve Chipman (slayeroffice.com)
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */


(function($) {
	$.fn.customAlert = function(options) {
		var settings = {
			'alertTitle' : 'Notice!',
			'alertOk'	 : 'OK',
			'draggable'	 : false
		};
		
		if (options) $.extend(settings, options);
		
		if(document.getElementById) {
			window.defaultAlert = window.alert;
			window.alert = function(msgTxt) {
				if ($('#modalDiv').length > 0) return; // Only ever show one alert
				
				// The modal div to block out the rest of the document whilst the alert is shown
				var modalDiv = $('<div></div>');
				modalDiv.attr('id', 'modalDiv');
				modalDiv.height($(document).height()); // Make overlay cover the whole window
				
				// The alert container
				var alertDiv = $('<div></div>');
				alertDiv.attr('id', 'alertDiv');
				
				
				// The alert text to display
				var msgP = $('<p></p>');
				msgP.html(msgTxt);
				
				// OK button - will remove/close the alert on click
				var okBtn = $('<a></a>');
				okBtn.addClass('okBtn');
				okBtn.addClass('addButt');
				okBtn.text(settings.alertOk);
				okBtn.attr('href', '#');
				
				// Append elements to document body
				alertDiv.append(msgP);
				if(settings.alertTitle == 'Login'){
					alertDiv.append('<form action=\"login_verify2.php\" method=\"post\" id=\"loginform\" name=\"loginform\" class=\"form\"><p><span>Email Address</span><input type=\"text\" name=\"email_address\" value=\"\"></p><p><span>Password</span><input type=\"password\" name=\"password\" value=\"\"></p></form>');
					var buttons = $('<div></div>');
					buttons.addClass('buttons');
					alertDiv.append(buttons);
					
					var okBtn = $('<a></a>');
					okBtn.addClass('okBtn');
					okBtn.addClass('addButt');
					okBtn.text('Log In');
					okBtn.attr('href', '#');
					buttons.append(okBtn);
					
					var cancelBtn = $('<a></a>');
					cancelBtn.addClass('okBtn');
					cancelBtn.addClass('addButt');
					cancelBtn.text('Cancel');
					cancelBtn.attr('href', '#');
					buttons.append(cancelBtn);
					
					$(okBtn).click(function() {
 						$('#loginform').submit();
					});
					
					alertDiv.append('<p>Forgot your password?  <a href=\"remindme.php\">Remind me!</a></p><p>Don\'t have an account?  <a href=\"registration2.php\">Create one now!</a></p></form>');
					
				} else {
					alertDiv.append(okBtn);
				}
				$('body').append(modalDiv);
				$('body').append(alertDiv);
				
				$(modalDiv).hide().fadeIn('fast');
				$(alertDiv).hide().fadeIn('fast');
				
				// Center alert on page
				$('#alertDiv').css('top', ($(window).height()/2.5) - ($('#alertDiv').height()/2)+'px');
				$('#alertDiv').css('left', ($(window).width()/2) - ($('#alertDiv').width()/2)+'px');
				
				
				// Bind OK button to remove/close alert
				$('#alertDiv .okBtn, #modalDiv').bind('click', function(e) {
					$('#alertDiv').fadeOut(300, function(){ 
   						$(this).remove();
					});
					$('#modalDiv').fadeOut(300, function(){ 
    					$(this).remove();
					});
					e.preventDefault();
					
				});
			};
		}
	};
})(jQuery);
