var pu_event = {

  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  }, 

  DOMit: function(e) { 
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;
    
    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
        
    return e;
  }  
}

var viewport = {
	
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
}

// IE versions 6 and older will always force a <select> element over any <div> element 
// but an <iframe> will reneder above the <select>
// I am adding additions to place an <iframe> the exact size of the popup 
// above the <select> but behind the <div> in IE v6 and lesser
// Ed Campodonico

var PopUp = {
	
    followMouse: true,
    offX: 8,
    offY: 12,
    tipID: "tipDiv",
    showDelay: 500,
    hideDelay: 100,
	Transparent: 100,
	ready:false,
	timer:null,
	tip:null,
	
	init:function(){
		if(document.createElement&&document.body&&typeof document.body.appendChild!="undefined"){
			if(!document.getElementById(this.tipID)){
				var el=document.createElement("DIV");
				el.id=this.tipID;
				el.style.zIndex="20";
				document.body.appendChild(el);
			}
		// adding section to create the iframe for IE v6 and lesser
			if (BrowserDetect.browser=="Explorer" && BrowserDetect.version < 7){
					if(!document.getElementById("iframeBlock")){
						var iel=document.createElement("iframe");
						iel.id="iframeBlock";
						iel.scrolling="no";
						iel.frameBorder="0";
						iel.style.position="absolute";
						iel.style.zIndex="10";
						iel.style.visibility="visible";
						iel.style.height="0px";// added to keep the iframe from having size on a page before it is called
						//added to prevent non secure references
						//iel.src="#"; //did not work, the error persisted
						//iel.src=void(0); //did not work, seemed to force a reload avery few seconds
						//iel.src="javascript:void(0);";//did not work in UAT, worked in test
						iel.src="../../images/pixel.gif";
						document.body.appendChild(iel);
					}
			}
			this.ready=true;
		}
	},
	
	show:function(e,msg,w){
		if(this.timer){
			clearTimeout(this.timer);
			this.timer=0;
		}
		this.tip=document.getElementById(this.tipID);
		if(this.followMouse) pu_event.add(document,"mousemove",this.trackMouse,true);
		this.writeTip("");
		this.writeTip(msg);
		viewport.getAll();
		this.positionTip(e);
		this.tip.style.width=w+"px";
		this.timer=setTimeout("PopUp.toggleVis('"+this.tipID+"', 'visible')",this.showDelay);
	},
	
	writeTip:function(msg){
		if(this.tip&&typeof this.tip.innerHTML!="undefined")this.tip.innerHTML=msg;
	},
	
	positionTip:function(e){
		if(this.tip&&this.tip.style){
			var x=e.pageX?e.pageX:e.clientX+viewport.scrollX;
			var y=e.pageY?e.pageY:e.clientY+viewport.scrollY;
			if(x+this.tip.offsetWidth+this.offX>viewport.width+viewport.scrollX){
				x=x-this.tip.offsetWidth-this.offX;
				if(x<0)x=0;
			}else x=x+this.offX;
			if(y+this.tip.offsetHeight+this.offY>viewport.height+viewport.scrollY){
				y=y-this.tip.offsetHeight-this.offY;
				if(y<viewport.scrollY)y=viewport.height+viewport.scrollY-this.tip.offsetHeight;
			}else y=y+this.offY;
			this.tip.style.left=x+"px";
			this.tip.style.top=y+"px";
			//position the iframe for IE v6 and lesser
			PopUp.positionIframe();
		}
	},
	
	hide:function(){
		if(this.timer){
			clearTimeout(this.timer);
			this.timer=0;
		}
		this.timer=setTimeout("PopUp.toggleVis('"+this.tipID+"', 'hidden')",this.hideDelay);
		if(this.followMouse)pu_event.remove(document,"mousemove",this.trackMouse,true);
		this.tip=null;
	},

	fadeout:function() {
		if(this.timer){
			clearTimeout(this.timer);
			this.timer=0;
		}
		this.tip = document.getElementById(this.tipID);
		var iframeBlock = document.getElementById('iframeBlock');
		this.Transparent -= 5;
		if (this.Transparent < 5) {
			//clearTimeout(this.timer);
			this.tip.style.visibility = "hidden";
			if(document.getElementById('iframeBlock'))iframeBlock.style.visibility = "hidden";
			PopUp.setOpacity(100);
			if(this.followMouse)pu_event.remove(document,"mousemove",this.trackMouse,true);
			this.tip=null;
			return false;
		} else {
			PopUp.setOpacity(this.Transparent);
			this.timer=setTimeout("PopUp.fadeout()",50);
		}
	},
	
	toggleVis:function(id,vis){
		
		// This ist the on/off for the iframe 
		if (BrowserDetect.browser=="Explorer" && BrowserDetect.version < 7){
			if (document.getElementById('iframeBlock')){
				PopUp.positionIframe();
				var iframeBlock = document.getElementById('iframeBlock');
				iframeBlock.style.visibility = vis;
			}
		}
		
		var el=document.getElementById(id);
		if(el) el.style.visibility=vis;
		
	},
	
	positionIframe:function(){ // This ist the positioning for the iframe 		
		
		if (BrowserDetect.browser=="Explorer" && BrowserDetect.version < 7){
			if (document.getElementById('iframeBlock')){
				var tiDiv = document.getElementById('tipDiv');
				var iframeBlock = document.getElementById('iframeBlock');
				iframeBlock.style.height = tiDiv.offsetHeight;
				iframeBlock.style.width = tiDiv.offsetWidth;
				iframeBlock.style.top = tiDiv.offsetTop;
				iframeBlock.style.left = tiDiv.offsetLeft;
			}
		}
		
	},
	
	trackMouse:function(e){
		e=pu_event.DOMit(e);
		PopUp.positionTip(e);
	},
	
	setOpacity: function(e){
		if (this.tip) {
			this.tip.style.filter = "alpha(opacity="+ e +")";
			this.tip.style.opacity = e/100;
		}
		this.Transparent = e;
	}
};

