//define loading and resize actions
Event.observe( document, 'dom:loaded', function() {
	layout.init();
});
Event.observe( window, 'resize', function() {
	layout.centre_page();
	layout.place_footer();	
});


//define regular expressions
var regexp = {

	name: new RegExp( /^[a-zA-Z\'\- ]+$/ ),
	email: new RegExp( /^[^@]+@[^@]+\.[a-zA-Z]{2,10}$/ ),
	telephone: new RegExp( /^[0-9 \(\)\-\+]+$/ )

}

//layout functions
var layout = {
	
	pmd: 1000, //product menu delay

	init: function() {
		
		//centre the page and place the footer
		layout.centre_page();
		layout.place_footer();
		
		//set up handlers to prolong showing of the product menu
		$('product_menu').observe( 'mouseover', function(e) { e.stop(); clearTimeout( layout.pmto ); });
		$$('#menu_our-products span').each( function(elm) {
			elm.observe( 'mouseover', function(e) { clearTimeout( layout.pmto ); })
		});
		
		//set up handler to show product menu
		$('menu_our-products').observe( 'click', function(e) {
			e.stop();
			clearTimeout( layout.pmto );
			layout.show_product_menu();
		});
		
		//set up handler to hide product after X seconds
		$('body').observe( 'mouseover', function(e) {
			e.stop();
			clearTimeout( layout.pmto );
			layout.pmto = setTimeout( function() { layout.hide_product_menu() }, layout.pmd );
		});
		
		//set up handler to cancel product menu
		$('body').observe( 'click', function(e) { layout.hide_product_menu() });
		
		//move the product menu to the correct position
		$('product_menu').setStyle( { left: ( $('menu').positionedOffset().left + $('menu_our-products').positionedOffset().left - 12 ) + 'px' } );

	},
	
	show_product_menu: function() {
		
		$('menu_our-products').addClassName('hover');
		
		//ie cant handle transparency properly
		if( is_ie ) {
			$('product_menu').show();
		} else {
			$('product_menu').show().setStyle( { opacity: 0 } );
			new Effect.Opacity( 'product_menu', { from: 0, to: 1, duration: 0.3 } )
		}
		
	},
	
	hide_product_menu: function() {
		
		$('menu_our-products').removeClassName('hover');
		$('product_menu').hide();
		
	},

	place_footer: function() {
			
		//get the height of the page, header, content and footer
		var ph = $('page').getHeight();
		var hh = $('header').getHeight();
		var ch = $('content').getHeight();
		var fh = $('footer').getHeight();
		
		if( ph - fh < hh + ch ) return;
	
		$('footer').setStyle( { position: 'absolute', top: ( ph - fh ) + 'px' } );
	
	},
	
	centre_page: function() {

		//if not on the homepage, return
		//if( !location.href.match( /\/$/ ) && !location.href.match( /welcome$/ ) ) return;
			
		//get height of the page and content
		var ph = $('page').getHeight();
		var ch = $('content').getHeight();
		
		//work out header height, ensuring a minium and max
		var hh = Math.ceil( ( ph - ch ) / 2 );
		var mimh = 150;
		var maxh = 381;
		if( hh < mimh ) hh = mimh;
		if( hh > maxh ) hh = maxh;
		
		$('header').setStyle( { height: hh + 'px' } );
	
	}
	
}