// JavaScript Document

function adjustLayout()
{				
	xHide("footer");
	
	// Get natural height of the tallest column
	var lHeight = xHeight("leftcolumn");
	var rHeight = xHeight("rightcolumn");
	
	//Set the max height
	var maxHeight = Math.max(lHeight, rHeight);
	
	// Assign max height to side columns
	xHeight("leftcolumn", maxHeight);
	xHeight("rightcolumn", maxHeight);
	
	xShow("footer");
}


function adjustLayoutListings()
{				
	xHide("footer");
	
	// Get natural height of the tallest column
	var rHeight = xHeight("listings");
	var lHeight = xHeight("listings-side");
	
	if (lHeight > rHeight) {
		//If main area is shorter than sidebar, make it bigger! 
		xHeight("listings", lHeight+50);
	} 
	
	xShow("footer");
}

function adjustLayoutint()
{				
	xHide("footer");
	
	// Get natural height of the tallest column
	var lHeight = xHeight("leftcolumninterior");
	var rHeight = xHeight("rightcolumninterior");
	
	//Set the max height
	var maxHeight = Math.max(lHeight, rHeight);
	
	// Assign max height to side columns
	xHeight("leftcolumninterior", maxHeight);
	xHeight("rightcolumninterior", maxHeight);
	
	xShow("footer");
}


