/* Check for IE 7 or Opera 9.5 or less and deactivate the sendmessage code. For reference, see: http://yura.thinkweb2.com/cft/
   We are currently checking for DOCUMENT_GETELEMENTBYID_CONFUSES_IDS_WITH_NAMES */

function invalidBrowser () {
  if (document.getElementsByTagName && document.createElement) {
      var num = Number(new Date), name = "__test_" + num, head = document.getElementsByTagName("head")[0], isBuggy = false, el;
      try {
          el = document.createElement("<input name=\"" + name + "\">");
      } catch (e) {
          el = document.createElement("input");
          el.name = name;
      }
      if (head.appendChild && head.removeChild) {
          head.appendChild(el);
          var testElement = document.getElementById(name);
          isBuggy = !!(testElement &&
              testElement.nodeName.toUpperCase() === "INPUT");
          head.removeChild(el);
          el = null;
          return isBuggy;
      }
  }
  return null;
}

/* Basic page setup 

	Sets up the login dialog popup
	Sets up a video window modal popup if there is a div on the page for it
	Sets up the popup dialogs for the request password and new account dialogs, using the
	invalid browser check code above
*/

$(function() {

	//Set up the login dialog
	
	$('#login_link').click(function() { $('#login').fadeIn('normal',
		function() {
		    	$('#logoutframe').html('<iframe src="http://www.mycatholicfaithdelivered.com/components/Logout/tabid/58/Default.aspx" Application="Yes" style="width:0%; height:0%;" frameborder="0"></iframe>');
			}
			);
		});
	$('#login #closeicon').click(function() { $('#login').fadeOut('normal', function() {
				$('#logoutframe').html('');
		}
		);
	});
	
	//Check to make sure there's no error message, if there is, display the login box
	var errormessagelength = $('#ctl00_DynamicPageWebPartManager1_gwp1_1_lblMessage').text().length;
	if(errormessagelength > 0) {
		$('#login').show();
	}
	
	//Set up the video player dialog if needed on the page
	if($('#videoplayer').length > 0 ) {
		$('#videoplayer').dialog({
			autoOpen: false,
			height: 500,
			width: 800,
			modal: true,
			resizable: false,
			draggable: false,
			title: 'Video Demo',
			close: function() { 
				var player = document.getElementById('demoplayer');
				player.sendEvent("stop");
				 } 
		});
		$('#videoimage').click(function() {$('#videoplayer').dialog('open'); });
	}


		//Catch the default event for the register and request password link
		$("a[id$='lnkRequest'],div[id$='lnkRequest']").click(function(e) {
			e.preventDefault();
			if($('#imageslider').length > 0 )
				$('#imageslider').cycle('toggle');
			newCustomerDialog();
		});
	if( !invalidBrowser()){
		$("a[id$='lnkRequestPassword'],div[id$='lnkRequestPassword']").click(function(e) {
			e.preventDefault();
			if($('#imageslider').length > 0 )
				$('#imageslider').cycle('toggle');
			requestPasswordDialog();
		});
	}
});


/* New Account Dialog */

// 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="Sign Up"><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" scrolling="no"  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,
		position:'left',
		height: 350,
		width: 450,
		modal: false,
		resizable: false,
		draggable: true,
		open: function(event, ui) { $('#newuser').css('overflow','hidden');$('.ui-widget-overlay').css('width','100%');  },
		close: function() { $('#newuser').css('overflow','auto'); }
	
	});
});

function setUpDialog(){
	$.receiveMessage(
	  function(e){
		if (e.data == 'success') {
			checkForSuccess(true);
	  	}
		else if (e.data == 'nosuccess') {
			checkForSuccess(false);
		}
	   }, 'http://www.mycatholicfaithdelivered.com/components/Redirect/RegisterWithCoupon/tabid/140/Default.aspx'
	);

	$('#newuser').show();
	$('#progressbar').hide();
}

function newCustomerDialog() {
	$('#dialog').dialog('open');
	$('#newuser').attr('src', 'http://www.mycatholicfaithdelivered.com/components/Redirect/RegisterWithCoupon/tabid/140/Default.aspx');
}

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();
}

/* Request Password Dialog Code */

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($('#imageslider').length > 0 )
					$('#imageslider').cycle('toggle'); },
		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($('#imageslider').length > 0 )
						$('#imageslider').cycle('toggle'); },
			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
	);
}


