﻿
var photoShow =
{
	viewer: null,
	imgLoader: null,
	imgSub: null,
	img: null,
	index: 0,
	list: [],
	path: page.href("~/lib/photoshow/"),
	animating: false,


	H: 9,
	W: 16,

	onReady: function () {
		this.init();
		this.isready = true;
		this.onResize();
	},

	init: function () {
		if (this.inited)
			return;
		this.inited = true;
		if (this.list.length == 0)
			return;
		var t = this;
		var v = this.viewer = GE("photoViewer");
		v.style.visibility = "hidden";

		function IMG(i) {
			var img = CE("IMG");
			v.appendChild(img);
			if (i) {
				img.src = t.path + t.list[i];
			}
			return img;
		}

		var img = this.img = IMG();
		this.imgLoader = IMG(); var s = this.imgLoader.style; s.height = s.width = "100px"; s.top = s.left = "-300px"; s.zIndex = "1000";
		this.imgLoader.onload = function () {
			photoShow.loading = false;
			if (!photoShow.fadingOut) {
				img.src = this.src;
				$(img).fadeIn("normal");
			}
		};

		this.img.onload = function () {
			this.onload = null;
			this.parentNode.style.visibility = "";
		};

		v.onclick = function (ev) {
			if (page.hideContent()) {
				if (ev)
					var event = ev;
				if (event && event.clientX < this.offsetWidth / 3) {
					photoShow.movePrev();
				}
				else {
					photoShow.moveNext();
				}
			}
		}
		v.onmouseover = function () {

		}
		v.onmouseout = function () {
		}

		var isForward = true;

		this.prepareNext = function () {

		};

		this.preparePrev = function () {

		}

		t.showImage(0);
	},

	moveNext: function () {
		this.showImage(this.index + 1);
	},

	movePrev: function () {
		this.showImage(this.index - 1);
	},

	showImage: function (i, forward) {
		L = this.list;
		if (i < 0) {
			i = L.length + i % L.length
		} else if (i >= L.length) {
			i = i % L.length;
		}
		this.index = i;
		this.loading = true;
		var src = this.path + L[i];
		this.imgLoader.src = src;
		var img = this.img;

		if (!this.fadingOut) {
			this.fadingOut = true;
			$(img).fadeOut("normal", function () {
				photoShow.fadingOut = false;
				img.src = src;
				if (!photoShow.loading) {
					$(img).fadeIn("normal");
				}
			}
		);
		}
	},

	imgLoaded: function () {

	},

	onResize: function () {
		if (!this.isready)
			return;
		this.init();
		var h = this.viewer.offsetHeight;
		var w_c = h / this.H * this.W;
		var w = this.viewer.offsetWidth;
		var h_c = w / this.W * this.H;
		var s = this.img.style;
		if (w_c > w) {
			s.left = ((-w_c + w) / 2).toFixed() + "px";
			s.top = "0px";
			w = w_c;
		}
		else {
			s.top = ((-h_c + h) / 2).toFixed() + "px";
			s.left = "0px";
			h = h_c;
		}
		s.width = w + "px";
		s.height = h + "px";
	}
}
