var groomingOptions = window.groomingOptions = {
	retailFadeDuration: 700,
	slideShowDelay: 3000,
        slideShowFadeOut:1000,
	slideShowFadeIn: 1000,
	slideShowInitialDelay: 2000,
	dropBoxDelay: 1000,
        dropBoxDownDuration: 4000,
        dropBoxUpDuration: 2000
};

$(document).ready(function(){
   var getGalleryPics = function(){
	var images = [];
	$("#pics > a[rel=lightbox[tgr]], .gallery-holder > a[rel=lightbox[tgr]], #pics_container > a[rel=lightbox[tgr]] ").each(function(){
		images.push([$(this).attr("href"), $(this).attr("title")]);
	});
	return images;
   }
   $("a[rel^=lt:]").click(function(){ 
	var $a = $(this);
	$.slimbox([[$a.attr("href"), "<span class=\"lbtitle\">" + $a.attr("title") + "<br />" + $a.attr("rel").substring(3) + "</span>"]],0,{});
	return false;
   });
   $("a[rel^='lightbox']").slimbox({loop:true/* Put custom options here */}, null, function(el) {
			var result =  (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
			return result;
   });
   $("#gallery-menu, .photo-gallery-link").click(function(){
	var rel = $(this).attr("rel") * 1;
	if ((typeof rel == undefined) || rel <= 0 || rel ==NaN) rel = 0;
	var images = getGalleryPics();
	$.slimbox(images, rel, {loop:true});
	return false;
   });
   var slideshowCache = [];
   var cachedPics = [];
   $(".slideshow").each(function(){
	var images = getGalleryPics();
	var currentImage = 0;
	var nextImage = 1;
	var slideshow = $(this);
	var preload = function(src) {
	    for(var i = 0;i<cachedPics.length;++i) if (cachedPics[i] == src) return;
	    cachedPics.push(src);
	    cachedPics.push($("<img></img>").attr("src", src));	
	}
	//preload the 2nd picture straight away:
	preload(images[nextImage][0].replace(".jpg", "_thumb.jpg").replace("images/", "images/thumbs/"));
	
	var switchImage = function() {
		currentImage++;
		if (currentImage >= images.length) currentImage = 0;
		nextImage++;
		if (nextImage >= images.length) nextImage = 0;
		var src = images[currentImage][0].replace(".jpg", "_thumb.jpg").replace("images/", "images/thumbs/");
		var nextSrc = images[nextImage][0].replace(".jpg", "_thumb.jpg").replace("images/", "images/thumbs/");
		slideshow.find("img").attr("src", src);	
		preload(nextSrc);
	}
	var changeImage = function(delay, outDuration, inDuration) {
		window.setTimeout(function(){
			var $target = slideshow.find("img");
			$target.fadeOut(outDuration, function(){ switchImage(); $target.fadeIn(inDuration, function(){ changeImage(delay, outDuration, inDuration); }); });
		}, delay);
	}
	window.setTimeout(function() { changeImage(groomingOptions.slideShowDelay,groomingOptions.slideShowFadeOut,groomingOptions.slideShowFadeIn); }, groomingOptions.slideShowInitialDelay);
   });
   var retailSpeed = groomingOptions.retailFadeDuration;
   var activeImage = "img_retail0";
   var $activeLink = $("#retail0");
   if ($("#img_retail1").length > 0) {
	   var transitionImage = function(oldPic, newPic) {
		if (oldPic) {
			$("#"+oldPic).fadeOut(retailSpeed, function(){if (newPic) {$("#"+newPic).fadeIn(retailSpeed); activeImage = newPic;}});
		}
		else if (newPic) { 
			$("#"+newPic).fadeIn(retailSpeed);
			activeImage=newPic;
		}
	   }
	   var $actives = $("#pics_container img.active");
	   if ($actives.length > 0) 
		activeImage = $($actives[0]).attr("id");
	   $activeLink = $("#"+activeImage.substring(4));
           if ($("#"+ activeImage).length>0) {
		$("#"+ activeImage).css("opacity", 1).css("display", "block");
		$activeLink.addClass("active");
	   }
	   $("#retail_links a").mouseenter(function(){ 
		$activeLink.removeClass("active"); 
		$(this).addClass("active"); 
		$activeLink = $(this); 
		var newImage = 'img_'+$(this).attr("id");
		transitionImage(activeImage, newImage);
	   }).mouseleave(function(){
		$activeLink.removeClass("active");
		transitionImage(activeImage, null);
		$activeLink = $([]);
		activeImage = "";
	   });
   }
});
hasIE6 = false;

