var swissArmyOverlay = new Class({
	Implements: [Options,Events,Chain],
	options: {opacity:'.5',cacheHTML:'false',relativeTo: $('body')},
	objCheck: function(options){
		this.setOptions(options);
		this.storage = $(this.options.storageElement);
		if(!this.index){
			this.createMooElements();
		}
		else{
			this.updateMooElements();
		}
	},
	setID: function(link){
		return link.getData('oid');
	},
	createMooElements: function(){
		this.grey = new Element('div',{id: 'grey',styles:{opacity: '0','display':'none','background-color': '#000','position': 'absolute','top': '0','left': '0','width': '100%','height': '100%','z-index':9000}});
		this.container = new Element('div',{id:'overlay',styles:{'opacity':'0','display':'none','position': 'absolute','z-index':9001}});
		this.grey.inject($('container'),'after');
		if(this.options.backgroundImage){this.container.setStyle('background',this.options.backgroundImage);}
		if(this.options.backgroundColor){this.container.setStyle('background-color',this.options.backgroundColor);}
		if(this.options.width){this.container.setStyle('width',this.options.width);}
		if(this.options.height){this.container.setStyle('height',this.options.height);}
		this.container.inject($('container'),'after');
		this.setPosition(this.options.overlayPosition,this.options.relativeTo);
		this.fadeUnder = new Fx.Tween(this.grey);
		this.fadeOver = new Fx.Tween(this.container);
		this.setStage(this.options.url);
	},
	updateMooElements: function(){
		if(this.options.backgroundImage){this.container.setStyle('background',this.options.backgroundImage);}
		if(this.options.backgroundColor){this.container.setStyle('background-color',this.options.backgroundColor);}
		if(this.options.width){this.container.setStyle('width',this.options.width);}
		if(this.options.height){this.container.setStyle('height',this.options.height);}
		this.setPosition(this.options.overlayPosition,this.options.relativeTo);
		this.setStage(this.options.url);
	},
	setPosition: function(x,y){
		var scroll = window.getScrollSize();
		this.grey.setStyle('height',scroll.y);
		this.container.position({position:x, relativeTo:y});
	},
	setStage: function(url){
		var preID = new URI(url);
		this.index = this.setID(preID);
		this.grey.fade(this.options.opacity);
		if(this.options.type == 'tooltip'){
			this.fadeOutStage();
			(function(){this.renderAndCacheHTML(url);}.bind(this)).delay(400);
		}
		else{
			this.grey.show();
			this.container.show();
			if(this.options.cacheHTML == 'false'){
				this.renderHTML(url);
			}
			else{
				this.renderAndCacheHTML(url);
			}
		}
	},
	renderAndCacheHTML: function(i){
		var dataRequest = this.storage.retrieve(this.index);
		if (!dataRequest){
			dataRequest = new Request({url:i,onFailure:function(response){alert(response)},method:'get',onComplete:function(response){$('overlay').set('html',response);this.showStage(response);}.bind(this)}).send();
		}
			else {
			$('overlay').set('html',dataRequest);
			this.showStage(dataRequest);
		}
	},
	renderHTML: function(dataRequest){
		$('overlay').load(dataRequest,{noCache:'true'});
		this.showStage(dataRequest);
	},
	showStage: function(dataRequest){
		$('overlay').show();
		$('grey').show();
		if($$('a.closeOverlay')){
			$$('a.closeOverlay').addEvent('click',function(e){
				e.preventDefault();
				this.closeIt();
			}.bind(this));
		}
		this.fadeUnder.start('0',this.options.opacity).chain(function(){
			$('overlay').fade(1);
		});
		if (!this.storage.retrieve(this.index) && (this.options.cacheHTML == 'true')) {
			this.storeIt(dataRequest);
		}
		this.grey.addEvent('click',function(e){
			e.preventDefault();
			if(this.container.get('opacity') == 1){
				this.closeIt();
			}
		}.bind(this));
	},
	storeIt: function(dataRequest){
		this.storage.store(this.index, dataRequest);
	},
	closeIt: function(){
		this.fadeOutStage();
		(function(){$('overlay').hide()}).delay(600);
		(function(){$('grey').hide()}).delay(1000);
	},
	fadeOutStage: function(){
		this.fadeOver.start('opacity','1','0').chain(function(){
			$('grey').tween('opacity',this.options.opacity,'0');
		});
	}
});

var modal;
window.addEvent('load',function(){
	modal = new swissArmyOverlay();

		//example of a single element onto which an overlay event is being added.
		//$('sendToAFriend').addEvent('click',function(e){
		//	e.preventDefault();
		//	var link = this.get('href');
		
		//	modal.objCheck({
		//		type: 'overlay',
		//		width: '500px',
		//		height: '300px',
		//		url: link,
		//		backgroundImage: 'url(images/overlay-bg.jpg) no-repeat',
		//		overlayPosition: 'upperLeft',
		//		opacity: '.5',
		//		storageElement: 'storage',
		//		cacheHTML: 'true'
		//	});
		//});
});

