/**
 * 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) {

        this.page_object_arr.push(new Resizer());
        this.page_object_arr.push(new PageOpener());
        this.page_object_arr.push(new VideoPlayer());

        // create lightbox links
        nl.code.lightbox.Lightboxer.parseLightboxLinks(this, root, {
            ajax: {
                css_class: 'js-ajax-lightbox',      // string, extra css class name that will be set on the lightbox
                overlay_opacity: 0.4,               // value between 0 and 1
                overlay_color: '#000000',           // css hex rgb color code (#ff0000)
                show_loading: true,                 // show a loading spinner
                animation_class: LightboxAnimation, // a animation class
                autostart_animation: true,          // start if the content is not available
                autostart_animation_width: 200,     // start width (when auto started)
                autostart_animation_height: 200     // start height (when auto started)
            }
        });
    }
});

/**
 * Resizer Class
 */
Resizer = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        this.anchor_container = $('resizer');
        this.content_container = $('content-main');

        // guard
        if (! this.anchor_container || ! this.content_container) {
            return;
        }

        // guard (element exists allready)
        if (this.anchor_container.getElement('span')) {
            return;
        }

        var label = new Element('span', {
            'text': 'Text '
        });
        label.inject(this.anchor_container);
        this.size_arr = ['xsmall', 'small', 'medium', 'large', 'xlarge', 'xxlarge'];
        this.current_size = 'small';

        var thisObject = this;
        var plus_anchor = new Element('a', {
            'text': '[+]',
            'href': 'javascript: void(null);'
        });
        plus_anchor.inject(this.anchor_container);
        plus_anchor.addEvent('click', function(e) {
            new Event(e).stop();

            thisObject.resize(1);
        });

        var slash = new Element('span', {
            'text': '/'
        });
        slash.inject(this.anchor_container);

        var minus_anchor = new Element('a', {
            'text': '[-]',
            'href': 'javascript: void(null);'
        });
        minus_anchor.inject(this.anchor_container);
        minus_anchor.addEvent('click', function(e) {
            new Event(e).stop();

            thisObject.resize(-1);
        });
    },

    /**
     * param string, "+" or "-"
     * @return void
     */
    resize: function(delta_index) {
        var current_index = this.size_arr.indexOf(this.current_size);
        var index = current_index + delta_index;

        if (index >= 0 && index < this.size_arr.length) {
            this.content_container.removeClass(this.current_size);
            this.current_size = this.size_arr[index];
            this.content_container.addClass(this.current_size);
        }
    }
});

/**
 * PageOpener Class
 */
PageOpener = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        var anchor_arr = [];

        anchor_arr.push($(document.body).getElement('ul#nav-top-right li.english a'));
        anchor_arr.push($(document.body).getElement('ul#nav-sub-left li.inbeeld a'));
        anchor_arr.push($(document.body).getElement('div#introduction p.image a'));
        anchor_arr.push($(document.body).getElement('div#introduction div.button-container a'));

        for (var i = 0; i < anchor_arr.length; i++) {
            // guard
            if (anchor_arr[i] === false) {
                continue;
            }
            // alert("test");
            // alert(anchor_arr[i]);
            this.addEvent(anchor_arr[i]);
        }
    },

    /**
     * @param Element
     * @return void
     */
    addEvent: function(anchor) {
        var thisObject = this;

        // guard
        if (! anchor) {
            return;
        }

        anchor.addEvent('click', function(event) {
            event.stop();
            thisObject.open(anchor);
        });
    },

    /**
     * Popup Function
     *
     * @param Element
     * @return void
     */
    open: function(anchor) {
        // alert('foo');
        var href = anchor.get('href');
        var li = anchor.getParent('li');
        var div = anchor.getParent('div');
        var p = anchor.getParent('p');
        var scroll = 'yes';
        var width = 720;
        var height = 500;

        if (li && li.hasClass('inbeeld')) {
            width = 550;
            height = 400;
            scroll = 'no';
        }

        if (div && div.hasClass('button-container')) {
            width = 550;
            height = 400;
            scroll = 'no';
        }

        if (p && p.hasClass('image')) {
            width = 550;
            height = 400;
            scroll = 'no';
        }

        var mywindow = window.open(href, 'English', 'location=no,status=no,resizable=yes,scrollbars='+ scroll +',width='+ width +',height='+ height);
        mywindow.moveTo(screen.width/2-(width/2),screen.height/2-(height/2));
        // alert();
     }
});

/**
 * VideoPlayer
 */
VideoPlayer = new Class({
    /**
     * Constructor
     */
    initialize: function() {
        // stop if the browser has not the appropriate flash plugin
        if (Browser.Plugins.Flash.version < 8) {
            return;
        }

        // get the flash container
        var container = document.id('video-player');

        // guard: stop if the flash container is not found
        if (!container) {
            return;
        }

        // get the anchor with the video meta data
        var anchor = container.getElement('a');

        // guard: stop if the anchor with video data is not foun
        // in the fre of the anchor is the source of the flv stored
        // in the rel attribute is the source of the preview image stored
        if (!anchor) {
            return;
        }

        // create the swiff object
        var obj = new Swiff(nl.code.pager.Uri.base_uri +'swf/lightbox-video/560x350_player.swf', {
            id: 'gr-player',
            container: container,
            width: 560,
            height: 350,
            params: {
                wmode: 'opaque',
                bgcolor: '#ffffff',
                scale: 'noscale',
                salign: 'tl',
                quality: 'best',
                allowfullscreen: 'true'
            },
            vars: {
                video_path: anchor.get('href')
            }
        });
    }
});

NavFixer = {
    show: function() {
        var nav_container = $('nav-top-container');
        var flash_container = $('nav-flash');

        nav_container.setStyle('z-index', 100);
        flash_container.setStyle('z-index', 10);
    },

    hide: function() {
        var nav_container = $('nav-top-container');
        var flash_container = $('nav-flash');

        nav_container.setStyle('z-index', 10);
        flash_container.setStyle('z-index', 100);
    }
};

FlashInterface = {};
FlashInterface.openLightbox = function(url) {

    // create dummy trigger anchor
    var dummy_anchor = new Element('a', {
        'href': url
    });
    dummy_anchor.inject(document.id(document.body));

    dummy_anchor.request = nl.code.pager.PageData.request(url, nl.code.lightbox.Lightboxer, 'lightbox_id=video-lightbox', dummy_anchor);
    nl.code.lightbox.Lightboxer.openLightbox(
        'video-lightbox',
        0,
        0,
        '',
        dummy_anchor,
        nl.code.lightbox.AjaxLightbox,
        {}
    );
};


