
var Contentbox = {

    init: function(options){
        this.options = $extend({
            resizeDuration: 400,
            resizeTransition: false,	// default transition
            initialWidth: 650,
            initialHeight: 700,
            animateCaption: true,
            showCounter: true
        }, options || {});

        this.anchors = [];
        $each(document.links, function(el){
            if (el.rel && el.rel.test(/^contentbox/i)){
                el.onclick = this.click.pass(el, this);
                this.anchors.push(el);
            }
        }, this);
        this.eventPosition = this.position.bind(this);

        this.overlay = new Element('div', {'id': 'lbOverlay'}).injectInside(document.body);

        this.center = new Element('div', {'id': 'lbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
        this.image = new Element('div', {'id': 'lbImage'});
        //$('hiddenContent').injectInside(this.center);
        this.center.appendChild($('headerTabs'));
        this.center.appendChild($('hiddenContent'));
        this.center.appendChild($('closedTabs'));



        this.bottomContainer = new Element('div', {'id': 'lbBottomContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
        this.bottom = new Element('div', {'id': 'lbBottom'}).injectInside(this.bottomContainer);
        new Element('a', {'id': 'lbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
        this.caption = new Element('div', {'id': 'lbCaption'}).injectInside(this.bottom);
        this.number = new Element('div', {'id': 'lbNumber'}).injectInside(this.bottom);
        new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);


        var nextEffect = this.nextEffect.bind(this);
        this.fx = {
            overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
            resize: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
            image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect}),
            bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
        };

        this.preloadPrev = new Image();
        this.preloadNext = new Image();
    },

    click: function(link){
        if (link.rel.length == 10) return this.show(link.href, link.title);

        return this.open();
    },

    position: function(){
        this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
    },

    setup: function(open){
        var elements = $A(document.getElementsByTagName('object'));
        //elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
        elements.each(function(el){
            //if (open) el.lbBackupStyle = el.style.visibility;
            //el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
        });
        var fn = open ? 'addEvent' : 'removeEvent';
        window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		$('hiddenContent').style.visibility = "visible";
		$('headerTabs').style.visibility = "visible";
        this.step = 0;
    },

    nextEffect: function(){
        switch (this.step++){
        case 1:
            this.center.className = '';
            this.image.style.backgroundImage = 'url('+this.images[this.activeImage][0]+')';
            this.image.style.width = this.bottom.style.width = this.preload.width+'px';
            this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height+'px';

            this.caption.setHTML(this.images[this.activeImage][1] || '');
            this.number.setHTML((!this.options.showCounter || (this.images.length == 1)) ? '' : '<b>'+(this.activeImage+1)+'</b> / '+this.images.length+' &nbsp;<a id="prevLinkUnten" href="#" style="color:#888888;">&lt;</a> <a id="nextLinkUnten" href="#" style="color:#888888;">&gt;</a>');
            $('prevLinkUnten').onclick = this.previous.bind(this);
            $('nextLinkUnten').onclick = this.next.bind(this);

            if (this.activeImage) this.preloadPrev.src = this.images[this.activeImage-1][0];
            if (this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage+1][0];
            if (this.center.clientHeight != this.image.offsetHeight){
                this.fx.resize.start({height: this.image.offsetHeight});
                break;
            }
            this.step++;
        case 2:
            if (this.center.clientWidth != this.image.offsetWidth){
                this.fx.resize.start({width: this.image.offsetWidth, marginLeft: -this.image.offsetWidth/2});
                break;
            }
            this.step++;
        case 3:
            this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
            this.fx.image.start(1);
            break;
        case 4:
            if (this.options.animateCaption){
                this.fx.bottom.set(-this.bottom.offsetHeight);
                this.bottomContainer.style.height = '';
                this.fx.bottom.start(0);
                break;
            }
            this.bottomContainer.style.height = '';
        case 5:
            if (this.activeImage) this.prevLink.style.display = '';
            if (this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
            this.step = 0;
        }
    },

    open: function(){

        this.position();
        this.setup(true);
        this.top = window.getScrollTop() + (window.getHeight() / 15);
        this.center.setStyles({top: this.top, display: ''});
        this.fx.overlay.start(0.8);
        return false;
    },

    close: function(){
        if (this.step < 0) return;
        this.step = -1;
        if (this.preload){
            this.preload.onload = Class.empty;
            this.preload = null;
        }
        for (var f in this.fx) this.fx[f].stop();
        this.center.style.display = this.bottomContainer.style.display = 'none';
        this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		$('hiddenContent').style.visibility = "hidden";
		$('headerTabs').style.visibility = "hidden";
        return false;
    }

};

function changeTab(newTab){
    var content = $('hiddenContent');
    var closedTabs = $('closedTabs');
    while(content.hasChildNodes()){
        var firstChild = content.firstChild;
        closedTabs.appendChild(firstChild);
    }
	
	if(newTab != "firmaAktionen" && newTab != "firmaBilder"){
		$('logoUpload').style.visibility = "hidden";
		$('additionalPic').style.visibility = "hidden";
		$('aktionUpload').style.visibility = "hidden";
	}
	
	if(newTab == "firmaBilder"){
		$('logoUpload').style.visibility = "visible";
		$('additionalPic').style.visibility = "visible";
		$('aktionUpload').style.visibility = "hidden";
	}
	
	if(newTab == "firmaAktionen"){
		$('logoUpload').style.visibility = "hidden";
		$('additionalPic').style.visibility = "hidden";
		$('aktionUpload').style.visibility = "visible";
	}
	
	
	
	var newTab = $(newTab);
	
    content.appendChild(newTab);

}



