/**
 * ModalBootstrap
 *
 * library which uses bootstrap modal or mobile menu
 *
 * @copyright Copyright (c) 2014 DMK E-BUSINESS GmbH <dev@dmk-ebusiness.de>
 * @license http://www.gnu.org/licenses/lgpl.html
 *          GNU Lesser General Public License, version 3 or later
 * @version 0.1.0
 * @requires jQuery, Base, Request
 * @author Michael Wagner <michael.wagner@dmk-ebusiness.de>
 */

/*
 * Sample to set the PageType:
 * DMK.ModalBootstrap.setData("pageType", 99);
 */
/*
 * Sample to override the RequestCall:
 * DMK.Objects.extend(
	"ModalBootstrapAjaxRequest",
	function MyRequest() {
		this.onStart = function(data, parameters) {
			// do some thinks and then call the parent!
			this.parent().onStart.call(this, data, parameters);
		}
	}
);
 */
(function(DMK, $){
	"use strict";
	var AjaxRequest, MobileMenu, VERSION = "0.1.0";

	// Ajax Request definieren
	AjaxRequest = DMK.Request.extend(function AjaxRequest() {});

	// die modalbox
	MobileMenu = function MobileMenu(options) {
		this.setData(
			$.extend(
				{
					template :
						'<div class="links menu-ajax" id="{modal-id}">' +
							'<div class="menu-body" id="{modal-id}-body"></div>' +
						'</div>',
					singlebox : true,
					// Zahl muss mit PAGE.typeNum im TS uebereinstimmen
					pageType : 9266
				},
				options
			)
		);
		this.setData("version", VERSION);
	};

	// MobileMenu extends Base
	MobileMenu = DMK.Base.extend(MobileMenu);

	// wir initialisieren das DOM
	MobileMenu.prototype.init = function() {
		this.initLinks();
		this.initForms();
	};

	// wir initialisieren
	MobileMenu.prototype.initLinks = function () {
		var _self = this;
		$(document).ready(function(){
			var element = $('a.menu-ajax');
			_self.open(element);
		})
	};

	// wir initialisieren alle Formulare, fuers popup
	MobileMenu.prototype.initForms = function (box) {
		var _self = this;
		box = typeof box === "undefined" ? this.getBox() : box;
	};

	// wir machen den ajax call
	MobileMenu.prototype.open = function(el) {
		var _self = this,
			_request = DMK.Objects.getInstance("MobileMenuAjaxRequest"),
			parameters = {type : this.getData("pageType")}
		;
		_request.onSuccess = function(data, parameters){

			_self.updateContent(data, parameters);
			$('#mobile-menu').mobileMenu();
			$('.spinner-wrapper').fadeOut( "slow" );

		};
		_request.doCall(el, parameters);
	};

	// wir ersetzen den inhalt vom calls in das menu
	MobileMenu.prototype.updateContent = function(data, parameters) {
		var $box = this.getBox(),
			$body = $box.find(".menu-body")
		;
		$body.html(data);
	};

	// liefert / erzeugt das html fuer's menu
	MobileMenu.prototype.getBox = function () {
		var id = 'menu-ajax',
			$box = $('#'+id)
		;
		// we need a new box for each call,
		// if the current box allready is opened
		// this is needed for foundation reveal
		if ($box.length > 0 && $box.is(':visible')) {
			if (this.getData("singlebox")) {
				return $box.first();
			}
			id = id + '-' + new Date().getTime();
			$box = $([]);
		}
		// menu erzeugen
		if ($box.length === 0) {
			$box = $(this.getData("template").replace(/{modal-id}/g, id));
			$('.nav-mobile').prepend($box);
			// formulare fuer jede neue box initialisieren!
			this.initForms($box);
		}
		return $box;
	};

	// wir registrieren unsere lib
	DMK.Objects.add(AjaxRequest, "MobileMenuAjaxRequest");
	DMK.Libraries.add(MobileMenu);

})(DMK, jQuery);
