$(document).ready(function(){


$('.click-clear').click(function(){
    if (this.value == this.defaultValue) {
    	this.value = '';
    	}
    }
);

$('.click-clear').blur(function(){
    if (this.value == '') {
    	this.value = this.defaultValue;
    	}
    }
);
			
var bar = $('.oneClick');
var upperLimit = 23;
var lowerLimit = $('.results').outerHeight() - bar.outerHeight() - 23;

if(lowerLimit < 0){
    lowerLimit = bar.outerHeight();
}

if($('.oneClick').length){		
	var bar = $('.oneClick');
	var upperLimit = 23;
	var lowerLimit = $('.results').outerHeight() - bar.outerHeight() - 23;
	
	if(lowerLimit < 0){
	    lowerLimit = bar.outerHeight();
	}
	
	var barOffset = $('.oneClick').offset();
	var scrollStart = barOffset.top - 23;
	
	$(window).scroll(function(){
	    var theScroll = $(this).scrollTop();
	
	    if((theScroll > scrollStart) && (theScroll < (scrollStart + lowerLimit))){
	    	bar.stop().animate({
	    		marginTop: (upperLimit + (theScroll - scrollStart))+'px'
	    	},300);
	    }
	    else if(theScroll >= (scrollStart + lowerLimit)){
	    	bar.stop().animate({
	    		marginTop: lowerLimit + 23+'px'
	    	},300);
	    }
	    else{
	    	bar.stop().animate({
	    		marginTop: upperLimit+'px'
	    	},300);
	    }
	});
}

$('.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();
	}
});

//Removed 2/12/10 dk
/*$('.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));
		}
	}

});*/

//Replaced with 2/12/10 dk
$('.thumbs .thumb').bind('click',function(){
	$('#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(){
		$('.inventoryHolder .leftCheck:gt(23)').show(700);
		$(this).text('View Less Details');
		return false;
	},function(){
		$('.inventoryHolder .leftCheck:gt(23)').hide(700);
		$(this).text('View More Details');
		return false;
	});
};

if ($('.inventoryHolder .leftCheck').length > 24){
	$('.inventoryHolder .leftCheck:gt(23)').hide();
	$('.inventoryHolder .options').append('<div style="width:100px;position:absolute;left:50%;margin-left:-50px;bottom:5px;"><a href="#" id="moreDetails">View More Details</a></div>');
	$.detailClick();
};*/

$.detailClick = function(n){
      $('#moreDetails').toggle(function(){
            $('.inventoryHolder .leftCheck:gt('+n+')').show(700);
            $(this).text('View Less Details');
            return false;
      },function(){
            $('.inventoryHolder .leftCheck:gt('+n+')').hide(700);
            $(this).text('View More Details');
            return false;
      });
};
 
if ($('.inventoryHolder .leftCheck').length > 25){
      $('.inventoryHolder .options .leftCheck:gt(0)').filter(function(){
            return ($('.inventoryHolder .details .leftCheck').index($(this))%5==0);
            }).before('<div style="clear:left;overflow:hidden;height:1px;"></div>');
      $('.inventoryHolder .leftCheck:gt(24)').hide();
      //$('.inventoryHolder .options').css('paddingBottom','10px').append('<div style="width:90%;position:static;text-align:center;"><a href="#" id="moreDetails">View More Details</a></div>');
	  $('.inventoryHolder .options').css('paddingBottom','20px').append('<div style="width:90%;position:static;text-align:center;"><a href="#" id="moreDetails">View More Details</a></div>');
      $.detailClick(24);
};

$('.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');
        var titles = $(this).parent('h3').parent('.details').find('.title');
        $(titles).css('background-position','0px -24px');
        $(titles).next('.rows').show(600);
},function(){
        $(this).text('expand all details');
        var titles = $(this).parent('h3').parent('.details').find('.title');
        $(titles).css('background-position','0px 0px');
        $(titles).next('.rows').hide(600);
});

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');
var form = $('#contact');

