// Ver 1.0.110426 mod sfb2

function aniRotator(containerId) {

	var container = $("#" + containerId);
	var Layers = new Array();
	var duration;
	var zIndex = 1;
	var currentLayer = null;
	var noPan = false;
	var slideShowTimer;
	var Pan = new anipanImage();
	var currentId = null;

	var addListener = function(element, type, handler) {

		if (element.addEventListener)
			element.addEventListener(type, handler, false);
		else if (element.attachEvent)
			element.attachEvent("on" + type, handler);

	}

	this.show = function(id, introMode) {

		var nextLayer = null;

		if (typeof id == "object")
			nextLayer = id;
		else
			nextLayer = id ? this.getById(id) : this.first();

		if (!nextLayer)
			return;

		currentId = nextLayer.attr("id");

		nextLayer = nextLayer.clone();

		container.find("*").stop(true, false);
		nextLayer.css("z-index", zIndex++);

		if (currentLayer)
			nextLayer.css("opacity", 0);

		container.append(nextLayer);

		nextLayer.animate({opacity: 1}, duration, "swing", function() {

			var img = null;

			if (currentLayer)
				currentLayer.remove();

			currentLayer = nextLayer;
			container.find("div[class=layer1][id!=" + currentLayer.attr("id") + "]").remove();

			if ((!window.Admin) && (!noPan))
				if (img = $(this).find("img[name=back]").get(0))
					Pan.init(img, 1.05, 100);

			var video = null;

			if (!introMode) {

				var thislayer = this;

				if (video = $(thislayer).find("video").get(0))
					if (video.play) {

						video.load();

						addListener(video, "ended", function() { this.play(); });
						addListener(video, "canplaythrough", function() { 

							video.play();
							$(thislayer).find("IMG").animate({opacity: 0}, duration, "swing");

						});

					}

			}

		});

	}

	this.first = function() {

		if (Layers.length > 0)
			return Layers[0];

		return null;

	}

	this.getById = function(id) {

		for (var i in Layers)
			if ((Layers[i].attr("id") == "layer" + id) || (Layers[i].attr("id") == id))
				return Layers[i];

		return null;

	}

	this.getNext = function() {

		var index;
		var i;

		for (i = 0; i < Layers.length; i++)
			if (Layers[i].attr("id") == currentId) {

				if (i == Layers.length - 1)
					index = 0;
				else
					index = i + 1;

				return Layers[index];

			}

		return Layers[0];

	}

	this.slideShow = function(interval) {

		var src = this;

		slideShowTimer = setInterval(function() {

			src.show(src.getNext());

		}, interval);

	}

	this.init = function(sduration, snoPan, sslideShow, sslideShowInterval) {

		duration = sduration ? sduration : 1000;
		noPan = snoPan ? snoPan : false;

		$(container).find("div").each(function() {

			var layer = $(this).clone();

			layer.css({
				display: "block",
				opacity:  0
			});

			Layers.push(layer);

			this.parentNode.removeChild(this);

		});

		this.show();

	}

}

