window.addEvent('domready', init);
var IE = Browser.Engine.trident;
function init() {
    $$('.delete').addEvent('click', function(e) {
        if (!window.confirm('Potwierdzenie spowoduje usunięcie. Kontynuować?')) {
            e.preventDefault();
        }
    });
    $$('a').each(function(item) {
        if (item.get('href') && item.get('rel') == 'external') item.set('target', '_blank');
    });
    var myTips = new Tips('#panel-last-seen img', {
        'text': function(el) {
            return el.get('alt');
        },
        'className': 'tooltip'
    });
    myTips.addEvent('show', function(tip) {
        tip.set('opacity', 0.25);
        tip.fade('in');
    });
    if ($('table')) new Table();
    ReMooz.assign('#table-rooms h2:first-child + a', {
        'origin': 'img',
        'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
        'resizeFactor': 0.8, // resize to maximum 80% of screen size
        'cutOut': false, // don't hide the original
        'opacityResize': 0.4, // opaque resize
        'dragging': false, // disable dragging
        'centered': true // resize to center of the screen, not relative to the source element
    });
    banners750x100();
}

var Table = new Class({
    initialize: function() {
        this.days = Array();
        this.temp = null;
        this.days[1] = $('table').getFirst('table').getFirst('tbody').clone();
        $$('#table ol > li > a').each(function(item, index) {
            item.addEvent('click', function(e) {
                $$('#table ol > li > a').removeClass('active');
                var loading = new Element('tr').grab(new Element('td', {
                    'class' : 'loading',
                    'colspan' : 8
                }));
                if (index != 7) e.preventDefault();
                if (index < 7) {
                    if (this.temp) {
                        $('table').getFirst('table').dispose();
                        $('table').grab(this.temp);
                        this.upDown();
                        this.temp = null;
                    }
                    $$('#table table > thead th').each(function(item) {
                        item.setStyle('width', item.getSize().x - item.getStyle('padding-left').toInt() - item.getStyle('padding-right').toInt() - item.getStyle('border-right-width').toInt() - item.getStyle('border-left-width').toInt());
                    });
                    item.set('class', 'active');
                    var tbody = $('table').getFirst('table').getFirst('tbody');
                    tbody.empty();
                    if (this.days[index]) {
                        this.days[index].clone().replaces(tbody);
                    } else {
                        tbody.grab(loading);
                        new Request.JSON({url: path + 'infusions/types_panel/data.php', onSuccess: function(response) {
                            this.days[index] = this.fetchDay(response);
                            this.days[index].clone(true, true).replaces(tbody);
                        }.bind(this)}).get({'tab': index - 1});
                    }
                } else if (index == 8) {
                    this.getEff(index);
                }

            }.bind(this));
        }.bind(this));
        this.upDown();
    },

    fetchDay: function(response) {
        var tbody = new Element('tbody');
        if (response == false) {
            var tr = new Element('tr').inject(tbody);
            new Element('td', {
                'text': "Brak typów na ten dzień",
                'colspan': 8
            }).inject(tr);
            return tbody;
        }
        var disc = response[0];
        var book = response[1];
        response.each(function(r, i) {
            if (i > 1) {
                d = disc[r['gd']];
                b = book[r['tb']];
                var tr = new Element('tr');
                if (i % 2 == 0) tr.set('class', 'odd');
                tr.inject(tbody);
                new Element('td', {'text': r['gt']}).inject(tr);
                new Element('td', {
                    'class': 'name',
                    'text': r['p1'] + ' - ' + r['p2']
                }).grab(new Element('img', {
                    'src': path + 'infusions/types_panel/images/disciplines/' + d['dim'],
                    'alt': d['bn'],
                    'title': d['bn']
                }), 'top').inject(tr);
                new Element('td').grab(new Element('img', {
                    'src': path + 'infusions/types_panel/images/nationalities/' + r['gc'] + '.gif',
                    'alt': r['gc'],
                    'title': r['gc']
                })).inject(tr);
                new Element('td', {'text': r['tt']}).inject(tr);
                new Element('td', {'text': r['tc']}).inject(tr);
                var td = new Element('td');
                if (r['gs']) td.set({'class': r['s'] ? 'win' : 'lose', 'title': r['gs']});
                td.inject(tr);
                var td = new Element('td', {'class': 'ans'});
                if (r['a']) td.grab(new Element('a', {'href': path + 'infusions/analysises_panel/analysis.php?id=' + r['a'], 'text': 'Analiza'}));
                td.inject(tr);
                new Element('td').grab(new Element('a', {'href': b['bu'], 'rel': 'external'}).grab(
                new Element('img', {
                    'src': path + 'infusions/types_panel/images/bookmakers/' + b['bim'],
                    'alt': b['bn'],
                    'title': b['bn']
                }))).inject(tr);
            }
        });
        return tbody;
    },

    upDown: function() {
        $('t-up').addEvent('click', function(e) {
            $('table').setStyle('height', '195px');
        });
        $('t-down').addEvent('click', function(e) {
            $('table').setStyle('height', 'auto');
        });
    },

    getEff: function(index, date) {
        new Request.HTML({url: path + 'infusions/types_panel/effectivity.php' + ($chk(date) ? date : ''), onSuccess: function(response) {
            this.days[index] = response[0];
            $('table').getFirst('table').getFirst('tbody').empty();
            this.temp = $('table').getFirst('table').clone(true, true);
            $('table').getFirst('table').dispose();
            $('table').grab(this.days[index].clone(true, true));
            $('eff_month_changer').addEvent('change', function(e) {
                this.getEff(index, '?month=' + e.target.get('value') + '&year=' + $('eff_year_changer').get('value'));
            }.bind(this));
            this.upDown();
            new SortingTable($('table').getFirst('table'), {
                zebra: true,                        // Stripe the table, also on initialize
                details: false,                     // Has details every other row
                dont_sort_class: 'nosort'          // Class name on th's that don't sort                
            });
        }.bind(this)}).get();
    }
});