$(document).ready(function(){


$('.bar').each(function(){
	$(this).width($(this).parent().outerWidth());
});

var thumbCount = $('.thumb').length;
var thumbRows = Math.ceil(thumbCount / 4);
var clickLimit = Math.ceil(thumbRows / 2) - 1;
var whichImg = 0;
var whichImgLimit = thumbCount - 1;
var clickCount = 0;
var slideHeight = $('.thumbs .thumb:first').height() + 2 + 4;

//setTimeout("$('.thumbs').height("+slideHeight+" * 2);",500);

$.borderChange = function(which){
	$('.thumb').css('border', '1px solid #262626');
	$('.thumb:eq(' + which + ')').css('border', '1px solid #f64a1a');
	var superP = $('.thumb:eq(' + which + ')').attr('super');
	if (superP != '') {
		$('.imgControls .center').show();
		$('.imgControls .center a').attr('href', superP + '?TB_iframe=flase&height=480&width=640');
		$('#mainImg').parent('a').attr('href', superP + '?TB_iframe=flase&height=480&width=640');
	}
	else{
		$('.imgControls .center').hide();
	}
	
};

$.borderChange(0);

$('.thumbs .thumb').data('clicked','false').wrapAll('<div id="moving"></div>');
$('.thumbs .thumb:first').data('clicked','true');

$.thumbsDown=function(){
	$('#moving').animate({
		'top':'-='+(slideHeight * 2)+'px'
	},800);
};

$.thumbsUp=function(){
	$('#moving').animate({
		'top':'+='+(slideHeight * 2)+'px'
	},800);
};

$('.thumbControls .floatRight').bind('click',function(){
	slideHeight = $('.thumbs .thumb:first').height() + 2 + 4;
	if (clickCount < clickLimit) {
		clickCount++;
		$.thumbsDown();
	}
});

$('.thumbControls .floatLeft').bind('click',function(){
	slideHeight = $('.thumbs .thumb:first').height() + 2 + 4;
	if (clickCount > 0) {
		clickCount--;
		$.thumbsUp();
	}
});

$('.thumbs .thumb').bind('click',function(){
	var complete = false;
	if($('.main .loadingBar').is(':not(:animated)')){
		if ($(this).data('clicked') == 'false') {
			$(this).data('clicked','true');
			
			var larger= $(this).attr('larger');
			$('.inventoryHolder .invis:eq(0) img').attr('src', larger).bind('load',function(){complete = true;});
			
			whichImg = $('.thumbs .thumb').index(this);
			
			$.runBar = function(){
				$('.main .loadingBar').width(1).show().animate({
					width: '400px'
				}, 1200, function(){
					if(complete == true || $('.inventoryHolder .invis:eq(0) img').complete){
						$('#mainImg').attr('src', larger);
						$('.main .loadingBar').fadeOut(300);
					}
					else{
						$.runBar();
					}
				});
			};
			
			$.runBar();
			
			$('#mainImg').attr('super', $(this).attr('super'));
			$.borderChange($('.thumbs .thumb').index(this));
		}
		else{
			$('#mainImg').attr('src', $(this).attr('larger'));
			whichImg = $('.thumbs .thumb').index(this);
			$('#mainImg').attr('super', $(this).attr('super'));
			$.borderChange($('.thumbs .thumb').index(this));
		}
	}
});

$('.imgControls .left').bind('click',function(){
	if (whichImg > 0) {
		whichImg--;
	}
	else{
		whichImg = whichImgLimit;
	}
	$('.thumbs .thumb:eq(' + whichImg + ')').click();
	$.borderChange(whichImg);
});

$('.imgControls .right').bind('click',function(){
	if (whichImg < whichImgLimit) {
		whichImg++;
	}
	else{
		whichImg = 0;
	}
	$('.thumbs .thumb:eq(' + whichImg + ')').click();
	$.borderChange(whichImg);
});

$.detailClick = function(){
	$('#moreDetails').toggle(function(){
		$('#moreChecks').slideDown(700);
		$(this).text('View Less Details');
		return false;
	},function(){
		$('#moreChecks').slideUp(700);
		$(this).text('View More Details');
		return false;
	});
};

if ($('.inventoryHolder .leftCheck').length > 24){
	$('.inventoryHolder .leftCheck:gt(23)').wrapAll('<div id="moreChecks" style="display:none;clear:left;"></div>');
	$('.inventoryHolder .details').append('<div style="width:100px;position:absolute;left:50%;margin-left:-50px;bottom:5px;"><a href="#" id="moreDetails">View More Details</a></div>');
	$.detailClick();
};



$('.inventoryHolder .other:last').css('margin-right','0px');

$('.inventoryHolder .detailChunk .title').toggle(function(){
	$(this).css('background-position','0px -24px');
	$(this).next('.rows').toggle(600);
},function(){
	$(this).css('background-position','0px 0px');
	$(this).next('.rows').toggle(600);
});

$('.inventoryHolder .detailChunk .rows').each(function(){
	$(this).find('p:odd').css('background-color','#dddddd');
});

$('.inventoryHolder #expandAll').toggle(function(){
	$(this).text('hide all details');
	$('.inventoryHolder .detailChunk .title').click();
},function(){
	$(this).text('expand all details');
	$('.inventoryHolder .detailChunk .title').click();
});

var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

if (jQuery.browser.msie && (ie55 || ie6)) {
	
	$('.lower').each(function(){
		var top = $(this).parent().outerHeight();
		$(this).css('top',top+'px');
	});

};

var pictureBox = $('.pictureBox');
var infoBox = $('.infoBox .box');
var form = $('#contact');

if(pictureBox.is(':visible') && infoBox.is(':visible')){
	
	if(infoBox.height() > pictureBox.height()){
		
		var diff = infoBox.height() - pictureBox.height() -21;
		pictureBox.height(pictureBox.height() + diff);
		
		if (jQuery.browser.msie && (ie55 || ie6)) {		
			$('.lower').each(function(){
				var top = $(this).parent().outerHeight();
				$(this).css('top',top+'px');
			});
		}

}else{
		
		var diff = pictureBox.height() - infoBox.height() + 20;
		infoBox.height(infoBox.height() + diff);
		
		if (jQuery.browser.msie && (ie55 || ie6)) {		
			$('.lower').each(function(){
				var top = $(this).parent().outerHeight();
				$(this).css('top',top+'px');
			});		
		};

	};
};
});

