/*globals $$: false, $: false, Request: false, Lightbox: false, Class: false, Accordion: false */
/*globals Showcase: false, Asset: false, Element: false, $defined: false, URI: false, $merge: false */
var acc;
var RAccordion = new Class({
    // A recursive accordion builder... I'm not sure why this
    // is a class, really it could just be a function
    initialize: function (container, toggles, elements, level, options) {
        this.container = $(container);
        this.toggleSelector = toggles;
        this.elementSelector = elements;
        this.level = level || 0;
        this.options = options || {};
        this.makeAccordion();
    },

    display: function (path) {
        var components = path.split('.').map(Number),
            len = components.length,
            acc = this,
            i, n;

        for (i = 0; i < len; i++) {
            n = components[i];
            acc.accordion.display(n);
            acc = acc.children[n];
            if (!acc) { break; }
        }
    },

    makeAccordion: function () {
        var toggles = this.container.getChildren(this.toggleSelector),
            elements = this.container.getChildren(this.elementSelector),
            ra = this;

        $$(toggles[0], elements[0]).addClass('active');

        this.accordion = new Accordion(toggles, elements, this.options);

        this.accordion.addEvents({
            // The onActive and onComplete events added to the stack here to
            // attempt to address some of the css issues.
            active: function (toggle, element) {
                var parent = toggle.getParent();

                $$(toggle, element).addClass('active');

                if (parent.getStyle('height') !== 0) {
                    parent.setStyle('height', '');
                }
            },

            background: function (toggle, element) {
                $$(toggle, element).removeClass('active');
            },

            complete: function (a) {
                var height; 

                if (a != (void 0)) {
                    height = 0;

                    a.getParent().getChildren().each(function (e) {
                        height = height + e.offsetHeight;
                    });

                    if (height !== a.getParent().offsetHeight &&
                        a.getParent().offsetHeight !== 0) {

                        a.getParent().setStyle('height','');
                    }
                }
            }
        });

        var children = this.children = [], level = this.level;

        elements.each(function (el, i) {
            var toggles, elements;

            toggles = el.getChildren(this.toggleSelector);
            elements = el.getChildren(this.elementSelector);

            if (toggles.length && elements.length) {
                children[i] = new RAccordion(el, this.toggleSelector, this.elementSelector, level + 1, this.options);
            }
        }, this);
    }
});

function setupSlideshow(element, urls) {
    element = $(element);
    urls = urls.map(function (url) {
        return "/umbraco/Imagegen.ashx?width=690&image="+encodeURIComponent(url);
    });
    
    var list, first, images, play, previous, next, showcase;

    list = new Element('ul', {'class': "slideshow"});
    first = new Element('li').adopt(new Asset.image(urls[0]));     
    element.grab(list.adopt(first), 'top');
    showcase = new Showcase(element.getElements('li'));

    images = new Asset.images(urls.slice(1), {
        onProgress: function (counter, index) {
            var item = new Element('li').grab(this);
            list.grab(item);
            showcase.adopt(item);
        }
    });

    play = element.getElements('a.play');
    previous = element.getElements('a.previous');
    next = element.getElements('a.next');

    showcase.addEvent('toggle', function () {
        var name = this.playing ? "pause" : "play";
        play.set('text', name).set('class', name);
    });

    play.addEvent('click', function (event) {
        event.stop();
        showcase.toggle();
    });

    previous.addEvent('click', function (event) {
        event.stop();
        showcase.previous();
    });

    next.addEvent('click', function (event) {
        event.stop();
        showcase.next();
    });

    showcase.play();
}

window.addEvent('domready', function () {
    var lightbox = new Lightbox({ assetBaseUrl: "/scripts/slimbox/" }),
        hash,
        technicalDocs;

    $$('a[href^=http:], a[href$=.pdf]').addEvent('click', function (e) {
        e.stop();
        return window.open(this.href);
    });

    if ((technicalDocs = $('TechnicalInformation'))) { 
        if (technicalDocs.hasClass('brochureList')) {
            acc = new RAccordion('TechnicalInformation', '.toggle', '.item', null,  {
                alwaysHide: true
            });

        } else {
            acc = new RAccordion('TechnicalInformation', '.toggle', '.element', null, {
                alwaysHide: true,
                show: -1
            });

            if ((hash = new URI().get('fragment')|| '0.0')) {
                acc.display(hash);
            }
        }
    }
});

