CAPTURE = {
  // CAPTURE.resize function gets called when the content of the iframe changes.
  resize: function(jargs) {
	if( window.console ){ console.info('Captrue.resize(jargs) called.',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'});

	if(jQuery.fancybox)
	{
    	jQuery.fancybox.resize();
    	jQuery.fancybox.center();
    }else if(window.console && console.warn ){ console.warn('no jQuery.fancybox object in scope.'); }
  },
  // 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() {
		if( window.console ){ console.info('CaptureIntegration.passwordRequestedListener() called.'); }
		// 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, and should arrive 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( window.console ){ console.info('CaptureIntegration.closedListener(currentArray, currentIndex, currentOpts) called.',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){
		if( window.console ){ console.info('CaptureIntegration._showMessage(msg, type, timeout) called.',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(){
		if( window.console ){ console.info('CaptureIntegration.editProfileListener() called.' ); }
		CaptureIntegration._closeMessages.push({message:"Your profile has been updated.",
												timeout:1500,
												type:'SUCCESS'}
												);
		jQuery.fancybox.close();
	},
	triggerAutoOpen:function( screen ){
		jQuery.ajax({
			  url: "/__/account/2.1/ajax/get_url.php",
			  data: {'for': screen },
			  success: function(data){
				  if( 'SUCCESS' == data.detail_code ){
					  if(window.console){console.log('url retrieved',data)}
						jQuery.fancybox({ href: data.url,
											padding: 0, 
											scrolling: "no", 
											autoScale: true, 
											autoDimensions: true, 
											type: "iframe",
											transitionOut: 'none',
											onClosed:CaptureIntegration.closedListener });
				  }
			  }
			});
	}
};

jQuery(document).ready(function($) {
	$("a.captureIframe").fancybox({ padding: 0, 
									scrolling: "no", 
									autoScale: true, 
									autoDimensions: true, 
									type: "iframe",
									transitionOut: 'none',
									onClosed:CaptureIntegration.closedListener });
	if( window.location.hash ) {
		var autoHash = /^#oauth-(\w+)$/.exec(window.location.hash);
		if( autoHash )
		{
			CaptureIntegration.triggerAutoOpen(autoHash[1]);
			window.location.hash = window.location.hash + ".complete";
		}
	}
});