var compare = new Object();
compare.photos = new Array();
compare.vehicle = new Array();
compare.price = new Array();
compare.mileage = new Array();
compare.transmission = new Array();
compare.engine = new Array();
compare.drivetrain = new Array();
compare.color = new Array();
compare.seller = new Array();
compare.remove = new Array();

$('.inventoryHolder .comparecolumn').each(function(){
	compare.photos.push($(this).find('.cell:eq(0)').html());
	compare.vehicle.push($(this).find('.cell:eq(1)').html());
	compare.price.push($(this).find('.cell:eq(2)').html());
	compare.mileage.push($(this).find('.cell:eq(3)').html());
	compare.transmission.push($(this).find('.cell:eq(4)').html());
	compare.engine.push($(this).find('.cell:eq(5)').html());
	compare.drivetrain.push($(this).find('.cell:eq(6)').html());
	compare.color.push($(this).find('.cell:eq(7)').html());
	compare.seller.push($(this).find('.cell:eq(8)').html());
	compare.remove.push($(this).find('.cell:eq(9)').html());
});

for($c=1;$c<5;$c++){
	$('.inventoryHolder #compareTable tr:eq(0) td:eq('+$c+')').html(compare.photos[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(1) td:eq('+$c+')').html(compare.vehicle[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(2) td:eq('+$c+')').html(compare.price[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(3) td:eq('+$c+')').html(compare.mileage[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(4) td:eq('+$c+')').html(compare.transmission[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(5) td:eq('+$c+')').html(compare.engine[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(6) td:eq('+$c+')').html(compare.drivetrain[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(7) td:eq('+$c+')').html(compare.color[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(8) td:eq('+$c+')').html(compare.seller[$c-1]);
	$('.inventoryHolder #compareTable tr:eq(9) td:eq('+$c+')').html(compare.remove[$c-1]);
};

$('.inventoryHolder #compareTable tr').filter(':not(:last)').filter(':not(:first)').css('background','transparent url("img/tr_bg.jpg") top left repeat-x');
$('.inventoryHolder #compareTable tr:odd').filter(':not(:last)').css('background','#efefef url("") top left repeat-x');
$('.inventoryHolder #compareTable td:empty').fadeTo(300,0.4);
