FoxFindIT = new Class({
    initialize: function(keyword) {
        var element = $('field_keyword_inline');

        if (! element) {
            return;
        }

        element.value = keyword;
        element.addEvent('focus', function() {
            if (this.value == keyword) {
                this.value = '';
            }
        });

        element.addEvent('blur', function() {
            if (this.value == '') {
                this.value = keyword;
            }
        });
    }
});

/**
 * There can be more the one FoldLists on one page
 */
FoldListController = new Class({
    initialize: function() {
        var foldlist_arr = $ES('div.foldlist');

        for (var i = 0; i < foldlist_arr.length; i++) {
            new FoldList(foldlist_arr[i]);
        }
    },

    listen: function(object, method) {
        if (object == 'PageData' && method == 'onRequest') {
            window.page_data.unregisterListener(this);
            FoldListController.destruct();
        }
    }
});
FoldListController.instance = null;
FoldListController.destruct = function() {
    delete this.instance;
    this.instance = null;
}
FoldListController.create = function() {
    if (this.instance == null) {
        this.instance = new FoldListController();
        window.page_data.registerListener(this.instance, 'onRequest');
    }
}

/**
 * FoldList Class
 */
FoldList = new Class({
    /**
     * Constructor
     *
     * @param Element
     */
    initialize: function(container) {
        this.list_item_arr = [];

        var thisObject = this;

        this.unfold_all = $E('a.unfold-all', container);
        if (this.unfold_all) {
            this.unfold_all.removeEvents();
            this.unfold_all.addEvent('click', function(e) {
                new Event(e).stop();

                thisObject.unFoldAll();
            });
        }

        this.fold_all = $E('a.fold-all', container);
        if (this.fold_all) {
            this.fold_all.removeEvents();
            this.fold_all.addClass('sel');
            this.fold_all.addEvent('click', function(e) {
                new Event(e).stop();

                thisObject.foldAll();
            });
        }

        var li_arr = $ES('li.foldlist', container);
        for (var i = 0; i < li_arr.length; i++) {
            this.list_item_arr.push(new FoldListItem(this, li_arr[i]));
        }
    },

    /**
     * Fold all items
     *
     * @return void
     */
    foldAll: function() {
        for (var i = 0; i < this.list_item_arr.length; i++) {
            this.list_item_arr[i].hide();
        }

        this.fold_all.addClass('sel');
        this.unfold_all.removeClass('sel');
    },

    /**
     * Unfold all items
     *
     * @return void
     */
    unFoldAll: function() {
        for (var i = 0; i < this.list_item_arr.length; i++) {
            this.list_item_arr[i].show();
        }

        this.unfold_all.addClass('sel');
        this.fold_all.removeClass('sel');
    },

    /**
     * Show a item
     *
     * @return void
     */
    show: function(list_item) {
        if (list_item.getStatus()) {
            list_item.hide();
        } else {
            list_item.show();
        }

        this.handleControls();
    },

    /**
     * Handle the fold and unfold controls
     *
     * @return void
     */
    handleControls: function() {
        // guard
        if (! this.unfold_all || ! this.fold_all) {
            return;
        }

        var unfold_items = 0;
        for (var i = 0; i < this.list_item_arr.length; i++) {
            if (this.list_item_arr[i].getStatus()) {
                unfold_items++;
            }
        }

        if (unfold_items == this.list_item_arr.length) {
            this.unfold_all.addClass('sel');
            this.fold_all.removeClass('sel');
        } else if (unfold_items) {
            this.unfold_all.removeClass('sel');
            this.fold_all.removeClass('sel');
        } else {
            this.unfold_all.removeClass('sel');
            this.fold_all.addClass('sel');
        }
    }
});

/**
 * FoldListItem Class
 */
FoldListItem = new Class({
    /**
     * Constructor
     *
     * @param FoldList, the controller
     * @param Element, a li element
     */
    initialize: function(controller, container) {
        this.container = container;
        
        // the anchor in the list item
        var anchor = $E('a.title', container);

        if (! anchor || anchor.hasClass('detail')) {
            return;
        }

        var thisObject = this;
        anchor.removeEvents();
        anchor.addEvent('click', function(e) {
            new Event(e).stop();

            controller.show(thisObject);
        });
    },

    /**
     * Show the list item, add a css class to the list item 
     *
     * @return void
     */
    show: function() {
        this.container.addClass('sel');
    },

    /**
     * Hide the list item, remove a css class from the list item 
     *
     * @return void
     */
    hide: function() {
        this.container.removeClass('sel');
    },

    getStatus: function() {
        return this.container.hasClass('sel')
    }
});

/**
 * SolutionModule Class
 */
