/*!
	Videobox - The ultimate lightweight Lightbox clone from Slimbox v1.63 by Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
	Modify by Jose Caicedo for escena purposes only, on Ikon Group Ltda.;
*/

var Videobox;

(function() {

	// Global variables, accessible to Videobox only
	var elementsStyle = {}, state = 0, options, video, top, fx, preload,
	// State values: 0 (closed or closing), 1 (open and ready), 2+ (open and busy with animation)

	// DOM elements
	overlay, center, video_div1, bottomContainer, caption, bottom;

	/*
		Initialization
	*/

	window.addEvent("domready", function() {
		// Append the Videobox 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")
		);

		video_div1 = new Element("div", {id: "lbImage"}).injectInside(center);

		bottom = new Element("div", {id: "lbBottom"}).injectInside(bottomContainer).adopt(
			new Element("a", {id: "lbCloseLink", href: "#"}).addEvent("click", close),
			caption = new Element("div", {id: "lbCaption"}),
			new Element("div", {styles: {clear: "both"}})
		);

		fx = {
			overlay: new Fx.Tween(overlay, {property: "opacity", duration: 500}).set(0),
			video_div1: new Fx.Tween(video_div1, {property: "opacity", duration: 500, onComplete: nextEffect}),
			bottom: new Fx.Tween(bottom, {property: "margin-top", duration: 400})
		};
	});


	/*
		API
	*/

	Videobox = {
		open: function(_video, video_caption, _options) {
			options = $extend({
				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: 250,			// Initial width of the box (in pixels)
				initialHeight: 250,			// Initial height of the box (in pixels)
				animateCaption: true
			}, _options || {});
			// The function is called for a single video, with URL and Title as first two arguments

			video = [[_video,video_caption]];
			position();
			setup(true);
			top = window.getScrollTop() + (window.getHeight() / 15);
			fx.resize = new Fx.Morph(center, $extend({duration: options.resizeDuration, onComplete: nextEffect}, 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 showVideo();
		}
	};
	/*
		Internal functions
	*/

	function position() {
		overlay.setStyles({top: window.getScrollTop(), height: window.getHeight()});
	}

	function setup(open) {
		["object", window.ie ? "select" : "embed"].for_each(function(tag) {
			Array.for_each(document.getElementsByTagName(tag), function(el) {
				if (open) elementsStyle[el] = el.style.visibility;
				el.style.visibility = open ? "hidden" : elementsStyle[el];
			});
		});

		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 showVideo() {
		if (state == 1) {
			state = 2;

			$$(video_div1, bottomContainer).setStyle("display", "none");
			fx.bottom.cancel().set(0);
			fx.video_div1.set(0);
			center.className = "lbLoading";
			nextEffect();
		}

		return false;
	}

	function nextEffect() {
		switch (state++) {
			case 2:
				center.className = "";

				////video_div1.set("html",AC_FL_RunContent_det("width","450","height","358","src","images/player","flashvars","&file=" + video[0][0] + "&skin=kleur.swf&autostart=true","allowfullscreen","true","quality","high","pluginspage","http://www.macromedia.com/go/getflashplayer","movie","images/player"));
				//video_div1.set("html",AC_FL_RunContent_det("width","450","height","358","src","documents/player","flashvars","&file=" + video[0][0] + "&skin=kleur.swf&autostart=true","allowfullscreen","true","quality","high","pluginspage","http://www.macromedia.com/go/getflashplayer","movie","documents/player"));
				video_div1.set("html", AC_FL_RunContent_det("width", "450", "height", "358", "src", "/scripts/player", "flashvars", "&file=" + video[0][0] + "&skin=kleur.swf&autostart=true", "allowfullscreen", "true", "quality", "high", "pluginspage", "http://www.macromedia.com/go/getflashplayer", "movie", "/scripts/player", "base", "."));
				video_div1.setStyles({display: ""});
				$$(video_div1, bottom).setStyle("width", "450");
				caption.set("html", video[0][1] || "");

				if (center.clientHeight != video_div1.offsetHeight) {
					fx.resize.start({height: video_div1.offsetHeight});
					break;
				}
				state++;
			case 3:
				if (center.clientWidth != video_div1.offsetWidth) {
					fx.resize.start({width: video_div1.offsetWidth, marginLeft: -video_div1.offsetWidth/2});
					break;
				}
				state++;
			case 4:
				bottomContainer.setStyles({top: top + center.clientHeight, marginLeft: center.style.marginLeft, visibility: "hidden", display: ""});
				$(bottomContainer).setStyle("width", "470px");
				fx.video_div1.start(1);
				break;
			case 5:
				if (options.animateCaption) {
					fx.bottom.set(-bottom.offsetHeight).start(0);
				}
				bottomContainer.style.visibility = "";
				state = 1;
		}
	}

	function close() {
		if (state) {
			state = 0;
			for (var f in fx) fx[f].cancel();
			$$(center, bottomContainer).setStyle("display", "none");
			fx.overlay.chain(setup).start(0);
			video_div1.set("html","");
			video_div1.setStyles({display: "none"});
		}

		return false;
	}

})();
