// MODALBOX
(function ($) {

	var objname = 'modalbox';

	$.fn.modalbox = function (options) {
		s = $.extend({
			box: $('.' + objname + ''),
			content: $('.' + objname + '_content'),
			inner: $('.' + objname + '_inner'),
			close: $('.' + objname + '_close'),
			arrow: $('.' + objname + '_arrow'),
			theme: 'theme1',
			animation: 500,
			parentwidth: false,
			parentheight: false,
			remove: true,
			overlay: $('<iframe frameborder="0" class="modalbox_overlay"></iframe><div class="modalbox_overlay"></div>'),
			callback: function () { },
			hideOther: true
		}, options || {});

		return this.each(function () {
			var element = $(this);

			var elementrel = element.attr('rel');
			var gettheme = function () { // get theme
				reg = /theme\d/i;
				return reg.exec(elementrel);
			}
			var fixedpos = function () {
				pos = false;
				reg1 = /posy\d*/i;
				reg2 = /posx\d*/i;
				if (reg1.exec(elementrel) != null) {
					pos = [String(reg1.exec(elementrel)).substr(4), String(reg2.exec(elementrel)).substr(4)]
				}
				return pos;
			}
			element.bind('click', function (e) {

				if (gettheme() == null) { s.box.addClass(objname + s.theme); }
				else { s.box.addClass('' + objname + gettheme() + ''); }

				if (fixedpos()) {
					s.box.css({ top: fixedpos()[0] + 'px', left: fixedpos()[1] + 'px' });
				} else {
	                pos = $.fn.modalbox.getPos($(this));
					s.box.css({ top: pos[0] + 'px', left: pos[1] + 'px' });
				}

				s.box.before(s.overlay);
				s.box.fadeIn(s.animation);
				return false;
			});
			s.close.bind('click', function () { $.fn.modalbox.hide(); return false; });
		});

	};

	$.fn.modalbox.show = function (id, pos) {
		id = $(id);
		if (pos) {
			str = pos.split(',');
			s.box.css({ top: str[0] + 'px', left: str[1] + 'px' });
		} else {
            pos = $.fn.modalbox.getPos();
			s.box.css({ top: pos[0] + 'px', left: pos[1] + 'px' });
		}
		id.before(s.overlay);
		id.fadeIn(s.animation);
	};

	$.fn.modalbox.hide = function () {
		if (s.box.find('.tooltipbox')) { s.box.find('.tooltipbox').toggleClass('none'); }
		s.box.fadeOut(s.animation, function () {
			s.overlay.remove();
			s.box[0].className = objname;
		});
	};

	$.fn.modalbox.getViewport = function () {
		return [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop()];
	};

	$.fn.modalbox.getPos = function (box) {
		var viewport = $.fn.modalbox.getViewport();
		var viewportW = s.parentwidth ? s.parentwidth / 2 : viewport[0] / 2 + viewport[2];
		var viewportH = s.parentheight ? s.parentheight / 2 : viewport[1] / 2 + viewport[3];
		var posX = Math.round(viewportW - box.width() / 2);
		var posY = Math.round(viewportH - box.height() / 2);
		return [posY, posX]
	};

	$.fn.showModalBox = function (options) {
		var effectiveOptions = $.extend({
			modal: true,
			center: false,
			top: false,
			hideOther: true,
			animation: 500,
			overlay: '<iframe frameborder="0" class="modalbox_overlay"></iframe><div class="modalbox_overlay"></div>',
			callback: function () { }
		}, options || {});

		$(this).each(function () {
			if (effectiveOptions.hideOther)
				$(".modalbox").not($(this)).hideModalBox();

			var box = $(this);

			box.find(".modalbox_close").unbind().bind(
				"click",
				function () { $(box).hideModalBox(effectiveOptions); return false; });

			if (effectiveOptions.modal) {
				this.overlaySelector = $(effectiveOptions.overlay);
				$('body').append(this.overlaySelector);
			} else {
				this.overlaySelector = null;
			}
		});
		$(this).css({ display: 'block', visibility: 'hidden' })/*.width(s.content.width() + 'px')*/;
		s.inner.width(s.content.width() + 'px');
		$(this).css({ display: 'none', visibility: 'visible' });

		if (effectiveOptions.center) {
			pos = $.fn.modalbox.getPos($(this));
			var toppos = effectiveOptions.top ? effectiveOptions.top : pos[0] > 0 ? pos[0] : 0;
			$(this).css({ top: toppos + 'px', left: pos[1] + 'px' });
		}

		if (effectiveOptions.modal) $('body').append($(this));
		$(this).show(0, function () { effectiveOptions.callback(); });
	};

	$.fn.hideModalBox = function (options) {
		var effectiveOptions = $.extend({
			modal: true,
			animation: 500,
			remove: true,
			callbackClose: function () { }
		}, options || {});

		$(this).find('.tooltipbox').toggleClass('none', true);
		$(this).hide(0, function () {
			if (this.overlaySelector) {
				this.overlaySelector.remove();
			}
			if (effectiveOptions.remove)
				if (effectiveOptions.modal) $(this).remove();
				else $(this).hide();
			effectiveOptions.callbackClose();
		});
	};

})(jQuery);
