var homeRotator;

window.addEvent('domready', function () {

    homeRotator = new Rotator('banner', { delay: 15000, speed: 500 });

    $$('a.search').addEvent('click', function (e) {
        e.stop();
        var parameters = '';
        var url = '/products/computers/';
        var first = true;
        $$('select[value!=0]').each(function (item, index) {
            if (item.get('value') != 0) {
                parameters += (first ? '?' : '&') + item.name + '=' + item.get('value');
                first = false;
            }
        });

        //if (parameters != '')
            window.open(url + parameters, '_self');
    });

    // make featured pod clickable
    $('body').getElements('div.item a.more').each(function (item, index) {
        item.getParent('div.item').addEvents({
            'click': function (e) {
                e.stop();
                window.open(item.href, '_self');
                return false;
            }
        }).setStyle('cursor', 'pointer');
    });
});

Rotator = new Class({
    options: {
        delay: 10000,
        speed: 500
    },
    initialize: function (el, options) {
        this.setOptions(options);
        this.id = el;
        this.el = $(el);

        if (this.el) {
            this.items = this.el.getElements('li');
            this.height = this.el.getSize().y;
            this.count = this.items.length;

            if (this.count > 1) {
                this.paused = false;
                this.complete = true;
                this.index = 0;
                this.next = 0;
                this.previous = -1;
                this.initialized = $defined(this.el) && this.count >= 1 && this.options.speed < this.options.delay;
                this.navigation = new Element('ul').addClass('navigation').inject(this.el, 'after').setOpacity('0.5').addEvents({
                    'mouseout': function () { this.setOpacity('0.5'); },
                    'mouseover': function () { this.setOpacity('1.0'); }
                });

                this.resume();
                if (this.initialized) {

                    // create back/forward
                    $$('div.banner').getElement('a[class=previous]').addEvent('click', function () { this.back(); } .bind(this));
                    $$('div.banner').getElement('a[class=next]').addEvent('click', function () { this.forward(); } .bind(this));


                    $$('#banner li a').addEvent('click', function () {
                        try {
                            pageTrackerCustom._trackEvent('Homepage Banner', 'Click', this.innerHTML);
                        } catch (er) { }
                    });

                    // pause on mouseover
                    //this.el.addEvents({
                    //	'mouseout':  function() { this.resume(); }.bind(this),
                    //	'mouseover': function() { this.pause(); }.bind(this)
                    //});

                    var baseUrl = '';
                    try { baseUrl = Site.Config.Path.Url.Mirror } catch (e) { }
                    this.items.each(function (item, index) {

                        // generate navigation
                        var i = index + 1;
                        var li = new Element('li');
                        var anchor = new Element('a').set('text', i).setStyle('background-image', 'url(' + baseUrl + '/web/resource/img/home/banner/navigation/' + i + '.png)').injectInside(li).addEvent('click', function () {
                            this.go(index);
                        } .bind(this));

                        if (window.ie6)
                            anchor.href = 'javascript:void(0);';

                        li.inject(this.navigation);
                        item.setStyles({ display: 'block', opacity: 0 });

                        // banner click event
                        var a = item.getElement('a[href!=#]');
                        if (a != null && a.href.contains('http://')) {
                            a.addEvent('click', function () {
                                window.open(a.href, '_self');
                                return false;
                            });
                        }
                    } .bind(this));

                    this.navigation.items = this.navigation.getElements('li');
                    this.start();
                }
            } else {
                this.items[0].setStyles({
                    'display': 'block'
                });
                $$('div.banner').getElement('a[class=previous]').setStyle('display', 'none');
                $$('div.banner').getElement('a[class=next]').setStyle('display', 'none');
            }
        }
    },
    go: function (index) {
        if (!this.complete || index == this.previous) return;

        this.pause();
        if (index == -1) {
            this.index = this.index - 1;
            this.index = this.index < 0 ? (this.count - 1) : this.index;
        } else {
            this.index = $defined(index) ? index : this.next;
        }

        this.navigation.items[this.index].addClass('selected');
        this.next = this.index + 1;
        this.next = this.next < 0 || this.next >= this.count ? 0 : this.next;
        this.paused = true; //this.debug('periodical:' + this.periodical + ', previous:' + this.previous + ', index:' + this.index + ', next:' + this.next);

        this.fx = new Fx.Tween(this.items[this.index], {
            duration: this.options.speed,
            onStart: function () {
                this.complete = false;
                this.items[this.index].setStyle('z-index', 99);

                if (this.previous != -1) {
                    this.navigation.items[this.previous].removeClass('selected');
                    this.items[this.previous].setStyle('z-index', 0);
                }
            } .bind(this)
        }).start('opacity', 0, 1).chain(function () {
            if (this.previous != -1)
                this.items[this.previous].setStyle('opacity', 0);

            this.previous = this.index;
            this.complete = true;
            this.paused = false;
            this.resume();
        } .bind(this));

        /** CONTROLS VIDEOS IN THE BANNER **/
        if (this.index != 0) {
            //console.log(this.index + ": Stop video, start slideshow");

            flowplayer().stop();
        }
        else {
            //console.log(this.index + ": Start video, pause slideshow");

            flowplayer().play();
        }
        /** END **/
    },
    start: function () { if (!this.paused) this.go(); },
    back: function () { this.go(-1); },
    forward: function () { this.go(); },
    pause: function () { $clear(this.periodical); },
    resume: function () { this.periodical = this.start.periodical(this.options.delay, this); },
    debug: function (m) { if (window.console) console.log(m); }
});
Rotator.implement(new Options);

