CAPTURE = {
  // CAPTURE.resize function gets called when the content of the iframe changes.
  resize: function(jargs) {

    var args = JSON.parse(jargs);

    jQuery("#fancybox-inner").css({'width': args.w + 'px', 'height': args.h + 'px'});
    jQuery("#fancybox-wrap").css({'width': args.w + 'px', 'height': args.h + 'px'});
    jQuery("#fancybox-content").css({ "height": args.h + 'px', "width": args.w + 'px'});
    jQuery("#fancybox-frame").css({'width': args.w + 'px', 'height': args.h + 'px'});

    jQuery.fancybox.resize();
    jQuery.fancybox.center();

  },
  // Capture.token_expired should be called if the token is ever expired.
  token_expired: function (jargs ){
	  // This functionality is thus far untested.
	  alert( 'Your session has expired.  You may need to log out, then log in again to continue.');
  }
};

CaptureIntegration = {
		_closeMessages:[],
		passwordRequestedListener: function() {
		// called (via recover_password_callback parameter) after a user enters their email and hits 'continue' to request a password reset email.
		CaptureIntegration._closeMessages.push({message:"<br />Thank you.<br /><br/>A password reset link has been sent to the email address entered shortly." +
				"<br /><br />If you do not see the email within the hour, please be sure to check your spam filter or junk email folder." +
				"<br />The email will come from service@dpsgaccounts.com and will have the subject &quot;Instructions to reset your Dr Pepper Snapple Group Website Password&quot;.<br /><br />" +
				"<span class='note'>Also, if you have more than one email address, be sure that you entered an address that corresponds to an account. (For your security, this site will not reveal whether or not a valid email address was entered.)</span><br /><br />" +
				"<br /><a href='#', onclick='$.fancybox.close();return false;'>Close<a><br/>",
				timeout:0, 
				type:'SUCCESS'});
		jQuery.fancybox.close();
	},
	closedListener:function(currentArray, currentIndex, currentOpts) {
		if( CaptureIntegration._closeMessages.length )
		{
			var msg = CaptureIntegration._closeMessages.pop();//its stupid that even the onclosed callback requires a timeout because fancybox still hasn't finsihed reseting its values before it calls the onClosed callback.
			setTimeout(function(){CaptureIntegration._showMessage(msg.message, msg.type, msg.timeout)},100);
		}
	},
	_showMessage:function(msg, type, timeout){
		parent.jQuery.fancybox('<div class="CaptureIntegrationMessage ' + type + '">'+msg+'</div>',
			{
        	'autoDimensions'	: true,
			'width'         	: 350,
			'height'        	: 'auto',
			'transitionIn'		: 'swing',
			'speedIn'			: 0,
			'centerOnScroll'	: true,
			'transitionOut'		: timeout?'fade':'swing'
		});
		if( timeout )
		{
			setTimeout(jQuery.fancybox.close, timeout );
		}
	},
	editProfileListener:function(){
		CaptureIntegration._closeMessages.push({message:"Your profile has been updated.",
												timeout:1500,
												type:'SUCCESS'}
												);
		jQuery.fancybox.close();
	}
};

jQuery(document).ready(function($) {

	$("a.captureIframe").fancybox({ padding: 0, 
									scrolling: "no", 
									autoScale: true, 
									autoDimensions: true, 
									type: "iframe",
									transitionOut: 'none',
									onClosed:CaptureIntegration.closedListener }); 
});
