
// SlimiFramebox v1.65 +Load
// r.081124

var SlimiFramebox;

(function() {

	// Global variables, accessible to SlimiFramebox only
	var state = 0, options, iframes, activeiFrame, top, fx,
	// State values: 0 (closed or closing), 1 (open and ready), 2+ (open and busy with animation)

	// DOM elements
	overlay, center, iframe, bottomContainer, bottom, caption, number;

	/*
		Initialization
	*/

	window.addEvent("domready", function() {
		// Append the SlimiFramebox HTML code at the bottom of the document
		$(document.body).adopt(
			$$([
				overlay = new Element("div", {id: "lbOverlay"}).addEvent("click", close),
				center = new Element("div", {id: "lbCenter"}),
				bottomContainer = new Element("div", {id: "lbBottomContainer"})
			]).setStyle("display", "none")
		);

		iframe = new Element("div", {id: "lbImage"});

		bottom = new Element("div", {id: "lbBottom"}).injectInside(bottomContainer).adopt(
			new Element("a", {id: "lbCloseLink", href: "#"}).addEvent("click", close),
			caption = new Element("div", {id: "lbCaption"}),
			number = new Element("div", {id: "lbNumber"}),
			new Element("div", {styles: {clear: "both"}})
		);

		fx = {
			overlay: new Fx.Tween(overlay, {property: "opacity", duration: 500}).set(0),
			iframe: new Fx.Tween(iframe, {property: "opacity", duration: 500}),
			bottom: new Fx.Tween(bottom, {property: "margin-top", duration: 400})
		};
	});


	/*
		API
	*/

	SlimiFramebox = {
		open: function(_iframes, start_iFrame, _options) {
			options = $extend({
				loop: false,				// Allows to navigate between first and last iframes
				overlayOpacity: 0.8,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
				resizeDuration: 400,			// Duration of each of the box resize animations (in milliseconds)
				resizeTransition: false,		// Default transition in mootools
				initialWidth: 550,			// Initial width of the box (in pixels)
				initialHeight: 550,			// Initial height of the box (in pixels)
				allowscroll: "no",			// allow iframe scroll
				showCounter: true,			// If true, a counter will only be shown if there is more than 1 iframe to display
				counterText: "iFrame {x} of {y}"		// Translate or change as you wish
			}, _options || {});

			// The function is called for a single iframe, with URL and Title as first two arguments
			if (typeof _iframes == "string") {
				_iframes = [[_iframes,start_iFrame]];
				start_iFrame = 0;
			}

			iframes = _iframes;
			options.loop = options.loop && (iframes.length > 1);
			position();
			setup(true);
			top = window.getScrollTop() + (window.getHeight() / 15);
			fx.resize = new Fx.Morph(center, $extend({duration: options.resizeDuration}, options.resizeTransition ? {transition: options.resizeTransition} : {}));
			center.setStyles({top: top, width: options.initialWidth, height: options.initialHeight, marginLeft: -(options.initialWidth/2), display: ""});
			fx.overlay.start(options.overlayOpacity);
			state = 1;
			return changeiFrame(start_iFrame);
		}
	};

	Element.implement({
		slimiFramebox: function(_options, linkMapper) {
			// The processing of a single element is similar to the processing of a collection with a single element
			$$(this).slimiFramebox(_options, linkMapper);

			return this;
		}
	});

	Elements.implement({
		/*
			options:	Optional options object, see SlimiFramebox.open()
			linkMapper:	Optional function taking a link DOM element and an index as arguments and returning an array containing 2 elements:
					the iframe URL and the iframe caption (may contain HTML)
			linksFilter:	Optional function taking a link DOM element and an index as arguments and returning true if the element is part of
					the iframe collection that will be shown on click, false if not. "this" refers to the element that was clicked.
					This function must always return true when the DOM element argument is "this".
		*/
		slimiFramebox: function(_options, linkMapper, linksFilter) {
			linkMapper = linkMapper || function(el) {
				return [el.href, el.title];
			};

			linksFilter = linksFilter || function() {
				return true;
			};

			var links = this;

			links.removeEvents("click").addEvent("click", function() {
				// Build the list of iframes that will be displayed
				var filteredLinks = links.filter(linksFilter, this);
				return SlimiFramebox.open(filteredLinks.map(linkMapper), filteredLinks.indexOf(this), _options);
			});

			return links;
		}
	});


	/*
		Internal functions
	*/

	function position() {
		overlay.setStyles({top: window.getScrollTop(), height: window.getHeight()});
	}

	function setup(open) {
		["object", Browser.Engine.trident ? "select" : "embed"].forEach(function(tag) {
			Array.forEach(document.getElementsByTagName(tag), function(el) {
				if (open) el._slimiFramebox = el.style.visibility;
				el.style.visibility = open ? "hidden" : el._slimiFramebox;
			});
		});

		overlay.style.display = open ? "" : "none";

		var fn = open ? "addEvent" : "removeEvent";
		window[fn]("scroll", position)[fn]("resize", position);
		document[fn]("keydown", keyDown);
	}

	function keyDown(event) {
		switch(event.code) {
			case 27:	// Esc
			case 88:	// 'x'
			case 67:	// 'c'
				close();
				break;
		}
		// Prevent default keyboard action (like navigating inside the page)
		return false;
	}


	function changeiFrame(iFrame_Index) {
		if ((state == 1) && (iFrame_Index >= 0)) {
			state = 2;
			activeiFrame = iFrame_Index;
			center.className = "lbLoading";
			center.innerHTML = '<iframe name="slimiframebox__iframe" scrolling="'+options.allowscroll+'" src="'+String(iframes[iFrame_Index][0])+'" style="width: 100%; height: 100%"></iframe>';
			center.className = "";
		}

		return false;
	}


	function close() {
		if (state) {
			state = 0;
			center.innerHTML = '<iframe name="slimiframebox__iframe" scrolling="no" src=""></iframe>';
			for (var f in fx) fx[f].cancel();
			$$(center, bottomContainer).setStyle("display", "none");
			fx.overlay.chain(setup).start(0);
		}

		return false;
	}

})();

//--

// AUTOLOAD CODE BLOCK (CAN BE CHANGED)
SlimiFramebox.scanPage = function() {
	var w, h, scroll ;
	var links = $$("a").filter(function(el) {
		var aDim = el.rel.match(/[0-9]+/g);
		if(el.rel.test(/^slim_ifrmbox/i)) {
			w = (aDim && (aDim[0] > 0)) ? aDim[0] : 640;
			h = (aDim && (aDim[1] > 0)) ? aDim[1] : 480;
			scroll = (aDim && (aDim[2] == 1)) ? "yes" : "no";
		} //end if
		w = parseInt(w);
		h = parseInt(h);
		return el.rel && el.rel.test(/^slim_ifrmbox/i);
	});
	$$(links).slimiFramebox({initialWidth: w, initialHeight: h, allowscroll: scroll}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
};
window.addEvent("domready", SlimiFramebox.scanPage);

// END

