var layers = new Class({
	options: {
		launchers: []
	},
	initialize: function(options) {
		this.setOptions(options);
		this.flashers=$$('OBJECT','EMBED');
		this.useFlash=false;
		if (this.flashers.length >= 1 && window.webkit==true) this.useFlash=true;
		this.buildLayer();
		this.options.launchers.each(function(launcher) {
			launcher.addEvent('click', this.checkUser.bindAsEventListener(this,launcher));
		}, this);
		this.close.addEvent('click', this.restore.bind(this));
		this.overlay.addEvent('click', this.restore.bind(this));
	},
	switchFlash: function(showorhide) {
		if (this.options.useFlash==true) this.flashers.each(function(flasher) {flasher.style.visibility=showorhide});
	},
	showOverlay: function() {
		var x,y;
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
		this.overlay.setStyles({width:x,height:y,display:'block'});
		this.oframe.setStyles({width:x,height:y,display:'block'});
	},
	checkUser: function(event,launcher) {
		var event=new Event(event);
		var posx=event.page.x;
		//var posy=event.page.y;
		var posy= window.ie ? event.page.y + (document.body.scrollTop || document.documentElement.scrollTop) : event.page.y;

		var l=launcher.getCoordinates().left;
		var r=launcher.getCoordinates().right;
		var t=launcher.getCoordinates().top;
		var b=launcher.getCoordinates().bottom;

		if (posx>=l && posx<=r && posy>=t && posy<=b)
		{
			this.fillLayer(launcher);
			event.preventDefault();
			return false;
		}
	},
	fillLayer: function(launcher,direct,header,override) {
		if (direct==true)
		{
			var yourl=launcher;
			
			if (header)
			{
				this.title.setHTML(header);
			}
		}
		else
		{
			var yourl=launcher.href;
			if (launcher.title!='')
			{
				this.title.setHTML(launcher.title);
			}
			if(override) {
				var event = new Event(event).stop();
			}
		}

		this.showOverlay();
		this.switchFlash('hidden');

		var len=yourl.length;
		var typ=yourl.substr(len-3,len);
		var isitadiv=false;
		var theactualdiv;
		
		if (launcher.className && launcher.className.match('width'))
		{
			var image_width=launcher.className.split('width')
			this.layer_image_width=image_width[1];
		}

		if (yourl.match('#'))
		{
			var a1=yourl.split('#');
			var current=window.location.href;

			if (current.match('#'))
			{
				var a2=current.split('#');
				current=a2[0];
			}

			if (a1[0]==current || a1[0]=='')
			{
				
				isitadiv=true;
				theactualdiv=a1[1];
			}
		}

		if (typ=='peg' || typ=='jpg' || typ=='gif' || typ=='png')
		{
			this.layer_image.addEvent('load', this.showLayer.bind(this,this.layer_image));
			this.layer_image.src=yourl;
		}
		else if (isitadiv==true)
		{
			var clone = $(theactualdiv).clone();
			clone.injectInside(this.layer_div).style.display='block';
			this.showLayer(this.layer_div);
		}
		else
		{
			this.layer_iframe.addEvent('load', this.showLayer.bind(this,this.layer_iframe));
			this.layer_iframe.src=yourl;
		}
	},
	showLayer: function(el) {
		el.setStyles({display:'block'});
		
		var w,h;
		w=600;
		h=560; 

	if (el==this.layer_iframe) 
		{
			try{

				if (frames['layer_iframe'].document.getElementById('layer_holder'))
				{
					w=frames['layer_iframe'].document.getElementById('layer_holder').style.width.toInt();
					h=frames['layer_iframe'].document.getElementById('layer_holder').style.height.toInt();
				}
			}
			catch(ex)
			{
				w=900;
				h=560;
			}
			this.layer_iframe.style.width=w+'px';
			this.layer_iframe.style.height=h+'px';	
		}
		else if (el==this.layer_image)
		{
			w=this.layer_image_width;
		}
		else if (el==this.layer_div)
		{
			w=this.layer_div.getFirst().style.width.toInt();
		}
		
		var x = document.body.scrollWidth;
		//var t=window.getScrollTop()+50;
		//var t=window.ie ? (document.body.scrollTop || document.documentElement.scrollTop)+ 100:50;
		//var t=window.webkit ? (document.body.scrollTop || document.documentElement.scrollTop)+ 100:50;

		if (t=window.ie)
		{
			t=(document.body.scrollTop || document.documentElement.scrollTop)+100;
		}
		else if (t=window.webkit)
		{
			t=(document.body.scrollTop || document.documentElement.scrollTop)+100;
		}
		else
		{
			t=window.getScrollTop()+50;
		}

		var l=(x/2)-(w/2);
		
		this.layer.setStyles({display:'block',width:w+'px',top:t+'px',left:l+'px'});
		dont = true;
	},
	buildLayer:	function() {
		this.body=document.body;
		
		this.layer=new Element('DIV', {'id':'layer','class':'layer'});
		this.layer.injectInside(this.body);
		if (window.ie6==true) this.layer.style.position='absolute';
		this.h3=new Element('H3');
		this.h3.injectInside(this.layer);
		this.s1=new Element('SPAN');
		this.s1.injectInside(this.h3);
		this.s2=new Element('SPAN');
		this.s2.injectInside(this.s1);
		this.s3=new Element('SPAN');
		this.s3.injectInside(this.s2);
		this.title=new Element('SPAN', {'id':'layer_title'});
		this.title.injectInside(this.s3);
		this.close=new Element('A', {'href':'javascript:void(0)','class':'close_primary'}).setHTML('Close');
		this.close.injectInside(this.s3);
		this.rule=new Element('DIV', {'class':'rule'});
		this.rule.injectInside(this.layer);		
		this.layer_inner=new Element('DIV', {'class':'layer_inner'});
		this.layer_inner.injectInside(this.layer);
		this.layer_div=new Element('DIV', {'class':'layer_div'});
		this.layer_div.injectInside(this.layer_inner);
		this.layer_image=new Element('IMG', {'class':'layer_image'});
		this.layer_image.injectInside(this.layer_inner);
		this.layer_iframe=new Element('IFRAME', {'id':'layer_iframe','name':'layer_iframe','src':'javascript:false;','frameborder':'0','scrolling':'auto','marginheight':'0','marginwidth':'0'});
		this.layer_iframe.injectInside(this.layer_inner);
		this.bl=new Element('DIV', {'class':'bl'});
		this.bl.injectInside(this.layer);
		this.br=new Element('DIV', {'class':'br'});
		this.br.injectInside(this.bl);
		this.bm=new Element('DIV', {'class':'bm'});
		this.bm.injectInside(this.br);
		
		this.overlay=new Element('DIV', {'id':'overlay'});
		this.overlay.injectInside(this.body);
		 
		dont = false;
		this.oframe=new Element('IFRAME', {'id':'overlay_iframe','name':'overlay_iframe','src':'javascript:false;','frameborder':'0','scrolling':'auto','marginheight':'0','marginwidth':'0'});
		this.oframe.injectInside(this.body);

		this.restore();
	},
	restore: function() {
 
		this.layer_div.style.display='none';
		this.layer_image.style.display='none';
		this.layer_iframe.style.display='none';
		this.layer.style.display='none';
		this.oframe.style.display='none';
		this.overlay.style.display='none';
		this.layer_div.empty();
		this.title.setHTML('');
		this.switchFlash('visible');
		if(dont)
			window.location.href = window.location.href;
	}
});

layers.implement(new Options, new Events);

window.addEvent('domready', function(){
	CallDomEvent();
});
function CallDomEvent()
{
	if ($ES('.launcher'))
	{
		pattern_layers = new layers({
			launchers: $ES('.launcher')
		});
	}
}

