window.addEvent('domready', function() {
    HrefParser.check();

    $$('#box_menu ul.folded').each(function(el) {
        el.getElements('li').each(function(li) {
            li.addEvent('click', function(e) {
                new Event(e).stop();
                var a = li.getChildren('a');
                if($chk(a[0])) {
                    window.location.href = a[0].get('href');
                }
            });

            var ul = li.getChildren('ul');
            if(ul.length > 0) {
                li._ul = $(ul[0]);
                li._ul.level = li._ul.get('class').replace('level_', '').toInt();
                li.addEvent('mouseenter', function(e) {
                    new Event(e).stop();
                    var c = this.getCoordinates(this.getParent());
                    this._ul.setStyles({
                        'display' : 'block'
                    });
                    this._ul.position({
                        'relativeTo' : this,
                        'position' : 'topRight',
                        'offset' : {
                            'x' : -1,
                            'y' : ( 1 == this._ul.level ? -1 : -2 )
                        }
                    });
                });
                li.addEvent('mouseleave', function(e) {
                    new Event(e).stop();
                    this._ul.setStyle('display', 'none');
                });
            }
        });
        var uls = el.getElements('ul');
        for(var x = uls.length - 1; x >= 0; --x)
            uls[x].setStyle('display', 'none');
    });


    $$('.box.slider').each(function(slider) {
        var podlist = slider.getElements('.product');
        if(podlist.length > 1) {
            slider.getElements('hr').setStyle('display', 'none');
            podlist.setStyle('display', 'none');
            podlist[0].setStyles({
                'display' : 'block',
                'vibility' : 'hidden'
            });
        }
    });

    $$('#box_polls dd .bar .filling.animate').each(function(el) {
        el._width = el.getSize().x;
        el.setStyle('width', '0px');
        el._fx = new Fx.Tween(el, {
            'unit' : 'px',
            'transition' : 'bounce:out'
        });
        ( function() {
            if($chk(this._fx)) this._fx.start('width', this._width);
        } ).delay(50, el);
    });

    new Tips('.tooltip', {
        'className' : 'tool-tip'
    });

    if($chk($('box_productcomments')) && 0 == $$('#box_productcomments ul.input_error').length) {
        $$('#box_productcomments form.comment').addClass('none');
        if(0 == $$('#box_productcomments h5').length)
            $('box_productcomments').addClass('none');
    }

    $$('#box_productfull .additionalinfo .addcomment').addEvent('click', function(e) {
        new Event(e).stop();
        $$('#box_productcomments form.comment').removeClass('none');
        $('box_productcomments').removeClass('none');
        if($chk($('commentform'))) new Fx.Scroll(window).toElement($('commentform'));
    });

    $$('#box_productsearch.folden').each(function(el) {
        new FoldenBox(el);
    });
});

