
Configurator = {

};




Configurator.Zielland = new Class();
Configurator.Zielland.prototype = {
    initialize: function(Trigger)
    {
        this.Trigger                = Trigger;
        this.Trigger.onclick    = this.showMenu.bind(this);

        if (this.Trigger.className.search(/\bnonjax\b/))
        {
            this.Nonjax = true;
        }


        this.buildMenu();
    }

    ,Nonjax: false
    ,Trigger: null
    ,Menu: null
    ,Countries: []
    ,CountryNames: {}

    ,showMenu: function()
    {
        this.Menu.style.display = 'block';
        this.startMenuTimeout(1500);
        Util.snapTo(this.Menu, this.Trigger, 'luh', 0);
        return false;
    }

    ,hideMenu: function()
    {
        this.Menu.style.display = 'none';
    }

    ,hideTimeOut: null
    ,resetMenuTimeout: function()
    {
        if (this.hideTimeOut)
            window.clearTimeout(this.hideTimeOut);
    }
    ,startMenuTimeout: function(TimeOut)
    {
        if (!arguments[0])
            TimeOut = 1000;

        this.hideTimeOut = window.setTimeout(this.hideMenu.bind(this), TimeOut);
    }

    ,buildMenu: function()
    {
        this.Menu   = document.createElement('ul');
        this.Menu.Countries = {};
        this.Menu.Zielland = this;

        this.Menu.onmouseout    = this.startMenuTimeout.bind(this);
        this.Menu.onmouseover   = this.resetMenuTimeout.bind(this);

        for (var i = 0; (Country = this.Countries[i]); i ++)
        {
            this.Menu.Countries[Country] = document.createElement('li');
            Elem2 = this.Menu.Countries[Country];
            Elem = document.createElement('a');
            Elem2.appendChild(Elem);

            Elem.style.backgroundImage = this.Trigger.style.backgroundImage.replace(/\/\w\w([^\/]+)png\)$/, '/' + Country + '$1png)');
            Elem.innerHTML = this.CountryNames[Country];
            Elem.href = '?Zielland=' + Country.toUpperCase();
            Elem.name = 'Zielland';
            Elem.value = Country.toUpperCase();
            Elem.Menu = this;

            Elem.onclick = function()
            {
                if (this.Menu.Nonjax)
                {
                    this.Menu.hideMenu();
                }
                else
                {
                    Cart.update(this);
                    this.Menu.hideMenu();
                    return false;
                }
            };
//            Elem.href = '?' + this.Trigger.name + '=' + Country.toUpperCase();

            this.Menu.appendChild(Elem2);
        }

        this.Menu.className = 'xp_menu visible';

        this.Menu.style.display = 'none';
        document.getElementsByTagName('body')[0].appendChild(this.Menu);
    }

    ,submit: function(Elem)
    {
        alert(Elem);
    }

    ,submitZielland: function()
    {
        this.parentNode.Zielland.submit(this);
    }
};


Configurator.Beinprofil = new Class();
Configurator.Beinprofil.prototype = {
    initialize: function(el)
    {
        this.Img = $('beinprofil_preview');

        el.getElements('input.radio').each(this.prepInputs.bind(this));
        el.getElements('label.radio').each(this.prepSpans.bind(this));
    }

    ,Img: null
    ,resetTimeout: null
    ,Inputs: []
    ,Spans: []
    ,Active: null

    ,prepSpans: function(Elem)
    {
        if (-1 == Elem.getAttribute('for').search(/^Beinprofil_/))
            return;

        this.Spans.push(Elem);
        Elem.RadioButton = this.Inputs[this.Spans.length - 1];


        Elem.onclick = function()
        {
           this.RadioButton.click();
        };

        Elem.onmouseover = function()
        {
           this.RadioButton.onmouseover();
        };

        Elem.onmouseout = function()
        {
           this.RadioButton.onmouseout();
        };
    }

    ,prepInputs: function(Elem)
    {
        if (-1 == Elem.id.search(/^Beinprofil_/))
            return;

        this.Inputs.push(Elem);
        if (Elem.checked)
            this.Active = Elem;

       Elem.Beinprofil = this;

       Elem.onclick = function()
       {
           this.Beinprofil.update(this);
       };

       Elem.onmouseover = function()
       {
           this.Beinprofil.preview(this);
       };

       Elem.onmouseout = function()
       {
           this.Beinprofil.reset(this);
       };
    }

    ,preview: function(Obj)
    {
        this.resetClear();

        this.updateSrc(Obj.value);
    }

    ,reset: function()
    {
        this.resetClear();

        this.resetTimeout = window.setTimeout(
             this.resetDo.bind(this)
            ,500
        );
    }

    ,resetClear: function()
    {
        if (this.resetTimeout)
            window.clearTimeout(this.resetTimeout);
    }

    ,resetDo: function()
    {
        this.updateSrc(this.Active.value);
    }

    ,updateSrc: function(Profil)
    {
        this.Img.src = this.Img.src.replace(/Beinprofil_[^\.]+\./, 'Beinprofil_' + Profil + '.');
    }

    ,update: function(Obj)
    {
        this.resetClear();
        this.Active = Obj;

        this.updateSrc(this.Active.value);
        Cart.update(Obj);
    }
};

