var timeToNextSlide;

$(document).ready(function() {
	
	/* home page gallery */
	var homeSliders = $("body#home div#space");
	var carSliders = $("body#newcars div#space");
	var galleryOn = false;
	var sliderOn = false;
	var whichPage = "gallery";
	
	if (homeSliders.find("img").length > 1) {
		galleryOn = true;
		whichPage = "home";
	}
	
	if (carSliders.find("img").length > 1) {
		sliderOn = true;
		whichPage = "cars";
	}
	
	$("div#homeGalleryControls a").click(function() {
		var theSlider;
		if (galleryOn) {
			theSlider = homeSliders;
		} else if (sliderOn) {
			theSlider = carSliders;
		}
		if (galleryOn || sliderOn) {
			if ($(this).attr("id") == "bannerNext") {
				$(theSlider).find("a.current").fadeOut();
				if ($(theSlider).find("a.current").next().length > 0) {
					$(theSlider).find("a.current").next().fadeIn().addClass("current");
					$(theSlider).find("a.current").first().removeClass("current");
				} else {
					$(theSlider).find("a.current").removeClass("current");
					$(theSlider).find("a").first().fadeIn().addClass("current");
				}
			} else {
				$(theSlider).find("a.current").fadeOut();
				if ($(theSlider).find("a.current").prev().length > 0) {
					$(theSlider).find("a.current").prev().fadeIn().addClass("current");
					$(theSlider).find("a.current").last().removeClass("current");
				} else {
					$(theSlider).find("a.current").removeClass("current");
					$(theSlider).find("a").last().fadeIn().addClass("current");
				}
			}
			startGallerySlideshow(whichPage);
		}
	});
	
	if (galleryOn) {
		startGallerySlideshow(whichPage);
	}	
	
	/* sub menus */
	var dropDown = $("div#dropDown");
	var subMenus = $("div.subNav");
	
	subMenus.each(function() {
		$(this).hide();
	});
	
	$("div#header ul#menu li").hover(function() {
		var subMenu = $(this).find("div.subNav");
		if (subMenu.length > 0) {
			subMenu.show();
		}
	}, function() {
		subMenus.each(function() {
			$(this).hide();
		});
	});
	
	$("div#header ul#menu2 li").hover(function() {
		var subMenu = $(this).find("div.subNav");
		if (subMenu.length > 0) {
			subMenu.show();
		}
	}, function() {
		subMenus.each(function() {
			$(this).hide();
		});
	});
	
	subMenus.each(function() {
		if (! $("body").hasClass("ie") ) {
			if ($(this).width() % 2 == 1) {
				$(this).width($(this).width() + 1);
			}
		}
	});
	
	$("input.calendarPopupDatePicker").datepicker({
		dateFormat : 'dd/mm/yy',
		maxDate : '+6m',
		minDate : '0'
	});
	
	/*newcars*/
	dropDown.hide();

	$("li#menu_newcars a, div#dropDown").hover(function() {			
		dropDown.show();
	}, function() {
		dropDown.hide();
	});
	
	$("div.carInfo").hide();
	
	$("div#dropDown ul.carLinks li ul li.cars a").hover(function() {
		$(this).find("div.carInfo").show();
	}, function() {
		$(this).find("div.carInfo").hide();
	});
});

function goToSlide(slideID) {
	var slider = $("body#newcars div#space");
	$(slider).find("a.current").fadeOut();
	$(slider).find("a.current").removeClass("current");
	
	if (slideID == "sedans") {
		$(slider).find("a#Sedans").fadeIn().addClass("current");
	} else if (slideID == "coupes") {
		$(slider).find("a#Coupes").fadeIn().addClass("current");
	} else if (slideID == "wagons") {
		$(slider).find("a#Wagons").fadeIn().addClass("current");
	} else if (slideID == "crossovers") {
		$(slider).find("a#Crossovers").fadeIn().addClass("current");
	}
}

function nextSlide() {
	var homeSliders = $("body#home div#space");
	
	if (homeSliders.find("img").length <= 0) {
		homeSliders = $("body#newcars div#space");
	}

	$(homeSliders).find("a.current").fadeOut();
	if ($(homeSliders).find("a.current").next().length > 0) {
		$(homeSliders).find("a.current").next().fadeIn().addClass("current");
		$(homeSliders).find("a.current").first().removeClass("current");
	} else {
		$(homeSliders).find("a.current").removeClass("current");
		$(homeSliders).find("a").first().fadeIn().addClass("current");
	}
	startGallerySlideshow();
}

function startGallerySlideshow(whichPage) {
	if (whichPage != "cars") {
		window.clearTimeout(timeToNextSlide);
		timeToNextSlide = setTimeout(nextSlide, 5000);
	}
}
