window.addEvent('domready', init);
var IE = Browser.Engine.trident;
var U = new URI(location.pathname, path);
function init() {
    $$('a').each(function(item) {
        if (item.get('href') && (item.get('rel') == 'external' || item.get('title') == 'autolink')) item.set('target', '_blank');
    });
    $$('.delete').addEvent('click', function(e) {
        if (!window.confirm('Potwierdzenie spowoduje usunięcie. Kontynuować?')) {
            e.preventDefault();
        }
    });
    $$('#subscribe', '#unsubscribe').addEvent('click', function(event){
        event.preventDefault();
        form = this.getParent('form');
        form.set('send', {
            'url': basedir + 'infusions/newsletter_panel/data.php?' + this.get('name') + '=true',
            'onSuccess': function(response) {
                Growl.Smoke({text:response});
            }
        }).send();
    });
    var file = U.get('file').toLowerCase();
    file = file.substr(0, file.lastIndexOf('.'));
    file = file.substr(file.lastIndexOf(',') + 1);
    //banner('themes/default/flash/' + file + '.swf');
    if ($('header').getElement('input[type=text]')) {
        $('header').getElement('input[type=text]').addEvent('click', function() {
            this.set('value', '');
        });
    }
    if ($('panel-recommendable')) new Panel();
}

function banner(file) {
    //alert(path + 'exists.php');
    new Request({
        url: path + 'exists.php',
        onSuccess: function(response) {
            if (response == 1) {
                show_banner(file);
            } else {
                show_banner('themes/default/flash/glowna.swf');
            }
        },
        onFailure: function() {
            show_banner('themes/default/flash/glowna.swf');
            //alert("4A. Błąd połączenia\n");
        }
    }).get({'file': file});
}

function show_banner(file) {
    new Swiff(basedir + file, {
        width: 837,
        height: 254,
        params: {
            'wmode': 'transparent',
            'bgcolor': 'ffffff',
            'quality': 'high'
        }
    }).inject($('flash'));
}

var Panel = new Class({
    initialize: function() {
        this.shoutsBox = $('panel-recommendable').getElement('div');
        this.shouts = this.shoutsBox.getElement('ol');
        this.upBtn = $('panel-recommendable').getLast('b');
        this.downBtn = $('panel-recommendable').getFirst('b');
        this.speed = 5;
        this.init();
        this.width = this.shouts.getSize().x - this.shoutsBox.getSize().x;
        this.fx = new Fx.Scroll(this.shoutsBox);
        this.downBtn.addEvent('click', function(e) {
            e.preventDefault();
            this.scroll(-1);
        }.bind(this));
        this.upBtn.addEvent('click', function(e) {
            e.preventDefault();
            this.scroll(1);
        }.bind(this));
    },

    init: function() {
        this.pos = 0;
        this.calculate();
        this.actualStep = 0;
    },

    scroll: function(direction) {
        this.actualStep = (this.actualStep + direction).limit(0, this.scrollSteps.length - 1);
        this.fx.start(this.scrollSteps[this.actualStep], 0);
        console.log(this.scrollSteps);
    },

    calculate: function() {
        this.scrollSteps = [0];
        var width = 0, twidth = 0;
        var shouts = this.shouts.getChildren('li');
        var windowwidth = this.shoutsBox.getSize().x;
        for (var i = 0; i < shouts.length; i++) {
            var elwidth = shouts[i].getComputedSize({
                styles: ['padding', 'border', 'margin'],
                mode: 'horizontal'
            }).totalWidth;
            if (width + elwidth > windowwidth) {
                twidth += width;
                this.scrollSteps.push(twidth);
                width = 0;
            }
            width += elwidth;
        }
        if (this.scrollSteps[this.scrollSteps.length - 1] != twidth) this.scrollSteps.push(twidth);
    }

});