// 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 insertDialogDiv() {
$("body").append('<div id="dialog" style="display: none;" title="Create a New Account"><div id="progressbar" style="display: none; position: absolute;"><p>Please wait while we create your account...</p><br /><img src="../images/ajax-loader.gif" /></div><iframe id="newuser" style="border: none;"  width="99%" height="99%" onload="removeCheckoutButton();"><p>Your browser does not support iframes.</p></iframe></div><div id="hidden-store" style="display: none;"></div>');
}

// register dialog	
$(function() {
	insertDialogDiv();
	iframe = document.getElementById("newuser");
	$('#dialog').dialog({
		autoOpen: false,
		height: 500,
		width: 800,
		modal: true,
		resizable: false,
		draggable: false,
		open: function(event, ui) { setUpDialog(); },
		close: function() { if($('#slider').length > 0 )
					$('#slider').data('nivo:vars').stop = false; },
		buttons: {
			'Create' : createAccount,
			'Cancel' : function() {
				
				$(this).dialog('close');
			  }
			}
	});
});

function setUpDialog(){
	$.receiveMessage(
	  function(e){
		if (e.data == 'success') {
			checkForSuccess(true);
	  	}
		else if (e.data == 'nosuccess') {
			checkForSuccess(false);
		}
	   }, 'https://store.mycatholicfaithdelivered.com'
	);

	$('#newuser').show();
	$('#progressbar').hide();
}

function newCustomerDialog() {
	$('#dialog').dialog('open');
	$('#newuser').attr('src', 'https://store.mycatholicfaithdelivered.com/createaccount.aspx?checkout=false&skinid=6');
}

function removeCheckoutButton() {
	if($('#dialog').dialog('isOpen')){
		$.postMessage(
		  'customize',
		  'https://store.mycatholicfaithdelivered.com/createaccount.aspx?checkout=false&skinid=6',
		  document.getElementById("newuser").contentWindow	
	
		);
		//$('#newuser').contents().find('#btnContinueCheckout').css('display', 'none');
	}
}

//called from the iframe page
function checkForSuccess(successful) {
	$('#progressbar').fadeOut();

	if(successful){
		$('#hidden-store').load('https://store.mycatholicfaithdelivered.com/default.aspx?skinid=1');
		window.location = 'http://www.mycatholicfaithdelivered.com/Default.aspx?pagename=MyDashboard';
	}
	else {
		$('#newuser').fadeIn();
	}
}

function createAccount() {
	$.postMessage(
	  'clickContinueCheckout',
	  'https://store.mycatholicfaithdelivered.com/createaccount.aspx?checkout=false&skinid=6',
	  document.getElementById("newuser").contentWindow
	);
	//var mybutton = $('#newuser').contents().find('#btnContinueCheckout');
	//mybutton.trigger('click');
	$('#progressbar').vAlign();
	$('#newuser').fadeOut();
	$('#progressbar').fadeIn();
}

