/*!
	CMS msgbox!!
	This source was borrowed from Msgbox v1.64 

	//Slimbox v1.64 - The ultimate lightweight Lightbox clone
	//(c) 2007-2008 Christophe Beyls <http://www.digitalia.be>
	//MIT-style license.
*/

var Msgbox;

(function() {

	// Global variables, accessible to Msgbox only
	var options, top, fx, 

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

	/*
		Initialization
	*/

	window.addEvent("domready", function() {
		// Append the Msgbox 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")
		);

		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),
			bottom: new Fx.Tween(bottom, {property: "margin-top", duration: 400})
		};
	});


	/*
		API
	*/

	Msgbox = {
		open: function(msg, msgtype, msglevel, _options) {
			options = $extend({
				overlayOpacity: 0.3,			// 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
				animateCaption: true,
				initialWidth: 250,			// Initial width of the box (in pixels)
				initialHeight: 250,			// Initial height of the box (in pixels)
				centerPadding: 20
			}, _options || {});

						
			position();
			setup(true);
			top = window.getScrollTop() + (window.getHeight() / 15);
			fx.resize = new Fx.Morph(center, $extend({duration: options.resizeDuration, onComplete: null}, options.resizeTransition ? {transition: options.resizeTransition} : {}));
			center.setStyles({top: top, width: options.initialWidth, height: options.initialHeight, marginLeft: -(options.initialWidth/2), padding: options.centerPadding, display: ""});
			fx.overlay.start(options.overlayOpacity);
			
			return loadMessage(msg, msgtype, msglevel);
		},
		setMessageBox: function(msg, _caption) {	//set message at outside class
			setMessage(msg, _caption);
		}
	};

	/*
		Internal functions
	*/

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

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

		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;
			case 37:	// Left arrow
			case 80:	// 'p'
				previous();
				break;	
			case 39:	// Right arrow
			case 78:	// 'n'
				next();
		}
		// Prevent default keyboard action (like navigating inside the page)
		return false;
	}

	function loadMessage(msg, msgtype, msglevel) {
		center.className = "lbLoading";

		if (msgtype == 'ajax') {
			xajax_getMessage(msg, msglevel, window.location.href);
		} else {
			setMessage(msg, '');
		}
	}

	function setMessage(msg, _caption) {	
		center.innerHTML = msg;
		
		center.className = "";
		caption.set('html', _caption);
		//number.set('html', '');

		bottomContainer.setStyles({top: top + center.clientHeight, width:  options.initialWidth +  (options.centerPadding*2), marginLeft: center.style.marginLeft, visibility: "hidden", display: ""});
		if (options.animateCaption) {
			fx.bottom.set(-bottom.offsetHeight).start(0);
		}
		bottomContainer.style.visibility = "";
	}

	function close() {
		for (var f in fx) fx[f].cancel();
		$$(center, bottomContainer).setStyle("display", "none");
		fx.overlay.chain(setup).start(0);
		return false;
	}

})();


// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
Msgbox.scanPage = function() {
	if (getURLParam('__msg') && 'undefined' != typeof(xajax_getMessage)) {
		Msgbox.open(getURLParam('__msg'), 
					getURLParam('__msgtype'),
					getURLParam('__msglevel'),{
						initialWidth:400, 
						initialHeight:80});
	}
};
window.addEvent("domready", Msgbox.scanPage);