if($('#contact2').length > 0){
	var leftHeight = $('.lhpane').outerHeight();
	var rightHeight = infoBox.outerHeight();
	var difference = leftHeight - rightHeight;
	var oldHeight = $('.infoBox .box').height();
	var newHeight = Number(difference + oldHeight);
	$('.infoBox .box').height(newHeight);
	
	
		if (jQuery.browser.msie && (ie55 || ie6)) {
		
			$('.lower').each(function(){
				var top = $(this).parent().outerHeight();
				$(this).css('top',top+'px');
			});
		
		};

}else if(pictureBox.is(':visible') && infoBox.is(':visible') && form.is(':visible')){
	if(infoBox.height() > pictureBox.height() + form.height()){
		var picAdjust = (infoBox.innerHeight() - pictureBox.innerHeight()) - form.height() + 21;
		
		pictureBox.height(pictureBox.height() + picAdjust);
		
		if (jQuery.browser.msie && (ie55 || ie6)) {
		
			$('.lower').each(function(){
				var top = $(this).parent().outerHeight();
				$(this).css('top',top+'px');
			});

		};
	}else{
		var infoAdjust = (pictureBox.innerHeight() + form.height() - infoBox.innerHeight()) + 21;
		var box = $('.infoBox .box');
		var offs = box.position();
		var upper = offs.top;
		
		infoBox.height(infoBox.height() + infoAdjust);
		
		$('.infoBox .box').height(infoBox.height() - upper - 15);
		
		if (jQuery.browser.msie && (ie55 || ie6)) {
		
			$('.lower').each(function(){
				var top = $(this).parent().outerHeight();
				$(this).css('top',top+'px');
			});
		
		};

	};
};
$.tbW=function(img, onClr, offClr){
	var wrapWidth=($('#TB_Image').width()-$('#TB_closeWindow').outerWidth());
	$('#TB_caption').css({'width':wrapWidth+'px','height':'51px','overflow':'hidden','marginLeft':'15px','padding':'7px 0px 10px 0px'});
	$('#TB_secondLine').css({'width':(wrapWidth-40)+'px','height':'51px','padding':'0px 20px','position':'relative'});
	$('.thumbs #moving img').each(function(){
		$('#TB_secondLine').append('<img src="'+$(this).attr('src')+'" class="tbThumb" super="'+$(this).attr('super')+'" width="87" />');
	});
	$('#TB_secondLine').advancedSlider({
		hotSpots:new Array(0,0,20,50),
		targets:'.tbThumb'
		});
	$('#TB_secondLine div:not(".list")').eq(1).css('background','#fff url('+img+') no-repeat left top');
	$('#TB_secondLine div:not(".list")').eq(2).css('background','#fff url('+img+') no-repeat right top');
	$('#TB_secondLine img.tbThumb').css({'cursor':'pointer','width':'65px','marginBottom':'0px','border':'1px '+offClr+' solid','vertical-align':'top'}).bind('click',function(){
		$('#TB_secondLine img.tbThumb').css('borderColor',offClr);
		$(this).css('borderColor',onClr);
		$('#TB_Image').attr('src',$(this).attr('super'));
	});
	if($('#TB_Image').length > 0){
		imgSrc=$('#TB_Image').attr('src').toString();
		srcStub=imgSrc.substring(imgSrc.lastIndexOf('/')+1,imgSrc.lastIndexOf('?'));
		itm=$('#TB_secondLine img.tbThumb').filter(function(){
			return ($(this).attr('src').indexOf(srcStub)!=-1);
		}).css('borderColor',onClr);
		$('#TB_secondLine .list').prepend($(itm));
	}
};
var tbChecker;
$.checkTB = function(){
	if($('#TB_Image').height() > 100){
		$.tbW("http://ww3.ldvis.com/common/inventory_manager/templates/tmpl_1/img/tb_scroll_bg.gif","#f31047","#262626");
		tbChecker = window.clearInterval(tbChecker);
	}
}
$('.pictureBox .main .thickbox, .pictureBox .imgControls .thickbox').click(function(){
	tbChecker = window.setInterval('$.checkTB()',500);
});

//Removed 2/12/2010 dk
/*if($('.thumbs #moving img').length > 1){
    var whichAuto = 1;
    var autoCount = $('.lhpane .thumb').length - 1;
    
    $.autoThumb=function(){
    	$('.lhpane .thumb:eq('+whichAuto+')').click();
    	doAuto = setTimeout("$.autoThumb();",5000);
    	if(whichAuto < autoCount){
    		whichAuto++;
    	}else{
    		whichAuto = 0;
    	}
    };
    
    $('.lhpane .thumb, .lhpane .imgControls div').bind('click',function(){
    	clearInterval(doAuto);
    });
    
    if($('.lhpane').length > 0){
    	doAuto = setTimeout("$.autoThumb();",5000);
    }
}*/

});

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);

