var PromoSlider = new Class({

    Implements: Options,

    options: {
        delay:                  60,
        duration:               500,
        transition:             Fx.Transitions.Cubic.easeIn,
        controls:               false,
        autostart:              true,
        direction:              'left',
        itemClass:              '.promotion',
        widgetUpClass:          'widgetUp',
        widgetDnClass:          'widgetDn'
    },

    initialize: function(els, options) {
        this.setOptions(options);
        $$(els).each(function(el) {
            el.slide            = el.getElement('.slide');
            if (el.slide) {
                el.pos          = 0;
                el.coord        = new Array();
                el.items        = el.slide.getElements(this.options.itemClass);
                el.width        = el.getCoordinates().width;
                el.slide.set('tween', {
                    transition: this.options.transition,
                    duration: this.options.duration
                });
                el.slide.tween('height', el.items[0].getCoordinates().height);
                el.setStyle('height', el.items[0].getCoordinates().height);
                el.delay        = this.options.delay + (5 - $random(1, 10));
                el.pause        = this.pause.periodical(100, this, [el]);
                if (this.options.controls && el.items.length > 1) {
                    var windowUp = new Element('div')
                        .addClass(this.options.widgetUpClass)
                        .addEvent('mouseover', this.hoverOn)
                        .addEvent('mouseout', this.hoverOff)
                        .addEvent('click', this._rewind.bind(this, el));
                    var windowDn = new Element('div')
                        .addClass(this.options.widgetDnClass)
                        .addEvent('mouseover', this.hoverOn)
                        .addEvent('mouseout', this.hoverOff)
                        .addEvent('click', this._advance.bind(this, el));
                    el.grab(windowUp);
                    el.grab(windowDn);
                    windowUp.setStyles({
                        top: 0,
                        left: 0,
                        opacity: 0.2
                    });
                    windowDn.setStyles({
                        bottom: 0,
                        left: 0,
                        opacity: 0.2
                    });
                }
                var x = 0;
                var y = 0;
                for (var loop = 0; loop < el.items.length; loop++) {
                    el.coord[loop] = new Hash({
                        top: y,
                        left: x
                    });
                    x += el.items[loop].getCoordinates().width;
                    y += el.items[loop].getCoordinates().height;
                }

            }
        }.bind(this));
    },

    hoverOn: function(el) {
        this.tween('opacity', 1);
    },

    hoverOff: function(el) {
        this.tween('opacity', 0.2);
    },

    pause: function(el) {
        if (el.delay == 0) { 
            el.delay            = this.options.delay + (5 - $random(1, 10));
            this.advance(el);
        } else {
            el.delay--;
        }
    },

    _advance: function(el) {
        $clear(el.pause);
        this.advance(el);
    },

    _rewind: function(el) {
        $clear(el.pause);
        this.rewind(el);
    },

    rewind: function(el) {
        el.pos--;

        if (el.pos < 0) {
            el.pos = el.items.length - 1;
        }

        this.refresh(el);
    },

    advance: function(el) {
        el.pos++;

        if (el.pos == el.items.length) {
            el.pos = 0;
        }

        this.refresh(el);
    },

    refresh: function(el) {
        switch (this.options.direction) {
            case 'left':
                el.slide.morph({
                    left: -el.coord[el.pos].left,
                    height: el.items[el.pos].getCoordinates().height
                });
                break;

            case 'up':
                el.slide.morph({
                    top: -el.coord[el.pos].top,
                    height: el.items[el.pos].getCoordinates().height
                });
                break;
        }

        el.tween('height', el.items[el.pos].getCoordinates().height);
    }
});

