var $j = jQuery.noConflict();
$j(document).ready(function($) {
	
	$('#homeImage ul').innerfade({timeout: 6000});
	
	$('.payment-calc form').submit(function(){
		cash = $('input[name=cash]', this).val();
		rate = $('input[name=rate]', this).val() / 100;
		term = $('select[name=term]', this).val();
		price = $('input[name=price]', this).val().replace(/,/g,"");
		
		alert(rate);
		
		
		loan = price - cash;
		
/* 		alert('Cash '+cash+' rate '+rate+' Term '+term+' Price '+price); */
				
		payment = loan*(rate*Math.pow(1+rate,term))/(Math.pow(1+rate,term)-1);
		
		$('#payment-val').html('$'+payment.toFixed(2));
		return false;
	});
	
	$('.specials-row img').resize({maxwidth: 230});
	$('#vehicle-rfq-img').resize({maxwidth: 300});
	
	$('.inventory-row').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	

	/* !primary nav dropdowns ====================================== */
	$('ul#primaryNav li').hover(function() {
		$(this).addClass('active').children('ul').show();
	}, function() {
		$(this).removeClass('active').children('ul').hide();
	});
	
	/* !single vehicle specs ====================================== */
	$('ul.specs-button li:first').addClass('active');
	$('ul.specs').hide();
	$('ul.specs:first').show();
	
	$('ul.specs-button li a').click(function(){
		var rel = $(this).attr('rel');
		
		$('ul.specs-button li').removeClass('active');
		$(this).parent().addClass('active');
		$('ul.specs:visible').hide();
		$('ul.specs[rel=' + rel + ']').show();
		
		return false;
	});

	/* !trims ============================== */
	$('#selectTrim').change(function(){
		var newPage = $(this).val();
		var currentURL = window.location;
		
		if(newPage != 'Select One'){
			window.location.href = newPage;
		}
	});
	
	/* gallery */
	$thumb_count = $('div.thumbs a').size()
	$clicks_count = 0;
	$clicks_max = ($thumb_count/8)-1;
	
	$("#gallery-next").click(function(){
		if($clicks_count < $clicks_max){
			$(".thumbs").animate({ 
	        	left: "-=343px"
	      	}, 500 );
	      	$clicks_count++;
      	}
	});

	$("#gallery-prev").click(function(){
		if($clicks_count > 0){
			$("div.thumbs").animate({ 
	        	left: "+=343px"
	      	}, 500 );
	      	$clicks_count--;
      	}
	});
	
	$("div.thumbs a").click(function(){
		$img = $(this).find("img").attr("src");
		$("div.current").find("img").attr("src",$img);

	});
	
});


(function($){  
	$.fn.resize = function(options) {  
		settings = jQuery.extend({
			maxwidth: 250,
			maxheight: 250
		}, options);
		
		return this.each(function() {
			maxW = settings.maxwidth;
			currW = $(this).width();
			currH = $(this).height();
			
			ratio = maxW / currW;
			newW = maxW;
			newH = currH * ratio;
			
			$(this).width(newW);
			$(this).height(newH);
			$(this).fadeIn('fast');
		});  
	};  
})(jQuery); 
