$(document).ready(function(){
	// offers

	$("#gallery_images .container:not(:first)").hide();
	$("#gallery_images").show();
	var images = $("#gallery_images .container");
	if ( images.length < 2 ) {
		return;
	}
	var index = 0;
	setInterval(function(){
		$("#gallery_images .container:nth(" + index + ")").fadeOut();
		var nextIndex = index + 1;
		if ( nextIndex >= images.length ) {
			nextIndex = 0;
		}
		$("#gallery_images .container:nth(" + nextIndex + ")").fadeIn();
		index++;
	}, 1000);
});

// galleries
$(document).ready(function(){
	var open = false;

	var fancybox = function(scope) {
		$("a", scope).fancybox({
			overlayColor: "#000",
			overlayOpacity: 0.9,
			transitionIn: "elastic",
			titleFormat: function(title, currentArray, currentIndex, currentOpts) {
				return '<table cellspacing="0" cellpadding="0" id="fancybox-title-float-wrap"><tbody><tr><td id="fancybox-title-float-left"></td><td id="fancybox-title-float-main">'
				+ (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '')
				+ '</td><td id="fancybox-title-float-right"></td></tr></tbody></table>';
			},
			onStart: function(){
				open = true;
			},
			onClosed: function(){
				open = false;
			}
		});
	};

	$(".gallery").each(function(){
		var images = $("img", this);
		if ( images.length < 2 ) {
			$("a", this).removeAttr("href");
			return;
		}
		var over = false;
		$(this).hover(function(){
			over = true;
		}, function(){
			over = false;
		});
		var current = $(images[0]);
		setInterval(function(){
			if ( over || open ) {
				return;
			}
			current.fadeOut(function(){
				current = current.parent().next().children();
				if ( current.length < 1 ) {
					current = $(images[0]);
				}
				current.fadeIn();
			});
		}, 7000);

		fancybox(this);
	});

	$(".fancy").each(function(){
		fancybox(this);
	});

	$(".small-gallery img")
		.css("opacity", 0.67)
		.hover(function(){
			$(this).fadeTo('fast', 1);
		}, function(){
			$(this).fadeTo('fast', 0.67);
		});

//	$(".small-gallery").each(function(){
//		var gallery = this;
//		var images = $("img", this);
//		var mod = images.length;
//		var rows = Math.ceil(images.length/3);
//		var row = 0;
//		var index = 0;
//		setInterval(function(){
//			var rotate = function(eq){
//				var img = $("img:eq(" + eq + ")", gallery);
//				img.fadeOut(600);
//			};
//			rotate(index++%mod);
//			setTimeout(function(){
//				rotate(index++%mod);
//			}, 600);
//			setTimeout(function(){
//				rotate(index++%mod);
//			}, 1200);
//		}, 1800);
//	});
});
