//++
//	GE Lightbox for survey popup on ge reports
//	author: kyle.crouse@frogdesign.com
//	inspired by: http://www.huddletogether.com/projects/lightbox2/
//++
//	1/4/2010 - modified from GE_Lightbox.js on GE.com to remove flash, video, image compontents and use static html.  added css inline.
//++
var GE_surveyLightbox = Class.create({
	buildImage:function(img){ 
		var self = this;
		this.lightbox
			.insert( new Element('div').setStyle({left:'-9999px',position:'absolute'})
				.insert( new Element('h2').insert('Help us improve our web site') )
				.insert( new Element('p').insert('Tell us about your experience in a brief ten-minute survey and you will be entered for a chance to win a $50 gift certificate from Amazon.com.') )
				.insert( new Element('p').insert('If you choose to participate, the survey will appear behind your current browser window.  You can continue to browse until you are ready to take the survey.') )
				.insert( new Element('p').insert( new Element('a',{href:'#'}).insert('Take the Survey').observe('click',function(e){ e.stop(); self.end(); window.open('http://survey.eqr1.com/T04934/Instructions.html','surveyWindow','z-lock=yes'); window.focus(); }) ) )
				.insert( new Element('p').insert('Only one entry per person.  Void where prohibited.') )
			)
			.insert( new Element('a',{href:'#',title:'Take the Survey'}).insert( img ).observe('click',function(e){ e.stop(); self.end(); window.open('http://survey.eqr1.com/T04934/Instructions.html','surveyWindow','z-lock=yes'); window.focus(); }) );
	},
	disableKeyboardNav:function() { document.onkeydown = ''; },
	destroyLightbox:function(){ this.lightbox.update( this.close_link ); },
	enableKeyboardNav: function() {
		document.onkeydown = function(e) {
			var keycode;
			if (e == null) { keycode = event.keyCode; } // ie
			else { keycode = e.which; } // mozilla
			var key = String.fromCharCode(keycode).toLowerCase();
			if( (key == 'x') || (key == 'o') || (key == 'c') ) { this.end(); } // close lightbox
		}.bindAsEventListener(this);
	},
	end: function() {
		this.disableKeyboardNav();
		this.hideLightbox();
		GE_surveyLightbox.showSelectBoxes();
	},
	fetchImage:function(){
		var self = this;
		var img = new Image();
		img.src = GE_surveyLightbox.imagePath + 'surveyCopy.gif';
		img.alt = '';
		img.onload = function(){ self.buildImage(this); self.showLightbox(); };
	},
	hideLightbox:function(){
		var self = this;
		new Effect.Parallel( [
				new Effect.Appear(this.lightbox,{from:1.0,to:0.0,sync:true}),
				new Effect.Appear(this.overlay,{from:0.5,to:0.0,sync:true})
			],
			{ 
				afterFinish:function(){
					self.lightbox.setStyle({display:'none'});
					self.overlay.setStyle({display:'none'});
					self.destroyLightbox();
				},
				duration:0.2
			}
		);
	},
	initialize:function(){
		var self = this;
		this.overlay = new Element('div',{title:'click to close this layer'}).addClassName('lb-overlay').setStyle({backgroundColor:'#000',height:'500px',left:'0',position:'absolute',top:'0',width:'100%',zIndex:'9000',display:'none'}).observe('click', function(e) { self.end(); e.stop(); });
		this.lightbox = new Element('div').addClassName('lb-content').setStyle({backgroundColor:'#fff',border:'1px solid #cfcfcf',color:'#333',height:'232px',marginLeft:'-186px',padding:'40px 25px 20px',position:'absolute',left:'50%',width:'322px',zIndex:'9002',display:'none'})
			.insert( this.close_link = new Element('a',{href:'#',title:'close this layer'}).addClassName('close').setStyle({background:'transparent url('+ GE_surveyLightbox.imagePath +'surveyCloseButton.gif) no-repeat 0 0',height:'11px',overflow:'hidden',position:'absolute',right:'12px',textIndent:'-9999px',top:'12px',width:'42px'}).observe('click',function(e) { self.end(); e.stop(); }) );
		document.body.appendChild(this.overlay);
		document.body.appendChild(this.lightbox);
		Event.observe(window,'resize',this.positionOverlay.bind(this));
	},
	positionOverlay:function(){ this.overlay.setStyle({height:$(document.body).getHeight()+'px'}); },
	showLightbox:function(){
		var self = this;
		new Effect.Parallel([
				new Effect.Appear(this.lightbox,{from:0.0,to:1.0,sync:true})
			],{
				beforeStart:function(){
					GE_surveyLightbox.centerInViewport(self.lightbox);
					self.lightbox.setOpacity(0).setStyle({display:'block'});
				},
				afterFinish:function(){
					self.enableKeyboardNav();
				},
				duration:0.2
			}
		);
	},
	start:function(){
		var self = this;
		GE_surveyLightbox.hideSelectBoxes();
		this.positionOverlay();
		new Effect.Parallel([
				new Effect.Appear(this.overlay,{from:0.0,to:0.5,sync:true})
			],{ 
				beforeStart:function(){
					self.overlay.setOpacity(0).setStyle({display:'block'});
				},
				afterFinish:function() {
					self.fetchImage();
				},
				duration:0.2
			}
		);
	}
});
GE_surveyLightbox._instance = null;
GE_surveyLightbox.centerInViewport = function(el){
	var h = (el.getHeight()+30)/2;
	var scrollY = document.viewport.getScrollOffsets().top;
	var winHeight = document.viewport.getHeight();
	var top = (scrollY+((winHeight/2) - h));
	el.setStyle({top:(top<0?0:top)+'px'});
};
GE_surveyLightbox.getInstance = function() { if (GE_surveyLightbox._instance == null) GE_surveyLightbox._instance = new GE_surveyLightbox(); return GE_surveyLightbox._instance; };
GE_surveyLightbox.hideSelectBoxes = function(){ var els = document.getElementsByTagName('select'); $A(els).each( function(el){ el.hide(); } ); };
GE_surveyLightbox.showSelectBoxes = function(){ var els = document.getElementsByTagName('select'); $A(els).each( function(el){ el.show(); } ); };
GE_surveyLightbox.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};
GE_surveyLightbox.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};
GE_surveyLightbox.eraseCookie = function(name) { GE_surveyLightbox.createCookie(name,"",-1); };
GE_surveyLightbox.imagePath = '/wp-content/themes/GE_reports/images/';

document.observe('dom:loaded', function(){ 
	if (GE_surveyLightbox.readCookie('gerSurvey2010')) return; 
	GE_surveyLightbox.getInstance().start();
	GE_surveyLightbox.createCookie('gerSurvey2010',true,365);
});
