var goAccordian = function() {
	if (!sidebarid) {
		sidebarid = 2;
	}
	var documentsAccordion = new Accordion($$('h3.toggler'), $$('.toggled'), {
		show : sidebarid
	});
}

var doAjax = function() {

	var searchFormActive = function() {
		if (this.value.length > 2) {
			var myHTMLRequest = new Request.HTML({url:'/form_search', update: $('search_results'), onComplete: function(){
				$('search_results').addClass('results');
				theHeight = 0;
				$('search_results').getChildren().each(function(el){
					theHeight = el.getSize().y + theHeight;
				});
				$('search_results').morph({top : [70, 90], right : [16, 0], width : [132, 400], height : [0, theHeight], borderColor : '#333333', backgroundColor : '#FFFFFF'});	
			} }).get({'search_value' : this.value});		
		} else {
			$('search_results').morph({top : 70, right : 16, width : 132, height : 0, borderColor : '#286383', backgroundColor : '#286383'});
			if ($('search_results').getFirst()) {
				$('search_results').getFirst().fade('out');
			}		
		}	
	}
	
	var searchFormFocus = function() {
		if (this.value == 'Search...') {
			this.value = '';
		}
	}
	
	var searchFormBlur = function() {
		if (this.value == '') {
			this.value = 'Search...';
		}
		$('search_results').morph({top : 70, right : 16, width : 132, height : 0, borderColor : '#286383', backgroundColor : '#286383'});
		$('search_results').getFirst().fade('out');	
	}	
	
	$('search_value').addEvent('keyup', searchFormActive);
	$('search_value').addEvent('focus', searchFormFocus);
	$('search_value').addEvent('blur', searchFormBlur);
	
	var enquiryForm = function(event) {
		event.stop();
		var myHTMLRequest = new Request.HTML({url:'/form_enquiry', update: $('form_enquiry'), onComplete: function(){
			getEnquiryForm();	
		} }).post($('form_enquiry').getFirst('form'));
	};
	
	var getEnquiryForm = function() {
		$('form_enquiry').fade('hide');
		if ($('form_enquiry').getFirst('form')) {
			$('form_enquiry').getFirst('form').addEvent('submit', enquiryForm);
			$('form_enquiry').morph({height: $('form_enquiry').getFirst('form').getSize().y, opacity: 100});
		} else {
			theHeight = 0;
			$('form_enquiry').getChildren().each(function(el){
				theHeight = el.getSize().y + theHeight;
			});
			$('form_enquiry').morph({height: theHeight, opacity: 100});
		}		
	}
	
	var initialEnquiryForm = function(event) {
		event.stop();
		$('form_enquiry').setStyle('height', $('form_enquiry').getSize().y);
		var myHTMLRequest = new Request.HTML({update: $('form_enquiry'), onComplete: function(){
			getEnquiryForm();	
		} }).get('/form_enquiry');
	};
	
	
	var quoteForm = function(event) {
		event.stop();
		
		var myFx = new Fx.Scroll(window).toTop().chain(function() {
		
			$('loading-icon').setStyles({'display' : 'block', 'top': $(window).getScroll().y+300});
		
			var myHTMLRequest = new Request.HTML({url:'/form_quote', update: $('ajax_content'), onComplete: function(){
				$('ajax_content').setStyles({'opacity' : 0, 'top': $(window).getScroll().y+20});
				doQuoteForm();
			} }).post($('ajax_content').getFirst('form'));
		});
	}
	
	var doQuoteForm = function() {
		
			
			
		
			if ($('popoutmessage')) {
				$('popoutmessage').setStyle('opacity', 0);
			}
			
			if (!$('theiframe')) {
				new Element('iframe', {
						'id': 'theiframe',
						'styles': {
							'width': 600,
							'height':600,
							'top': $(window).getScroll().y+20,
							'position': 'absolute',
							'left': '50%',
							'margin-left':-300,
							'z-index': 99,
							'opacity' : 0,
							'visibility': 'visible'
						}
					}).inject(document.body);
			}
		
			$('ajax_background').removeClass('notdisplayed');
			$('ajax_content').removeClass('notdisplayed');
		

			if ($('ajax_content').getFirst('form')) {
				//$('ajax_content').getFirst('form').addEvent('submit', quoteForm);
				//$('ajax_content').morph({height: $('ajax_content').getFirst('form').getSize().y, opacity: 100});
				theHeight = 0;
				$('ajax_content').getChildren().each(function(el){
					theHeight = el.getSize().y + theHeight;
				});
				var myFx = new Fx.Tween('ajax_background');
				myFx.start('opacity',0.8).chain(
					function(){
						var myFx1 = new Fx.Morph('ajax_content');
						myFx1.start({'opacity':1, 'height':theHeight}).chain(
							function(){
								if ($('popoutmessage')) {
									$('popoutmessage').morph({opacity : 1, width : [0, 835]});
								}
							}
						);
					}
				);	
						
				$('ajax_content').getFirst('form').addEvent('submit', quoteForm);
		
				$('quote_solderresist_yes').addEvent('click', function() {
					$('quote_solderresist_yes').getParent().addClass('arrow');
					$('solder_options').fade('in');
				});
				
				$('quote_solderresist_no').addEvent('click', function() {
					$('quote_solderresist_no').getParent().removeClass('arrow');
					$('solder_options').fade('out');
				});
				
				$('quote_legend_yes').addEvent('click', function() {
					$('quote_legend_yes').getParent().addClass('arrow');
					$('legend_options').fade('in');
				});
				
				$('quote_legend_no').addEvent('click', function() {
					$('quote_legend_no').getParent().removeClass('arrow');
					$('legend_options').fade('out');
				});
				
				$('quote_holes_yes').addEvent('click', function() {
					$('quote_holes_yes').getParent().addClass('arrow');
					$('hole_options').fade('in');
				});
				
				$('quote_holes_no').addEvent('click', function() {
					$('quote_holes_no').getParent().removeClass('arrow');
					$('hole_options').fade('out');
				});				
				
				if ($('quote_solderresist_yes').checked != true) {
					$('solder_options').fade('hide');
				}
				
				if ($('quote_legend_yes').checked != true) {
					$('legend_options').fade('hide');
				}
				
				if ($('quote_holes_yes').checked != true) {
					$('hole_options').fade('hide');	
				}
		
				$('loading-icon').setStyle('display', 'none');
				
				myCal = new Calendar({ quote_required: 'd-m-Y' });
		
				$('closegallery').addEvent('click', function() {
					closeQuoteForm();
				});						
						
			} else {
				theHeight = 0;
				$('ajax_content').getChildren().each(function(el){
					theHeight = el.getSize().y + theHeight;
				});
				//$('ajax_content').morph({height: theHeight 46, opacity: 100});
				$('loading-icon').setStyle('display', 'none');
				
				$('ajax_content').morph({opacity:100, height:theHeight, top : $('ajax_content').getPosition().y+227});
				//$('ajax_content').innerHTML = '<h2>The file has been selected. Please continue editing the page and choose \'Submit\' to confirm selection.</h2>';
				closeQuoteForm.delay(3500);
			}
	};
	

	var openQuote = function openQuote(event) {
		event.stop();
		
		var myFx = new Fx.Scroll(window).toTop().chain(function() {
																
   	 		$('loading-icon').setStyles({'display' : 'block', 'top': $(window).getScroll().y+300});

			var myHTMLRequest = new Request.HTML({url:'/form_quote', update: $('ajax_content'), onComplete: function(){
			
				$('ajax_background').setStyles({'opacity' : 0, 'top': -$(window).getScroll().y,'height':$(window).getScrollSize().y+$(window).getScroll().y });
				$('ajax_content').setStyles({'opacity' : 0, 'top': $(window).getScroll().y+20, 'height' : 0});
			
				doQuoteForm();
		
				$('ajax_background').addEvent('click', function() {
					closeQuoteForm();
				});
	
			} }).get({});	
		});		

	}

	var closeQuoteForm = function() {
		$('theiframe').destroy();
		var myFx = new Fx.Tween('ajax_content');
		myFx.start('opacity',0).chain(
			function(){ $('ajax_background').tween('opacity',0); },
			function(){	$('ajax_background').addClass('notdisplayed');
							$('ajax_content').addClass('notdisplayed');	
							}
		);
		$('ajax_background').removeEvents();
	}

	if ($('form_enquiry')) {
		$('form_enquiry').getFirst('div').getFirst('.email').addEvent('click', initialEnquiryForm);
	}
	
	if ($('getaquote')) {
		$('getaquote').addEvent('click', openQuote);
	}
	//$('getaquote').getFirst('a').addEvent('click', openQuote);

}
	
window.addEvent('domready', goAccordian);
window.addEvent('domready', doAjax);