PopUp.tipOutCheck = function(e) {
  e = pu_event.DOMit(e);
  // is element moused into contained by PopUp?
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
  if ( this != toEl && !contained(toEl, this) ) PopUp.fadeout(); 
}

PopUp.timerId = 0;
PopUp.clearTimer = function() {
	if (PopUp.timerId) { clearTimeout(PopUp.timerId); PopUp.timerId = 0; }
	PopUp.setOpacity(100);
}

PopUp.unHookHover = function () {
    var tip = document.getElementById? document.getElementById(PopUp.tipID): null;
    if (tip) {
        tip.onmouseover = null; 
        tip.onmouseout = null;
        tip = null;
	}
}
// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
  return false;
}
// e=event, msg=one of the above variables, w=width of popup
function doPopUp(e, msg, w) {
  if ( typeof PopUp == "undefined" || !PopUp.ready ) return;
  PopUp.clearTimer();
  var tip = document.getElementById? document.getElementById(PopUp.tipID): null;
  if ( tip && tip.onmouseout == null ) {
		tip.onmouseout = PopUp.tipOutCheck;
		tip.onmouseover = PopUp.clearTimer;
  }
  PopUp.show(e, msg, w);
}

function hideTip() {
  if ( typeof PopUp == "undefined" || !PopUp.ready ) return;
  PopUp.timerId = setTimeout("PopUp.fadeout()", 100);
}

// Removes the titles from the hover links that appear when JS is disabled
function nullLinks() {
	hoverLinks = document.getElementsByTagName("a");
	for (var i=0; i < hoverLinks.length; i++)	{
		if (hoverLinks[i].className == "rollover") { 
			//alert(hoverLinks[i].getAttribute("title"));
			hoverLinks[i].setAttribute("title","");
		}
	}
}

pu_event.add(window, "unload", PopUp.unHookHover, true);

// adjust horizontal and vertical offsets here
// (distance from mouseover event which activates PopUp)
PopUp.offX = 20;  
PopUp.offY = -2;
PopUp.followMouse = false;  // must be turned off for hover-tip