function emptyField(sel){
	if (sel == 'manufacturers'){
		$('#manufacturers').val('');
		document.getElementById('manufacturers').selectedIndex = 0;
		$('#models').val('');
		document.getElementById('models').selectedIndex = 0;
		$('#trims').val('');
		document.getElementById('trims').selectedIndex = 0;
		$('#body_style_id').val('');
		document.getElementById('body_style_id').selectedIndex = 0;
		$('#stock').val('');
		document.getElementById('stock').selectedIndex = 0;
		$('#year').val('');
		document.getElementById('year').selectedIndex = 0;
		$('#min_year').val('');
		document.getElementById('min_year').selectedIndex = 0;
		$('#max_year').val('');
		document.getElementById('max_year').selectedIndex = 0;
		$('#min_price').val('');
		document.getElementById('min_price').selectedIndex = 0;
		$('#max_price').val('');
		document.getElementById('max_price').selectedIndex = 0;
	}
	if (sel == 'models'){
		$('#models').val('');
		document.getElementById('models').selectedIndex = 0;
		$('#trims').val('');
		document.getElementById('trims').selectedIndex = 0;
		$('#body_style_id').val('');
		document.getElementById('body_style_id').selectedIndex = 0;
		$('#stock').val('');
		document.getElementById('stock').selectedIndex = 0;
		$('#year').val('');
		document.getElementById('year').selectedIndex = 0;
		$('#min_year').val('');
		document.getElementById('min_year').selectedIndex = 0;
		$('#max_year').val('');
		document.getElementById('max_year').selectedIndex = 0;
		$('#min_price').val('');
		document.getElementById('min_price').selectedIndex = 0;
		$('#max_price').val('');
		document.getElementById('max_price').selectedIndex = 0;
	}
	if (sel == 'trims'){
		$('#trims').val('');
		document.getElementById('trims').selectedIndex = 0;
		$('#body_style_id').val('');
		document.getElementById('body_style_id').selectedIndex = 0;
		$('#stock').val('');
		document.getElementById('stock').selectedIndex = 0;
		$('#year').val('');
		document.getElementById('year').selectedIndex = 0;
		$('#min_year').val('');
		document.getElementById('min_year').selectedIndex = 0;
		$('#max_year').val('');
		document.getElementById('max_year').selectedIndex = 0;
		$('#min_price').val('');
		document.getElementById('min_price').selectedIndex = 0;
		$('#max_price').val('');
		document.getElementById('max_price').selectedIndex = 0;
	}
}

function updateSearch(){
	var link = 'http://' + $('#root_url').val();
	link += '/inventory/listing';
	link += '/' + $('#is_used').val();
	link += '/1';
	if (($('#min_year').val() != '' || $('#max_year').val() != '')){
		link += '/' + $('#min_year').val() + '-' + $('#max_year').val();
	}else{
		if ($('#year').val() != '' && $('#year').val()){
			link += '/' + $('#year').val();
		}else{
			link += '/xxxx';
		}
	}
	link += '/' + $('#manufacturers').val();
	link += '/' + $('#models').val();
	link += '/' + $('#trims').val();
	if ($('#body_style_id').val() != '' && $('#body_style_id').val()) link += '&body_style_id=' + $('#body_style_id').val();
	if ($('#stock').val() != '' && $('#stock').val()) link += '&stock=' + $('#stock').val();
	if ($('#min_price').val() != '' && $('#min_price').val()) link += '&min_price=' + $('#min_price').val();
	if ($('#max_price').val() != '' && $('#max_price').val()) link += '&max_price=' + $('#max_price').val();
	if ($('#max_mileage').val() != '' && $('#max_mileage').val()) link += '&max_mileage=' + $('#max_mileage').val();
	if ($('#sortBy').val() != '' && $('#sortBy').val()) link += '&sort_order=' + $('#sortBy').val();
	window.location = link;
	return false;
};