var imageRotateTimeout = 5 * 1000;
var imageRotate = null;
var fadeSpeed = 300;

var showNextImage = function() {
	var nextImage = $(".photorotatordiv li.active").next("li");
	if(!nextImage.length) {
		nextImage = $(".photorotatordiv li").first();
		nextImage.show();
		$(".photorotatordiv li.active").fadeOut(fadeSpeed,function() {
			$(".photorotatordiv li.active").removeClass("active");
		});
		nextImage.addClass("active");
	} else {
		nextImage.fadeIn(fadeSpeed,function() {
			$(".photorotatordiv li.active").hide();
			$(".photorotatordiv li.active").removeClass("active");
			$(this).addClass("active");
		});
	}
	resetImageRotate();
}
var resetImageRotate = function() {
	clearTimeout(imageRotate);
	imageRotate = setTimeout(showNextImage,imageRotateTimeout);
}

var currentPictureClick = function() {
	var img = $(this).attr("src").split("?");
	img = img[1].split("&");
	var tmpImg = "";
	for(var x in img) {
		tmpImg = img[x].split("=");
		if(tmpImg[0] == "img") break;
	}
	if(tmpImg[0] == "img") {
		window.open("/images/photos/" + tmpImg[1]);
	}
}
var showImage = function() {
	$(this).parent().fadeIn();
}
$(document).ready(function() {
	if($(".photogallery").length) {
		$(".photogallery a").colorbox({
			next: "Next Image",
			previous: "Previous Image",
			current: "Image {current} of {total}",
			close: "Close",
			pictureTitle: "View Full Size",
			rel: "gallery",
			photo: "true",
			preloading: true,
			slideshow: true,
			slideshowAuto: false,
			slideshowSpeed: 5000,
			slideshowStart: "Start Slideshow",
			slideshowStop: "Stop Slideshow"
		}).hide();
		$(".photogallery a img").load(showImage);
	}
	if($(".photorotator").length) {
		$.get("/images/rotator/rotator.php",null,function(response) {
			$(".photorotator").replaceWith(response);
			resetImageRotate();
		});
	}
});