// Standard PopUp content variables that contain the actual HTML for the popup; add more as necessary.
var popup000="The appearance of this page and the sections above that you have opened or closed are stored in a small text file on your machine so that when you leave this page and later return, it looks the same.  No personally identifiable information is stored or transmitted.";
var popup001='Click to expand or collapse this section';
var popup002='Consumer-directed health plans give members more flexibility and control over their health care dollars.';
var popup003='Alternative care typically means services provided by chiropractors, naturopaths, acupuncturists, and massage therapists.';
// The 004 - 014 definitions below should be used on pages in states where we can use the word "member." Otherwise, use 015-020 for pages in states where must say "insured."
var popup004='The coinsurance maximum is the most a member would have to pay in a year for their portion of the covered expense. After that, Regence pays 100% of all covered expenses, up to the lifetime maximum.';
var popup005='Preventive care services include routine services like well-child care, adult physicals and exams, lab and x-ray, immunizations and more.';
var popup006='To learn about life and disability plans, visit our partner, <a href="http://www.regencelife.com/" target="_blank">Regence Life & Health &raquo;</a>';
var popup007='A fixed, annual dollar amount that a member pays for medical services before Regence begins paying for covered medical services.';
var popup008='The maximum amount Regence pays for covered expenses across coverage years for each member.';
var popup009='A fixed dollar amount the member pays the provider when they receive a medical service.';
var popup010='The percentage Regence pays toward the total negotiated charges for medical services.';
var popup011='A group of selected health care providers (such as hospitals and physicians) who agree to provide Regence members with services at a negotiated rate.';
var popup012='Providers include hospitals, clinics, physicians, and other health care professionals (such as midwives and nurse practitioners).';
var popup013='Preferred Provider Organization (PPO) plans generally cover any provider. Members get more coverage and lower out-of-pocket costs for seeing "preferred" network providers.';
var popup014='No referrals means a member can go directly to a specialist without first seeing their regular doctor.';
// The 015 - 020 definitions below should be used in states where we must use the term "insured." Otherwise, use definitions above for states where we can use the word "member."
var popup015='The coinsurance maximum is the most an insured would have to pay in a year for their portion of the covered expenses, after meeting the deductible. After that, Regence pays 100% of all covered expenses, up to the lifetime maximum.';
var popup016='A fixed, annual dollar amount that an insured pays for medical services before Regence begins paying for covered medical services.';
var popup017='The maximum amount Regence pays for covered expenses for each insured. This amount accumulates across coverage years, as long as an insured is enrolled on this health plan.';
var popup018='A fixed dollar amount the insured pays the provider when they receive a medical service.';
var popup019='Preferred Provider Organization (PPO) plans generally cover any provider. An insured gets more coverage and lower out-of-pocket costs for seeing "preferred" network providers.';
var popup020='No referrals means an insured can go directly to a specialist without first seeing their regular doctor.';
// 100-108 definitions for dental
var popup100='A fixed, annual dollar amount that a member pays for medical services before Regence begins paying for covered medical services.';
var popup101='A fixed, annual dollar amount that an insured pays for medical services before Regence begins paying for covered medical services.';
var popup102='The maximum amount Regence will pay for dental services in the plan year.';
var popup103='The maximum amount Regence pays for covered expenses for each member. This amount accumulates across coverage years, as long as a member is enrolled on this dental plan.';
var popup104='The maximum amount Regence pays for covered expenses for each insured. This amount accumulates across coverage years, as long as an insured is enrolled on this dental plan.';
var popup105='A fixed dollar amount the member pays the provider when they receive a service.';
var popup106='A fixed dollar amount the insured pays the provider when they receive a service.';
var popup107='The percentage Regence pays toward the total negotiated charges for services.';
var popup108='Providers include dentists, orthodontists, dental surgeons and other licensed dental care professionals.';
var popup109='Endodontics is a restorative treatment for the center of a tooth, such as root-canal therapy.';


// Adding browser detection

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

