var simpleBox = {

    relValue: 'none',
    contentWidth: 0,
    contentHeight: 0,

    makeContainer: function(){
    },

	init: function(options){

        this.options = $extend({
            resizeDuration: 400,
			resizeTransition: false,	// default transition
			showCounter: true
		}, options || {});

		$each(document.links, function(el){
			if (el.rel && el.rel == this.relValue){
				el.onclick = this.click.pass(el, this);
			}
		}, this);
        
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div', {'class': 'layer'}).injectInside(document.body);

		this.center = new Element('div', {'class': 'eee', 'styles': {'width': this.contentWidth, 'height': this.contentHeight, 'marginLeft': -(this.contentWidth/2), 'display': 'none'}}).injectInside(document.body);

		this.container = this.makeContainer(this);

       

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide()
		};
	},

	click: function(link){
        return this.open(link);
	},

	open: function(link){
		this.position();
		this.setup(true);
		this.topPosition = window.getScrollTop() + (window.getHeight() / 2 - this.contentHeight / 2);
		this.center.setStyles({top: this.topPosition, display: ''});

		this.fx.overlay.start(0.7);
        return this.onOpen(link);
	},

	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){
            var parent = el.parentNode;
            while (typeof(parent) != 'undefined' && typeof(parent.className) != 'undefined' && parent.className != 'eee') {
                parent = parent.parentNode;
            }
            if (parent.parentNode == undefined) {
                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);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
		}
	},

	onOpen: function(){
        return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';
			
			if (this.center.clientHeight != this.container.offsetHeight){
				this.fx.resize.start({height: this.container.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.container.offsetWidth){
				this.fx.resize.start({width: this.container.offsetWidth, marginLeft: -this.container.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			this.fx.container.start(1);
			break;
		case 4:
			this.step = 0;
		}
	},

    onClose: function(){

    },

	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 = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
        this.onClose();
		return false;
	}
};