//reg exps
var regexp = {
	name: new RegExp( /^[a-zA-Z \'\-]+$/ ),
	email: new RegExp( /^[^@]+@[^@]+\.[a-zA-Z]{2,10}$/ ),
	telephone: new RegExp( /^[0-9 \(\)\+\-]+$/ )
}


//object to handle the quotation form
var qform = {

	//shows the form
	show : function() { $('quote_form').show() },
	
	//checks the form for errors
	check : function() {
	
		//check everything
		if( $('name').length == 0 ) qform.show_error( "Please enter your name" );
		else if( !$F('name').match( regexp.name ) ) qform.show_error( "Please enter a valid name (containing no numbers or other characters)" );
		else if( $F('email').length > 0 && !$F('email').match( regexp.email ) ) qform.show_error( "Please enter a valid email address, i.e. yourname@company.co.uk" );
		else if( $F('telephone').length == 0 ) qform.show_error( "Please enter your telephone number" );
		else if( !$F('telephone').match( regexp.telephone ) ) qform.show_error( "Please enter a valid telephone number" );
		else if( $F('message').length == 0 ) qform.show_error( "Please enter a message" );
		else qform.send();
	
	},

	//shows an error
	show_error : function( str ) {
	
		var d = new Element( 'div', { id: 'quote_container' } );
		$('quote_form').insert( { 'top' : d } );
		d.update( '<p><strong>' + str + '</strong></p><p class="grey">Click <a href="javascript:qform.hide_error();" class="grey">here</a> to go back and try again.</p>' );
		
	},

	//hides the error
	hide_error : function() {
	
		$('quote_form').removeChild( $('quote_container') );
	
	},
	
	//submits the form via AJAX
	send : function() {
	
		//create container and show loading message
		var d = new Element( 'div', { id: 'quote_container' } );
		$('quote_form').insert( { 'top' : d } );
		d.update( '<p><strong>Please wait...</strong></p><p class="grey">We are sending your enquiry</p>' );

		//send request via AJAX
		new Ajax.Request( 'quote_handler.php', {
			method: 'post',
			postBody: 'name=' + escape( $('qform').name.value ) + '&email=' +  escape( $('qform').email.value ) + '&telephone=' +  escape( $('qform').telephone.value ) + '&message=' + escape(  $('qform').message.value ),
			onSuccess: function( t ) {
				
				if( t.responseText == 'OK' ) {
					
					//display a message saying it's been sent
					$('quote_container').update( '<p><strong>Thank you</strong></p><p class="grey">Your enquiry has been sent.</p>' );
					
				} else {

					//display a message saying it can't be sent
					$('quote_container').update (
						'<p><strong>Sorry</strong></p>' + 
						'<p class="grey">Your enquiry could not be sent then, please click ' + 
						'<a href="javascript:qform.hide_error();" class="grey">here</a> to go back and try again.</p>' );

				}
			
			}
		
		});
	
	}

}

//object to handle the picture fading
var slideshow = {

	speed: 1,
	duration: 2,
	//speed: 0.5,
	//duration: 1,
	path: 'images/photos/', //path to photos
	photos: new Array(),
	current: 1,
	total: 0,
	order: [],
	
	init: function() {

		//total images in the slideshow
		slideshow.total = slideshow.order.length;

		//preload images and set photo order
		for( var i = 0; i < slideshow.total; i++ ) {
			slideshow.photos[i] = new Image();
			slideshow.photos[i].src = slideshow.path + slideshow.order[i] + '.jpg';
		}

		//initialise rotation
		setTimeout( function() { slideshow.advance() }, slideshow.duration * 1000 );

		//set handler
		Event.observe( $('photo2'), 'load', function() { slideshow.animate() } );
		
	},

	//work out next image to show
	advance: function() {

		//set the src of photo 2 and update current pointer
		$('photo2').src = slideshow.photos[ slideshow.current ].src;
		slideshow.current = slideshow.current == slideshow.total - 1 ? 0 : slideshow.current + 1

	},
	
	//when it's loaded...
	animate: function() {

		//show it and set the opacity to 0
		$('photo2').show();
		$('photo2').setStyle( { opacity: 0 } );
		
		//fade the photos into each other
		new Effect.Opacity( 'photo1', { from: 1, to: 0, duration: slideshow.speed } );
		new Effect.Opacity( 'photo2', { from: 0, to: 1, duration: slideshow.speed } );
		
		//recurse
		setTimeout( function() {
		
			//swap the src of the 2 and hide photo 2
			$('photo1').src = $('photo2').src;
			$('photo1').setStyle( { opacity: 1 } );
			$('photo2').setStyle( { opacity: 0 } );
			$('photo2').hide();
		
		}, ( slideshow.speed * 1000 ) + 100 );
		
		//recurse
		setTimeout( function() {
		
			slideshow.advance();
		
		}, ( ( slideshow.speed + slideshow.duration ) * 1000 ) + 100 );
	
	}
	
}


//handles heading collapsing and IE 6 PNG alpha fixes
var headings = {
	
	init: function() {
	
		//work out selected item - if any
		var id = location.href.split( '-' );
		id = id.length == 3 ? id[2] : 0;

		//get list of headings, add handlers to them all to show/hide the text below them
		var hc = 1;
		$$('#copy h3').each( function( h ) {

			var c = h.nextSiblings();

			//get elements between this heading and the next, and wrap this with a div
			var i = 0;
			var wrapper = new Element( 'div' );
			wrapper.className = 'wrapper';
			var cont = true;
			c.each( function( e ) {
			
				if( cont == false || e.tagName.toUpperCase() == 'H3' ) { cont = false; return; }
				
				wrapper.insert( { 'bottom': e.remove() } );

			});

			h.insert( { 'after': wrapper } );
			//if( hc > 1 ) wrapper.hide(); //show initial items
			if( hc != id ) wrapper.hide(); //show selected
			//wrapper.hide(); //hide all

			//add hander and style
			Event.observe( h, 'click', function(e) {

				//hide all the wrapping divs apart from the one we want to show
				$$('div.wrapper').each( function( elm ) {
	
					if( elm == wrapper ) { 
						if( elm.visible() ) elm.hide();
						else elm.show();
					} else elm.hide();

				});
												
				e.stop();

			});
			
			//set style
			h.setStyle( { cursor: 'pointer' } );
			
			//fix png bug in ie6
			if( h.getStyle( 'filter' ) != undefined && is_ie ) {
			
				var bg = h.getStyle( 'backgroundImage' ).replace( /^url\((.+)\)$/, '$1' );
				var d = new Element( 'div' );
				h.setStyle( { background: 'none' } );
				h.insert( { 'top' : d } );
				d.setStyle( { height: '19px', width: '400px', filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + bg + '\')' } );
			
			}
			
			hc++;
			
		});
		
	}
	
}

Event.observe( window, 'load', function() {
	if( slideshow.order.length > 0 ) slideshow.init();
	headings.init();
});