var rotate = 0;

function _rotate() {
	
	if( ( images_.length - 1 ) == rotate )
		rotate = -1;

	$( '.top img' ).attr( 'src', '/images/phpThumb.php?src=' + images_[++rotate] + '&w=435' );
}

function initHomeRatation() {
	
	setInterval( '_rotate()', 5000 );
}

$( function() {
	
	$( '#checkout-confirmation .tocheckout' ).click( function() {
		
		$( '#epdq-form' ).submit();
		return false;
	});
	
	$( '#how-did-you-hear-about-us' ).change( function() {
		
		if( this.value == 7 ) {

			$( '#hear-about-us-other' ).show();
		} else {

			$( '#hear-about-us-other' ).hide();
		}
	});

	$( '#product-details .table li a' ).click( function() {
		
		$( this ).parents( '.table' ).find( 'li' ).each( function() {

			$( this ).removeClass( 'active' );
		});
		
		$cls = $( this ).parents( 'li' );
		$cls.addClass( 'active' );

		$( '#product-details .table .text' ).html( $( '#pd-' + $cls.attr( 'id' ) ).html() );
		
		return false;
	});

	$( '#basket .table .remove a' ).click( function() {

		if( confirm( 'Are you sure want to remove this item?' ) ) {
			
			var product_id	= $( this ).parents( '.row' ).find( 'input[name="product_id[]"]' ).val();
			var size_id		= $( this ).parents( '.row' ).find( 'input[name="size_id[]"]' ).val();
			
			$row = $( this ).parents( '.row' );
			$.post( '/basket', { action : 'remove_from_cart', product_id : product_id, size_id : size_id }, function( r ) {
				
				eval( 'var response = ' + r );
				if( response.status == 'success' ) {
					
					$( '#basket .table .cart-weight span' ).html( response.weight );
					$( '#basket .table .total span' ).html( response.subtotal );
					$( '#basket .totals .subtotal .value span' ).html( response.subtotal );
					$( '#basket .totals .delivery .value span' ).html( response.delivery );

					if( parseFloat( response.discount ) > 0 ) {

						$( '#basket .totals .discount' ).show();
						$( '#basket .totals .discount .value span' ).html( response.discount );
					} else {

						$( '#basket .totals .discount' ).hide();
					}
					$( '#basket .totals .vat .value span' ).html( response.vat );
					$( '#basket .totals .total .value span' ).html( response.total );

					$row.hide( 'slow' );

					// Change basket items
					var products_count = parseInt( response.products_count );
					$( '.basket_items' ).html( products_count == 0 ? 'empty' : products_count + ' item' + ( products_count > 1 ? 's' : '' ) );

				}
			});
		}

		return false;
	});

	$( '#billing-copy' ).click( function() {
		
		if( this.checked == true ) {

			$( 'fieldset:eq(1) input' ).each( function() {
				
				var input_id = $( this ).attr( 'id' );
				input_id = input_id.replace( /billing/g, 'shipping' );
				$( '#' + input_id ).val( this.value );
			});
		}
	});
});