SolutionModule = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        var container = $('solution-module');

        // guard: stop if th container is not found
        if (! container) {
            return;
        }

        // get the solution containers
        this.product_arr = $ES('div.solution-item', container);

        // guard: stop if there is one or none solution containers
        if (! this.product_arr.length || this.product_arr.length == 1) {
            return;
        }

        // find the currently active solution
        this.active = 0;
        for (var i = 0; i < this.product_arr.length; i++) {
            if (this.product_arr[i].getStyle('display') == 'block') {
                this.active = i;
            }
        }

        // the scope indicator
        this.scope = $E('div.scope', container);

        // start the slideshow
        var thisObject = this;
        this.interval = this.next.periodical(8000, thisObject);

        // add actions to the previous and next anchors
        var thisObject = this;
        var next_anchor = $E('a.next', container);
        var previous_anchor = $E('a.previous', container);

        // guard: stop if the anchors are not found
        if (! next_anchor || ! previous_anchor) {
            return;
        }

        // remove the ajax call that was added by the Pager
        next_anchor.removeEvents();
        next_anchor.addEvent('click', function(e) {
            new Event(e).stop();

            $clear(thisObject.interval);
            thisObject.next();
        });

        // remove the ajax call that was added by the Pager
        previous_anchor.removeEvents();
        previous_anchor.addEvent('click', function(e) {
            new Event(e).stop();

            $clear(thisObject.interval);
            thisObject.previous();
        });
    },

    /**
     * Show the next solution
     *
     * @return void
     */
    next: function() {
        var index = 0;

        if (this.active < (this.product_arr.length -1)) {
            index = this.active + 1;
        }

        this.show(index);
    },

    /**
     * Show the previous solution
     *
     * @return void
     */
    previous: function() {
        var index = this.product_arr.length - 1;

        if (this.active) {
            index = this.active - 1;
        }

        this.show(index);
    },

    /**
     * Show a solution
     *
     * @param integer, the index of the solution to show
     * @return void
     */
    show: function(index) {
        this.product_arr[this.active].setStyle('display' , 'none');

        this.active = index;
        this.product_arr[this.active].setStyle('display' , 'block');

        this.scope.setText((index + 1) +'/'+ this.product_arr.length);
    },

    /**
     * @param string, the name of the object that calls this method
     * @param string, the name of the method that calls this method
     * @return void
     */
    listen: function(object, method) {
        if (object == 'PageData' && method == 'onRequest') {
            $clear(this.interval);
            window.page_data.unregisterListener(this);
            SolutionModule.destruct();
        }
    }
});
SolutionModule.instance = null;
SolutionModule.destruct = function() {
    delete this.instance;
    this.instance = null;
}
SolutionModule.create = function() {
    if (this.instance == null) {
        this.instance = new SolutionModule();
        window.page_data.registerListener(this.instance, 'onRequest');
    }
}

/**
 * NewsModule Class
 * This Module is used on the home page, and is a controller for the NewsItems
 * A list of news items, on hover list item show the corresponding news item in the viewport
 */
NewsModule = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        // news module is ony present at the homepage
        if (document.body.id != 'home') {
            return false;
        }

        var container = $('news-module');

        // guard: stop if th container is not found
        if (! container) {
            return false;
        }

        // to store the currently active module item
        this.active = null;

        // get all list items
        var li_arr = $ES('ul.menu li', container);

        for (var i = 0; i < li_arr.length; i++) {
            var news_item = new NewsModuleItem(this, li_arr[i]);

            // if the list itemhas a ccs class active, the store this news item as the cuurently active one
            if (li_arr[i].hasClass('sel')) {
                this.active = news_item;
            }
        }
    },

    /**
     * Show the given NewsModuleItem and hide the currently active
     *
     * @param NewsItem
     * @return void
     */
    show: function(module_item) {
        // do nothing if the given NewsModuleItem is the same as the currently active
        if (this.active == module_item) {
            return;
        }

        // hide the currently active
        if (this.active) {
            this.active.hide();
        }

        this.active = module_item;
        this.active.show();
    },

    listen: function(object, method) {
        if (object == 'PageData' && method == 'onRequest') {
            window.page_data.unregisterListener(this);
            NewsModule.destruct();
        }
    }
});
NewsModule.instance = null;
NewsModule.destruct = function() {
    delete this.instance;
    this.instance = null;
}
NewsModule.create = function() {
    if (this.instance == null) {
        this.instance = new NewsModule();
        window.page_data.registerListener(this.instance, 'onRequest');
    }
}

/**
 * NewsModuleItem Class
 * A news item consist of two pieces a listitem and the actual news item
 */
NewsModuleItem = new Class({
    /**
     * Constructor
     *
     * @param NewsModule, the controller
     * @param Element, a li element
     */
    initialize: function(controller, listitem) {
        this.listitem = listitem;

        // get the news item
        this.container = $('news-module-item-'+ this.listitem.id.replace('news-module-listitem-', ''));

        // the anchor in the list item
        var anchor = $E('a', this.listitem);
        var thisObject = this;
        anchor.addEvent('mouseover', function() {
            controller.show(thisObject);
        });
        anchor.addEvent('click', function(e) {
            new Event(e).stop();
        });
    },

    /**
     * Show the news item and add a css class to the list item 
     *
     * @return void
     */
    show: function() {
        this.listitem.addClass('sel');
        this.container.setStyle('display', 'block');
    },

    /**
     * Hide the news item and remove a css class from the list item 
     *
     * @return void
     */
    hide: function() {
        this.listitem.removeClass('sel');
        this.container.setStyle('display', 'none');
    }
});