/*
 * Connect affiliate network wizard
 */

(function ($) {

$(function () {
	
	/**
	 * Display first step of checkout dialog
	 * 
	 * @param {string} params Additional parameters to pass through checkout process (must be url encoded)
	 */
	aj.checkoutDialog = function (params) {
		var params = params || '';
		Lightview.show({
			href: '/connect/checkout',
			rel: 'ajax',
			options: {
				topclose: true,
				autosize: true,
				ajax: {
					parameters: params,
					onComplete: function () {
						// bind actions for plan buttons
						$('#checkout .payment-btn').click(function () {
							Lightview.show({
								href: '/connect/offer/' + $(this).attr('id').replace(/^payment-/, ''),
								rel: 'ajax',
								options: {
									topclose: true,
									autosize: true,
									ajax: {
										onComplete: function () {
											// bind action for special offer buttons
											$('#offer .payment-btn').click(function () {
												Lightview.show({
													href: '/connect/paypal/' + $('#offer input[name="plan"]').val(),
													rel: 'ajax',
													options: {
														topclose: true,
														ajax: {
															parameters: "special=" + $(this).attr('id').replace(/-btn$/, '') + '&' + params,
															onComplete: function () {
																//automatically rederect user to paypal
																$('form#paypal')[0].submit();
																Lightview.hide();
															}
														}
													}
												});
												return false;
											});
											// bind back button
											$('#offer .back-button').click(function () {
												aj.checkoutDialog(params);
											});
										}
									}
								}
							});
							
							return false;
						});
					}
				}
			}
		});
		
		return false;
	};
		
});
	
})(jQuery);