window.addEvent('load', function() {
    HrefParser.check();

    $$('.box.slider').each(function(el) {
        if(el.getElements('.product').length < 2) return;

        el.getElements('hr').destroy();
        var innerbox = el.getElement('.innerbox');
        if(!$chk(innerbox)) return;

        var outterdiv = new Element('div', {
            'styles' : {
                'overflow' : 'hidden',
                'position' : 'relative'
            }
        }).inject(innerbox);

        var innerdiv = new Element('div').inject(outterdiv);
        var outter_size = outterdiv.getSize();
        var width = 0;
        var scroll_step = outter_size.x;

        el.getElements('.product')
            .setStyle('display', 'block')
            .inject(innerdiv)
            .setStyle('width', outter_size.x + 'px')
            .each(function(div) {
            width += outter_size.x;
        });

        outterdiv.setStyle('width', outter_size.x + 'px');
        innerdiv.setStyle('width', width + 'px');
        innerdiv.getChildren('.product').setStyle('float', 'left');

        outter_size = outterdiv.getSize();
        outterdiv.setStyle('height', outter_size.y + 'px');
        innerdiv.setStyles({
            'height' : outter_size.y + 'px',
            'position' : 'absolute',
            'left' : '0px',
            'top' : '0px'
        });

        var left = new Element('img', {
            'src' : base_href + 'public/images/1px.gif',
            'class' : 'arrow_left'
        }).inject(outterdiv);

        var right = new Element('img', {
            'src' : base_href + 'public/images/1px.gif',
            'class' : 'arrow_right'
        }).inject(outterdiv);

        left.fade('hide');
        right.fade('hide');

        left._outter = right._outter = outterdiv;
        outterdiv._scroll_step = scroll_step;
        outterdiv._left = left;
        outterdiv._right = right;
        outterdiv._n = 0;
        outterdiv._max = innerdiv.getChildren('.product').length - 1;
        outterdiv._inner = innerdiv;
        outterdiv._fx = new Fx.Tween(innerdiv, {
            'duration': 600,
            'transition' : 'bounce:out'
        });

        outterdiv.addEvent('mouseenter', function(e) {
            if(this._n > 0)
                outterdiv._left.fade('in');
            if(this._n < this._max)
                outterdiv._right.fade('in');
        });

        outterdiv.addEvent('mouseleave', function(e) {
            outterdiv._left.fade('out');
            outterdiv._right.fade('out');
        });

        outterdiv.scrollToLeft = function() {
            if(this._n > 0) {
                this._n--;
                this._fx.start('left', -1 * this._n * this._scroll_step + 'px');

                this._right.fade('in');
                if(0 == this._n)
                    this._left.fade('out');
            }
        };

        outterdiv.scrollToRight = function() {
            if(this._n < this._max) {
                this._n++;
                this._fx.start('left', -1 * this._n * this._scroll_step + 'px');

                this._left.fade('in');
                if(this._max == this._n)
                    this._right.fade('out');
            }
        };

        left.addEvent('click', function(e) {
            new Event(e).stop();
            this._outter.scrollToLeft();
        });

        right.addEvent('click', function(e) {
            new Event(e).stop();
            this._outter.scrollToRight();
        });

        lh = left.getSize().y;
        if(lh < 1) lh = 10;
        rh = right.getSize().y;
        if(rh < 1) rh = 10;

        left.setStyle('top', Math.ceil( ( outter_size.y - lh ) / 2 ) + 'px');
        right.setStyle('top', Math.ceil( ( outter_size.y - rh ) / 2 ) + 'px');
    });

    $$('img.productimg').each(function(img) {
        if(img.get('class').match(/gallery_[0-9]+/)) {
            var id = img.get('class').replace(/.*gallery_([0-9]+).*/, 'prodimg$1');
            if(id.match(/^prodimg[0-9]+$/) && $(id)) {
                img.addEvent('click', function(e) {
                    new Event(e).stop();
                    var f = new Event(e);
                    f.target = this._related_img;
                    this._related_img.fireEvent('click', f);
                }).setStyle('cursor', 'pointer')._related_img = $(id);
            }
        }
    });

    if($chk(window.Milkbox) && 'class' == $type(window.Milkbox))
        new Milkbox();

    if(Browser.Engine.webkit) {
        $$('.centercol input', '.centercol button').each(function(el) {
            if(el.get('value').length > 0 && el.get('name').match(/(mail|pass|login)/)) {
                el.focus();
                el.blur();
            }
        });
    }

    /* layout fix * /
    if($chk($('box_mainproducts')))
        $$('#box_mainproducts table.products tr.fewperrow').each(function(tr) {
            y = 0;
            tr.getElements('.product .details img').getSize().each(function(el) { if(el.y > y) y = el.y });
            if(y > 0) tr.getElements('.product .details img').each(function(el) {
                var t = Math.ceil( (y - el.getSize().y) / 2);
                var b = y - el.getSize().y - t;
                el.setStyle('margin-top',  t + 'px');
                el.setStyle('margin-bottom',  b + 'px');
                el.setStyle('visibility',  'visible');
            });
        });
    /* */
});







var FoldenBox = new Class({

    'box' : false,
    'arrow' : false,
    'folden' : false,
    'innerbox' : false,

    'initialize' : function(box) {
        this.box = $(box);

        this.arrow = new Element('span', {
            'class' : 'foldenbox_arrow',
            'html' : '&#x25bc;'
        }).inject( this.box.getElements('.boxhead').pop() );

        this.arrow._class = this;

        this.arrow.addEvent('click', function(e) {
            new Event(e).stop();
            var x = this._class.innerbox.getStyle('height').toInt();
            if(0 === x) {
                this._class.innerbox._fx.start('height', this._class.innerbox._height);
                this.set('html', '&#x25b2');
            } else {
                this._class.innerbox._fx.start('height', 0);
                this.set('html', '&#x25bc');
            }
        });

        this.innerbox = this.box.getElements('.innerbox').pop();

        this.innerbox._height = this.innerbox.getSize().y;
        this.innerbox.setStyles({
            'height' : '0px',
            'overflow' : 'hidden',
            'min-height' : '0px'
        });

        this.innerbox._fx = new Fx.Tween(this.innerbox, {
            'duration' : 333,
            'transition' : 'quad:out'
        });
    }

});



