var slideshow = {
	srcs: null,
	descrs: null,
	curIndex: -1,
	timer: null,
	loadingImageSrc: "",
	
	init: function() {
		this.srcs = new Array();
		this.descrs = new Array();
		
		var mainImgSrc = document.getElementById("main_img").src;
		
		$("#tmbnls img").each(function(index) {
			slideshow.srcs[index] = this.src;
			if (slideshow.getBigPicSrc(this.src) == mainImgSrc) slideshow.curIndex = index;
			
			slideshow.descrs[index] = this.title;
			this.title = "";
		});
		
		$("#tmbnls img").mouseover(function() {
			slideshow.showHintBox(this.src);
		}).mouseout(function() {
			slideshow.hideHintBox();
		}).mousemove(function(e) {
			slideshow.moveHintBox(e);
		});
		
		this.initPreload();
		
		$("#main_img").css("cursor", "pointer");
		$("#main_img").click(function() {
			slideshow.goNextPic();
		});
		
		$("#main_img_box a:nth-child(1)").click(function(e) {
			e.preventDefault();
			slideshow.goPrevPic();
		});
		
		$("#main_img_box a:nth-child(3)").click(function(e) {
			e.preventDefault();
			slideshow.goNextPic();
		});
		
		$("#tmbnls a").click(function(e) {
			e.preventDefault();
			slideshow.setNewPic($(this).index());
		});
	},
	
	showHintBox: function(imgSrc) {
		var index = this.srcs.indexOf(imgSrc);
		
		if (index == this.curIndex || index < 0) return;

		$("#tmbnl_info_block").html(this.descrs[index]);
		$("#tmbnl_info_container").show();
	},
	
	hideHintBox: function() {
		$("#tmbnl_info_block").html("");
		$("#tmbnl_info_container").hide();
	},
	
	moveHintBox: function(e) {
		if ($("#tmbnl_info_container").is(":visible")) {
			var x = e.pageX;
			var y = e.pageY;

			x += 8;
			y -= 160;

			var w = $(document).width();
			if (x > w - 250) x = w - 250;

			$("#tmbnl_info_container").offset({left: x, top: y});
		}
	},
	
	
	// -------------- Set new pictures --------------
	
	setNewPic: function(picIndex) {
		if (this.isPreloaded(picIndex)) {
			this.onNewPicLoaded(picIndex);
			return;
		}
		
		var loadingImage = new Image();
		loadingImage.onload = function() {
			slideshow.onNewPicLoaded(picIndex);
		};
		this.loadingImageSrc = this.getBigPicSrc(this.srcs[picIndex]);
		loadingImage.src = this.loadingImageSrc;

		this.timer = setTimeout("slideshow.showPreloadBlock()", 300);
	},
	
	getBigPicSrc: function(smPicSrc) {
		return smPicSrc.replace("/smsm/", "/");
	},
	
	goNextPic: function() {
		this.preloadDirection = 1;
		
		if (this.curIndex == this.srcs.length - 1)
			this.setNewPic(0);
		else
			this.setNewPic(this.curIndex + 1);
	},

	goPrevPic: function() {
		this.preloadDirection = -1;
		
		if (this.curIndex == 0)
			this.setNewPic(this.srcs.length - 1);
		else
			this.setNewPic(this.curIndex - 1);
	},
	
	onNewPicLoaded: function(picIndex) {
		if (this.timer) clearTimeout(this.timer);
		
		$("#main_img").attr("src", this.getBigPicSrc(this.srcs[picIndex]));
		$("#pic_descr").html(this.descrs[picIndex]);
		
		this.curIndex = picIndex;

		this.updateThumbnails();
		
		$("#img_preload_box").hide();
		$("#img_preload_box img").attr("src", "");
		
		document.title = this.descrs[picIndex];
		
		this.preloadedSrcs.push(picIndex);
		this.preloadPicture();
	},
	
	updateThumbnails: function() {
		var num = this.curIndex + 1;
		$("#tmbnls a").removeClass("cur_tmbnl").css("cursor", "pointer");
		$("#tmbnls a:nth-child(" + num.toString() + ")").addClass("cur_tmbnl").css("cursor", "default");
	},
	
	showPreloadBlock: function() {
		clearTimeout(this.timer);

		if (this.loadingImageSrc) {
			$("#img_preload_box img").attr("src", this.loadingImageSrc);
			$("#img_preload_box").show();
		}
	},
	
	
	// -------------- Preload --------------
	
	preloadedSrcs: null,
	preloadDirection: 1,
	
	initPreload: function() {
		this.preloadedSrcs = new Array();
		this.preloadedSrcs.push(this.curIndex);
		this.preloadPicture();
	},
	
	preloadPicture: function() {
		if (this.preloadDirection == 0) return;
		
		this.doPreload(this.curIndex + this.preloadDirection);
		
		this.preloadDirection = 0;
	},
	
	doPreload: function(preloadPictureIndex) {
		if (preloadPictureIndex < 0 || preloadPictureIndex >= this.srcs.length) return;
		
		if (!this.isPreloaded(preloadPictureIndex)) {
			var preloadingImage = new Image();
			preloadingImage.src = this.getBigPicSrc(this.srcs[preloadPictureIndex]);
		}
	},
	
	isPreloaded: function(preloadPictureIndex) {
		if (this.preloadedSrcs == null) return false;
		return ($.inArray(preloadPictureIndex, this.preloadedSrcs) != -1);
	}
};

$(document).ready(function() {
	slideshow.init();
});

