var ACAlertbox = new Class({
    options: {
        resizeDuration: 400,
        resizeTransition: Fx.Transitions.Sine.easeInOut,
        fadeDuration: 500,
        startWidth: 400,
        startHeight: 600,
        animateCaption: true,
        captionDuration: 300,
        showCounter: true
    },
	
    initialize: function(options) {			
        this.overlay = new Element('div', {id: 'altOverlay'});
        this.overlay.inject(document.body,'bottom');
        this.center = new Element('div', {id: 'altCenter', 'styles': {'width': this.options.startWidth, 'height': this.options.startHeight, 'marginLeft': -(this.options.startWidth/2), 'display': 'none'}}).injectInside(document.body);
		
        this.bottomContainer = new Element('div', {'id': 'altBottomContainer'}).injectInside(document.body);
        this.bottom = new Element('div', {'id': 'altBottom'}).injectInside(this.bottomContainer);
        new Element('a', {'id': 'altCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.closeLightbox.bind(this);
        new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

        this.fx = {
	        overlay: new Fx.Tween(this.overlay,{property: 'opacity', duration: this.options.fadeDuration}).set(0),			
	        image: new Fx.Tween(this.image,{property: 'opacity', duration: this.options.fadeDuration, onComplete: this.nextEffect.bind(this)}),
	        bottom: new Fx.Tween(this.bottom, {property: "margin-top", duration: this.options.captionDuration, onComplete: this.nextEffect.bind(this)})
        };
        return this.openAlert($('Alert').innerHTML);
    },
	
    setOverlay: function(){
        this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
    },

    openAlert: function(alertText) {
        this.alertText = alertText;
        this.center.innerHTML = this.alertText;
        this.setOverlay();
        this.setupLightbox(true);
        this.top = window.getScrollTop() + (window.getHeight() / 15);
        this.fx.resize = new Fx.Morph(this.center, $extend({duration: this.options.resizeDuration,onComplete: this.nextEffect.bind(this)}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {}));
        this.center.setStyles({top: this.top, width: this.options.startWidth, height: this.options.startHeight, marginLeft: -(this.options.startWidth/2), display: ''});	
        this.bottomContainer.setStyles({'top': this.top + this.center.clientHeight,'width':this.center.clientWidth,'marginLeft': this.center.style.marginLeft});
        this.fx.overlay.start(0.7);
    },
	
    setupLightbox: function(open) {
        //if ie, hide select boxes and flash
	if(open) {
		AC.addEvent(window, 'scroll', this.setOverlay.bind(this));
		AC.addEvent(window, 'resize', this.setOverlay.bind(this));
	} else {
		AC.removeEvent(window, 'scroll', this.setOverlay.bind(this));
		AC.removeEvent(window, 'resize', this.setOverlay.bind(this));
	}

        this.fxStep = 0;
    },

    nextEffect: function(){
        switch (this.fxStep++){
        case 1:
	        this.center.removeClass('altLoading');
	        this.fxStep++;		
        case 2:		    
	        this.fxStep++;
        case 3:
	        this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, marginLeft: this.center.style.marginLeft});
	        this.fx.image.start(1);
	        break;
        case 4:
	        this.fx.bottom.set(-this.bottom.offsetHeight);
	        this.bottomContainer.style.height = '';
	        this.fx.bottom.start(0);
        case 5:
	        this.fxStep = 0;
        }
    },

    closeLightbox: function(){
        if (this.fxStep < 0) return;
        this.fxStep = -1;
        if (this.preload){
	        this.preload.onLoad = Class.empty;
	        this.preload = null;
        }        
        for (var f in this.fx) this.fx[f].cancel();
        this.center.style.display = this.bottomContainer.style.display = 'none';
        this.fx.overlay.chain(function(){ this.setupLightbox(false) }.bind(this)).start(0);
        return false;
    }
});	