/**
 * Pager
 *
 * @extends nl.code.pager.Pager
 */
SitePager = new Class({
    Extends: nl.code.pager.Pager,

    /**
     * Constructor
     *
     * @param string, the base url of this project
     */
    initialize: function(base_url) {
        // call parent constructor
        this.parent(base_url);

        // settings
        nl.code.pager.PageData.show_loading = false;

        this.scanContent($(document.body));
    },

    /**
     * Set the content of the page
     *
     * @param JSON
     * @param string
     * @return void
     */
    setContent: function(data, text) {
        this.parent(data, text);
    },

    /**
     * Scan the inner html of an element
     *
     * @param Element, the element to scan
     */
    scanContent: function(root) {
        // make sure that links to an external website open up in a new window
        nl.code.pager.PageData.parseExternalLinks(root);

        // create page specific objects
        // this.page_object_arr.push(new Streamer());
        this.page_object_arr.push(new EmailPrint());
        this.page_object_arr.push(new CalcasaOnline());
        this.page_object_arr.push(new ElementFolder('ul#faq-list', 'li', 'a', 'expanded', false));
    }
});

/**
 * CalcasaOnline Class
 */
CalcasaOnline = new Class({
    /**
     * Constructor
     */
    initialize: function() {

        var submit_anchor_arr = $(document.body).getElements('a.submit-next');

        for (var i = 0; i < submit_anchor_arr.length; i++) {
            this.createSubmitAnchor(submit_anchor_arr[i]);
        }

        var pay_anchor_arr = $(document.body).getElements('a.pay');

        for (var i = 0; i < pay_anchor_arr.length; i++) {
            this.createPayAnchor(pay_anchor_arr[i]);
        }
    },

    /**
     * @param Element, an anchor
     * @return void
     */
    createPayAnchor: function(anchor) {
        anchor.addEvent('click', function(event) {
            //event.stop();

            var width = 600;
            var height = 700;
            var x = (screen.width - width) / 2;
            var y = (screen.height - height) / 2;
            var win = window.open(this.get('rel'), 'CALCASA_Waardebepaling', 'height='+ height +',width='+ width +',top='+ y +',left='+ x +',scrollbars=yes,resizable');

            return true;
        });
    },

    /**
     * @param Element, an anchor
     * @return void
     */
    createSubmitAnchor: function(anchor) {
        if (anchor.hasClass('prev') || anchor.hasClass('pay')) {
            return;
        }

        var form = anchor.getParent('form');

        // guard
        if (! form) {
            return;
        }

        anchor.addEvent('click', function(event) {
            event.stop();

            form.submit();
        });
    }
});

/**
 * EmailPrint Class
 */
EmailPrint = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        var print = $(document.body).getElement('.email-print .print');
        var email = $(document.body).getElement('.email-print .email');
        var container = $(document.body).getElement('.send_page_form');

        if (! print || ! email || ! container) {
            return;
        }

        email.addEvent('click', function(event) {
            event.stop();

            container.setStyle('display', 'block');
        });

        print.addEvent('click', function(event) {
            event.stop();

            window.print();
        });
    }
});

/**
 * Streamer Class
 * This is displayed at the homepage, there are multiple streamers, only one is displyed at the time, walk through them in a delay of 40 sec
 */
Streamer = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        this.active_index = 0;
        this.streamer_arr = $(document.body).getElements('div.streamer');

        // guard
        if (! this.streamer_arr.length) {
            return;
        }

        for (var i = 0; i < this.streamer_arr.length; i++) {
            if (this.streamer_arr[i].hasClass('sel')) {
                this.active_index = i;
                break;
            }
        }

        var thisObject = this;
        this.interval = this.slide.periodical(40000, thisObject);
    },

    /**
     * @return void
     */
    slide: function() {
        this.streamer_arr[this.active_index].removeClass('sel');

        if (this.active_index < (this.streamer_arr.length - 1)) {
            this.active_index++;
        } else {
            this.active_index = 0;
        }

        this.streamer_arr[this.active_index].addClass('sel');
    }
});