// ** 
// **              These functions are copyright of 
// **      	Nitin Vericherla <nitin.vericherla@gmail.com>
// **					except as indicated.
// ** 
// ** 

//________________________________________________________________________________________ 
//________________________________________________________________________________________ getElementById shorthand 

function GEBI(theid) {
	
	try {
		if (top.document.all)
			return top.document.all[theid];
		else if (top.document.getElementById(theid))
			return top.document.getElementById(theid);
		else
			return null;
	}
	catch (e) { return null; }
}


//________________________________________ OTHER
//____________________ set styles

function $style(theid,the_attr,the_val) {
	eval("GEBI('" + theid + "').style." + the_attr + " = " + the_val + ";");
}


//________________________________________________________________________________________ 
//________________________________________________________________________________________  Offsets
	function get_winDimensions() {
		// Thanks to QuirksMode: https://www.quirksmode.org/viewport/compatibility.html
		var x,y;
		if (self.innerHeight) {
			// all except Explorer
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) {
			// Explorer 6 Strict Mode
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) {
			// other Explorers
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		
		return [x,y];
	}
	
	
	
	function get_pageOffset() {
		// Thanks to QuirksMode: https://www.quirksmode.org/viewport/compatibility.html
		var x,y;
		
		var test1 = document.body.scrollHeight;
		var test2 = document.body.offsetHeight;
		
		if (test1 > test2) {
			// all but Explorer Mac
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		}
		else {
			// Explorer Mac;
			// would also work in Explorer 6 Strict, Mozilla and Safari
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}
		
		return [x,y];
	}
	
	
	function get_scrollOffset() {
		// Thanks to QuirksMode: https://www.quirksmode.org/viewport/compatibility.html
		var x,y;
		if (self.pageYOffset) {
			// all except Explorer
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop) {
			// Explorer 6 Strict
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body) {
			// all other Explorers
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		
		return [x,y];
	}
	
//________________________________________________________________________________________ 
//________________________________________________________________________________________  tooltip on hover
	
		function findPos(obj) {
			var curleft = curtop = 0;
			
			if (obj.offsetParent) {
				curleft = obj.offsetLeft;
				curtop = obj.offsetTop;
				while (obj = obj.offsetParent) {
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
				}
			}
			
			return [curleft,curtop];
		}
		



/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 * 
 * 05.19.2008 - Modified to remove inversions
 **************************************************/

var minX = 5;
var maxX = null;
var minY = 5;
var maxY = null;

var Drag = {

	obj : null,

	init : function(o, oRoot)
	{
		o.onmousedown	= Drag.start;

		o.root = oRoot && oRoot != null ? oRoot : o ;
		
		o.setLimits = Drag.setLimits;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},
	
	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.root.style.top);
		var x = parseInt(o.root.style.left);
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (minX != null)	o.minMouseX	= e.clientX - x + minX;
		if (maxX != null)	o.maxMouseX	= o.minMouseX + maxX - minX;
		
		if (minY != null)	o.minMouseY	= e.clientY - y + minY;
		if (maxY != null)	o.maxMouseY	= o.minMouseY + maxY - minY;
		
		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;
		
		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.root.style.top);
		var x = parseInt(o.root.style.left);
		var nx, ny;

		if (minX != null) ex = Math.max(ex, o.minMouseX);
		if (maxX != null) ex = Math.min(ex, o.maxMouseX);
		if (minY != null) ey = Math.max(ey, o.minMouseY);
		if (maxY != null) ey = Math.min(ey, o.maxMouseY);

		nx = x + (ex - o.lastMouseX);
		ny = y + (ey - o.lastMouseY);

		Drag.obj.root.style["left"] = nx + "px";
		Drag.obj.root.style["top"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style["left"]), 
									parseInt(Drag.obj.root.style["top"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};


/*
 * 
 * Main Page functions
 * 
 * 
*/


//if (document.images) {
//	
//	pic1= new Image(); 
//	pic1.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_tl.png"; 
//	
//	pic2= new Image(); 
//	pic2.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_tc.png"; 
//	
//	pic3= new Image(); 
//	pic3.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_close.png"; 
//	
//	pic4= new Image(); 
//	pic4.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_tr.png"; 
//	
//	pic5= new Image(); 
//	pic5.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_ml.png"; 
//	
//	pic6= new Image(); 
//	pic6.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_mr.png"; 
//	
//	pic7= new Image(); 
//	pic7.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_bl.png"; 
//	
//	pic8= new Image(); 
//	pic8.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_bc.png"; 
//	
//	pic9= new Image(); 
//	pic9.src = appPath + "docimages/mycareoneimages/DC_overlay/DC_overlay_br.png"; 
//	
//}

var theHandle, theRoot;
var winDimensions;
var maxOverlayHeight;
var topOverlayMargin;
var G_maxWidth;

var G_overlayIsLoaded = false;

if (!agent.firefox)
    addOnLoadEvent('init_overlay()');
else
    setTimeout("init_overlay()", 500);

function create_overlay(width, content, title, showclose)
{
	destroy_overlay();
	var divClear = document.createElement('div');
	divClear.className = "ClearBoth";
	
	var dw = 11;
	var db = 20;
	var iw = width - (db - 1), ow = width + dw, bw = width;
	var id = "";
	
	var T_greyBack = document.createElement("div");
	T_greyBack.id = "DC_Overlay_Backdrop";
	T_greyBack.className = "DC_Overlay_Backdrop";
	
	// Fix for placing overlay over drop-down boxes.
	if (agent.ie) {
	    var IFrameControl = "<IFrame src='../ui/blankpage.htm' />";
	    var T_greyBackIframe = document.createElement(IFrameControl);
	}
	else {
	    var T_greyBackIframe = document.createElement("div");
	}
	T_greyBack.appendChild(T_greyBackIframe);
	// End fix for placing overlay over drop-down boxes.
	
	document.body.appendChild(T_greyBack);
	
	if(!existsDefined(showclose))
		showclose = true;
	
	if(existsDefined(document.getElementById('DC_Overlay')))
		document.body.removeChild(document.getElementById('DC_Overlay'));
	
	var divContainer = document.createElement('div');
	divContainer.id = "DC_Overlay" + id;
	divContainer.style.width = ow + "px";
	//divContainer.style.border = "solid 1px Black";
	
	/* Handle */
	var divHandle = document.createElement('div');
	divHandle.id = "DC_Overlay_Handle" + id;
	divHandle.className = "TheHandle";
	
	var divHandleLeft = document.createElement('div');
	divHandleLeft.className = "Left";	
	
	var divHandleRight = document.createElement('div');
	divHandleRight.className = "Right";
	
	var divHandleCenter = document.createElement('div');
	divHandleCenter.className = "Center";
	divHandleCenter.style.width = iw + "px";
			

	var divHandleCenterTitle = document.createElement('span');
	divHandleCenterTitle.className = "Title";
	if(existsDefined(title)){
	    divHandleCenterTitle.innerHTML = title;
	}
	divHandleCenterTitle.id = "DC_Overlay_Handle_Title";
	divHandleCenterTitle.style.width = (iw - 70) + "px";
	divHandleCenter.appendChild(divHandleCenterTitle);
	
	if(showclose)
	{
		var divHandleCenterClose = document.createElement('a');
		divHandleCenterClose.href="#";
		divHandleCenterClose.onclick = function () { show_overlay(); return false; }
		divHandleCenterClose.className = "CloseLink";
		divHandleCenter.appendChild(divHandleCenterClose);
	}
	divHandle.appendChild(divHandleLeft);
	divHandle.appendChild(divHandleCenter);
	divHandle.appendChild(divHandleRight);

	/* End Handle */
	
	/* Body */
	var divBody = document.createElement('div');
	divBody.id = "DC_Overlay_Body" + id;
	divBody.className = "TheBody";
	
	var divBodyLeft = document.createElement('div');
	divBodyLeft.className = "Left";
	divBodyLeft.id = "DC_Overlay_Body_Left" + id;
	
	var divBodyRight = document.createElement('div');
	divBodyRight.className = "Right";
	divBodyRight.id = "DC_Overlay_Body_Right" + id;
	
	var divBodyCenter = document.createElement('div');
	divBodyCenter.className = "Center";
	divBodyCenter.id = "DC_Overlay_Body_Center" + id;
	divBodyCenter.innerHTML = content;
	divBodyCenter.style.width = bw + "px";
	divBodyCenter.appendChild(divClear);

	divBody.appendChild(divBodyLeft);
	divBody.appendChild(divBodyCenter);
	divBody.appendChild(divBodyRight);
	

	/* End Body */


	/* Footer */
	var divFooter = document.createElement('div');
	divFooter.id = "DC_Overlay_Footer" + id;
	divFooter.className = "TheFooter";
	
	var divFooterLeft = document.createElement('div');
	divFooterLeft.className = "Left";
	
	var divFooterRight = document.createElement('div');
	divFooterRight.className = "Right";
	
	var divFooterCenter = document.createElement('div');
	divFooterCenter.className = "Center";
	divFooterCenter.style.width = iw + "px";
	
	divFooter.appendChild(divFooterLeft);
	divFooter.appendChild(divFooterCenter);
	divFooter.appendChild(divFooterRight);

	/* End Footer */

	divContainer.appendChild(divHandle);
	divContainer.appendChild(divBody);
	divContainer.appendChild(divFooter);
	
	divContainer.appendChild(divClear);
	
	var hf = document.createElement('input');
	hf.type = 'hidden';
	hf.id = "hf_DC_Overlay_Width";
	hf.value = width + "";
	
	divContainer.appendChild(hf);
	//alert(divContainer.outerHTML);
	document.body.appendChild(divContainer);
	
	var centerDiv = GEBI("DC_Overlay_Body_Center" + id);
    var leftDiv = GEBI("DC_Overlay_Body_Left" + id);
    var rightDiv = GEBI("DC_Overlay_Body_Right" + id);

	if (centerDiv != null && leftDiv != null)
	{
        //alert(centerDiv.offsetHeight);
	    leftDiv.style.height = centerDiv.offsetHeight + "px";
	    rightDiv.style.height = centerDiv.offsetHeight + "px";
	}

	//resize_elements(new Array("DC_Overlay_Body_Left" + id,"DC_Overlay_Body_Center" + id,"DC_Overlay_Body_Right" + id));
			    
	theHandle = document.getElementById("DC_Overlay_Handle");
	theRoot = document.getElementById("DC_Overlay");
	
	Drag.init(theHandle, theRoot);
	
	winDimensions = get_winDimensions();
	maxX = parseInt(winDimensions[0]) - 5;
	maxY = parseInt(winDimensions[1]) - 5;
	
	G_maxWidth = Math.floor(winDimensions[0] * 0.8);
	
	maxOverlayHeight = Math.floor(winDimensions[1] * 0.8) - 51;
	topOverlayMargin = Math.floor(winDimensions[1] * 0.1) + 30;
		
	G_overlayIsLoaded = true;
	
	adjust_overlay();
	
	position_overlay(width);
	
}

function adjust_overlay()
{
	var overlayWidth = "990px";
	var overlay = document.getElementById('DC_Overlay_Backdrop');
	overlay.style.width = overlayWidth;
	
	//Set Height for Overlay has to be done on click to get the height of the expanded Help Center Callout (endfaqdiv)
	var allDivs = document.getElementsByTagName('div');
	
	var T_divsLength = allDivs.length;
	var endfaqdiv = false;
	var bodydiv = false;
		
	//Get the position of the tag in the Master page that sets the bottom of the overlay 
	for (var i_divs = 0; i_divs < T_divsLength; i_divs++) {
		//Div is located in CareOne.Master
		if (allDivs[i_divs].className == 'endOverlay')
		    endfaqdiv = findPos(allDivs[i_divs]);	
			    
        if (allDivs[i_divs].className == 'Overlay_body')
		    bodydiv = findPos(allDivs[i_divs]);			    		    
	}	
	
	//Overlay - Grey Area Height Set Here For Each Browser
	//TODO: Set height for other browsers
	if (endfaqdiv) {
		if (agent.ie) {
			overlay.style.height = endfaqdiv[1] + 'px'; 			    
		}
		else
			overlay.style.height = endfaqdiv[1] + 'px'; 
		
		//GEBI('overlay').style.top =  bodydiv[1] - ((agent.ie) ? 4 : 0) + 'px';
		//Shifts Grey Area
		overlay.style.left = bodydiv[0] + 'px';
	}
	else {
		var dims = get_pageOffset();
		overlay.style.height = winDimensions[1] + 'px';
	}

	
	overlay.position = "absolute";
}

function position_overlay(thewidth)
{
	var ovr = document.getElementById("DC_Overlay");
	var ovrcont = document.getElementById('DC_Overlay_Primary_Container');
	ovr.style.zIndex = "9999";//"4300";
	ovr.style.position = "absolute";
	var bottomOffset = findPos(document.getElementById('DC_Overlay_Footer'));
	var mainBodyOffset = findPos(document.getElementById('DC_Overlay_Body'));
	var scrollOffset = get_scrollOffset();
	var T_pageOffset = get_pageOffset();
	winDimensions = get_winDimensions();
	var T_mainBodyHeight = bottomOffset[1] - mainBodyOffset[1];
	
	/*if (T_mainBodyHeight > maxOverlayHeight) {
		$style('DC_Overlay_Body','overflowY',"'scroll'");
		document.getElementById('DC_Overlay_Body').style.height = maxOverlayHeight + 'px';
		T_mainBodyHeight = maxOverlayHeight;
	}
	else {
		$style('DC_Overlay_Body','overflowY',"'auto'");
	}*/
	
	var widthAdd = 0;
	
	if(agent.ie)
		widthAdd = 30;
	else
		widthAdd = 45;
	
	
	document.getElementById('DC_Overlay').style.left = Math.floor((winDimensions[0] / 2) - ((widthAdd + thewidth) / 2)) + 'px';
	
	maxX = (parseInt(winDimensions[0]) - thewidth - 50)
	maxY = (parseInt(T_pageOffset[1]) - T_mainBodyHeight - 70);
	
	bottomOffset = findPos(document.getElementById('DC_Overlay_Footer'));
	var mainBodyOffset = findPos(document.getElementById('DC_Overlay_Body'));
	
	document.getElementById('DC_Overlay').style.top = Math.floor(scrollOffset[1] + (winDimensions[1] * 0.5) - ((bottomOffset[1] - mainBodyOffset[1]) / 2)) + 'px';
	
	//alert(document.getElementById('DC_Overlay_Backdrop').outerHTML);
	document.getElementById('DC_Overlay_Backdrop').style.visibility = "visible";
	document.getElementById('DC_Overlay').style.visibility = "visible";
}

function init_overlay(checkQuery) 
{


	if(!existsDefined(checkQuery))
		checkQuery = true;
	
	if(checkQuery)
	{
	
		var T_performAction = "";
	
		try 
		{
			var T_location = parent.location + "";
			var T_urlparts = T_location.split("?");
			
			if (T_urlparts[1]) 
			{
				var T_test = T_urlparts[1] + "";
				var T_queryString_parts = T_test.split("&");
				
				var T_length = T_queryString_parts.length;
				var T_currPart;
				var T_cid;
				
				for (var i_DC_this = 0; i_DC_this < T_length; i_DC_this++) 
				{
					T_currPart = T_queryString_parts[i_DC_this].split("=");
					//G_overlayIsLoaded = true;
					
					if (T_currPart[0] == "dispoverlay")
						T_performAction = T_currPart[1];
				}
			}
		}
		catch (EXC_dummy) 
		{
			T_cid = "";
		}
		//G_overlayIsLoaded = true;
		
		//alert("T_performAction: [" + T_performAction + "]");
		
//		if (T_performAction != "") 
//		{
//			switch (T_performAction) 
//			{
//				case '2':
//					show_overlay(398,'<div class="BodyContainer"><img src="' + appPath + 'docimages/mycareoneimages/DC_overlay/mycareonelogo_sm.jpg" style="float:right; margin:15px 0px 10px 15px;" /><h2>Hi there!</h2><p>We\'re glad to see you\'re about to visit <br /><strong>My CareOne</strong>, an exciting new section of our site. You\'ll find all kinds of tools and resources here to help you budget and set financial goals. And this is just the beginning - we plan on adding even more.</p><p style="border-bottom:1px solid #dce9d9; padding-bottom:10px; ">Already enrolled in a debt management plan? You\'ll find all your details in the My Program section.</p><a class="OrangeBlockContinueToMyCareOne" onclick="show_overlay();"></a></div>','',false);
//					break;
//				case 'TRANSIT':
//					show_overlay(398,'<div class="BodyContainer"><img src="' + appPath + 'docimages/mycareoneimages/DC_overlay/mycareonelogo_sm.jpg" style="float:right; margin:15px 0px 10px 15px;" /><h2>Hi there!</h2><p>We\'re glad to see you\'re about to visit <br /><strong>My CareOne</strong>, an exciting new section of our site. You\'ll find all kinds of tools and resources here to help you budget and set financial goals. And this is just the beginning - we plan on adding even more.</p><p style="border-bottom:1px solid #dce9d9; padding-bottom:10px; ">Already enrolled in a debt management plan? You\'ll find all your details in the My Program section.</p><a class="OrangeBlockContinueToMyCareOne" onclick="show_overlay();"></a></div>','',false);
//					break;
//				case 'Tour':
//					if(!agent.firefox)
//						show_video();
//					else
//						waitForGo("show_video();",2000);
//					break;
//				default:
//					
//					break;
//			}
//		}
	}
	
	return true;
	
}

function removeChildNodes(ctrl)
{  
    while (ctrl.childNodes[0])  
    {    
        ctrl.removeChild(ctrl.childNodes[0]);  
    }
}

function destroy_overlay()
{
	try
	{
	    var backDrop = document.getElementById('DC_Overlay_Backdrop');
	    var overlay = document.getElementById('DC_Overlay');
	    FixMenuBack();
	    removeChildNodes(backDrop);
	    removeChildNodes(overlay);	   
		document.body.removeChild(backDrop);
		document.body.removeChild(overlay);
	}
	catch(e)
	{
		
	}
}

function show_overlay(thewidth,thetext,thetitle,showclose,checkflash) 
{
	if(!existsDefined(thewidth) && !existsDefined(thetext) && !existsDefined(thetitle) && !existsDefined(showclose)  && !existsDefined(checkflash)){
		destroy_overlay();
	}else{
		create_overlay(thewidth,thetext,thetitle,showclose);
		FixMenu();	
    }
	if(!existsDefined(showclose))
		showclose = true;
		
	if(!existsDefined(checkflash))
		checkflash = false;
		
	if(checkflash)
		if(GetSwfVer() == -1)
			show_overlay(550,'<div class="NoFlashPopup"><img src="../docimages/careoneimages/careone/images/icons/icon_no_flash.jpg" class="FlashIcon" /><p>You\'ll need Flash 8 or later to see the tour of My CareOne.<br /><a href="https://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&promoid=BUIGP" target="_blank">Download it here</a>.<p></div>',thetitle,true);
    
	return false;
}

function existsDefined(i)
{
	if(i != null && i != undefined)
		return true;
	else
		return false;
}

// Puts the menu table at the proper z-index when the overlay is shown. (AMB)
function FixMenu(overlay)
{
    var menuTable = document.getElementById('menuTable');
    var tables = document.getElementsByTagName('table');
    var pos = null;
    var zIdx = null; 
    
    for (i=0; i < tables.length; i++)
    {
        if (tables[i].className == "header")
        {
            menuTable = tables[i];
            break;
        }
    }
        pos = "relative";
        zIdx = 99; // grey background is 200, overlay window is 300.

    if (menuTable != null)
    {
        menuTable.style.position = pos;
        menuTable.style.zIndex = zIdx;
    }
}

//Returns menu z index back;
function FixMenuBack()
{
    var menuTable = document.getElementById('menuTable');
    var tables = document.getElementsByTagName('table');
    var zIdx = null; 
    for (i=0; i < tables.length; i++)
    {
        if (tables[i].className == "header")
        {
            menuTable = tables[i];
            break;
        }
    }
    zIdx = 999999; 
    if (menuTable != null)
    {
        menuTable.style.zIndex=zIdx;
    }
}

function resize_overlay()
{
    var hf = document.getElementById("hf_DC_Overlay_Width");
    if(hf != null && hf != undefined)
    {
	    var w = parseInt(hf.value);
	    position_overlay(w);
	    adjust_overlay();
    }
}