function filterListings(ck, cl) 
{
	//Get an array of all the listings to work with
	var els = xGetElementsByClassName('listing');
	
	//Get the class variable in the right format
	var cln = cl.replace(/ /gi, '_');
	
	//alert(cln);
	
	if (cln == 'reset') {
		for (i = 0; i < els.length; i++) {
    	  	xRemoveClass(els[i], 'hidden');
    	  	if (xHasClass(els[i], 'c_avionics')) { 	  				
    			xRemoveClass(els[i], 'c_avionics');
    		}
    	  	if (xHasClass(els[i], 'c_engine')) { 	  				
    			xRemoveClass(els[i], 'c_engine');
    		}
    		if (xHasClass(els[i], 'c_ac')) { 	  				
    			xRemoveClass(els[i], 'c_ac');
    		}
    		if (xHasClass(els[i], 'c_generation')) { 	  				
    			xRemoveClass(els[i], 'c_generation');
    		}
    		if (xHasClass(els[i], 'c_deice')) { 	  				
    			xRemoveClass(els[i], 'c_deice');
    		}
    	}
	
		var av = xGetElementById('c_avionics');
		var engine = xGetElementById('c_engine');
    	var ac = xGetElementById('c_ac');
    	var af = xGetElementById('c_generation');
    	var ice = xGetElementById('c_deice');
    	
    	av.selectedIndex = 'empty';
    	engine.selectedIndex = 'empty';
    	ac.selectedIndex = 'empty';
    	af.selectedIndex = 'empty';
    	ice.selectedIndex = 'empty';
	
	} else {
	
		for (i = 0; i < els.length; i++) {
	
			//Get the attributes for this listing
			var av = els[i].getAttribute('c_avionics');
			var eng = els[i].getAttribute('c_engine');			
			var ac = els[i].getAttribute('c_ac');			
			var gen = els[i].getAttribute('c_generation');
			var ice = els[i].getAttribute('c_deice');
							
    		switch (ck)
    		{
    			case 'c_avionics':
    				if (xHasClass(els[i], 'hidden')) { //The listing is already hidden
    					
    					if (xHasClass(els[i], 'c_engine') || xHasClass(els[i], 'c_ac') || xHasClass(els[i], 'c_generation') || xHasClass(els[i], 'c_deice')) { 
    						//We don't want to un-hide the listing if it is hidden for any of the other drop-downs
    						if (xHasClass(els[i], 'c_avionics') && av == cln) {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_avionics');
    						
    						} else if (xHasClass(els[i], 'c_avionics') && cln == 'empty') {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_avionics');
    							
    						} else if (av != cln && cln != 'empty') {
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also	
    						} 
    	 		
    					} else {
    						if (cln == av || cln == 'empty') {
    							//alert('Need to unhide a listing');
    							xRemoveClass(els[i], 'hidden'); //Un-hide the listing
    							
    							if (xHasClass(els[i], 'c_avionics')) { 	  				
    								xRemoveClass(els[i], 'c_avionics');
    							}
    						} else {				
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also
    						}
    					}
    				
    				    	
    				} else { //The listing is not already hidden
    				
    					if (av != cln && cln != 'empty') {
    						//If it doesn't match, we need to hide it and tell why
    						xAddClass(els[i], 'hidden'); //Hide the listing if it's the wrong engine
    						xAddClass(els[i], ck); //Add the class to show that it is hidden because of this		
    					}
    				}
    				break;
    				
    			case 'c_engine':
    				if (xHasClass(els[i], 'hidden')) { //The listing is already hidden
    					
    					if (xHasClass(els[i], 'c_avionics') || xHasClass(els[i], 'c_ac') || xHasClass(els[i], 'c_generation') || xHasClass(els[i], 'c_deice')) { 
    						//We don't want to un-hide the listing if it is hidden for any of the other drop-downs
    						if (xHasClass(els[i], 'c_engine') && eng == cln) {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_engine');
    						
    						} else if (xHasClass(els[i], 'c_engine') && cln == 'empty') {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_engine');
    							
    						} else if (eng != cln && cln != 'empty') {
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also	
    						} 
    	 		
    					} else {
    						if (cln == eng || cln == 'empty') {
    							//alert('Need to unhide a listing');
    							xRemoveClass(els[i], 'hidden'); //Un-hide the listing
    							
    							if (xHasClass(els[i], 'c_engine')) { 	  				
    								xRemoveClass(els[i], 'c_engine');
    							}
    						} else {				
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also
    						}
    					}
    				
    				    	
    				} else { //The listing is not already hidden
    				
    					if (eng != cln && cln != 'empty') {
    						//If it doesn't match, we need to hide it and tell why
    						xAddClass(els[i], 'hidden'); //Hide the listing if it's the wrong engine
    						xAddClass(els[i], ck); //Add the class to show that it is hidden because of this		
    					}
    				}
    				break;
    				
    			case 'c_ac':
    				if (xHasClass(els[i], 'hidden')) { //The listing is already hidden
    					
    					if (xHasClass(els[i], 'c_avionics') || xHasClass(els[i], 'c_engine') || xHasClass(els[i], 'c_generation') || xHasClass(els[i], 'c_deice')) { 
    						//We don't want to un-hide the listing if it is hidden for any of the other drop-downs
    						if (xHasClass(els[i], 'c_ac' && ac == cln) ) {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_ac');
    							
    						} else if (xHasClass(els[i], 'c_ac') && cln == 'empty') {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_ac');
    							
    						} else if (ac != cln && cln != 'empty') {
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also	
    						}
    	 		
    					} else {
    						if (ac == cln || cln == 'empty') {
    							if (xHasClass(els[i], 'c_ac')) {
    								//Remove this class to show that it's not hidden because of this 	  				
    								xRemoveClass(els[i], 'c_ac');
    							}
    							xRemoveClass(els[i], 'hidden'); //Un-hide the listing
    						} else {				
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also
    						}
    					}
    				
    				    	
    				} else { //The listing is not already hidden
    				
    					if (ac != cln && cln != 'empty') {
    						//If it doesn't match, we need to hide it and tell why
    						xAddClass(els[i], 'hidden'); //Hide the listing if it's the wrong engine
    						xAddClass(els[i], ck); //Add the class to show that it is hidden because of this		
    					} 
    				}
    				break;
    				
    			case 'c_generation':
    				if (xHasClass(els[i], 'hidden')) { //The listing is already hidden
    					
    					if (xHasClass(els[i], 'c_avionics') || xHasClass(els[i], 'c_ac') || xHasClass(els[i], 'c_engine') || xHasClass(els[i], 'c_deice')) { 
    						//We don't want to un-hide the listing if it is hidden for any of the other drop-downs
    						if (xHasClass(els[i], 'c_generation') && gen == cln) {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_generation');
    							
    						} else if (xHasClass(els[i], 'c_generation') && cln == 'empty') {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_generation');
    							
    						} else if (gen != cln && cln != 'empty') {
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also	
    						} 
    	 		
    					} else {
    						if (gen == cln || cln == 'empty') {
    							if (xHasClass(els[i], 'c_generation')) {
    								//Remove this class to show that it's not hidden because of this 	  				
    								xRemoveClass(els[i], 'c_generation');
    							}
    							xRemoveClass(els[i], 'hidden'); //Un-hide the listing
    						} else {				
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also
    						}
    					}
    				
    				    	
    				} else { //The listing is not already hidden
    				
    					if (gen != cln && cln != 'empty') {
    						//If it doesn't match, we need to hide it and tell why
    						xAddClass(els[i], 'hidden'); //Hide the listing if it's the wrong engine
    						xAddClass(els[i], ck); //Add the class to show that it is hidden because of this		
    					} 
    				}
    				break;
    				
    			case 'c_deice':
    				if (xHasClass(els[i], 'hidden')) { //The listing is already hidden
    					
    					if (xHasClass(els[i], 'c_avionics') || xHasClass(els[i], 'c_ac') || xHasClass(els[i], 'c_engine') || xHasClass(els[i], 'c_generation')) { 
    						//We don't want to un-hide the listing if it is hidden for any of the other drop-downs
    						if (xHasClass(els[i], 'c_deice') && ice == cln) {
    							//Only remove this class to show that it's not hidden because of this	  				
    							xRemoveClass(els[i], 'c_deice');
    							
    						} else if (xHasClass(els[i], 'c_deice') && cln == 'empty') {
    							//Only remove this class to show that it's not hidden because of this 	  				
    							xRemoveClass(els[i], 'c_deice');
    							
    						} else if (ice != cln && cln != 'empty') {
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also	
    						} 
    	 		
    					} else {
    						if (ice == cln || cln == 'empty') {
    							if (xHasClass(els[i], 'c_deice')) {
    								//Remove this class to show that it's not hidden because of this 	  				
    								xRemoveClass(els[i], 'c_deice');
    							}
    							xRemoveClass(els[i], 'hidden'); //Un-hide the listing
    						} else {				
    							xAddClass(els[i], ck); //Add the class to show that it is now hidden because of this also
    						}
    					}
    				
    				    	
    				} else { //The listing is not already hidden
    				
    					if (ice != cln && cln != 'empty') {
    						//If it doesn't match, we need to hide it and tell why
    						xAddClass(els[i], 'hidden'); //Hide the listing if it's the wrong engine
    						xAddClass(els[i], ck); //Add the class to show that it is hidden because of this		
    					} 
    				}
    				break;
    				
    		} //End switch
    		
    	} //End for loop
    		
    } //End if/else conditional
    
    adjustLayoutListings();
}
