$(document).ready(function(){

	$("#icons a, a.hover").hover(
				// over
				function(){
					// store the original image src
					$(this).children(":first-child").attr( "originalsrc" , $(this).children(":first-child").attr("src") );
					
					// add -over to the image unless it is already -over
					if (!$(this).children(":first-child").attr("src").match(/-over/) ) {
						$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/\./, "-over.") );
					}
					
				},
				
				// out
				function(){
					// replace the original src
					$(this).children(":first-child").attr(
						"src", $(this).children(":first-child").attr("originalsrc")
					);
					
					$(this).children(":first-child").attr(
						"originalsrc", null
					);
				}
			);

		// accordian
		$("#nav li ul").hide();
		$("#nav .head").click(function(){
			$("#nav li ul:visible").slideUp("slow");
			$(this).next().slideDown("slow");
			return false;
		});

		// open current section
		if (section && $("#nav_" + section)) {
			$("#nav_" + section).next().show();
		}

		
		// hilight current section
		if (section && subsection && $("#sub_" + section + '_' + subsection)) {
			$("a#sub_" + section + '_' + subsection).addClass('subnav_hilight');
		}
		
	// preload icons
	$("#icons a img").each(
			function() {
				$("<img>").attr( "src", $(this).attr("src"));
				if (!$(this).attr("src").match(/-over/) ) {
					$("<img>").attr( "src", $(this).attr("src").replace(/\./, "-over.") );
				}
			}
		);
		
		
	// figure out where to start carousel
	// work around bug that breaks the scrolling if start is closer to the end than number of visible
	 var visible = 4;
	 var start =  $(".carousel img").index( $(".carousel img[src*='-over']") );
	 var length = $(".carousel img").length;
	 
	 if (length <= visible) {
		 $(".next").addClass("disabled");
	 }
	 
	 if (length - start < visible) {
	 	start = length - visible;
	 	if (start < 0) {
			start = 0;
		}
		$(".next").addClass("disabled");
	 }
		
	if (start > 0) {
		$(".prev").removeClass("disabled");
	}
		
	// init carousel
	 $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		visible: visible,
		circular: false,
		start: start
    });


	// footer showrooms
	var selShowroom;
	$("#showroomNav a").hover(
		// over
		function(){
			var showroom = $(this).attr("href").substr(1);
			
			// show the div
			$("#showrooms .showroom:visible").hide();
			$("#" + showroom).show();
			
			// reset the nav color unless this is the selected on
			if ( $(this).attr("href").substr(1) != selShowroom ) {
				$("#showroomNav a").css("color", "#746B54");
			}
			
			// set hover color
			$(this).css("color", "#000");
			
		},
		
		// out
		function(){
			
			// hide all divs
			$("#showrooms .showroom:visible").hide();
		
			if ( $(this).attr("href").substr(1) == selShowroom ) {
				// reshow the div if it is the selected on
				$("#" + selShowroom).show();
			} else {
				// otherwise reset the colors and selected
				selShowroom = '';
				$("#showroomNav a").css("color", "#746B54");
			}
					
		}
	);
	
	// set the clicked one to be selected and stay on
	$("#showroomNav a").click(function() {
		selShowroom = $(this).attr("href").substr(1);
		return false;
	});
	
});


window.onload = function() {
		// preload
		$("#icons a img, a.hover img").each(
			function() {
				if (!$(this).attr("src").match(/-over/) ) {
					$("<img>").attr( "src", $(this).attr("src").replace(/\./, "-over.") );
				}
			}
		);
}




