var IframeShim = new Class({

	Implements: [Options, Events],

	options: {
		className: 'iframeShim',
		display: false,
		zIndex: null,
		margin: 0,
		offset: {x: 0, y: 0},
		browsers: true || (Browser.Engine.trident4 || (Browser.Engine.gecko && !Browser.Engine.gecko19 && Browser.Platform.mac))
	},

	property: 'IframeShim',

	initialize: function(element, options){
		this.element = $(element);
		this.setOptions(options);
		this.makeShim();
		return this;
	},

	makeShim: function(){
		if(this.options.browsers){
			var zIndex = this.element.getStyle('zIndex').toInt();

			if (!zIndex){
				var pos = this.element.getStyle('position');
				if (pos == 'static' || !pos) this.element.setStyle('position', 'relative');
				this.element.setStyle('zIndex', zIndex || 1);
			}
			zIndex = ($chk(this.options.zIndex) && zIndex > this.options.zIndex) ? this.options.zIndex : zIndex - 1;
			if (zIndex < 0) zIndex = 1;
			this.shim = new Element('iframe', {
				src: (window.location.protocol == 'https') ? '://0' : 'javascript:void(0)',
				scrolling: 'no',
				frameborder: 0,
				styles: {
					zIndex: zIndex,
					position: 'absolute',
					border: 'none',
					filter: 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
				},
				'class': this.options.className
			}).store('IframeShim', this);
			var inject = (function(){
				this.shim.inject(this.element, 'after');
				this[this.options.display ? 'show' : 'hide']();
				this.fireEvent('inject');
			}).bind(this);
			if (Browser.Engine.trident && !IframeShim.ready) window.addEvent('load', inject);
			else inject();
		} else {
			this.position = this.hide = this.show = this.dispose = $lambda(this);
		}
	},

	position: function(){
		if (!IframeShim.ready) return this;
		var size = this.element.measure(function(){ return this.getSize(); });
		if ($type(this.options.margin)){
			size.x = size.x - (this.options.margin * 2);
			size.y = size.y - (this.options.margin * 2);
			this.options.offset.x += this.options.margin;
			this.options.offset.y += this.options.margin;
		}
		if (this.shim) {
			this.shim.set({width: size.x, height: size.y}).position({
				relativeTo: this.element,
				offset: this.options.offset
			});
		}
		return this;
	},

	hide: function(){
		if (this.shim) this.shim.setStyle('display', 'none');
		return this;
	},

	show: function(){
		if (this.shim) this.shim.setStyle('display', 'block');
		return this.position();
	},

	dispose: function(){
		if (this.shim) this.shim.dispose();
		return this;
	},

	destroy: function(){
		if (this.shim) this.shim.destroy();
		return this;
	}

});

IframeShim.ready = false;

window.addEvent('load', function(){
	IframeShim.ready = true;
});




var Blanket = new Class({

	Implements: [Options, Events],

	options: {
		className: 'Blanket',
		display: false,
		zIndex: null,
		margin: 0,
		offset: {x: 0, y: 0},
		browsers: true || (Browser.Engine.trident4 || (Browser.Engine.gecko && !Browser.Engine.gecko19 && Browser.Platform.mac))
	},

	property: 'Blanket',

	initialize: function(element, options){
		this.element = $(element);
		this.setOptions(options);
		return this;
	},

	makeModal: function(){
		if(this.options.browsers){
			var zIndex = this.element.getStyle('zIndex').toInt();
			if (!zIndex){
				var pos = this.element.getStyle('position');
				if (pos == 'static' || !pos) this.element.setStyle('position', 'relative');
				this.element.setStyle('zIndex', zIndex || 1);
				zIndex = 1;
			}
			zIndex = ($chk(this.options.zIndex) && zIndex > this.options.zIndex) ? this.options.zIndex : zIndex - 1;
			if (zIndex < 0) zIndex = 1;
            this.blanket = new Element('div',{
                'class': this.options.className,
                styles:{
                    display:'none',
                    zIndex: zIndex,
                    position: 'absolute',
                    opacity: 0.8,
                    left: 0,
                    top: 0
                }
            }).store('Blanket',this);
            this.blanket.resize = (function() {
                var ss = $(document.body).getScrollSize();
                this.setStyles({
                    width: ss.x,
                    height: ss.y
                });
            }).bind(this.blanket);
            //this.options.parent.adopt(this.blanket);
            window.addEvent('resize', this.blanket.resize);
            			
			var inject = (function(){
			    //this.blanket.resize();
				this.blanket.inject(this.element, 'after');
				this[this.options.display ? 'show' : 'hide']();
				this.fireEvent('inject');
			}).bind(this);
			if (Browser.Engine.trident && !IframeShim.ready) window.addEvent('load', inject);
			else inject();
			//inject();
		} else {
			this.position = this.hide = this.show = this.dispose = $lambda(this);
		}
	},

	hide: function(){
		if (this.blanket) this.blanket.setStyle('display', 'none');
		this.options.display = 'hide';
		return this;
	},

	show: function(){
	    if (!this.blanket) {
	        this.makeModal();
        }
		if (this.blanket) {
		    this.blanket.resize();
		    this.blanket.setStyle('display', 'block');
		    this.options.display = 'show';
		}
		//return this.position();
	},

	dispose: function(){
		if (this.blanket) this.blanket.dispose();
		return this;
	},

	destroy: function(){
		if (this.blanket) this.blanket.destroy();
		return this;
	}

});


        var Alert = new Class({
            Implements: [Options],
            
            options: {
                modal: true,
                shim:  true,
                width: 300
            },
            
            initialize: function(msg, options) {
                this.setOptions(options);
                this.dom     = new Element('div',{styles: {
                    width: this.options.width
                }});
                this.wrapper = new Element('div', {styles: {
                    backgroundColor: 'white',
                    opacity: 0.8,
                    position: 'absolute',
                    left: 0,
                    top: 0
                }}).inject(this.dom);                
                this.content = new Element('div', {styles: {
                    backgroundColor: 'lightgrey',
                    fontFamily: 'Verdana',
                    fontSize: '9px',
                    textAlign: 'justify',
                    margin: 5,
                    padding: 5,
                    position: 'relative',
                    left: 0,
                    top: 0                    
                }, html: msg}).inject(this.dom);
                this.blanket = new Blanket(this.dom);
                this.shim    = new Element('div');//IframeShim(this.dom);
                this.dom.addListener('click', this.hide.bind(this) );
            },
            
            show: function() {
                var ss = $(document.body).getScrollSize();
                this.dom.setStyles({
                    position: 'absolute',
                    visibility: 'hidden'
                }).inject(document.body,'top');
                var sz = this.dom.getSize();
                this.dom.setStyles({
                    left: ss.x/2 - sz.x / 2,
                    top: ss.y/2 - sz.y / 2,
                    visibility: 'visible'                
                });
                this.wrapper.setStyles({
                    width: sz.x,
                    height: sz.y
                });
                if (this.options.modal) {
                    this.blanket.show();
                }
                if (this.options.shim) {
                   //this.shim.show();
                }
            },
            
            hide: function() {
                this.shim.dispose();
                this.blanket.dispose();
                this.dom.dispose();
            }
        });
        