var FolioViewer = new Class({

    Implements: Options,

    options: {
        delay:                  60,
        duration:               2000,
        pause:                  4500,
        transition:             Fx.Transitions.Cubic.easeIn,
        itemClass:              '.promotion'
    },

    initialize: function(el, options) {
        this.setOptions(options);
        if (!$(el)) {
            return false;
        }
        this.el                 = $(el);
        this.ctr                = 0;
        this.play               = 1;
        this.cascade            = 0;
        this.els                = $$('#' + el + ' dd');
        this.loaded             = this.els.length;
        this.els.each(function(el) {
            var img             = el.getElement('img');
            if (this.ctr > 0) {
                el.setStyle('opacity', 0);
            }
            el.setStyle('left', 0);
            this.ctr++;
        }.bind(this));
        this.ctr                = 0;
        this.cur                = this.els[0];
        window.addEvent('load', function() {
            this.loaded = 0;
        }.bind(this));
        this.autoadvance.delay(this.options.pause, this);
    },

    autoadvance: function() {
        if (!this.play) {
            return false;
        }
        if (this.loaded == 0) {
            var ctr = this.ctr + 1;
            if (ctr == this.els.length) {
                ctr = 0;
            }
            this.swap(this.els[ctr], ctr);
        }
        if (this.play) {
            this.autoadvance.delay(this.options.pause, this);
        }
    },

    loadImage: function(e, img) {
        this.loaded--;
    },

    refresh: function(e) {
        this.els.each(function(el) {
            var img             = el.getElement('img');
            if (img.img) {
                img.img.fireEvent('load');
            }
        });
    },

    update: function() {
    },

    swap: function(cur, ctr)
    {
        if (cur == this.cur) {
            return false;
        }

        this.cur.get('tween', {property: 'opacity', transition: Fx.Transitions.Sine.easeIn, duration: this.options.duration}).start(0);
        cur.get('tween', {property: 'opacity', transition: Fx.Transitions.Sine.easeIn, duration: this.options.duration}).start(1);
        this.cur                = cur;
        this.ctr                = ctr;
        this.update();
    }
});

var flipMenu = new Class({
    parseflip: function(){
        var flip = $$('#flip .flip');
        var fw = $('flip').getCoordinates().width / flip.length;
        var fws = ($('flip').getCoordinates().width - 325) / (flip.length - 1);
        var fx = new Fx.Elements(flip, {wait: false, duration: 400, transition: Fx.Transitions.quadOut});
        flip.each(function(kwick, i){
            var panel = kwick.getElement('.panel');
            panel.get('tween').set('top', -530);
            panel.addEvent('click', function(e) {
                e = new Event(e);
                e.stop();
                panel.get('tween', {transition: Fx.Transitions.Quint.easeOut, duration: 500}).start('top', -530);
            });
            kwick.setStyle('opacity', 1);
            kwick.addEvent('click', function(e) {
                e = new Event(e);
                e.stop();
                panel.get('tween', {transition: Fx.Transitions.Quint.easeOut, duration: 750}).start('top', 0);
            });
            kwick.addEvent('mouseenter', function(e){
                var obj = {};
                obj[i] = {'width': [kwick.getStyle('width').toInt(), 325],
                          'opacity': [1]};
                flip.each(function(other, j) {
                    if (other != kwick) {
                        var w = other.getStyle('width').toInt();
                        if (w != 100) {
                            obj[j] = {'width': [w, fws], 'opacity': [1, 1]};
                        }
                    }
                });
                fx.start(obj);
            });
        });
        $('flip').addEvent('mouseleave', function(e){
            var obj = {};
            flip.each(function(other, j){
                obj[j] = {'width': [other.getStyle('width').toInt(), fw],
                          'opacity': [1]};
            });
            fx.start(obj);
        });
    }
});

window.addEvent('domready', function() {
    $$('a.external').each(function(link) {
        link.target = '_blank';
    });
    if ($('flip')) {
        var flip = new flipMenu();
        flip.parseflip();
    }
});
