/* =============
advanced search area
 ============= */

// if the open search div is present the page will load with the advanced search pre opened
if ( $("#opensearch").length ) {
	$(".refine-search").addClass("search-down");
	$("#search-area-wrapper").css('display','block');
	$("#search-area").css('display','block');
};
 
$('#search-wrapper').find('.refine-search').click(function() {
	stickyslider()
	$("#search-area").css('visibility','visible');
	$("#search-area").hide();

	$(this).parents("#search-wrapper").find("#search-area-wrapper").slideToggle(200);
	$(this).delay(200).toggleClass("search-down");
	$("#search-area").fadeIn(600);
   // setTimeout( function() {
   // $("#search-area-left, #search-area-right").fadeIn(3000);
   // }, 600 );
}); 



/* =============
ui slide bars
 ============= 
updateslider();
function updateslider() {
	$(function(){
		
		//mileage slider start
		$("select#MinPrice_DD option:first").prepend("From ");
		$("select#MinPrice_DD option:not(:first)").prepend("Up to ");
		
		$("#MinPrice_DD").hide();		
		$("#MaxPrice_DD").hide();	
		
		$('select#MinPrice_DD, select#MaxPrice_DD').selectToUISlider({
			labels: 0,
			labelSrc: "text"
		});
		
		//remove the -1 function to stop two £0 values
		$("select#MinPrice_DD option:first, select#MaxPrice_DD option:first").remove();
		// get rid of selected option on max price option
		$("select#MaxPrice_DD option:selected").removeAttr('selected', true);
		// grab last max value and append to min price 
		$('select#MinPrice_DD').append($('select#MaxPrice_DD option:last').clone());
		// grab first min value and prepend to max price 
		$('select#MaxPrice_DD').prepend($('select#MinPrice_DD option:first').clone());
		// set the last value on max price as new selected value
		$("select#MaxPrice_DD option:first").attr('selected', 'selected');
		//mileage slider end

		//price slider start
		$("select#MinPrice1_DD option:first").prepend("From ");
		$("select#MinPrice1_DD option:not(:first)").prepend("Up to ");
		
		$("#MinPrice1_DD").hide();		
		$("#MaxPrice1_DD").hide();		
			
			$('select#MinPrice1_DD, select#MaxPrice1_DD').selectToUISlider({
				labels: 0,
				labelSrc: "text"
			});
		
		// remove the -1 function to stop two £0 values
		$("select#MinPrice1_DD option:first, select#MaxPrice1_DD option:first").remove();
		// get rid of selected option on max price option
		$("select#MaxPrice1_DD option:selected").removeAttr('selected', true);
		// grab last max value and append to min price 
		$('select#MinPrice1_DD').append($('select#MaxPrice1_DD option:last').clone());
		// grab first min value and prepend to max price 
		$('select#MaxPrice1_DD').prepend($('select#MinPrice1_DD option:first').clone());
		// set the last value on max price as new selected value
		$("select#MaxPrice1_DD option:first").attr('selected', 'selected');
		//price slider end
		
		//age slider start
		$("select#MinPrice2_DD option:first").prepend("From ");
		$("select#MinPrice2_DD option:not(:first)").prepend("Up to ");
			
		$("#MinPrice2_DD").hide();		
		$("#MaxPrice2_DD").hide();		
			
		$('select#MinPrice2_DD, select#MaxPrice2_DD').selectToUISlider({
			labels: 0,
			labelSrc: "text"
		});
		
		// remove the -1 function to stop two £0 values
		$("select#MinPrice2_DD option:first, select#MaxPrice2_DD option:first").remove();
		// get rid of selected option on max price option
		$("select#MaxPrice2_DD option:selected").removeAttr('selected', true);
		// grab last max value and append to min price 
		$('select#MinPrice2_DD').append($('select#MaxPrice2_DD option:last').clone());
		// grab first min value and prepend to max price 
		$('select#MaxPrice2_DD').prepend($('select#MinPrice2_DD option:first').clone());
		// set the last value on max price as new selected value
		$("select#MaxPrice2_DD option:first").attr('selected', 'selected');
			
		//fix color 
		fixToolTipColor();
	});
}

//purely for theme-switching demo... ignore this unless you're using a theme switcher
//quick function for tooltip color match
function fixToolTipColor(){
	//grab the bg color from the tooltip content - set top border of pointer to same
	$('.ui-tooltip-pointer-down-inner').each(function(){
		var bWidth = $('.ui-tooltip-pointer-down-inner').css('borderTopWidth');
		var bColor = $(this).parents('.ui-slider-tooltip').css('backgroundColor')
		$(this).css('border-top', bWidth+' solid '+bColor);
	});	
}

*/

