$( function() {
	$( '#noScript' ).hide();//hide the no javascript warning
	$( '#shoeSelectorSubmitter' ).show();//show the entire builder
	
	$( '#shoeSelectorSubmitter' ).submit( function() {
		var runningTotal = 0;
		$( '#shoeSelectorSubmitter select' ).each( function() {
			runningTotal += parseInt( $( this ).val() );
		} );
		
		if ( runningTotal == 0 ) {
			alert( 'You must select a shoe box quantity before you can add to your basket.' );
			return false;
		}
	} );
	
	$( '#shoeSelectorSubmitter select' ).change( function() {
		//get price
		var selectName = $( this ).attr( 'name' );//shoeBoxSelector[UPID]
	
		Finder = new RegExp( "[^0-9]+", "g" );
		UPID = selectName.replace( Finder, '' );
		
		var qty = parseInt( $( this ).val() );
	
		if ( qty > 0 ) {
			$.ajax( {
				url: 'http://www.caraselledirect.com/structural/GLOBAL/ajax.priceFinder.php?productCode=' + UPID + '-' + qty,
				cache: false,
				success: function( data ) {
					$( '#price_' + UPID ).html( data );
					
					switch( UPID ) {
						case '271':
						case '315':
						case '1059':
						case '261':
						case '1438':
						case '1439':
						case '1560':
						case '1440':
						case '1441':
							$( '#fancyBground' ).css( 'background', '#FFF url(/shoebox_selector/' + UPID + '.jpg) center top no-repeat' );
							break;
						default:
							$( '#fancyBground' ).css( 'background', '#FFF url(/shoebox_selector/boxes_layout.jpg) center top no-repeat' );
							break;
					}
				},
				beforeSend: function(){
					$( '#price_' + UPID ).html( '<span class="priceLoading">Finding best price...</span>' );
				}
			} );
		} else {
			$( '#price_' + UPID ).html( '' );
		}
	} );
	
	//detect current page
	if ( $( '#viewingUPID' ).length ) {
		$( "#shoeBoxSelector_" + $( '#viewingUPID' ).val() + ":selected" ).removeAttr( 'selected' );
		$( "#shoeBoxSelector_" + $( '#viewingUPID' ).val() + " option[value='1']" ).attr( 'selected', true ).change();
	}
} );
