define('package/quiqqer/tour/bin/guides/SiteAdmin', [

    'qui/QUI',
    'package/quiqqer/tour/bin/classes/Tour',
    'utils/Panels',
    'Locale',
    'Projects',
    'qui/controls/windows/Alert'

], function (QUI, Tour, PanelUtils, QUILocale, Projects, QUIAlert) {
    "use strict";

    var SiteAdmin = new Tour();
    var lg = 'quiqqer/tour';
    var SitePanel;
    var ProjectPanel;
    var LastObj;

    SiteAdmin.addStep({
        title  : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text   : QUILocale.get(lg, 'SiteAdmin.Step1.Text'),
        buttons: {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.LosGehts'),
            action: function () {
                var ProjektBar = document.getElements('.project-container');
                var Panel = QUI.Controls.getById(
                    ProjektBar.getParent('.qui-panel').get('data-quiid')
                );
                ProjectPanel = Panel;
                Panel.open(function () {
                    Projects.getList(function (result) {
                        var keys        = Object.keys(result),
                            project     = result[keys[0]],
                            projectName = keys[0],
                            projectLang = project.default_lang;

                        Panel.setAttribute('project', projectName);
                        Panel.setAttribute('lang', projectLang);
                        Panel.openProject();

                        PanelUtils.openSitePanel(projectName, projectLang, 1).then(function (CreatedPanel) {
                            SitePanel = CreatedPanel;
                            SiteAdmin.next();
                        });
                    });
                });
            }
        }
    });

    SiteAdmin.addStep({
        title  : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text   : QUILocale.get(lg, 'SiteAdmin.Step2.Text'),
        buttons: [{
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Ja'),
            action: function () {
                SiteAdmin.next();
            }
        }, {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Nein'),
            action: function () {
                SiteAdmin.cancel();
                new QUIAlert({
                    title  : QUILocale.get(lg, 'SiteAdmin.Alert.Badjump.Title'),
                    content: QUILocale.get(lg, 'SiteAdmin.Alert.Badjump')
                }).open();
            }
        }]
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step3.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getButtons('save').getElm();
            },
            on     : 'bottom'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step4.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getButtons('status').getElm();
            },
            on     : 'bottom'
        },
        when    : {
            show: function () {
                var DButton = SitePanel.getButtons('status').getElm();
                DButton.addEvent('click', SiteAdmin.next);
            },
            hide: function () {
                var DButton = SitePanel.getButtons('status').getElm(); //@todo scheint noch nicht zu funktionieren die Funktionalität zue ntfernen
                DButton.removeEvent('click', SiteAdmin.next);
            }
        },
        buttons : false
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step5.Text'),
        attachTo: {
            element: function () {
                return ProjectPanel.$Categories.nextElementSibling;
            },
            on     : 'right'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step6.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getButtons('status').getElm();
            },
            on     : 'bottom'
        },
        when    : {
            show: function () {
                SitePanel.getButtons('status').getElm().addEvent('click', SiteAdmin.next);
            },
            hide: function () {
                SitePanel.getButtons('status').getElm().removeEvent('click', SiteAdmin.next);
            }
        },
        buttons : false
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step7.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getButtons('preview').getElm();
            },
            on     : 'bottom'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step8.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getButtons('new').getElm();
            },
            on     : 'left'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step9.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getButtons('delete').getElm();
            },
            on     : 'left'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });


    //@todo Pfeil fehlt
    SiteAdmin.addStep({
        title        : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text         : QUILocale.get(lg, 'SiteAdmin.Step10.Text'),
        tetherOptions: {
            constraints: null // this disables pinning (which breaks the arrows)
        },
        attachTo     : {
            element: function () {
                return SitePanel.getElm().getElements('button[name = "sort"]')[0];
            },
            on     : 'left'   //@todo kleiner test
        },
        buttons      : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });


    SiteAdmin.addStep({
        title        : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text         : QUILocale.get(lg, 'SiteAdmin.Step11.Text'),
        tetherOptions: {
            constraints: null // this disables pinning (which breaks the arrows)
        },
        attachTo     : {
            element: function () {
                return SitePanel.getElm().getElements('button[name = "media"]')[0];
            },
            on     : 'left'
        },
        buttons      : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }

    });

    SiteAdmin.addStep({
        title        : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text         : QUILocale.get(lg, 'SiteAdmin.Step12.Text'),
        tetherOptions: {
            constraints: null // this disables pinning (which breaks the arrows)
        },
        attachTo     : {
            element: function () {
                return SitePanel.getElm().getElements('button[name = "permissions"]')[0];
            },
            on     : 'left'
        },
        buttons      : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });


    SiteAdmin.addStep({
        title  : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text   : QUILocale.get(lg, 'SiteAdmin.Step13.Text'),
        buttons: {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step14.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getCategory('information').getElm();
            },
            on     : 'right'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        },
        when    : {
            show: function () {
                SitePanel.getCategory('information').click();
            }
        }
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step15.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getElm().getElement('input[name = "title"]');
            },
            on     : 'bottom'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step16.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getElm().getElement('textarea[name = "short"]');
            },
            on     : 'bottom'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });


    SiteAdmin.addStep({
        title        : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text         : QUILocale.get(lg, 'SiteAdmin.Step17.Text'),
        attachTo     : {
            element: function () {
                return SitePanel.getElm().getElement('button[name = "project-types"]');
            },
            on     : 'left'
        },
        tetherOptions: {
            constraints: null // this disables pinning (which breaks the arrows)
        },
        scrollTo     : true,
        buttons      : [{
            text  : QUILocale.get(lg, 'SiteAdmin.Button.ErzählMirMehr'),
            action: function () {
                SitePanel.getElm().getElements('button[name = "project-types"]').fireEvent('click'); //@todo man könnte sich über qui-id holen und javascrpt click auslösen
                SiteAdmin.next();
            }
        }, {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.skip(6);
            }
        }]
    });

    SiteAdmin.addWaitingStepByType('controls/projects/TypeWindow');
    SiteAdmin.addStep({
        classes: '.no-show',
        buttons: false,
        when   : {
            show: function () {
                LastObj = QUI.Controls.getByType('controls/projects/TypeWindow')[0].getElm();
                SiteAdmin.next();
            }
        }
    });

    SiteAdmin.addStep({
        classes: '.no-show',
        when   : {
            show: function () {
                var iter = 0;
                var step = function () {
                    iter++;
                    var Timeout = 20;
                    if (iter > Timeout) {
                        SiteAdmin.skip(0);
                        return;
                    }
                    var gefunden = LastObj.getElement('[name = "details"]');
                    if (gefunden) {
                        SiteAdmin.next.delay(100);
                        return;
                    }
                    step.delay(100);
                };
                step();
            }
        },
        buttons: false
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step18.Text'),
        attachTo: {
            element: function () {
                var PopUp = QUI.Controls.getByType('controls/projects/TypeWindow');
                return PopUp[0].$HeaderButtons.children[1];
            },
            on     : 'right'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                var DetailAnzeige = document.getElement('.qui-button-text-image.fa.fa-file-text').getParent();
                DetailAnzeige.fireEvent('click');
                SiteAdmin.next();
            }
        }
    });

    SiteAdmin.addStep({
        classes: '.no-show',
        when   : {
            show: function () {
                var iter = 0;
                var step = function () {
                    iter++;
                    var Timeout = 20;
                    if (iter > Timeout) {
                        SiteAdmin.skip(0);
                        return;
                    }
                    var gefunden = LastObj.getElement('[name = "close"]');
                    if (gefunden) {
                        SiteAdmin.next.delay(100);
                        return;
                    }
                    step.delay(100);
                };
                step();
            }
        },
        buttons: false
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step19.Text'),
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        },
        attachTo: {
            element: function () {
                return LastObj.getElement('[name="close"]');
            },
            on     : 'right'
        },
        when    : {
            show: function () {
                var XButton = LastObj.getElement('[name="close"]');
                XButton.addEvent('click', function () {
                    SiteAdmin.next();
                });
            }
        }
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step20_1.Text') + URL_OPT_DIR + QUILocale.get(lg, 'SiteAdmin.Step20_2.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getElm().getElement('select[name = "layout"]');
            },
            on     : 'bottom'
        },
        scrollTo: true,
        buttons : [{
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }]
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step21.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getElm().getElement('[name = "nav_hide"]');
            },
            on     : 'bottom'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        }
    });

    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step24.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getCategory('content').getElm();
            },
            on     : 'left'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.next();
            }
        },
        when    : {
            show: function () {
                SitePanel.getCategory('content').getElm().fireEvent('click');
            }
        }
    });


    SiteAdmin.addStep({
        title   : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text    : QUILocale.get(lg, 'SiteAdmin.Step25.Text'),
        attachTo: {
            element: function () {
                return SitePanel.getCategory('settings').getElm();
            },
            on     : 'left'
        },
        buttons : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Weiter'),
            action: function () {
                SiteAdmin.wait(500);
                SiteAdmin.next();
            }
        },
        when    : {
            show: function () {
                SitePanel.getCategory('settings').getElm().fireEvent('click');
            }
        }
    });


    SiteAdmin.addStep({
        title        : QUILocale.get(lg, 'SiteAdmin.Step1.Title'),
        text         : QUILocale.get(lg, 'SiteAdmin.Step30.Text'),
        tetherOptions: {
            constraints: null // this disables pinning (which breaks the arrows)
        },
        buttons      : {
            text  : QUILocale.get(lg, 'SiteAdmin.Button.Ende'),
            action: function () {
                SiteAdmin.cancel();
            }
        }
    });


    return SiteAdmin;
});