/**
 * Any events need to be connected with jQuery(document).on([event], [selector], [function/callback];
 *
*/
jQuery(function(){

	// Test menu for changing states
	jQuery(document).on('click', 'a.it-exchange-test-load-state-via-ajax', function(event){
		event.preventDefault();
		var state = jQuery(this).data('it-exchange-sw-state');
		itExchangeGetSuperWidgetState( state );
	});

	// Register Clear Cart event
	jQuery(document).on('click', '.it-exchange-super-widget a.it-exchange-empty-cart', function(event) {
		event.preventDefault();
		// itExchangeSWOnProductPage is a JS var set in lib/super-widget/class.super-widget.php. It contains an ID or is false.
		itExchangeSWEmptyCart( itExchangeSWOnProductPage );
	});

	// Register Remove Product from Cart event
	jQuery(document).on('click', '.it-exchange-super-widget a.remove-cart-item', function(event) {
		event.preventDefault();
		var product  = jQuery(this).data('cart-product-id');
		itExchangeSWRemoveItemFromCart( product );
	});

	// Register Update Quantity event
	jQuery(document).on('input keyup change', '.it-exchange-super-widget input.product-cart-quantity', function(event) {
		event.preventDefault();
		var product  = jQuery( this ).data('cart-product-id');
		var quantity = jQuery( this ).val();
		itExchangeSWUpdateQuantity(product, quantity);
	});

	jQuery(document).on('submit', '.it-exchange-super-widget form.it-exchange-sw-update-cart-quantity', function(event) {
		event.preventDefault();
		jQuery( '.it-exchange-super-widget input.product-cart-quantity', jQuery(this).closest('.it-exchange-super-widget') ).each( function() {
			var product  = jQuery( this ).data('cart-product-id');
			var quantity = jQuery( this ).val();
			itExchangeSWUpdateQuantity(product, quantity);
		});
		itExchangeGetSuperWidgetState( 'checkout' );
	});

	// Register View Checkout link
	jQuery(document).on('click', '.it-exchange-super-widget a.it-exchange-checkout-cart', function(event) {
		if ( ! jQuery(this).hasClass( 'no-sw-js' ) ) {
			event.preventDefault();
			itExchangeGetSuperWidgetState( 'checkout' );
		}
	});

	// Register Buy Now event
	jQuery(document).on('submit', 'form.it-exchange-sw-buy-now', function(event) {
		event.preventDefault();
		var quantity = jQuery(this).children('.product-purchase-quantity').length ? jQuery(this).children('.product-purchase-quantity').val() : 1;
		var product  = jQuery(this).children('.buy-now-product-id').val();
		itExchangeSWBuyNow( product, quantity );
	});

	// Register Add to Cart event
	jQuery(document).on('submit', 'form.it-exchange-sw-add-to-cart', function(event) {
		event.preventDefault();
		var quantity = jQuery(this).children('.product-purchase-quantity').length ? jQuery(this).children('.product-purchase-quantity').val() : 1;
		var product  = jQuery(this).children('.add-to-cart-product-id').attr('value');
		itExchangeSWAddToCart( product, quantity );
	});

	/******************************************************
	 * COUPON CALLS WILL NEED TO MOVE TO COUPON ADDON CODE *
	 ******************************************************/
	// Register Apply Coupon event for Basic Coupons Add-on
	jQuery(document).on('submit', '.it-exchange-super-widget form.it-exchange-sw-update-cart-coupon', function(event) {
	//jQuery(document).on('submit', 'form.it-exchange-sw-update-cart', function(event) {
		event.preventDefault();
		var coupon = jQuery('.apply-coupon', jQuery(this).closest('.it-exchange-super-widget') ).val();
		itExchangeSWApplyCoupon(coupon);
	});

	// Register Remove Coupon event for Basic Coupons Add-on
	jQuery(document).on('click', '.it-exchange-super-widget a.remove-coupon', function(event) {
		event.preventDefault();
		var coupon  = jQuery(this).data('coupon-code');
		itExchangeSWRemoveCoupon(coupon);
	});

	// Register Add / View Coupons Event
	jQuery(document).on('click', '.it-exchange-super-widget a.sw-cart-focus-coupon', function(event) {
		event.preventDefault();
		itExchangeSWViewCart( 'coupon' );
	});

	// Register Edit / View Quantity Event
	jQuery(document).on('click', '.it-exchange-super-widget a.sw-cart-focus-quantity', function(event) {
		event.preventDefault();
		itExchangeSWViewCart( 'quantity' );
	});

	jQuery(document).on('click', '.it-exchange-super-widget a.it-exchange-sw-cancel-login-link', function(event) {
		event.preventDefault();
		if ( itExchangeSWMultiItemCart )
			if ( itExchangeSWOnProductPage )
				itExchangeGetSuperWidgetState( 'product', itExchangeSWOnProductPage );
			else
				itExchangeGetSuperWidgetState( 'cart' );
		else
			itExchangeSWEmptyCart( itExchangeSWOnProductPage );
	});

	// Register the Register Link event (switching to the register state from the login state)
	jQuery(document).on('click', '.it-exchange-super-widget a.it-exchange-sw-register-link', function(event) {
		event.preventDefault();
		itExchangeGetSuperWidgetState( 'registration' );
	});

	jQuery(document).on('click', '.it-exchange-super-widget a.it-exchange-sw-cancel-register-link', function(event) {
		event.preventDefault();
		itExchangeGetSuperWidgetState( 'login' );
	});

	// Register login form submit event
	jQuery(document).on('submit', 'form.it-exchange-sw-log-in', function(event) {
		event.preventDefault();
		data = jQuery( ':input', this ).serializeArray();
		itExchangeSWLogIn(data);
	});

	// Register registration submit event
	jQuery(document).on('submit', 'form.it-exchange-sw-register', function(event) {
		event.preventDefault();
		data = jQuery( ':input', this ).serializeArray();
		itExchangeSWRegister(data);
	});
});

