/* DomReady Event fires when all Elements are ready, but not images. */ 
window.addEvent('domready', function() { 
	
	
	
	var modal = new Element('div',{
		'id':'modal_win',
		'styles':{
					'opacity':.8,
					'background':'#000',
					'height':window.getScrollHeight(),
					'width':window.getScrollWidth(),
					'position':'absolute',
					'top':0,
					'left':0,
					'z-index':99,
					'display':'none'
		}
	});
	
	document.body.appendChild(modal);
		
	//document.body.appendChild($('modal_message'));
	
	$('modal_message').setStyles({
		'display':'none',
		'height':600,
		'width':600,
		'position':'absolute',
		'z-index':999,
		'top':-600
	});
	
	var showModalMessage = new Fx.Styles('modal_message',{
		'duration':500,
		'wait':false
	});
	
	$$('.get_quote').addEvent('click',function(ev){
		if(ev) ev = new Event(ev).stop();
		
		modal.setStyle('display','');
		$('modal_message').setStyles({
			'display':'',
			'left':(((window.getSize().size.x/2)+window.getScrollLeft())-300)
		});
		
		showModalMessage.start({
			top:[-600,(((window.getSize().size.y/2)+window.getScrollTop())-300)],
			'opacity':[0,1]
		});
	});
	
	window.addEvent('resize',function(ev){
		if(modal.getStyle('display')!='none'){
			modal.setStyles({'height':window.getScrollHeight(),'width':window.getScrollWidth()});
			$('modal_message').setStyle('left',(((window.getSize().size.x/2)+window.getScrollLeft())-300));
		}
	});
	
	
	$('close_modal').addEvent('click',function(ev){
		if(ev) ev = new Event(ev).stop();
		showModalMessage.start({
			top:-600,
			'opacity':0
		}).chain(function(){
			modal.setStyle('display','none');
		});
	});
	
	
		
 	var showTips = new Fx.Styles('',{duration:100,wait:false});
 	
	var myTips3 = new MooTips($$('.thumb'), {
				
				fixed:true,
				showDelay:650,
				evalAlways: false,		// always run the eval statement
				maxTitleChars: 500,		// very long caption
				offsets: {'x':-110,'y':-100} // offset by 100,100
				
			});

	
	//form processing section
		
	function checkOptions(option) {
		
		if($chk(modifiers.forceOff[option.id])) {
			
			$each(modifiers.forceOff[option.id], function(value,index){
				var element = $(value);
				if(element != null && element.inputElement.checked) {
					elementName = $E('a', element).innerHTML;
					new StickyWinModal({
										content: stickyWinHTML('Notice', elementName+' will be removed if you select this option. Proceed?', {
												 width: '400px',
												 buttons: [
												      {
												        text: 'Do It',
												        onClick: function(){
												        	FancyForm.deselect(element,true);
												        }
												      },
												      {
												        text: 'Don\'t Do It', 
												        onClick: function(){
												        	FancyForm.deselect(option,true);
												        }
												      }
												    ]
												  }),
											modalOptions: {
											    modalStyle:{
											      'opacity':.7
											    },
											    hideOnClick:false
											  }
											});
										   //confirm(elementName+' will be removed if you select this option. Would you still like to add this option?')
				}
			});	
			
		}		
			
		if($chk(modifiers.forceOn[option.id])) {
		
			$each(modifiers.forceOn[option.id], function(value,index){
				var element = $(value);
				if(element != null && !element.inputElement.checked) {
					elementName = $E('a', element).innerHTML;
					new StickyWinModal({
										content: stickyWinHTML('Notice', elementName+' is required if you select this option. Would you still like to add This Option?', {
												 width: '400px',
												 buttons: [
												      {
												        text: 'Do It',
												        onClick: function(){
												        		FancyForm.select(element);
														}
												      },
												      {
												        text: 'Don\'t Do It', 
												        onClick: function(){
												        	FancyForm.deselect(option,true);
												        }
												      }
												    ]
												  }),
											modalOptions: {
											    modalStyle:{
											      'opacity':.7
											    },
											    hideOnClick:false
											  }
											}); //confirm(elementName+' will is required if you select this option. Proceed?')
				}
				
			});
			
		}
		
	}
	
	//var indicator = new Element('div', {'class': 'loading', 'styles': {'position':'absolute','top':-3,'right':5,'height':21,'width':21}}).setHTML('');
	//var indicator = new Element('div', {'class': 'loading', 'styles': {'display':'inline','vertical-align':'middle','height':16,'width':16}}).setHTML('');
		
	var indicator = new Asset.image('/lib_css/images/elements/spinner.gif',{'styles': {'display':'inline','vertical-align':'middle','height':16,'width':16,'opacity':0}});
	var indicator2 = indicator.clone();
	var indicator3 = indicator.clone();
	
	indicator.injectAfter('msrp');
	indicator2.injectAfter('total');
	indicator3.injectAfter('savings');
	
	var price = null;
	
//fancy form fields	

	var updatePrice = new Ajax('/rv_builder/ajaxUpdatePrice.html', {
		method:'post',
		autoCancel:true,
		evalScripts:true,
		data:$('custom_build_form'),
		onRequest: function(){
			
			indicator.setStyle('opacity',1);
			indicator2.setStyle('opacity',1);
			indicator3.setStyle('opacity',1);
						
		},
		onComplete: function(responseText){
			
			price = Json.evaluate(responseText);
			
			indicator.setStyle('opacity',0);
			indicator2.setStyle('opacity',0);
			indicator3.setStyle('opacity',0);
			
			$('msrp').empty().setHTML(price.msrp);
			$('total').empty().setHTML(price.total);
			$('savings').empty().setHTML(price.savings);
			
		}
	});
				

	FancyForm.start(0,{
		
		onSelect: function(chk){
			
				checkOptions(chk);
				
				updatePrice.request();
								
				chk.getParent().addClass('isselected').removeClass('notselected');
				
				c = new Element('input',{'type':'hidden','id':'holder'+chk.id,'name':'options[]','value':+chk.inputElement.value});
				
				$('holder').appendChild(c);
									
		},
		onDeselect: function(chk){
					
				updatePrice.request();
				
				chk.getParent().addClass('notselected').removeClass('isselected');
				
				if($('holder'+chk.id))
					$('holder'+chk.id).remove();
					
		}
		
	});	

	
	
	
	
	
	
	
		/*
	
		var custom_build_form = $('custom_build_form').getCoordinates();
		var price_box = $('prices').getCoordinates();
		
		if( window.ie6 ) {
			
			var movePricing = new Fx.Styles('prices', {
										wait: false,
										duration: 1000
									});
										
			this.addEvent('scroll',function(){
					
					var scrollIt = 0;
					
					if( this.getScrollTop() < price_box.top ) {
						if($('prices').getStyle('top') != 0);
							movePricing.start({'top':0,'right':10});
					}
					
					if( this.getScrollTop() > price_box.top && (this.getScrollTop() + 500) <= custom_build_form.bottom ) {
						scrollIt = this.getScrollTop() - price_box.top + 10 ;
						movePricing.start({'top':scrollIt,'right':null});
					}
					
				}.bind(this));
				
		} else {
		
			var custom_build_form = $('custom_build_form').getCoordinates();
			var price_box = $('prices').getCoordinates();
			
			$('sum_wrapper').setStyle('height',custom_build_form.height+40);
									
			this.addEvent('scroll',function(){
				
				if( this.getScrollTop() >= (price_box.top - 10) && (this.getScrollTop() + 500) <= custom_build_form.bottom ) {
					
					if($('prices').getStyle('position') == 'fixed') {
						return;
					} else {
						$('prices').removeProperty('style');
						$('prices').setStyles({'position':'fixed','top':5,'right':null});
					}
						
					
				} else {
					
					if( this.getScrollTop() <= (price_box.top - 10) ) {
					
						$('prices').setStyles({'position':'absolute','top':0,'right':10});
						
					} else {
					
						$('prices').removeProperty('style');
						$('prices').setStyles({'position':'absolute','bottom':0});
						
					}
					
				}
				
			}.bind(this));
				
		}
		
		*/
		
		
		
		
		var indicator4 = new Asset.image('/lib_css/images/form/form_submit.gif',{'id':'loading'});
	
		new Asset.javascript('/lib_js/niceforms.js', {id: 'niceforms'});
	
			
		var formSubmit = new Ajax('/rv_builder/sendQuote.html',{
							data: $('quote'),
							method:'post',
							autoCancel:true,
							evalScripts:true,
							update: $('submit_form'),
							onRequest: function(){
								$('submit_form').empty().appendChild(indicator4);
							},
							onComplete: function(){
								$('close_form').addEvent('click',function(ev){
									if(ev) ev = new Event(ev).stop();
									showModalMessage.start({
										top:-600,
										'opacity':0
									}).chain(function(){
										modal.setStyle('display','none');
									});
								});
							}
						});
					
		$('quote').addEvent('submit',function(ev){
			if(ev) ev = new Event(ev).stop();
			formSubmit.request();
		});
		
		/*
		
		var gdetails = new Fx.Scroll('prices_sliding_container',{'wait':false,'duration':250,'transition':Fx.Transitions.Elastic.easeOut});
		
		
		$('guarantee').addEvents({
			'mouseover':function(ev){
				ev = new Event(ev).stop();
				gdetails.toRight();
				
			},
			'mouseout':function(ev){
				ev = new Event(ev).stop();
				gdetails.toLeft();
			}
		});
		
		*/
		
	
	var show_menu = new Fx.Styles('',{duration:500,wait:false});
	
	$$('.show_menu').addEvent('click',function(ev){
		if(ev) ev = new Event(ev).stop();
		$$('.trail_menu').setStyles({'opacity':0,'height':0});
		el = this.getProperty('rel') + '_menu';
		show_menu.element = $(el);
		show_menu.start({'opacity':1,'height':(show_menu.element.getSize().scrollSize.y-46)});
	});
	
	$$('.close_menu').addEvent('click',function(ev){
		if(ev) ev = new Event(ev).stop();
		$$('.trail_menu').setStyles({'opacity':0,'height':0});
	});
		
	$$('.makes_link').each(function(element){
		var element = element;
		element.addEvents({
			'mouseenter':function(ev){ ev = new Event(ev).stop(); element.removeClass('makes_notselected').addClass('makes_isselected'); },
			'mouseleave':function(ev){ ev = new Event(ev).stop(); element.removeClass('makes_isselected').addClass('makes_notselected'); }
		});
	});
	
	
	

});