// slideshow 
function processSlideshow(elem, imageList, imageDuration, fadeSpeed, current) {
	var listSize = imageList.length;
  if (!current || current >= listSize) current = 0;
  if (!imageDuration) imageDuration = 2000;
  if (!fadeSpeed) fadeSpeed = 1000;
  $(elem + " img").attr("src", imageList[current]);
  if (current == (listSize - 1)) { $(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
  } else {
      $(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
  }
  $(elem + " img").animate({ opacity: "1" }, imageDuration).
     animate({ opacity: "0.01" }, fadeSpeed, function() 
   { 
     $(this).css("opacity", "1"); processSlideshow(elem, imageList, imageDuration, fadeSpeed, current + 1) 
  });
} 

$(function () {
	// add styles for forms
	$('input[type=text], textarea').addClass('textfield');

  // GA tracking on external links
// 	$("a[href^=http]").click(function(){ 
// 	  if(pageTracker) pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));
// 	});

  var slideshow_cache = [];
	var slideShow = new Array();
	
	if($('#homepage_slideshow').length) {
		for(var s=1; s <=12; s++) {
			var slide_uri = "/images/slideshow/" + s + ".jpg";
	    var cacheImage = document.createElement('img');
	    cacheImage.src = slide_uri;
	    slideshow_cache.push(cacheImage);
			slideShow[slideShow.length] = slide_uri;
		}	
	  processSlideshow("#homepage_slideshow", slideShow, 4000, 1000);
	}
	
  // initialize scrollable 
  $("div.scrollable").scrollable({size: 3}); 
	$("#vehicle_thumbnails .items img").click(function() { 

	    // calclulate large image's URL based on the thumbnail URL (flickr specific) 
	    var url = $(this).attr("src").replace("small", "large"); 

	    // the large image from flickr 
	    var img = new Image(); 

	    // call this function after it's loaded 
	    img.onload = function() { 

	        // change the image 
	        $("#vehicle_large_photo img").attr("src", url); 

	    }; 

	    // begin loading the image from flickr 
	    img.src = url; 

	// when page loads simulate a "click" on the first image 
	}).filter(":first").click();
	

});
