$(document).ready(function(){  
	 $.modelPage = function(whichTags){
	 	var moved = false;
	 	
	 	$('.model_container .model:has(.trimWindow)').bind('click', function(){
	 	
			var trimWindow = $(this).find('.trimWindow');
			
			$('.trimWindow:visible').not(trimWindow).slideUp(600);
			$('.model_container .model').css('background-image', 'url("img/model/model_bg.jpg")');
	 		
	 		$(trimWindow).slideDown(600);
	 		$(this).css('background-image', 'url("img/model/model_bg_active.jpg")');
	 		
			/*
			$(this).bind('mouseleave', function(){
	 			var par = $(this);
	 			$(trimWindow).slideUp(600, function(){
	 				$(par).css('background-image', 'url("img/model/model_bg.jpg")');
	 			});
	 			
	 			$(this).unbind('mouseleave');
	 		});
	 		*/
	 		
	 	});
	 	
	 	var blanks = $('.model_container .model_blank').length;
	 	var newWidth = 750 - (blanks * 250);
	 	$('.model_container .modelRow:last').css('width', newWidth);
	 	
	 	$('.model_container .model:not(:has(.trimWindow))').height($('.model_container .model:first').height());
	 	
	 	$('.model_container #find').click(function(){
	 		var entCity = $('.model_container #city').val();
	 		var entHwy = $('.model_container #hwy').val();
	 		var entHp = $('.model_container #hp').val();
	 		var entMsrp = $('.model_container #msrp').val();
	 		var entPass = $('.model_container #pass').val();
	 		
	 		$('.model_container .trimWindow p').each(function(){
	 			var tCity = Number($(this).attr('cityMpg'));
	 			var tHwy = Number($(this).attr('hwyMpg'));
	 			var tMsrp = Number($(this).attr('msrp'));
	 			var tHp = Number($(this).attr('hp'));
	 			var tPass = Number($(this).attr('pass'));
	 			
	 			if (tCity < entCity || tHwy < entHwy || tMsrp > entMsrp || tHp < entHp || tPass < entPass) {
	 				$(this).hide();
	 			}
	 			else {
	 				$(this).show();
	 			}
	 		});
	 		$.modelCheck();
	 		
	 	});
	 	
	 	$.modelCheck = function(){
	 		$('.model_container .model').each(function(){
	 			var visibles = $(this).find('.trimWindow p:visible').length;
	 			
	 			if (visibles == 0) {
	 				$(this).find('img,p,span').not('.trimWindow').hide();
	 			}
	 			else {
	 				$(this).find('img,p,span').not('.trimWindow').show();
	 			}
	 		});
	 	};
	 	
	 	$('.model_container .model').each(function(){
			var carIndex = $('.model_container .model').index(this);
			var mpgArray = new Array();
			var hpArray = new Array();
			var indivs = $(this).find('.trimWindow p');
	
			$(indivs).each(function(){
				hwy = Number($(this).attr('hwyMpg'));
				hp = Number($(this).attr('hp'));
				hpArray.push(hp);
				mpgArray.push(hwy);
			});
			$('.tinys .tiny:eq('+carIndex+')').attr({'maxmpg':maxValue(mpgArray),'maxhp':maxValue(hpArray)});
	
		});
	 	
	 	var firstColumn = $('.model_container .model:nth-child(3n-1)');
	 	var secondColumn = $('.model_container .model:nth-child(3n)');
	 	var thirdColumn = $('.model_container .model:nth-child(3n+1)');
	 	
	 	$(firstColumn).wrapAll('<div style="width:250px;float:left;padding:0px;margin:0px;position:relative;" class="col"></div>');
	 	$(secondColumn).wrapAll('<div style="width:250px;float:left;padding:0px;margin:0px;position:relative;" class="col"></div>');
	 	$(thirdColumn).wrapAll('<div style="width:250px;float:left;padding:0px;margin:0px;position:relative;" class="col"></div>');
	 	
	 	function maxValue(array){
	 		mxm = array[0];
	 		for (i = 0; i < array.length; i++) {
	 			if (array[i] > mxm) {
	 				mxm = array[i];
	 			}
	 		}
	 		return mxm;
	 	};
	 	
	 	function minValue(array){
	 		mxm = array[0];
	 		for (i = 0; i < array.length; i++) {
	 			if (array[i] < mxm) {
	 				mxm = array[i];
	 			}
	 		}
	 		return mxm;
	 	};
	 	
	 	if (whichTags == 'mpg') {
			$('.model_container .model').each(function(){
				var mpgs = new Array();
				$(this).find('.trimWindow p').each(function(){
					var mpg = Number($(this).attr('hwyMpg'));
					mpgs.push(mpg);
				});
				var maxi = maxValue(mpgs);
				if (maxi >= 30) {
					$(this).append('<img src="img/model/30.gif" class="leaf">');
				}
				else 
					if (maxi >= 25) {
						$(this).append('<img src="img/model/25.gif" class="leaf">');
					}
			});
		}
	else if(whichTags == 'hp'){
		$('.model_container .model').each(function(){
				var hps = new Array();
				$(this).find('.trimWindow p').each(function(){
					var hp = Number($(this).attr('hp'));
					hps.push(hp);
				});
				var maxi = maxValue(hps);
				if (maxi >= 500) {
					$(this).append('<img src="img/model/500.gif" class="leaf">');
				}
				else 
					if (maxi >= 400) {
						$(this).append('<img src="img/model/400.gif" class="leaf">');
					}
				else 
					if (maxi >= 300) {
						$(this).append('<img src="img/model/300.gif" class="leaf">');
					}
			});
	}

	
	$.mpgSort=function(){
		$('.bar p').hide();
		$('.tiny span').remove();
		$('.tinys h2').text('Sorted by Mileage');		
		mpgs = new Array();
		$('.tinys .tiny').each(function(){
			mpgs.push(Number($(this).attr('maxmpg')));
			$(this).find('.bar').after('<span style="float:left;height:17px;padding-top:15px;">&nbsp;'+$(this).attr('maxmpg')+' MPG</span>');
		});
		
		var mins = Number(minValue(mpgs));
		var maxs = Number(maxValue(mpgs));
		mpgs = mpgs.sort();
		
		for(m=mins;m<=maxs;m++){
			$('.tiny[maxmpg='+m+']').insertAfter('.tinys h2');
		}
		
		var barMin = mins - 5;
		var barMax = maxs + 5;
		var totalBar = 650;

		$.mpgBars = function(){
			$('.tinys .tiny').each(function(){
				var thisMpg = $(this).attr('maxmpg');
				var newWidth = (thisMpg * totalBar) / barMax;
				$(this).find('.bar').animate({
					width: newWidth + 'px'
				}, 1500,function(){
					$('.bar p').show();
				});
			});
		}
	};
	
	$.hpSort=function(){
		$('.bar p').hide();
		$('.tiny span').remove();
		$('.tinys h2').text('Sorted by Horsepower');	
		hps = new Array();
		$('.tinys .tiny').each(function(){
			hps.push(Number($(this).attr('maxhp')));
			$(this).find('.bar').after('<span style="float:left;height:17px;padding-top:15px;">&nbsp;'+$(this).attr('maxhp')+' HP</span>');
		});
		
		var mins = Number(minValue(hps));
		var maxs = Number(maxValue(hps));

		hps = hps.sort();
		
		for(m=mins;m<=maxs;m++){
			$('.tiny[maxhp='+m+']').insertAfter('.tinys h2');
		}
		
		var barMin = mins - 5;
		var barMax = maxs + 5;
		var totalBar = 600;

		$.hpBars = function(){
			$('.tinys .tiny').each(function(){
				var thisHp = $(this).attr('maxhp');
				var newWidth = (thisHp * totalBar) / barMax;
				$(this).find('.bar').animate({
					width: newWidth + 'px'
				}, 1500,function(){
					$('.bar p').show();
				});
			});
		}
	};
	
	$('.tiny img').bind('click',function(){
		$('#returnClick').fadeOut(600);
		var ref = $(this).attr('refId');
		$('.tinys').slideUp(800,function(){
			$('.col').slideDown(800,function(){
				$('.model img[modelId='+ref+']').click();
			});
		});
	});
	
	$.greens=function(){
		var startGreen = 0;
		var endGreen = 100;
		var steps = Math.round(endGreen / $('.tiny').length);
		var loop = Number($('.tiny').length - 1);
		
		for(g=0;g<=loop;g++){
			$('.bar:eq('+g+')').css('background-color','rgb('+startGreen+',143,'+startGreen+')');
			startGreen = startGreen + steps;
		}
	}
	
	$.reds=function(){
		var startRed = 0;
		var endRed = 100;
		var steps = Math.round(endRed / $('.tiny').length);
		var loop = Number($('.tiny').length - 1);
		
		for(g=0;g<=loop;g++){
			$('.bar:eq('+g+')').css('background-color','rgb(143,'+startRed+','+startRed+')');
			startRed = startRed + steps;
		}
	}
	
	$('#mpgClick').click(function(){

		$('.col').slideUp(800);
		$.mpgSort();
		$.greens();
		$('.tinys').slideDown(800,function(){
			$.mpgBars();
			$('#returnClick').fadeIn(600);
		});

	});
	
	$('#hpClick').click(function(){
		
		$('.col').slideUp(800);
		$.hpSort();
		$.reds();
		$('.tinys').slideDown(800,function(){
			$.hpBars();
			$('#returnClick').fadeIn(600);
		});

	});
	
	$('#returnClick').click(function(){
		$('#returnClick').fadeOut(600);
		$('.tinys').slideUp(800,function(){
			$('.col').slideDown(800);
		});
	});
	
	if($('.tiny[maxhp=0]').length == 0 && $('.tiny[maxhp]').length == $('.tiny').length){
		$('#hpClick').show();
	}
	
	if($('.tiny[maxmpg=0]').length == 0 && $('.tiny[maxmpg]').length == $('.tiny').length){
		$('#mpgClick').show();
	}
	
	}

});
