var products = {

	current_tab: 'overview',
	current_step: 1,
	current_gallery: 1,
	current_pdf: 1,
	current_cad: 1,
	maxtravel: 0,
	
	init: function() {
		
		//work out current tab
		[ 'overview', 'gallery', 'specifications', 'options', 'pdf', 'cad' ].each( function( s ) {
			if($('tab_' + s) && $('tab_' + s).hasClassName('selected') ) products.current_tab = s;
		});
		
		//work out maximum travel
		this.maxtravel = $F('maxtravel');
		
		//zoom slider
		zoomer_slider = new Control.Slider( 'slider_handle', 'slider_track', {
			axis: 'vertical',
			range: $R( 0, this.maxtravel ),
			increment: 5,
			sliderValue: this.maxtravel - ( $F('travel') * 100 ),
			onSlide: update_slider,
			onChange: update_slider
		} );
		
		//function to update the slider and all associated labels
		function update_slider( val ) {
			var travel = Math.ceil( products.maxtravel - val ) / 100;
			if( String( travel ).length == 1 ) travel = travel + '.00';
			while( String( travel ).length < 4 ) travel = travel + '0';
			$('slider_value').update( travel );
			$('travel_label').update( travel );
			$('travel').value = travel;
		}
		
		//hide slider properly now we have initialised it
		$('step_3').hide().setStyle( { visibility: 'visible' } );
		
		//add event handlers to radio box containers
		$$('input.radio').each( function( elm ) {
			
			elm.up().observe( 'click', select_this ); //label
			elm.up().previous().observe( 'click', select_this ); //swatch
			elm.observe( 'focus', select_this ); //swatch
			elm.observe( 'click', select_this ); //swatch
					   
			function select_this(e) {
				
				e.stop();
				
				elm.checked = true;
				
				//update value and label
				if( elm.name == 'door_colour' ) {
					$('door_colour').value = $F(elm);
					$('door_colour_label').update( $F(elm) );
				}
				
				else if( elm.name == 'floor_colour' ) {
					$('floor_colour').value = $F(elm);
					$('floor_colour_label').update( $F(elm) );
				}
				
			}
		
		});
		
		//set initial labels
		$('door_colour_label').update( $F('door_colour') );
		$('floor_colour_label').update( $F('floor_colour') );
		$('travel_label').update( $F('travel') );
		$('slider_value').update( $F('travel') );
		
	},
	
	show: function( tab ) {

		if( this.current_tab == tab ) return;

		$( 'tab_' + this.current_tab ).removeClassName('selected');
		$( 'tab_' + tab ).addClassName('selected');

		$( 'product_' + this.current_tab ).hide();
		$( 'product_' + tab ).show();
		
		//if gallery, change the image too
		if( tab == 'gallery' ) {
			$('overview_img').hide();	
			$('gallery_img').show();	
		} else {
			$('overview_img').show();	
			$('gallery_img').hide();	
		}
		
		this.current_tab = tab;

	},
	
	gallery_show: function( src ) {
		
		$('gallery_img').src = src;
		
	},
	
	gallery_switch: function( i ) {

		if( this.current_gallery == i ) return;

		$( 'gallery_' + this.current_gallery ).hide();
		$( 'gallery_' + i ).show();

		$('gallery_link_' + this.current_gallery).removeClassName('bold').addClassName('light_grey').removeClassName('grey');
		$('gallery_link_' + i).addClassName('bold').removeClassName('light_grey').addClassName('grey');
		
		this.current_gallery = i;
		
	},
	
	pdf_switch: function( i ) {

		if( this.current_pdf == i ) return;

		$( 'pdf_' + this.current_pdf ).hide();
		$( 'pdf_' + i ).show();

		$('pdf_link_' + this.current_pdf).removeClassName('bold').addClassName('light_grey').removeClassName('grey');
		$('pdf_link_' + i).addClassName('bold').removeClassName('light_grey').addClassName('grey');
		
		this.current_pdf = i;
		
	},
	
	cad_switch: function( i ) {

		if( this.current_cad == i ) return;

		$( 'cad_' + this.current_cad ).hide();
		$( 'cad_' + i ).show();

		$('cad_link_' + this.current_cad).removeClassName('bold').addClassName('light_grey').removeClassName('grey');
		$('cad_link_' + i).addClassName('bold').removeClassName('light_grey').addClassName('grey');
		
		this.current_cad = i;
		
	},
	
	step: function( step ) {

		if( this.current_step == step ) return;
		
		if( step == 2 && $F('door_colour') == 'Not chosen' ) { alert('Please choose a door colour'); return; }
		if( step == 3 && $F('floor_colour') == 'Not chosen' ) { alert('Please choose a floor style'); return; }
		if( step == 4 && $F('travel') == 0 ) { alert('Please choose an approximate travel amount'); return; }

		$( 'step_' + this.current_step ).hide();
		$( 'step_' + step ).show();
		
		this.current_step = step;
		
	
	},
	
	send: function() {

		if( this.check() ) {
			
			//define strings
			var b =
				'name=' + $F('name') + '&company=' + $F('company') + '&email=' + $F('email') + '&telephone=' + $F('telephone') + 
				'&lift=' + $F('lift') + '&door_colour=' + $F('door_colour') + '&floor_colour=' + $F('floor_colour') + '&travel=' + $F('travel');
			var c = '<p class="blue">Thank you</p><p>Your enquiry has been sent, we will be in touch shortly.</p>'
			var e = '<p class="blue">Sorry</p><p>We are experiencing technical difficulties, please contact us on 0116 291 6000</p>'

			//call script to send form
			new Ajax.Request( 'send-enquiry', {
				method: 'post',
				postBody: b,
				onComplete: function( t ) {
					$('details_form').update( t.responseText == 'OK' ? c : e );
					$('get_quote_btn').hide();
					$('final_back_btn').setStyle( { right: '10px' } );
				}
			});
			
		}
	
	},
	
	check: function() {
		
		if( $F('name').strip() == '' ) this.error( 'Please enter your name' );
		else if( !$F('name').match( regexp.name ) ) this.error( 'Please enter a valid name' );
		else if( $F('email').strip() == '' ) this.error( 'Please enter your email address' );
		else if( !$F('email').match( regexp.email ) ) this.error( 'Please enter a valid email address' );
		else if( $F('telephone').strip() == '' ) this.error( 'Please enter your telephone number' );
		else if( !$F('telephone').match( regexp.telephone ) ) this.error( 'Please enter a valid telephone number' );
		else return true;
		
		return false;
		
	},
	
	register: function() {

		if( this.check_register() ) {
			
			//define strings
			var b = 'name=' + $F('reg_name') + '&company=' + $F('reg_company') + '&email=' + $F('reg_email') + '&telephone=' + $F('reg_telephone');
			var c = '<br /><p class="blue">Thank you</p><p>Your registration request has been sent, we will be in touch shortly.</p>'
			var e = '<br /><p class="blue">Sorry</p><p>We are experiencing technical difficulties, please contact us on 0116 291 6000</p>'

			//call script to send form
			new Ajax.Request( 'register', {
				method: 'post',
				postBody: b,
				onComplete: function( t ) {
					$('register_forms').update( t.responseText == 'OK' ? c : e );
				}
			});
			
		}
	
	},
	
	check_register: function() {
		
		if( $F('reg_name').strip() == '' ) this.error( 'Please enter your name' );
		else if( !$F('reg_name').match( regexp.name ) ) this.error( 'Please enter a valid name' );
		else if( $F('reg_email').strip() == '' ) this.error( 'Please enter your email address' );
		else if( !$F('reg_email').match( regexp.email ) ) this.error( 'Please enter a valid email address' );
		else if( $F('reg_telephone').strip() == '' ) this.error( 'Please enter your telephone number' );
		else if( !$F('reg_telephone').match( regexp.telephone ) ) this.error( 'Please enter a valid telephone number' );
		else return true;
		
		return false;
		
	},
	
	error: function( msg ) {
	
		alert( msg );
	
	}
}

Event.observe( document, 'dom:loaded', function() { products.init(); });