var ImageViewer = new Class({

	options: {
		closeClass: 'pv_close',
		blanket: 'pv_blanket',
		container: 'pv_container',
		speed: 500
	},

	initialize: function(el, options){
		this.setOptions(options);
		this.link = el;
		this.link.addEvent('click', this.viewPage.bindWithEvent(this));
	},

	viewPage: function(e) {
		e.stop();
		this.hideThings(true);
		var url = this.link.getProperty('href');
		if ($(this.options.blanket)) {
			this.pv_blanket = $(this.options.blanket);
		} else {
			this.pv_blanket = new Element('div',{ 'id': this.options.blanket }).inject(document.body,'bottom');
			this.pv_blanket_fx = this.pv_blanket.effect('opacity', {
				duration: this.options.speed,
				transition: Fx.Transitions.linear,
				fps: 30
			});
			this.pv_blanket_fx.set(0);
			this.pv_blanket.setStyles({
				'height': window.getScrollHeight(),
				'display': 'block'
			});
		}
		if ($(this.options.container)) {
			this.pv_container = $(this.options.container);
		} else {
			this.pv_container = new Element('div',{ 'id': this.options.container, 'style': 'display:none' }).injectInside(document.body);
			this.pv_imagelink = new Element('a',{ 'class': this.options.closeClass, 'href': '#' }).injectInside(this.pv_container)
				.addEvent('click', this.closeViewPage.bindWithEvent(this));
			this.pv_closelink = new Element('a',{ 'class': this.options.closeClass + ' closebutton', 'href': '#' }).setText('close').injectInside(this.pv_container)
				.addEvent('click', this.closeViewPage.bindWithEvent(this));
			this.pv_container_fx = this.pv_container.effect('opacity', {
				duration: this.options.speed,
				transition: Fx.Transitions.linear,
				fps: 30
			});
			this.pv_container_fx.set(0);
			this.pv_container.setStyle('display', 'block');
			this.pv_image = new Element('img',{ 'src': url })
				.addEvent('load', this.beginPageView.bindWithEvent(this))
				.injectInside(this.pv_imagelink);
		}
	},

	setPagePosition: function(){
		if (this.pv_container) {
			this.pv_container.setStyles({
				'top': (window.getScrollTop() + 50),
				'left': ((window.getScrollWidth() - this.pv_container.getSize().size.x) / 2)
			});
		}
	},

	beginPageView: function(e){
		this.setPagePosition();
		window.addEvent('resize', this.setPagePosition.bind(this))
		.addEvent('scroll', this.setPagePosition.bind(this));
		this.pv_blanket_fx.start(.8);
		this.pv_container_fx.start(1);
	},
	
	closeViewPage: function(e){
		e.stop();
		this.pv_blanket_fx.start(0);
		this.pv_container.effect('opacity', {
			duration: this.options.speed,
			transition: Fx.Transitions.linear,
			fps: 30,
			onComplete: this.trashPageViewer.bind(this)
		}).start(0);
	},
	
	trashPageViewer: function(){
		window.removeEvent('resize');
		this.pv_blanket.remove();
		this.pv_container.remove();
		this.hideThings(false);
	},

	hideThings: function(open){
		var elements = $A($$('object'));
		elements.extend($$(window.ActiveXObject ? 'select' : 'embed'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
	}

});
ImageViewer.implement(new Options);
