window.addEvent('domready', function() { 
	
		$$('.back').addEvent('click',function(ev){
			if(ev) ev = new Event(ev).stop();
			history.back();
		});
		
		if($('shortcut_url')){
			if(!(window.ie))
			var wrap_url = $('shortcut_url').getText().replace("inventory/","inventory /");
			$('shortcut_url').setHTML(wrap_url);
		}
	
	
		if($('inventory_search')) {
		
			var mySlider = new Fx.Slide('inventory_search', {
				duration: 1000, 
				wait:false, 
				transition: Fx.Transitions.Expo.easeOut,
				onStart: function(){
					$('inventory_search').getParent().setStyle('overflow','hidden');
				},
				onComplete: function(){
					if($('inventory_search').getParent().getStyle('height') != '0px') {
						$('inventory_search').getParent().setStyles({'overflow':'visible','height':null});
					}
				}
			});
			
		//	mySlider.hide();
		
		}

		if($('inventory_handle_image')) {
			
			$('inventory_handle_image').addEvent('click',function(ev){
				if (ev) ev = new Event(ev).stop();
				mySlider.toggle();
			});
		
		}
		
	
		
		var update_selects = new Ajax('',{
			autoCancel: true,
			data:$('inventory_search_form'),
			method:'post',
			onRequest: function(){
				$$('.search_multiple').each(function(el){
					el.empty();
					el.setStyles({
					    'height': 0,
					    'opacity': 0,
					    'margin':0
					});	
				});
			},
			onComplete: function(data) {
								
					var selects = Json.evaluate(data);
				
					var i = 0;
					
					if(update_selects.target == 'designation' || ( update_selects.target != 'model_year' && update_selects.target != 'type' && update_selects.target != 'manufacturer' && update_selects.target != 'brand' && update_selects.target != 'olength' ) ) {
											
						$('model_year').options.length = 0;
						
						selects.model_year.each(function(el){
							
							if(el == '')
								$('model_year').options[i] = new Option('View All','');
							else {
								$('model_year').options[i] = new Option(el,el);
							}
								
							i++;
						});
					
					}
					
					
					if(update_selects.target == 'designation' || ( update_selects.target != 'type' && update_selects.target != 'manufacturer' && update_selects.target != 'brand' && update_selects.target != 'olength' ) ) {
						
						i = 0;
						$('type').options.length = 0;
						
						selects.type.each(function(el){
							if(el == '')
								$('type').options[i] = new Option('View All','');
							else
								$('type').options[i] = new Option(el,el);
							i++;
						});
					
					}
					
						
					if(update_selects.target == 'designation' || ( update_selects.target != 'manufacturer' && update_selects.target != 'brand' && update_selects.target != 'olength' ) ) {
						
						i = 0;
						$('manufacturer').options.length = 0;
											
						selects.manufacturer.each(function(el){
							if(el == '')
								$('manufacturer').options[i] = new Option('View All','');
							else
								$('manufacturer').options[i] = new Option(el,el);
							i++;
						});
				
					}
			
						
					if(update_selects.target == 'designation' || ( update_selects.target != 'brand' && update_selects.target != 'olength') ) {
						
						i = 0;
						$('brand').options.length = 0;
															
						selects.brand.each(function(el){
							if(el == '')
								$('brand').options[i] = new Option('View All','');
							else
								$('brand').options[i] = new Option(el,el);
							i++;
						});
					
					}
					
					if(update_selects.target == 'designation' || update_selects.target != 'olength') {
						
						i = 0;
						$('olength').options.length = 0;
															
						$('olength').options[i] = new Option('View All','');
						
						selects.olength.each(function(el){
							
							i++;
							
							el2 = el.split('.',el);
							el2[1] = el2[1].toInt();
							el2[1] = ((el2[1]*.01)*12).round();
							$('olength').options[i] = new Option(el2[0]+'\' '+el2[1]+'"',el);
														
						});
					
					}
					
					
			}
		});
		
		
		
		
		var search_multiple = new Fx.Styles('', {duration: 500, wait:false, transition: Fx.Transitions.Cubic.easeInOut});
		
		
		var update_inventory =	new Ajax('', {
										method:'post',
										data:$('inventory_search_form'),
										autoCancel: true,
										onRequest: function(){
											$$('.search_multiple').each(function(el){
												el.empty();
												el.setStyles({
												    'height': 0,
												    'opacity': 0,
												    'margin':0
												});	
											});
										},
										onSuccess:function(response){
											$(update_inventory.target+'_multiple').setHTML(response);
											search_multiple.element = $(update_inventory.target+'_multiple');
											search_multiple.start({
											    'height': $(update_inventory.target+'_multiple').getSize().scrollSize.y,
											    'opacity': 1,
											    'margin':'10px 0px 0px 0px'
											});
											
											$(update_inventory.target).options[$(update_inventory.target).selectedIndex].value = update_inventory.holder.join(',');
											
											$$('.label_multiple').each(function(label){
												
												var ckbox = label.getFirst();
																								
												ckbox.addEvents({
													'click':function(ev){
														
														var currently_selected_value = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value;
														var currently_selected_text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].text;
															
														if(ckbox.checked) {
														
															if(currently_selected_value.contains(ckbox.getProperty('value'))) {
																return;
															} else {
																if(currently_selected_text.contains('View All') ) {
																	$(update_inventory.target).options[$(update_inventory.target).options.length] = new Option(currently_selected_text,currently_selected_value);
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].value = ckbox.getProperty('value');
																} else {
																	
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].value = currently_selected_value + ',' +ckbox.getProperty('value');
																	
																}

																if($(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.length > 20)
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize().substr(0,17) + '...';
																else
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize();
																															
															}
															
															$(label).setStyle('background','#dfd');
															
														} else {
															
															var haystack = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value;
															var chkval = ckbox.getProperty('value');
															
															haystack = haystack.replace(chkval,'');
															
															if(haystack.contains(',,')) {
																$(update_inventory.target).options[$(update_inventory.target).selectedIndex].value = haystack.replace(',,',',');
																
																if($(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.length > 20)
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize().substr(0,17) + '...';
																else
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize();
															
															} else if(haystack.substr(-1) == ',') {
																$(update_inventory.target).options[$(update_inventory.target).selectedIndex].value = haystack.substring(0,haystack.length - 1);
															
																if($(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.length > 20)
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize().substr(0,17) + '...';
																else
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize();
															
															} else if(haystack.substring(0,1) == ',') {
																$(update_inventory.target).options[$(update_inventory.target).selectedIndex].value = haystack.substr(1);
															
																if($(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.length > 20)
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize().substr(0,17) + '...';
																else
																	$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = $(update_inventory.target).options[$(update_inventory.target).selectedIndex].value.capitalize();
															
															} else {
																$(update_inventory.target).options[$(update_inventory.target).selectedIndex].value = '';
																$(update_inventory.target).options[$(update_inventory.target).selectedIndex].text = 'View All';
																$(update_inventory.target).options[$(update_inventory.target).options.length - 1] = null;
															}
															
															$(label).setStyle('background','#fff');

														}
														
													}
																																					
												});
												
												if(update_inventory.holder.contains(ckbox.value)){
													ckbox.checked = true;
													$(label).setStyle('background','#dfd');
												}
												
											});
										}
									});
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		$$('#inventory_search_form select').each(function(el){
			
			var el = el;
			el.addEvent('change',function(ev){
								
				var target = el.getProperty('id');
				
				if(target != 'model_year' && target != 'type' && target != 'manufacturer' && target != 'brand' && target != 'olength') {
					$('model_year').options[$('model_year').selectedIndex].value 		= '';
				}
				if(target != 'type' && target != 'manufacturer' && target != 'brand' && target != 'olength') {
					$('type').options[$('type').selectedIndex].value 					= '';
				}
				if(target != 'manufacturer' && target != 'brand' && target != 'olength') {
					$('manufacturer').options[$('manufacturer').selectedIndex].value 	= '';
				}
				if(target != 'brand' && target != 'olength') {
					$('brand').options[$('brand').selectedIndex].value 					= '';
				}
				if(target != 'olength') {
					$('olength').options[$('olength').selectedIndex].value 				= '';
				}
				
				
				update_selects.target = target;
				update_selects.url = '/inventory/json_search_form/'+target;
				update_selects.request();
						
			});
				
		});
		
		$$('.radio').each(function(el){
			
			var el = el;
			el.addEvent('click',function(ev){
				
				$('model_year').options[$('model_year').selectedIndex].value 		= '';
				$('type').options[$('type').selectedIndex].value 					= '';
				$('manufacturer').options[$('manufacturer').selectedIndex].value 	= '';
				$('brand').options[$('brand').selectedIndex].value 					= '';
				$('olength').options[$('olength').selectedIndex].value 				= '';
												
				var target = el.getProperty('name');
								
				update_selects.target = target;
				update_selects.url = '/inventory/json_search_form/'+target;
				update_selects.request();
						
			});
				
		});
		
		
		$$('.inventory_select_multiple').each(function(el){
			
			var el = el;
			el.addEvent('click',function(ev){
				
				if (ev) ev = new Event(ev).stop();
				
				var target = el.getProperty('ref');
				
				update_inventory.holder = new Array();
				
				update_inventory.holder = $(target).options[$(target).selectedIndex].value.split(',');
				
				$(target).options[$(target).selectedIndex].value = '';
					
				if(target != 'model_year' && target != 'type' && target != 'manufacturer' && target != 'brand' && target != 'olength') {
					$('model_year').options[$('model_year').selectedIndex].value = '';
					$('model_year').options[$('model_year').selectedIndex].text = 'View All';
				}
				if(target != 'type' && target != 'manufacturer' && target != 'brand' && target != 'olength') {
					$('type').options[$('type').selectedIndex].value = '';
					$('type').options[$('type').selectedIndex].text = 'View All';
				}
				if(target != 'manufacturer' && target != 'brand' && target != 'olength') {
					$('manufacturer').options[$('manufacturer').selectedIndex].value = '';
					$('manufacturer').options[$('manufacturer').selectedIndex].text = 'View All';
				}
				if(target != 'brand' && target != 'olength') {
					$('brand').options[$('brand').selectedIndex].value = '';
					$('brand').options[$('brand').selectedIndex].text = 'View All';
				}
				if(target != 'olength') {
					$('olength').options[$('olength').selectedIndex].value = '';
					$('olength').options[$('olength').selectedIndex].text = 'View All';
				}
				
				update_inventory.target = target;
				update_inventory.url = '/inventory/ajax_search_form/'+target;
				update_inventory.request();
						
			});
				
		});
		
		
		
});
window.addEvent('load',function(){
	
	

	
		var move_overlay = new Fx.Styles('',{duration:500,wait:false});
		
		$$('.utility_form').addEvent('submit',function(ev){
			if (ev) ev = new Event(ev).stop();
			if($('overlay')){
				return;
			} else {
				
				var dims = this.getSize();
				
				if (window.ie6) {
					if(!$('DivShim')){
						var divShimProperties = {'id': "DivShim", 'src': "/lib_css/blank.html",'scrolling': "no",'frameborder': 0, 'allowtransparency': "true"};
						var divShimStyles = {'position': "absolute",'top': "0px",'left': "0px",'display': "block", 'font-size': "6px", 'background-color': "transparent", 'filter': "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)",'height':dims.size.y,'width':dims.size.x};
						var divShim = new Element('iframe').setProperties(divShimProperties).setStyles(divShimStyles).injectTop(this);
					}
				}
				
				
				new Element('div',{
								'id':'overlay',
								'styles':{
									'opacity':.2,
									'height':dims.size.y,
									'width':dims.size.x
								}
				}).injectTop(this);
				this.send({
					evalScripts:true,
					autoCancel:true,
					onComplete:function(data){
						
						//var message = Json.evaluate(data);
						
						//$('overlay').adopt(new Element('div',{'styles':{'padding-top':200}}).setHTML(message.response));
					
						$('overlay').adopt(new Element('div',{'styles':{'padding-top':200}}).setHTML(data));
						
						move_overlay.element = $('overlay');
						move_overlay.start({'opacity':1}).chain(function(){
							move_overlay.start.delay(2500,this,{'opacity':0});
						}).chain(function(){
								$('overlay').remove();
								if (window.ie6) {
									divShim.remove();
								}
							});;
					
					}
				});
			}		
		});
		
});