$(document).ready(function() {
	/* !primaryNav dropdowns ====================================== */
	$('ul#primaryNav li').hover(function() {
		$(this).toggleClass('active');
		$(this).children('ul').show();
	}, function() {
		$(this).toggleClass('active');
		$(this).children('ul').hide();
	});
	
	/* !single vehicle gallery ====================================== */
	$('#singleVehicle .gallery .thumbs a').click(function(){
		return false;
	});
	
	/* !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;
	});
	
	$('.bucket').hover(function(){
		$(this).siblings().stop().animate({opacity: .2}, 500);
	},function(){
		$(this).siblings().stop().animate({opacity: 1}, 500);
	});
	
	$('.bucket').click(function(){
		window.location.replace($(this).children('a').attr('href'));
	});
	
	
	$('.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');
	});
	
	/* pop ups */
/* 	 $("a[rel='pop-up']").colorbox({iframe:true, scrolling:true, width:"90%", height:"90%", innerWidth:"90%"});  */

	/* 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); 
