function lpCheck() {
	if (typeof(lpMarket)!='undefined') {
		lpSendData('page','ShoppingAssistantVisible','false');
	}
}

var layers = new Class({
	options: {
		launchers: []
	},
	initialize: function(options) {
		this.setOptions(options);
		this.flashers=$$('OBJECT','EMBED');
		this.iframe_count = 0;
		this.cur_frame_id = '';
		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=window.getScrollWidth();
		var y=window.getScrollHeight();
		if (window.ie6==true) x=window.getWidth();
		this.overlay.setStyles({width:x,height:y,display:'block'});
		this.oframe.setStyles({width:x,height:y,display:'block'});
	},
	parseClass:function(klass) {
    var re = /(\#[^\s]+)/;
    return klass.match(re)[0];
  },
	checkUser: function(event,launcher,url,header) {
	  var launcher = $(launcher);
	  url = url || launcher.href || this.parseClass(launcher.getProperty('class'));
	  header = header || launcher.title;
	  
		var event=new Event(event);
		var posx=event.page.x;
		var posy=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(url,header);
	    event.preventDefault();
	    return false;
		}
	},
	getElementCoords:function(el) {
    var c = {}
    if (el === "window") {
     el = document.documentElement || document.body; 
    }
    c.visHeight = el.clientHeight;
    c.visWidth = el.clientWidth;
    c.height = el.scrollHeight;
    c.width = el.scrollWidth;
    c.sTop = window.pageYOffset?window.pageYOffset:el.scrollTop;
    c.sLeft = window.pageXOffset?window.pageXOffset:el.scrollLeft;
    return c;
  },
  removeScroll:function() {
  	var body = $$('body')[0];
    var coords = this.getElementCoords('window');
    this.cur_scroll_top = coords.sTop;
	  body.setStyle('overflow','hidden');	
	  //$$('html')[0].setStyle('overflow','hidden');
	  this.showOverlay();
  },
  restoreScroll:function(first) {
  	first = first || false;
  	var body = $$('body')[0];
  	var html = $$('html')[0];
	  body.setStyles({
	  	'overflow':'auto',
	  	'height':'auto'
	  });
	  if ($chk(this.cur_scroll_top)) {
	  	(document.documentElement || document.body).scrollTop = this.cur_scroll_top;
	  }
	  
	  
	  if (first !== true) {
	  	if (window.ie6) {
	  		html.setStyle('overflow-y','auto');	
	  	} else {
	  		html.setStyle('overflow','auto');	
	  	}	
	  }
	  
	  
  },
	fillLayer: function(url,header) {
    this.title.setHTML((function(){
        return header?header:"";
    })());
    var yourl = url;
    
    
		this.showOverlay();
		this.switchFlash('hidden');
		if (window.ie6==true) this.layer.style.top=(window.getScrollTop()+50)+'px';

		var len=yourl.length;
		var typ=yourl.substr(len-3,len);
		var isitadiv=false;
		var theactualdiv;
		
		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).removeClass('hidden');
			this.showLayer(this.layer_div);
		}
		else
		{
			this.iframe_count = this.iframe_count + 1;
			this.cur_frame_id = 'layer_iframe'+this.iframe_count;
			var me = this;
			this.layer_iframe=new Element('IFRAME', {'id':me.cur_frame_id,'name':me.cur_frame_id,'src':yourl,'frameborder':'0','scrolling':'no','marginheight':'0','marginwidth':'0'});
			this.layer_inner.getElement('iframe').remove();
			this.layer_iframe.injectInside(this.layer_inner);
			this.layer_iframe.addEvent('load', this.showLayer.bind(this,this.layer_iframe));
			/*this.layer_iframe.src=yourl;*/
		}
	},
	showLayer: function(el) {
		var layer_reveal = new Fx.Styles(this.layer, {
			duration: 500,
			transition: Fx.Transitions.Quad.easeOut,
			wait: true,
			fps: 30
		});

		var content_reveal = new Fx.Styles(el, {
			duration: 300,
			transition: Fx.Transitions.Quad.easeOut,
			wait: true,
			fps: 30
		});
		
		layer_reveal.addEvent('onComplete', function(){
			content_reveal.start({
				'opacity': 1
			});
		});

		content_reveal.set({'opacity':0});

		el.setStyles({height:'auto',overflow:'hidden'});
    
		if (el==this.layer_iframe) 
		{
			if (frames[this.cur_frame_id].document.getElementById('layer_holder'))
			{
				var w=frames[this.cur_frame_id].document.getElementById('layer_holder').style.width.toInt();
				var h=frames[this.cur_frame_id].document.getElementById('layer_holder').style.height.toInt();
			}
			else
			{
				var w=400;
				var h=300;
			}
			if (frames[this.cur_frame_id].document.getElementById('layer_back'))
			{
				frames[this.cur_frame_id].document.getElementById('layer_back').style.display='none';
			}
			this.layer_iframe.style.width=w+'px';
			this.layer_iframe.style.height=h+'px';	
		}
		else if (el==this.layer_image)
		{
			var w=el.getCoordinates().width;
		}
		else if (el==this.layer_div)
		{
			var w=this.layer_div.getFirst().getCoordinates().width;
		}
		
		var ww=window.getWidth();
		var w1=w/2;
		var l1=(ww/2)-(w1/2);
		var l2=(ww/2)-(w/2);
		this.layer.style.width=w1+'px';
		this.layer.style.left=l1+'px';
		
		var me = this;
		me.overlayRestoreBind = me.overlayRestore.bind(me);
		/* commented out to block user from clicking on black area
		var cb = function() {
			me.overlay.addEvent('click',me.overlayRestoreBind);	
		}
		layer_reveal.addEvent('onComplete',cb);*/
		var body = $$('body')[0];
		
    var w_coords = this.getElementCoords('window');

    var layer_height = this.layer.getCoordinates().height;

		/*if(w_coords.visHeight > (layer_height + 30)) {
			this.removeScroll();
			if (window.ie || window.webkit || navigator.userAgent.match(/Firefox\/3/)){
				this.layerToTop(w_coords.sTop);	
			}
		} else {
			this.layerToTop(w_coords.sTop);
		}*/
		
		this.removeScroll();
		if (window.ie || window.webkit || navigator.userAgent.match(/Firefox\/3/)){
			this.layerToTop(w_coords.sTop);	
		}
		/*if (window.ie || window.webkit || navigator.userAgent.match(/Firefox\/3/)){
			this.layerToTop(w_coords.sTop);	
		}*/
		

		layer_reveal.start({
			'width': w,
			'left': l2
		});		
	},
	layerToTop:function(t) {
		this.layer.style.top = t + 50 + 'px';
	},
	overlayRestore:function() {
		this.restore();
	},
	buildLayer:	function() {
	 
		this.body=document.body;
		this.oframe=new Element('IFRAME', {'id':'overlay_iframe','name':'overlay_iframe','src':gn_iframe,'frameborder':'0','scrolling':'no','marginheight':'0','marginwidth':'0'});
		this.oframe.injectInside(this.body);
		this.overlay=new Element('DIV', {'id':'overlay'});
		this.overlay.injectInside(this.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.title=new Element('SPAN', {'id':'layer_title'});
		this.s3.injectInside(this.s2);
		this.title.injectInside(this.s3);
		this.close=new Element('A', {'href':'javascript:void(0);lpCheck();','class':'close_primary', 'id':'close_id'}).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':gn_iframe,'frameborder':'0','scrolling':'no','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.restore(true);
	},
	restore: function(first) {
		first = first || false;
		this.layer_div.setStyles({height:0,overflow:'hidden'});
		this.layer_div.empty();
		this.layer_image.setStyles({height:0,overflow:'hidden'});
		this.layer_iframe.setStyles({height:0,overflow:'hidden'});
		this.layer_iframe.removeEvents();
		this.layer.style.left='-1000em';
		this.oframe.style.display='none';
		this.overlay.setStyles({width:0,height:0,display:'none'});
		this.title.setHTML('');
		this.switchFlash('visible');
		this.restoreScroll(first);
		var me = this;
		this.overlay.removeEvent('click', me.overlayRestoreBind);
	},
	specialLaunch:function(e,launcher,url,header,element) {
	  var element = element || null;
	  var event;
	  if($chk(this.persistedEvent)) {
	  	event = this.persistedEvent;
	  }else {
	  	event = e || window.event;
	  	event = new Event(event);
	  }
	  var launcher = $(launcher);
	  url = url || launcher.href || this.parseClass(launcher.getProperty('class'));
	  header = header || launcher.title;
		
		var posx=event.page.x;
		var posy=event.page.y;
		
    
    var l=launcher.getCoordinates().left;
		var r=launcher.getCoordinates().right;
		var t=launcher.getCoordinates().top;
		var b=launcher.getCoordinates().bottom;
		
		this.persistedEvent = false;
    if (posx>=l && posx<=r && posy>=t && posy<=b)
		{
		  var u = (element === null)?url:element;
      this.fillLayer(u,header);
	    return false;	    
		} else {
      window.location = url;
      return true;
    }
	},
	persistEvent:function(e) {
		e = e || window.event;
		this.persistedEvent = new Event(e).stop();
	}
});

layers.implement(new Options, new Events);



window.addEvent("domready", function () {
    if ($$(".launcher").length > 0) {
		pattern_layers = new layers({
            launchers: $$(".launcher")
        })
	}
});


