/**
 * Pager Class
 */
Pager = new Class({

    /**
     * Constructor
     *
     * @param string, the base url, corresponds with the php constant BASE_URL
     */
    initialize: function(base_url) {
        this.base_url = base_url;

        this.parseExternalLinks();

        new MainMenu();
        new CourseCategories();
        new Faq();
        new Sitemap();
    },

    /**
     * @param Element, optional HTML element
     * @return void
     */
    parseExternalLinks: function(root) {
        if (! $type(root)) {
            root = $(document.body);
        }

        // the regular expressions a href has to match to open in a new window/tab
        var file_re       = /\.[a-z0-9]{2,4}$/i;
        var javascript_re = /^javascript\:/;
        var http_re       = /^https?\:\/\//i;
        var mailto_re     = /^mailto\:/;

        // get all anchors and loop through them
        var anchor_arr = root.getElements('a');
        for (var i = 0; i < anchor_arr.length; i++) {
            var href = anchor_arr[i].get('href');

            if (http_re.test(this.base_url)) {
                href = href.replace(this.base_url, '');
            }

            // if the href does not match go to the next anchor in the array
            // a mailto href with an email address will match the re for a file
            if (mailto_re.test(href) || javascript_re.test(href) || ! (file_re.test(href) || http_re.test(href))) {
                continue;
            }

            // open the uri in a new window at the onclick event
            anchor_arr[i].addEvent('click', function(e) {
                // stop default behavior
                new Event(e).stop();

                // open a new window/tab
                window.open(this.get('href'), '_blank');
            });
        }
    }
});

/**
 * Sitemap Class
 */
Sitemap = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        var root = $('sitemap');

        if (! root) {
            return;
        }

        var anchor_arr = root.getElements('a.expand');
        for (var i = 0; i < anchor_arr.length; i++) {
            this.addEvent(anchor_arr[i]);
        }
    },

    addEvent: function(anchor) {
        var li = anchor.getParent('li');

        // guard
        if (! li) {
            return;
        }

        anchor.addEvent('click', function(e) {
            new Event(e).stop();

            if (li.hasClass('expanded')) {
                li.removeClass('expanded');
            } else {
                li.addClass('expanded');
            }
        });
    }
});

/**
 * Faq Class
 */
Faq = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        var ul_category = $(document.body).getElement('ul.faq-list');

        if (! ul_category) {
            return;
        }

        this.active_category = null;
        this.active_question = null;

        var category_arr = ul_category.getElements('li.category');
        var question_arr = ul_category.getElements('li.question');

        for (var i = 0; i < category_arr.length; i++) {
            this.addCategoryEvent(category_arr[i]);
        }

        for (var i = 0; i < question_arr.length; i++) {
            this.addQuestionEvent(question_arr[i]);
        }
    },

    setActiveCategory: function(category) {
        if (this.active_category) {
            this.active_category.removeClass('expanded');
        }

        if (this.active_category != category) {
            this.setActiveQuestion(null);

            this.active_category = category;
            this.active_category.addClass('expanded');
        } else {
            this.active_category = null;
        }
    },

    addCategoryEvent: function(category) {
        if (category.hasClass('expanded')) {
            this.active_category = category;
        }

        var thisObject = this;
        var anchor = category.getElement('a.category');
        anchor.addEvent('click', function(e) {
            new Event(e).stop();

            thisObject.setActiveCategory(category);
        });
    },

    setActiveQuestion: function(question) {
        if (this.active_question) {
            this.active_question.removeClass('expanded');
        }

        if (this.active_question != question) {
            this.active_question = question;
        } else {
            this.active_question = null;
        }

        if (this.active_question) {
            this.active_question.addClass('expanded');
        }
    },

    addQuestionEvent: function(question) {
        if (question.hasClass('expanded')) {
            this.active_question = question;
        }

        var thisObject = this;
        var anchor = question.getElement('a.question');
        anchor.addEvent('click', function(e) {
            new Event(e).stop();

            thisObject.setActiveQuestion(question);
        });
    }
});

/**
 * MainMenu Class
 * Drop down main menu
 */
MainMenu = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        var container = $('nav-main');

        // guard
        if (! container) {
            return;
        }

        // get all anchors and loop through them
        var anchor_arr = container.getElements('a.main');
        for (var i = 0; i < anchor_arr.length; i++) {

            // open the uri in a new window at the onclick event
            anchor_arr[i].addEvent('click', function(e) {
                // stop default behavior
                new Event(e).stop();

                body.addClass('expanded-submenu');
            });
        }

        // hide menu: empty menu class
        var body = $(document.body);

        body.addEvent('click', function() {
            body.removeClass('expanded-submenu');
        });
    }
});

/**
 * CourseCategories Class
 */
CourseCategories = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        this.course_arr = $(document.body).getElements('div.course-container');
        this.active = null;

        if (! this.course_arr) {
            return;
        }

        for (var i = 0; i < this.course_arr.length; i++) {
            this.createCategory(this.course_arr[i]);
        }
    },

    /**
     * @param Element
     * @return void
     */
    createCategory: function(category) {
        if (category.hasClass('expanded')) {
            this.active = category;
        }

        var thisObject = this;
        var anchor = category.getElement('h4 a');
        anchor.addEvent('click', function(e) {
            new Event(e).stop();
            this.blur();

            thisObject.setActive(category);
        });
    },

    /**
     * @param Element
     * @return void
     */
    setActive: function(category) {
        if (this.active) {
            this.active.removeClass('expanded');
        }

        this.active = category;
        this.active.addClass('expanded');
    }
});

/**
 * Function called by Flash
 */
function expandHeader() {
    var fx = new Fx.Morph('top', {duration: 1200, transition: Fx.Transitions.linear});
    fx.start({
        'height': [130, 300]
    });
}

/**
 * Function called by Flash
 */
function shrinkHeader() {
    var fx = new Fx.Morph('top', {duration: 1200, transition: Fx.Transitions.linear});
    fx.start({
        'height': [300, 130]
    });
}

/**
 * Function called by Flash
 */
function expandHeaderVMBO() {
    var fx = new Fx.Morph('top', {duration: 1200, transition: Fx.Transitions.linear});
    fx.start({
        'height': [130, 330]
    });
}

/**
 * Function called by Flash
 */
function shrinkHeaderVMBO() {
    var fx = new Fx.Morph('top', {duration: 1200, transition: Fx.Transitions.linear});
    fx.start({
        'height': [330, 130]
    });
}