﻿/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

/*-----------------------------------------------------------------------------------------------*/

//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 checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;	
	return place;
}

function set_Scookie(name, value, exp_y, exp_m, exp_d) {
  var cookie_string = name + "=" + escape(value);
  if (exp_y) {
    var expires = new Date(exp_y, exp_m, exp_d);
    cookie_string += "; path=/";
    //cookie_string += "; domain=" + cookieDomain;
    cookie_string += "; expires=" + expires.toGMTString();
  }  
  document.cookie = cookie_string;
}

function get_Scookie (cookie_name) {
  var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');  
  if (results) return (unescape(results[1]));
  else return null;
} 

/*-----------------------------------------------------------------------------------------------*/
Event.observe(window, 'unload', Event.unloadCache, false); 
var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 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);
			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.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		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); 
	},
	
	displayLightbox: function(display){
		
		var content ='<div class="lbCloseBar"><a href="#" id="btnClose" rel="deactivate" ><img src="http://www.martindale.com/images/buttons/sbs_remove.gif" border="0"/></a></div><div>' +
		'<strong>' + surveyHeadline + '</strong><br/><br/>' +
		surveyInvitation +
		'</div><div class="lbButtons"><a href="#" id="btnYes" class="lbAction" rel="activator">Yes</a>&nbsp;&nbsp;&nbsp;<a href="#" id="btnNo" class="lbAction" rel="deactivate" >No</a></div>';
		info = '<div id="lbContent">' + content + '</div>';
		
		$('lightbox').innerHTML = info;
			
		var startX = YAHOO.util.Dom.getViewportWidth() / 2;
		var endX = startX - 180;
		var startY = YAHOO.util.Dom.getViewportHeight() / 2;
		var endY = startY - 110;
			
		$('lightbox').style.top = endY + "px";
		$('lightbox').style.left = endX + "px";
		
		$('lightbox').style.display = display;	
		
		//Actions
		Event.observe($('btnYes'), 'click', this['activator'].bindAsEventListener(this), false);
		$('btnYes').onclick = function(){set_Scookie(MHCookieName, '0', cookie_year, cookie_month, cookie_day);return false;};
		Event.observe($('btnNo'), 'click', this['deactivate'].bindAsEventListener(this), false);
		$('btnNo').onclick = function(){set_Scookie(MHCookieName, '0', cookie_year, cookie_month, cookie_day);return false;};
		Event.observe($('btnClose'), 'click', this['deactivate'].bindAsEventListener(this), false);
		$('btnClose').onclick = function(){set_Scookie(MHCookieName, '0', cookie_year, cookie_month, cookie_day);return false;};

			
		if(browser == 'Safari'){
			$('lightbox').style.width = "360px";
			$('lightbox').style.height = "220px";
			
			$('lightbox').className = "done";
		}
		else{	
			$('overlay').style.display = display;
					
			var attributes = {opacity: { from: 0, to: 0.3 }};
			var darken = new YAHOO.util.Anim('overlay', attributes, .3);
			darken.animate(); 			
		
			var wAnim = new YAHOO.util.Anim('lightbox', {
								width: { to: 360 }  
								}, 1, YAHOO.util.Easing.easeOut);
			
			var hAnim = new YAHOO.util.Anim('lightbox', {
								height: { to: 220 }  
								}, 1, YAHOO.util.Easing.easeOut);
								
			hAnim.onComplete.subscribe(
				function(){
					$('lightbox').className = "done";
				}
			);
									
			wAnim.onComplete.subscribe(
				function(){
					hAnim.animate();
				}
			); 				
						
			wAnim.animate();	
		}
	},
	
	// 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");
	},
	
	activator: function() {
		this.deactivate();
		window.open(surveyURL,'','width=700,height=500,left=200,top=200,resizable=1,scrollbars=1').focus();
		//window.focus();	
	}
	
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	if (!document.getElementById('overlay')) {
		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);
}
