// Set the domain
document.domain = "mycatholicfaithdelivered.com";

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	var aw = $(this).width();
	var pw = $(this).parent().width();
	var mw = (pw - aw) /2;
	$(this).css('margin-left', mw);
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

function insertRequestPasswordDialogDiv() {
$("body").append('<div id="newpassword-message" style="display: none;" title="Password Sent"><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span><p align=left>Your password has been sent.<br />Please check your email.</p></div><div id="newpassworddialog" style="display: none;" title="Request a Password"><div id="newpasswordprogressbar" style="display: none; position: absolute;"><p>Please wait while we send you a new password...</p><br /><img src="../images/ajax-loader.gif" /></div><iframe id="newpasswordframe" style="border: none;"  width="99%" height="99%" onload="customizeForm();"><p>Your browser does not support iframes.</p></iframe></div><div id="hidden-store"></div>');
}

// register dialog	
$(function() {
	insertRequestPasswordDialogDiv();
	$('#newpassworddialog').dialog({
		autoOpen: false,
		height: 500,
		width: 800,
		modal: true,
		resizable: false,
		draggable: false,
		close: function() { if($('#slider').length > 0 )
					$('#slider').data('nivo:vars').stop = false; },
		open: function(event, ui) { setUpNewPasswordDialog(); },
		buttons: {
			'Send' : sendPassword,
			'Cancel' : function() {
				$(this).dialog('close');
			  }
			}
	});

	$('#newpassword-message').dialog({
			modal: true,
			resizable: false,
			draggable: false,
			autoOpen: false,
			close: function() { if($('#slider').length > 0 )
						$('#slider').data('nivo:vars').stop = false; },
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});



});

function setUpNewPasswordDialog() {
	$.receiveMessage(
	  function(e){
		if (e.data == 'success') {
			newPasswordCheckForSuccess(true);
	  	}
		else if (e.data == 'failure') {
			newPasswordCheckForSuccess(false);
		}
		else if (e.data == 'requesting') {
			$('#newpasswordprogressbar').vAlign();
			$('#newpasswordframe').fadeOut();
			$('#newpasswordprogressbar').fadeIn();
		}
	   }, 'https://store.mycatholicfaithdelivered.com'
	);

	$('#newpasswordframe').show();
	$('#newpasswordprogressbar').hide();
}

function requestPasswordDialog() {
	$('#newpassworddialog').dialog('open');
	$('#newpasswordframe').attr('src', 'https://store.mycatholicfaithdelivered.com/signin.aspx?skinid=6');
}

function customizeForm() {
	if($('#newpassworddialog').dialog('isOpen'))
	{
		$.postMessage(
		  'customize',
		  'https://store.mycatholicfaithdelivered.com/signin.aspx?skinid=6',
		  document.getElementById("newpasswordframe").contentWindow

		);
	}
}

//called from the iframe page
function newPasswordCheckForSuccess(successful) {
	$('#newpasswordprogressbar').fadeOut();

	if(successful){
		$('#newpassworddialog').dialog('close');
		$('#newpassword-message').dialog('open');
		$('#hidden-store').load('https://store.mycatholicfaithdelivered.com/default.aspx?skinid=1');
	}
	else {
		$('#newpasswordframe').fadeIn();
	}
}

function sendPassword() {
	$.postMessage(
	  'clickRequestPassword',
	  'https://store.mycatholicfaithdelivered.com/signin.aspx?skinid=6',
	  document.getElementById("newpasswordframe").contentWindow
	);
}

