var BOTM = new Class({
		
	currentTab : null,
	parts : null,
	basket : {},
	checkingVin : false,
	sendingEmail : false,
	bike : null,
	bike2 : null,
	bike3 : null,
	bike4 : null,
	bike5 : null,
	bike6a : null,
	bike6b : null,
	range : null,
	
	initialize : function()
	{
		
		$$( '.botm_tab' ).each(function( item, index )
		{
			if( !$( item.get( 'id' ).replace( 'tab_', '' ) ) )
			{
				item.dispose();
			}
		});
		
		var pid = null;
		
		$$( '.botm_tab' ).each(function( item, index )
		{
			var id = item.get( 'id' ).replace( 'tab_', '' );
			item.tableContainer = $( id );
			if( !pid )
			{
				pid = id;
			}
			item.addEvent( 'click', function()
			{
				this.selectTab( id );
			}.bind( this ))
		}.bind( this ));
		
		this.selectTab( pid );
		
		$$( '.botm_add' ).each(function( item, index )
		{
			item.addEvent( 'click', function()
			{
				this.addToBasket( item.get( 'id' ) );
				return false;
			}.bind( this ))
		}.bind( this ))
				
		$( 'botm_vin_submit' ).addEvent( 'click', this.checkVin.bind( this ) );
		$( 'botm_vin_input' ).addEvent( 'keyup', function( e )
		{
			if( e.code == '13' )
			{
				this.checkVin();
				return false;
			}
		}.bind( this ));
		
		$( 'botm_email_submit' ).addEvent( 'click', this.sendEmail.bind( this ) );
		$( 'botm_email_input' ).addEvent( 'keyup', function( e )
		{
			if( e.code == '13' )
			{
				this.sendEmail();
				return false;
			}
		}.bind( this ));
		
		$( 'botm_voucher_generate' ).addEvent( 'click', this.generateVoucher.bind( this ) );
        $( 'botm_shopatron' ).addEvent( 'click', this.generateShopatron.bind( this ) );

        try {
		    $( 'botm_toggle' ).addEvent( 'click', this.toggleInfo.bind( this ) );
        } catch( e ){};
			
	},
	
	toggleInfo : function()
	{
		if( !this.infoShowing )
		{
			this.infoShowing = true;
			$( 'botm_info' ).setStyle( 'display', 'block' );
			$( 'botm_items' ).setStyle( 'display', 'none' );
			$( 'botm_toggle' ).set( 'html', 'For list of items, click here' );
		} else {
			this.infoShowing = false;
			$( 'botm_info' ).setStyle( 'display', 'none' );
			$( 'botm_items' ).setStyle( 'display', 'block' );
			$( 'botm_toggle' ).set( 'html', 'For specs and reviews, click here' );
		}
		return false;
	},
	
	currentBasket : function( ola )
	{
		for( var a in ola )
		{
			if( this.parts[a] )
			{
				var p = this.parts[a];
				var item = p;
				this.basket[item.id] = {
					qnty : parseInt( ola[a] ),
					price : parseFloat( item.pp ),
					retail : parseFloat( item.rp ),
					description : item.name,
					'id' : a					 
				}
			}
		}
		this.redisplayBasket();
	},
	
	addToBasket : function( info )
	{
		var item = this.parts[info];
		if( !this.basket[item.id] )
		{
			this.basket[item.id] = {
				qnty : 0,
				price : item.pp,
				retail : item.rp,
				description : item.name,
				'id' : item.id
			};
		}
		this.basket[item.id].qnty++;
		this.redisplayBasket();
	},
	
	redisplayBasket : function()
	{
		var pprice = 0;
		var rprice = 0;
		var qnty = 0;
		var b = '<ul>';
		for( var i in this.basket )
		{
			var item = this.basket[i];
			if( item.qnty )
			{
				qnty += item.qnty;
				rprice += ( item.qnty * item.retail );
				pprice += ( item.qnty * item.price );
				b += '<li><span class="botm_remove" rel="' + item.id + '"><span>x</span></span><b>' + item.qnty + '</b> x ' + item.description + '</li>';
			}
		};
		b += '</ul>';
		rprice = this.roundTo( rprice, 2 );
		pprice = this.roundTo( pprice, 2 );
		if( qnty > 0 )
		{
			$( 'botm_basket' ).set( 'html', 'You have ' + qnty + ' item' + ( qnty > 1 ? 's' : '' ) + '<br />' + b + '<div class="botm_prices"><b>RRP: £' + rprice.toFixed( 2 ) + '</b><br /><b>YOU PAY: £' + pprice.toFixed( 2 ) + '<br /><b>YOU SAVE: £' + this.roundTo( rprice - pprice, 2 ).toFixed( 2 ) + '</b></div>' );
			$$( '.botm_remove' ).each( function( item, index )
			{
				item.addEvent( 'click', function()
				{
					this.basket[item.get( 'rel' )].qnty--;
					this.redisplayBasket();
					return false;
				}.bind( this ))
	
			}.bind( this ));
			$( 'botm_voucher_generate' ).setStyle( 'display', 'block' );
           $( 'botm_shopatron' ).setStyle( 'display', 'block' );
		} else {
			$( 'botm_basket' ).set( 'html', '<div class="botm_prices">Your basket is currently empty.</div>' );
			$( 'botm_voucher_generate' ).setStyle( 'display', 'none' );
            $( 'botm_shopatron' ).setStyle( 'display', 'none' );
		}
		
	},
	
	selectTab : function( id )
	{
		if( this.currentTab )
		{
			this.currentTab.removeClass( 'botm_tab_selected' );
			this.currentTab.tableContainer.setStyle( 'display', 'none' );
		}
		var tab = $( 'tab_' + id );
		if (tab != undefined) {
			tab.addClass( 'botm_tab_selected' );
			tab.tableContainer.setStyle( 'display', 'block' );					
			this.currentTab = tab;	
		}				
	},
	
	roundTo : function( no, n )
	{
		return Math.round( no * Math.pow( 10, n ) ) / Math.pow( 10, n );
	},
	
	sendEmail : function()
	{
		if( !$( 'botm_email_input' ).get( 'value' ) || $( 'botm_email_input' ).get( 'value' ).indexOf( '@' ) == -1 )
		{
			$( 'botm_email_input' ).focus();
			return;
		}
		if( !this.sendingEmail )
		{
			$( 'botm_email_submit' ).set( 'value', 'Sending...' );
			this.sendingEmail = true;
			new Request.HTML({
				url : '/botm/email_friend.php',
				onSuccess : function( a, b, c, d )
				{
					this.sendingEmail = false;
					if( c == 'OK' )
					{
						this.didSendEmail();
					}
				}.bind( this )
			}).post({
				email : $( 'botm_email_input' ).get( 'value' )
			});
		}
	},
	
	didSendEmail : function()
	{
		$( 'botm_email_box' ).setStyle( 'display', 'block' );
		$( 'botm_email_submit' ).set( 'value', 'Send Email' )
	},
	
	checkVin : function()
	{
		if( !$( 'botm_vin_input' ).get( 'value' ).replace( ' ', '' ).length )
		{
			$( 'botm_vin_input' ).focus();
			return false;
		}
		if( !this.checkingVin )
		{
			$( 'botm_vin_submit' ).set( 'value', 'Validating...' );
			this.checkingVin = true;
			if( ! this.reg )
			{
				//console.log(this);
				new Request.HTML({
					url : '/botm/vin_check.php',
					onSuccess : function( a, b, c, d )
					{
						//console.log( c );
						if( c != 'NO' )
						{
							this.vinChecked( true, JSON.decode( c ) );
						} else {
							this.vinChecked( false );
						}
						this.checkingVin = false;
					}.bind( this )
				}).post({
					bike : this.bike,
					bike2 : this.bike2,
					bike3 : this.bike3,
					bike4 : this.bike4,
					bike5 : this.bike5,
					bike6a : this.bike6a,
					bike6b : this.bike6b,
					bike7 : this.bike7,
					bike8 : this.bike8,
					vin : $( 'botm_vin_input' ).get( 'value' ),
					range : this.range
				});
			} else {
				new Request.HTML({
					url : '/botm/vin_check_reg.php',
					onSuccess : function( a, b, c, d )
					{
						//console.log( c );
						if( c != 'NO' )
						{
							this.vinChecked( true, JSON.decode( c ) );
						} else {
							this.vinChecked( false );
						}
						this.checkingVin = false;
					}.bind( this )
				}).post({
					vin_number : $( 'botm_vin_input' ).get( 'value' ),
					expression : this.reg
				});
			}
		}
	},
	
	vinChecked : function( flag, args )
	{
     //   console.log(args);
		$( 'botm_vin_submit' ).set( 'value', 'Check' );
		if( flag )
		{
			$( 'botm_model' ).set( 'html', args.bike );
			$( 'botm_colour' ).set( 'html', args.colour );
			$( 'botm_vin_box' ).removeClass( 'botm_light_grey' );
			$( 'botm_vin_box' ).removeClass( 'botm_light_red' );
			$( 'botm_vin_box' ).addClass( 'botm_green' );
			$( 'botm_vin_valid_box' ).setStyle( 'display', 'block' );
			$( 'botm_vin_invalid_box' ).setStyle( 'display', 'none' );
		} else {
			$( 'botm_vin_box' ).removeClass( 'botm_green' );
			$( 'botm_vin_box' ).removeClass( 'botm_light_grey' );
			$( 'botm_vin_box' ).addClass( 'botm_light_red' );
			$( 'botm_vin_valid_box' ).setStyle( 'display', 'none' );
			$( 'botm_vin_invalid_box' ).setStyle( 'display', 'block' );
		}
	},
	
	generateVoucher : function()
	{
		if( !this.isGenerating )
		{
			$( 'botm_voucher_generate' ).set( 'value', 'Generating...' )
			this.isGenerating = true;
			var b = {};
			for( var a in this.basket )
			{
				var item = this.basket[a];
				if( item.qnty )
				{
					b[item.id] = item.qnty;
				}
			}
			new Request.HTML({
				url : '/botm/gen_voucher.php',
				onSuccess : function( a, b, c, d )
				{
					document.location = '/bikeofthemonth/voucher_' + this.month + '.php';
				}.bind( this )
			}).post( b );
		}
	},
	generateShopatron : function()
    {
      if( !this.isGenerating )
		{
			$( 'botm_shopatron' ).set( 'value', 'Processing...' )
			this.isGenerating = true;
			var b = {};
			for( var a in this.basket )
			{
				var item = this.basket[a];
				if( item.qnty )
				{
					b[item.id] = item.qnty;
				}
			}
			new Request.HTML({
				url : '/botm/gen_shopatron.php',
				onSuccess : function( a, b, c, d )
				{
					document.location = '/bikeofthemonth/checkout/';
				}.bind( this )
			}).post( b );
		}
    }
})


