Prototype.require('jojoPopupEffect.js');

// Effect is not compatible with auto created element
FullScreenElement=Class.create(PopupEffect, {
	onClickHandler: null,
	ownElement: null,

	initialize: function($super, element, options) {
		$super(element, options);

		this.ownElement=false;
		this.setOptions({
			alpha: 50,
			zIndex: 10,
			backgroundColor: '',
			className: '',
			onClick: null,
			showEffect: null /*{method: 'appear', options}*/,
			onShowEffect: null,
			hideEffect: null /*{method: 'appear', options}*/,
			onHideEffect: null
		});

		if (!this.element) {
			this.ownElement=true;
		}

		this.setOptions(options);
	},

	show: function($super) {
		if (!this.element || !this.element.visible()) {
			if (this.ownElement) {
				body=document.getElementsByTagName("body")[0];

				if (body) {
					this.element=new Element('DIV');
					this.element.hide();
					body.appendChild(this.element);
				}
			}

			if (this.element) {
				var dimensions=Prototype.Browser.getDimensions();
				if (this.options.className!='') {
					this.element.addClassName(this.options.className);
				}

				this.element.setStyle({
					position: 'absolute',
					top: '0px',
					left: '0px',
					width: [dimensions.width, dimensions.clientWidth].max()+'px',
					height: [dimensions.height, dimensions.clientHeight].max()+'px',
					opacity: this.options.alpha/100,
					zIndex: this.options.zIndex
				});

				if (this.options.backgroundColor!='') {
					this.element.setStyle({
						backgroundColor: this.options.backgroundColor
					});
				}

				if (this.options.onClick) {
					this.onClickHandler=this.onClick.bindAsEventListener(this);
					this.element.observe('click', this.onClickHandler);
				}

				$super();
				// link event
//				Event.observe(window, 'resize', this.onBrowserResizeHandler);
//				Event.observe(window, 'scroll', this.onBrowserResizeHandler);
			}
		}
	},

	hide: function($super) {
//		Event.stopObserving(window, 'scroll', this.onBrowserResizeHandler)

		if (this.onClickHandler!=null) {
			this.element.stopObserving('resize', this.onClickHandler);
			this.onClickHandler=null;
		}

		$super();
	},

	onHide: function($super) {
		$super();

		if (this.ownElement) {
			this.element.remove();
		}
	},

	onBrowserResize: function() {
		if (this.element && this.element.visible()) {
			this.element.hide();
			var dimensions=Prototype.Browser.getDimensions();

			this.element.setStyle({
				width: [dimensions.width, dimensions.clientWidth].max()+'px',
				height: [dimensions.height, dimensions.clientHeight].max()+'px'
			});
			this.element.show();
		}
	},

	onClick: function(event) {
		this.options.onClick.apply(this, $(arguments));
	},

	setAlpha: function (alpha) {
		this.setOpacity(alpha/100);
	},

	setOpacity: function(opacity) {
//		this.options.alpha=opacity*100;

		if (this.element) {
			this.element.setStyle({
				'opacity': opacity
			});
		}
	}
});