// create namespace
Core.createNamespace('nl.code.pager');

/**
 * Pager Class
 *
 * Events: 'updateElement', 'updatePage'
 *
 * @abstract
 */
nl.code.pager.Pager = new Class({
    /**
     * @implements Events
     */
    Implements: Events,

    /**
     * @var Array
     */
    page_object_arr: [],

    /**
     * @var string
     */
    template: null,

    /**
     * @var string
     */
    id: null,

    /**
     * Constructor
     *
     * @param base_uri string, the base url of this project
     */
    initialize: function(base_uri) {
        this.template = document.id(document.body).get('id');
        this.id       = this.template;

        // store the base url
        nl.code.pager.Uri.setBaseUri(base_uri);
    },

    /**
     * Set the content of a page
     *
     * @param JSON {id: ,template: ,content: ["html_id1": "contents", "html_id2": "contents"]}
     * @param string
     * @return void
     */
    setContent: function(data, text) {
        try {
            this.template = data.template;
            this.id       = data.id;

            document.id(document.body).set('id', data.template);

            // delete the page objects
            for (var i = 0; i < this.page_object_arr.length; i++) {
                delete this.page_object_arr[i];
            }

            // insert the received html
            for (html_id in data.content) {
                var element = document.id(html_id);

                // guard: if the element does not exist skip
                if (! element) {
                    continue;
                }

                element.empty();
                element.set('html', data.content[html_id]);

                this.scanContent(element);
                this.fireEvent('updateElement', {html_id: html_id});
            }

            this.fireEvent('updatePage', {uri: nl.code.pager.Uri.getHash()});
        } catch (e) {
            //alert('An error occured');
        }
    },

    /**
     * Scan the content of an Element
     *
     * @param Element, the element to update
     */
    scanContent: function(root) {
    }
});