var t;
h_class = 2;
var fadeLogo = function() { t.fade('toggle'); }

var initNews = function(s) {
    window.addEvent('domready', function() {
        var accordion = new Accordion($$('.toggler'),$$('.element'), {
            opacity: 0,
            alwaysHide: true,
            display: false,
            show: s,
            onActive: function(toggler) { toggler.setStyles({'font-weight': 'bold', 'color': '#666666'}); },
            onBackground: function(toggler) { toggler.setStyles({'font-weight': 'normal', 'color': '#333333'}); }
        });
    });
}

var opened_hov = null;
var timer;
var busy = false;
var p_pos = 0;

function initProductGallery (div, t, cnt, vis, w) {
    var n = (cnt - (cnt % vis)) / vis;
    var hid, l;
    
    div.fx = new Fx.Tween(
        div, 
        {
            'duration': 1000,
            'transition': Fx.Transitions.Quint.easeOut,
            'onComplete': function() { busy = false; }
        }
    );
    div.slide = function(dir) {
        if (!busy)
        {
            busy = true;
            p_pos = p_pos + dir;
            hid = false;
            if (p_pos < 0) 
            {
                $(t[0]).setStyle('visibility', 'visible');
            } 
            else {
                $(t[0]).setStyle('visibility', 'hidden');
            
            }
        
            if ((p_pos - 1) * -1 * vis >= cnt) 
            {
                $(t[1]).setStyle('visibility', 'hidden');
                if ((p_pos - 1) * -1 * vis > cnt)
                {
                    hid = true;
                }
            } 
            else {
                $(t[1]).setStyle('visibility', 'visible');
            }
        
            if (hid === true) 
            {
                l = (p_pos * vis * w) + ((vis - (cnt % vis)) * w); 
            }
            else
            {
                l = p_pos * vis * w;
            }
            //var l = dir * (cnt - vis) * w;
            this.fx.start('margin-left', l);
        }
    }
    
    $(t[0]).addEvent('click', function() { div.slide(1) });
    $(t[1]).addEvent('click', function() { div.slide(-1) });
}

window.addEvent('domready', function() {
    if (hov_links.length > 0) {
        hov_links.each(function(l) {
            var ul = $(l).getParent().getElement('ul');
            var tw = new Fx.Tween(ul);
            tw.set('opacity', 0);
            ul.hideSub = function() {
                opened_hov = null;
                tw.start('opacity', 0);
            }
            $(l).getParent().addEvent('mouseover', function() {
                try {
                    $clear(timer);
                } catch(e) {}
                if (opened_hov != null && opened_hov != ul) {
                    opened_hov.hideSub();
                }
                tw.start('opacity', 1);
                opened_hov = ul;
            });
            $(l).getParent().addEvent('mouseout', function() {
                timer = ul.hideSub.delay(5000);
            });
        });
    }
    
    $$('a').each(function(link) {
        if (link.getProperty('rel') == 'external')
        {
            link.setProperty('target', '_blank');
        }
    });
});

window.addEvent('load', function() {
    t = $('home_link');
    t.set(
        'tween', 
        { 
            duration: 3500,
            onComplete: function() {
                t.setStyle('visibility', 'visible');
            }
        }
    );

    t.addEvent('mouseover', function() {
        t.set('opacity', 1);
    });

    fadeLogo.periodical(5500);
})