/**
 * Loads a template part for the widget
*/
function itExchangeGetSuperWidgetState( state, product, focus ) {
	var productArg = '';
	var focusArg = '';

	// Set product if needed
	if ( product )
		productArg = '&sw-product=' + product;

	// Set focus if needed
	if ( 'coupon' == focus )
		focusArg = '&ite-sw-cart-focus=coupon';
	if ( 'quantity' == focus )
		focusArg = '&ite-sw-cart-focus=quantity';

	// Make call for new state HTML
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=get-state&state=' + state + productArg + focusArg, function(data) {
		jQuery('.it-exchange-super-widget').html(data);
		itExchangeSWState = state;
	});
}

/**
 * Makes an ajax request to buy a product and cycle through to the checkout
 * We force users to be logged-in before seeing the cart. This is also checked on the AJAX script to prevent URL hacking via direct access.
*/
function itExchangeSWBuyNow( product, quantity ) {
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=buy-now&sw-product=' + product + '&sw-quantity=' + quantity, function(data) {
		if ( itExchangeIsUserLoggedIn )
			itExchangeGetSuperWidgetState( 'checkout' );
		else
			itExchangeGetSuperWidgetState( 'registration' );
	});
}

/**
 * Makes an ajax request to add a product to the cart and cycle through to the checkout
 * We force users to be logged-in before seeing the cart. This is also checked on the AJAX script to prevent URL hacking via direct access.
*/
function itExchangeSWAddToCart( product, quantity ) {
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=add-to-cart&sw-product=' + product + '&sw-quantity=' + quantity, function(data) {
		if ( itExchangeSWMultiItemCart ) {
			itExchangeGetSuperWidgetState( 'cart', product );
		} else {
			if ( itExchangeIsUserLoggedIn )
				itExchangeGetSuperWidgetState( 'checkout' );
			else
				itExchangeGetSuperWidgetState( 'registration' );
		}
	});
}

/******************************************************
 * COUPON CALLS WILL NEED TO MOVE TO COUPON ADDON CODE *
 ******************************************************/
/**
 * Makes AJAX request to Apply a coupon to the cart
*/
function itExchangeSWApplyCoupon(coupon) {
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=apply-coupon&sw-coupon-type=cart&sw-coupon-code=' + coupon, function(data) {
		itExchangeGetSuperWidgetState( 'checkout' );
	});
}
/**
 * Remove a coupon from the cart
*/
function itExchangeSWRemoveCoupon(coupon) {
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=remove-coupon&sw-coupon-type=cart&sw-coupon-code=' + coupon, function(data) {
		itExchangeGetSuperWidgetState( 'checkout' );
	});
}


/**
 * Changes view back to cart with an optional focus on the coupons or the quantity
*/
function itExchangeSWViewCart(focus) {
	itExchangeGetSuperWidgetState( 'cart', false, focus );
}

/**
 * Makes an ajax request to empty the cart
*/
function itExchangeSWEmptyCart( product ) {
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=empty-cart', function(data) {
		if ( itExchangeSWOnProductPage )
			itExchangeGetSuperWidgetState( 'product', itExchangeSWOnProductPage );
		else
			itExchangeGetSuperWidgetState( 'cart' );
	});
}

/**
 * Makes an ajax request to remove an item from the cart
*/
function itExchangeSWRemoveItemFromCart( product ) {
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=remove-from-cart&sw-cart-product=' + product, function(data) {
		if ( itExchangeSWMultiItemCart ) {
			if ( itExchangeSWOnProductPage )
				itExchangeGetSuperWidgetState( 'product', itExchangeSWOnProductPage );
			else
				itExchangeGetSuperWidgetState( 'cart' );
		} else {
			if ( itExchangeIsUserLoggedIn )
				itExchangeGetSuperWidgetState( 'checkout' );
			else
				itExchangeGetSuperWidgetState( 'login' );
		}
	});
}

/**
 * Update Quantity
*/
function itExchangeSWUpdateQuantity(product, quantity) {
	jQuery.get( itExchangeSWAjaxURL+'&sw-action=update-quantity&sw-cart-product=' + product + '&sw-quantity=' + quantity );
}

/**
 * Log the user in
*/
function itExchangeSWLogIn(data) {
	jQuery.post( itExchangeSWAjaxURL+'&sw-action=login', data, function(data) {
		if ( '0' === data ) {
			itExchangeIsUserLoggedIn = '';
			itExchangeGetSuperWidgetState( 'login' );
		} else {
			itExchangeIsUserLoggedIn = '1';
			itExchangeGetSuperWidgetState( 'checkout' );
		}
	});
}

/**
 * Register a new users
*/
function itExchangeSWRegister(data) {
	jQuery.post( itExchangeSWAjaxURL+'&sw-action=register', data, function(data) {
		if ( '0' === data )
			itExchangeGetSuperWidgetState( 'registration' );
		else
			itExchangeGetSuperWidgetState( 'checkout' );
	});
}
