window.onload = function() {
    setup_h2();
    setup_nav();
    setup_gallery();
}

function setup_h2() {
    var els = $$('h2');
    
    for ( var i = 0; i < els.length; i++ ) {
        els[i].zof_title_effect = new Fx.Tween(els[i], {
                'property': 'padding-left',
                'duration': '800',
                'link': 'cancel'
        });
        els[i].zof_title_effect.removeEvents('complete');
        els[i].zof_title_effect.addEvent('complete', function(){
            this.removeEvents('complete');
            this.start('0');
        });
        els[i].zof_title_effect.start('50');
    }
}

function setup_nav() {
    var els = $$('#nav a');
    
    document.body.zof_fx = new Fx.Tween(document.body, {
            'property': 'margin-top',
            'duration': 400,
            'link': 'cancel',
            'transition': 'back:in'
        }
    );
    
    for ( var i = 0; i < els.length; i++ ) {
        els[i].href += '?js=1';
        els[i].onclick = function() {
            document.body.zof_fx.removeEvents('complete');
            document.body.zof_loc = this.href;
            document.body.zof_fx.addEvent('complete', function() {
                this.removeEvents('complete');
                window.location.href = document.body.zof_loc;
            });
            document.body.zof_fx.start(-1000);
            
            return false;
        }
    }
}

function setup_gallery() {
    if ( ! $('gallery_main_container') ) {
        return;
    }
    
    var els = $$('.plug_image_gallery_list li');
    $('gallery_container').set({'styles': {'height': '162px'}});

    var gallery = $$('.plug_image_gallery_list')[0];
    gallery.set({'styles': {'width': 152*els.length + 'px'}});
    
    var left_arrow  = new Element('div', {'id': 'left_arrow',  'styles': {'opacity': '0'}});
    var right_arrow = new Element('div', {'id': 'right_arrow', 'styles': {'opacity': '0'}});
    left_arrow.inject(gallery,'top');
    right_arrow.inject(gallery,'bottom');

    $('gallery_container').zof_scroll = new Fx.Scroll($('gallery_container'),{
            'duration': 200,
            'transition': 'linear'
    });

    left_arrow.onmouseover = function() {
        var gal_el = $('gallery_container');
        clearInterval(gal_el.zof_interval);
        gal_el.zof_interval = setInterval( function() {
            var scroll = $(gal_el).getScroll();
            gal_el.zof_scroll.start(scroll.x-50, scroll.y);
        }, 10);
    }
    left_arrow.onmouseout = function() {
        clearInterval($('gallery_container').zof_interval);
    }

    right_arrow.onmouseover = function() {
        var gal_el = $('gallery_container');
        clearInterval(gal_el.zof_interval);
        gal_el.zof_interval = setInterval( function() {
            var scroll = $(gal_el).getScroll();
            gal_el.zof_scroll.start(scroll.x+50, scroll.y);
        }, 10);
    }
    right_arrow.onmouseout = function() {
        clearInterval($('gallery_container').zof_interval);
    }

    gallery.zof_fx = new Fx.Elements($$('#left_arrow', '#right_arrow'), {'link': 'cancel'});
    
    gallery.addEvent('mouseenter', function() {
        this.zof_fx.start({
            '0': {'opacity': [0,1]},
            '1': {'opacity': [0,1]}
        });
        return false;
    });
    
    gallery.addEvent('mouseleave', function() {
        this.zof_fx.start({
            '0': {'opacity': [1,0]},
            '1': {'opacity': [1,0]}
        });
        return false;
    });

    new ZofBox('#gallery_container a').setup();
}
