// SIMPLETABS

var simpleTabs = Class.create({
    
    initialize: function(container, links)
    {
        this.container = container;
        this.links = links;
	
        this.attach();
		this.load(0);
    },
    
    attach: function()
    {
        this.links.each(function(link, i){
            if(i == 0)
			{
				link.addClassName('active');
			}

            link.observe('click', function(event){
                event.stop();
                this.load(i);
				this.container.select('.active')[0].removeClassName('active');
				link.addClassName('active');
            }.bind(this));
        }, this);
    },
	
	load: function(n)
	{
		this.container.select('div.content').each(function(el, i){
			el.setStyle({
				display: i == n ? 'block' : 'none'
			});
		});
	}
    
});

var contentBrowser = Class.create({

	current: null,

	initialize: function(container, elements)
	{
		this.container = container;
		this.elements = elements;

		this.createButtons();

		this.show(0);
	},
	
	show: function(n)
	{
		this.current = n;
		this.elements.each(function(el, i){
			el.setStyle({
				display: i == n ? '' : 'none'
			});
		});
		this.updateButtons();
	},
	
	previous: function()
	{
		var p = this.current - 1 >= 0 ? this.current - 1 : this.elements.length - 1;
		this.show(p);
	},
	
	next: function()
	{
		var n = this.current + 1 < this.elements.length  ? this.current +1 : 0;
		this.show(n);
	},

	createButtons: function()
	{
		this.btn_prev = new Element('a', {
			'class': 'prev',
			'href': '#'
		}).observe('click', function(e){
			e.stop();
			this.previous();	
		}.bind(this));

		this.container.insert(this.btn_prev);

		this.btn_next = new Element('a', {
			'class': 'next',
			'href': '#'
		}).observe('click', function(e){
			e.stop();
			this.next();	
		}.bind(this));

		this.container.insert(this.btn_next);
	},

	updateButtons: function()
	{
		var p = this.current - 1 >= 0 ? this.current - 1 : this.elements.length - 1;

		this.btn_prev.update(this.elements[p].select('span')[0].innerHTML);

		var n = this.current + 1 < this.elements.length  ? this.current +1 : 0;

		this.btn_next.update(this.elements[n].select('span')[0].innerHTML);
	}

});

var Switchteaser = Class.create({

    options: $H({
        fxDelay: 0.5
    }),

    initialize: function(container)
    {
        this.container = $(container);
        this.elements = this.container.childElements();
        this.current = 0;
        this.isActive = false;
        
        if(this.elements.length == 0)
        {
            return false;
        }
        
        this.container.setStyle({
            position: 'relative' 
        });
        
        this.elements.each(function(el, i){
            el.setStyle({
                left: 0,
                opacity: i > 0 ? 0 : 1,
                position: 'absolute',
                top: 0,
                zIndex: i > 0 ? 0 : 1
            });
        });
        
        this.build();
    },
    
    build: function()
    {
        // left arrow
        var l =  new Element('a', { href: '#' }).addClassName('switch_left').observe('click', function(e){
            Event.stop(e);
            this.change(0);
        }.bind(this));
        this.container.insert(l);

        // right arrow
        var r =  new Element('a', { href: '#' }).addClassName('switch_right').observe('click', function(e){
            Event.stop(e);
            this.change(1);
        }.bind(this));
        this.container.insert(r);
    },

    change: function(d)
    {
        if(this.isActive)
        {
            return false;
        }
        this.isActive = true;
        // back
        if(d == 0)
        {
            this.elements[this.current].fade({ duration: 1.5, from: 1, to: 0 });

            this.current = this.current - 1 > -1 ? this.current - 1 : this.elements.length-1;
        }
        // forward
        else
        {
            this.elements[this.current].fade({ duration: 1.5, from: 1, to: 0 });
            
            this.current = this.current + 1 < this.elements.length ? this.current + 1 : 0;
        }
        (function(){
            this.elements[this.current].setStyle({
                display: 'block',
                opacity: 0
            }).fade({ duration: 1.0, from: 0, to: 1 });
        }.bind(this)).delay(this.options.get('fxDelay'));
        (function(){
            this.isActive = false;
        }.bind(this)).delay(this.options.get('fxDelay')*2);
    }
    
});

var zebra = function(t)
{
	t.select('tbody tr').each(function(tr, i){
		tr.addClassName((i+1) % 2 == 0 ? 'even' : 'odd');	
	});
}

Event.observe(window, 'load', function(){
    $$('div.switchable').each(function(el){
        new Switchteaser(el);
    });
	$$('.simple-tabs').each(function(el){
		if(!el.hasClassName('smart-edit'))
		{
			new simpleTabs(el, el.select('ul.tabs li'));
		}
	});
	$$('table.zebra').each(function(t){
		zebra(t);	
	});	
	if($$('.pricelist_home')[0])
	{
		if(!$$('.pricelist_home .content')[0].hasClassName('smart-edit'))
		{
			// Liste Formate
			new contentBrowser($$('.pricelist_home .content')[0], $$('.pricelist_home .content')[0].select('.pricelist-content'));
		}
		if(!$$('.pricelist_home .content')[1].hasClassName('smart-edit'))
		{
			// Liste Preise
			new contentBrowser($$('.pricelist_home .content')[1], $$('.pricelist_home .content')[1].select('.pricelist-content'));
		}
	}
	// Arrows
	var classNames = ['arrow-red','arrow-blue','arrow-orange'];
	var leftPos = [160,370,585];

	$$('#dlbtnwin, #dlbtnmac, #dlbtnlin').each(function(el, i){

		if(!el.hasClassName('smart-edit'))
		{
			$('home_top_full').insert(
				new Element('span').addClassName('download-arrow '+classNames[i]).setStyle({
					bottom: 15+'px',
					left: leftPos[i]+'px'
				})
			);
	
			$('home_top_full').insert(
				new Element('a', {
					href: el.href	
				}).addClassName('anchor-clone').setStyle({
					bottom: 0,
					height: '49px',
					left: 210*i+4+'px',
					position: 'absolute',
					width: '210px',
					zIndex: 11
				})
			);
	
			var anchor = $$('.anchor-clone')[i];
			
			if(Prototype.Browser.IE)
			{
				anchor.setStyle({
					background: '#fff',
					opacity: 0
				});
			}
	
			anchor.observe('mouseover', function(){
				new Effect.Morph($$('.'+classNames[i])[0], {
					style: 'bottom: 10px;',
					duration: 0.2
				});
			});
			anchor.observe('mouseout', function(){
				new Effect.Morph($$('.'+classNames[i])[0], {
					style: 'bottom: 15px;',
					duration: 0.2
				});
			});
		}
	});
});