﻿M2 = this.M2 || {};

(function() {
	function addEventSimple(obj, evt, fn) {
		if (obj.addEventListener) {
			obj.addEventListener(evt, fn, false);
		} else if (obj.attachEvent) {
			obj.attachEvent('on' + evt, fn);
		}
	}

	var ready = false;

	addEventSimple(window, 'load', function() { ready = true; });

	function onReady(f) {
		if (ready) f();
		else addEventSimple(window, 'load', f);
	}

	function $(e) {
		if (typeof e == 'string') e = document.getElementById(e);
		return e;
	}

	function left(obj) {
		var curleft = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}

		return curleft;
	}

	function top(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}

		return curtop;
	}

	var nohide = false;

	M2.Lightbox = function(params) {
		this.params = params = params || {};
		var self = this;

		this.frame = document.createElement('div');
		this.closer = this.frame.appendChild(document.createElement('div'));
		this.content = this.frame.appendChild(document.createElement('div'));

		this.frame.className = 'lightboxframe';

		var close_img = this.closer.appendChild(document.createElement('div'));
		close_img.className = 'close_img';
		close_img.onclick = function() { self.hide(); nohide = false; }

		this.title = this.closer.appendChild(document.createElement('div'));
		this.title.className = 'lightboxtitle';

		this.content.className = 'lightboxcontent';

		addEventSimple(document, 'click', function(e) { if (!nohide) self.hide(); else nohide = false; });

		if (params.clickable) {
			addEventSimple(this.frame, 'click', function(e) { nohide = true; });
		}

		onReady(function() { document.forms[0].appendChild(self.frame); });
		//onReady(function() { document.body.insertBefore(self.frame, document.body.firstChild); self.frame.style.top = '200px'; });

		if (params) {
			if (params.width) this.frame.style.width = params.width + 'px';
			if (params.height) this.frame.style.height = params.height + 'px';
			if (!params.position) params.position = 'right, below';
			if (params.element) this.setElement(params.element);
			if (params.html) this.setHtml(params.html);
			if (params.place) place.call(this, $(params.place));
			if (params.title) this.setTitle(params.title);
		}

	};

	function place(item) {
		item = $(item);
		var x = left(item);
		var y = top(item);
		var w = this.params.position.indexOf('left') >= 0 ? this.frame.offsetWidth - item.offsetWidth : 0;
		var h = this.params.position.indexOf('over') >= 0 ? 0 : item.offsetHeight;

		this.frame.style.left = (x - w) + 'px';
		this.frame.style.top = (y + h) + 'px';
	}

	M2.Lightbox.prototype.show = function(item) {
		this.last = item;
		nohide = true;
		this.frame.style.visibility = 'visible';
		place.call(this, item);
	};

	M2.Lightbox.prototype.hide = function() {
		this.last = null;
		nohide = false;
		this.frame.style.visibility = 'hidden';
	};

	M2.Lightbox.prototype.toggle = function(item) {
		if (this.isVisible()) {
			this.hide();
		} else {
			this.show(item);
		}
	};

	M2.Lightbox.prototype.isVisible = function() {
		return this.frame.style.visibility == 'visible';
	};

	M2.Lightbox.prototype.setHtml = function(html) {
		this.content.innerHTML = html;
	};

	M2.Lightbox.prototype.setElement = function(node) {
		node = $(node);
		this.content.innerHTML = '';
		this.content.appendChild(node);
		node.style.visibility = '';
		node.style.display = '';
	};

	M2.Lightbox.prototype.getTitle = function() {
		return this.title.innerHTML;
	};

	M2.Lightbox.prototype.setTitle = function(html) {
		this.title.innerHTML = html;
	};

	M2.Lightbox.prototype.getItem = function() {
		return this.last;
	};
})();