/* =============
deal of the day carousel
 ============= */
$(function() {
	$(".carousel").jCarouselLite({
		btnNext: ".scrollleft",
		btnPrev: ".scrollright",
		visible: 6, 
		auto: 4000,
		speed: 1000
    });
});



/* =============
sticky scrolling left
 ============= */
if ($("#left").length){
  stickyslider(topPadding=15);
}
function stickyslider() {
	$(function(){ 
 
		var $sidebar	= $("#left"),
		$window			= $(window),
		offset			= $sidebar.offset()
		//topPadding		= 15;
	
		$window.scroll(function() {
			if ($window.scrollTop() > offset.top) {
				$sidebar.stop().animate({
					marginTop: $window.scrollTop() - offset.top + topPadding
				});
			} else {
				$sidebar.stop().animate({
					marginTop: 0
				});
			}
		});
	});
}


/* =============
add/remove garage
 ============= */
function pageLoad() {
	$(function(){
		var garagestate = $(".RemoveFromGarageButton, .AddToGarageButton").attr("value");
		//alert(garagestate);

		if (garagestate == "ingarage") {
			$(".addgaragebtn").hide();
			$(".rmvgaragebtn").show();
		} else {
			$(".addgaragebtn").show();
			$(".rmvgaragebtn").hide();	
		}
	});
}



/* =============
details page tabs
 ============= */
 $(".tab2").click(function() {
	 $(".tab1").removeClass("detail-tab-selected");
	 $(".tab2").addClass("detail-tab-selected");
	 
	 $(".tab-content-1").addClass("hide");
	 $(".tab-content-2").removeClass("hide");
 });
 
 $(".tab1").click(function() {
	 $(".tab2").removeClass("detail-tab-selected");
	 $(".tab1").addClass("detail-tab-selected");

	 $(".tab-content-2").addClass("hide");
	 $(".tab-content-1").removeClass("hide");
 });
 
 
 
/* =============
details page fancybox hack 12/12/2011
 ============= */
var thumbwidth = 75;
var lightboxwidth = 650;
var thumbclassint = 3; // integer for every nth thumb you want to change class from detailsthumb to detailsthumbend
 
$(window).load (function () {
	var totalImg = $("#detail-slide img").length;	
	var largewidth = $('#detail-slide img:first').width();
	$('#detail-slide').after('<ul id="thumbsnav">').cycle({ 
		fx: 'fade',
		delay: 0,
		timeout: 6000,
		speed: 1000,
		pager:  '#thumbsnav',
		pagerAnchorBuilder: function(idx, slide) { 
			thumbslideid = slide.src.replace(largewidth,thumbwidth);//thumbnail
			thumbslidelinkid = slide.src.replace(largewidth,lightboxwidth);//thumbsnav popup
			var smlPicNum = $("#thumbsnav li").length + 1;
			return '<li><a href="' + thumbslidelinkid + '" rel="detimglightbox" title="Tinymiles ' + smlPicNum + ' of ' + totalImg + '"><img src="' + thumbslideid + '" class="detailsthumb" /></a></li>';
		} 
	});
	
	var lrgDetailsImg = $('#detail-slide img')
	lrgDetailsImg.removeAttr('onclick');
	lrgDetailsImg.css('cursor', 'pointer');

	lrgDetailsImg.each(function(){
		var lrgDetailsImgSrcLink = $(this).attr('src').replace(largewidth,lightboxwidth);
		$(this).wrap("<a href='" + lrgDetailsImgSrcLink + "' rel='detimglightbox' title='Tinymiles " + ($("#detail-slide a").length+1) + " of " + totalImg +"'></a>");
	});
	$("#thumbsnav a, #detail-slide a").fancybox({//thumbsnav popup
		'autoScale'			: false,
		'cyclic': true,//loop gallery
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'overlayShow' : true,
		'overlayColor' : '#000'
	});
	$("#thumbsnav").insertAfter('.insertthumbs');
	$('ul#thumbsnav li:nth-child(' + thumbclassint + 'n) img').removeClass("detailsthumb").addClass("detailsthumbend");
});


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