MediaPopup = new Class({

    Implements: Options,

    options: {
        popupClass:             '.popupcontent',
        widgetId:               'widgetclose'
    },

    initialize: function(link, target, options) {
        this.link               = $(link);
        this.target             = $(target);
        this.widget             = null;
        this.overlay            = new Overlay();
        this.setOptions(options);
        this.prepare();
    },
    
    prepare: function() {
        if (!this.link || !this.target) {
            return false;
        }
        this.options.content = this.target.getElement(this.options.popupClass);
        this.options.content.setStyles({
            width:  this.options.width,
            height: this.options.height
        });
        this.link               = $(this.link);
        this.target             = $(this.target);
        this.link.addEvent('click', this.show.bind(this));
        this.target.setStyles({
            width:  this.options.width,
            height: this.options.height,
            top:    50
        });
        this.widget = new Element('div')
            .injectInside(this.target)
            .setProperty('id', this.options.widgetId)
            .setStyle('visibility', 'hidden');
        this.widget.addEvent('click', this.hide.bind(this));
    },

    show: function(event) {
        event = new Event(event).stop();
            this.widget.setStyle('visibility', 'visible');
            this._sendEvent('playpause');
        var y = window.getScrollTop();
        var yoffset = window.getCoordinates().height;
        var woffset = this.target.getCoordinates().height;
        this.target.setStyle('top', y + (yoffset/2 - woffset/2));
        this.target.setStyle('left', window.getCoordinates().width / 2 - this.target.getCoordinates().width / 2);
        this.overlay.show();
    },
    
    hide: function(e) {
        e = new Event(e).stop();
        this._sendEvent('stop');
        this.target.setStyle('left', '-9999px');
        this.widget.setStyle('visibility', 'hidden');
        this.overlay.hide();
    },
    
    trap: function(evt) {
        event = new Event(evt);
        event.stopPropagation();
    },

    _sendEvent: function(type, param) {
        var mv = $(this.options.movie);
        if (mv) {
            mv.sendEvent(type, param);
        }
    }
});

Overlay = new Class({

    initialize: function() {
        /* if (!$('overlay')) {
            this.el = new Element('div')
                .setProperty('id', 'overlay')
                .inject($('layout'), 'after')
                .setStyles({
                    top:             1,
                    left:            1,
                    height:          Window.getCoordinates().height - 2,
                    width:           $(document.body).clientWidth - 2,
                    position:        'absolute',
                    backgroundColor: '#000',
                    opacity:         0,
                    zIndex:          '100'
                });
            window.addEvent('resize', this._resize.bind(this));
            window.addEvent('scroll', this._resize.bind(this));
        } else {
            this.el = $('overlay');
        }
        this.hide(); */
    },

    show: function() {
        if (window.ie6) {
            $$('select').each(function(sel) {
                sel.setStyle('visibility', 'hidden');
            })
        }
        this.el.tween('opacity', 0.8);
    },

    hide: function() {
        if (window.ie6) {
            $ES('select').each(function(sel) {
                sel.setStyle('visibility', 'visible');
            })
        }
        this.el.tween('opacity', 0);
    },
    
    _resize: function() {
        this.el.setStyles({
            top:    window.getScrollTop()+'px',
            height: Window.getCoordinates().height,
            width:  $(document.body).clientWidth
        });
    }
});

HistoryTimeline = new Class({
    
    initialize: function(timeline, marker, gallery, copy) {
        this.slider = new Slider(timeline, marker, {
            snap: true,
            steps: 8,
            onComplete: this.refresh.bind(this)
        });
        this.gallery = $(document.body).getElement(gallery);
        this.copy = $(document.body).getElement(copy);
    },

    refresh: function(step) {
        this.gallery.getElement('.slide').tween('left', -(step * this.gallery.getCoordinates().width));
        this.copy.getElement('.slide').tween('left', -(step * this.copy.getCoordinates().width));
    }
});

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
	}
	
function loadProgress(t) {
	var theTarget = document.getElementById(t);
	theTarget.innerHTML = '<img src=\'/_i/ajax-loader.gif\' alt=\'Loading...\' class=\'loader\' />';
	theTarget.style.visibility = 'visible';
}