Configurator.Stuhl = {
    updateColour: function(Obj)
    {
        var Src = $('stuhl_preview').src.split(/\//);
        switch (Src.length)
        {
            case 10 :
                var Hold = Src[9];
                Src[9] = Util.ucFirst(Obj.value);
                Src.push(Hold);
                break;
            case 11 :
                Src[9] = Util.ucFirst(Obj.value);
                break;
        }
        $('stuhl_preview').src = Src.join('/');
    }
};

Configurator.Lederbank = {
    updateFromLehne: function(Obj, Target)
    {
        switch (Obj.value)
        {
            case 'mit' :
                $(Target).innerHTML = '63';
                break;
            case 'ohne' :
                $(Target).innerHTML = '39';
                break;
        }
    }
};

Configurator.Storage = {
    save: function()
    {
        var JB = Jax_BrowserUtil.getInstance();
        JB.prepare('browse', 'kunden.konfiguration.speichern');
    },

    tell: function()
    {
        var JB = Jax_BrowserUtil.getInstance();
        JB.prepare('browse', 'kunden.konfiguration.senden');
    },

    tellSubmit: function()
    {
      var data = {
         from: $('tell-from').get('value')
        ,to: $('tell-to').get('value')
        ,cc: $('tell-cc').get('value')
        ,subject: $('tell-subject').get('value')
        ,text: $('tell-text').get('value')
        ,Engine: 'kunden.konfiguration.senden'
      };

      var JB = Jax_BrowserUtil.getInstance();
      JB.prepare('browse-free', '/plugin/de.masstisch.ajax/Engine/get.php', data);
    },

    remove: function(ID)
    {
        new Request.JSON({
          url: '/plugin/de.masstisch.ajax/Cart/Remove.php',
            method: 'post',
            data: {ID: ID},
          onComplete: MTJax.Action.receive
        }).send();
    },

    /**
     * Speichern
     */
    saveDo: function()
    {
        new Request.JSON({
          url: '/plugin/de.masstisch.ajax/Cart/CheckName.php',
            method: 'post',
            data: {Name: $('userform_configname').get('value')},
          onComplete: Configurator.Storage.nameChecked
        }).send();
    },

    nameChecked: function(r)
    {
        switch (r.Status)
        {
            case 'OK' :
                Configurator.Storage.saveExecute();
                break;
            case 'Double' :
                if (confirm(r.AlertMessage))
                    Configurator.Storage.saveExecute();
                break;
        }
    },

    saveExecute: function()
    {
        var Params = {
          Foo: 'Bar'
        };

        if ($('userform_vorname'))
        {
            Params.Vorname  = $('userform_vorname').get('value');
            Params.Nachname = $('userform_nachname').get('value');
            Params.Password = $('userform_passwort').get('value');
        }

        if ($('userform_email'))
        {
            Params.Email = $('userform_email').get('value');
        }

        if ($('userform_send_me'))
        {
            if (($('userform_send_me').type = 'checkbox'))
                Params.SendTo = $('userform_send_me').checked;
            else
                Params.SendTo = $('userform_send_me').get('value');
        }
        else
        {
            Params.SendTo = 'Imply';
        }

        if ($('userform_configname'))
        {
            Params.ConfigName = $('userform_configname').get('value');
        }

        new Request.JSON({
          url: '/plugin/de.masstisch.ajax/Cart/Save.php',
          method: 'post',
          data: Params,
          onComplete: Configurator.Storage.saveDone
        }).send();
    }

    ,Delete: function(ID)
    {
        new Request.JSON({
          url: '/plugin/de.masstisch.ajax/Cart/Remove.php',
          method: 'post',
          data: {ID: ID.split(/_/)[1]},
          onComplete: Configurator.Storage.deleteDone
        }).send();
    }

    ,deleteDone: function(r)
    {
        MTJax.Action.execute(r.Actions);
        var JB = Jax_BrowserUtil.getInstance();
        JB.prepare('browse', 'kunden.konfiguration.loeschen');
    }

    ,saveDone: function(r)
    {
        MTJax.Action.execute(r.Actions);
        var JB = Jax_BrowserUtil.getInstance();
        JB.prepare('browse', 'kunden.konfiguration.speichern');
    }
};

Configurator.Rueckenteil = {

    /* Leitet das Update über Cart ein und
     * verändert das Vorschaubild
     */
    update: function(Elem)
    {
        // Update des Warenkorbs
        Cart.update(Elem);

        // Bild bestimmen
        Img = $('rueckenteilvorschau');
        Src = Img.src.split(/\//);
        Pic = Src.pop();
        Pic = Pic.split(/\./);

        if (Elem.value > 0)
            Pic[0] = 'Rueckenteil_Std';
        else
            Pic[0] = 'Rueckenteil_0';

        Src.push(Pic.join('.'));
        Img.src = Src.join('/');
    }
};

window.addEvent('domready', function()
{
    if ((Probe = $('plt_beinprofil')))
    {
        new Configurator.Beinprofil(Probe);
    }


    if ((el = $('zielland')))
    {
        new Configurator.Zielland(el);
    }


    if ((el = $('Freiform_Neu_Submit')))
    {
        el.onclick = function()
        {
            Cart.update([
                 $('Freiform[Neu][Anzahl]')
                ,$('Freiform[Neu][Preis]')
                ,$('Freiform[Neu][Titel]')
                ,$('Freiform[Neu][Beschreibung]')
            ]);
        }
    }


    if ((el = $('Freiform_Edit_Submit')))
    {
        el.onclick = function()
        {
            Cart.update([
                 $('Freiform[Edit][Anzahl]')
                ,$('Freiform[Edit][Preis]')
                ,$('Freiform[Edit][Titel]')
                ,$('Freiform[Edit][Beschreibung]')
                ,$('Freiform[Edit][Ident]')
            ]);
        }
    }
});