//	PopUpWindow.js
//
//	TEG, Jan 15, 2009 - Created
//  TEG, Aug  7, 2011 - Added popEmbedVideo() and toggleEmbedVideo()
//  TEG, Oct 24, 2011 - Added popUpDivToggle()
//
var _isIE = document.all ? true : false;

var _vLink    = "/EmailSignUp.asp";
var _sName    = "EmailSignUp";
var _puwin    = null;

var _counter  = 0;
var _timerObj = null;

var _vVidLink    = "/VidWin.asp";
var _sVidName    = "VidWin";
var _puvidwin    = null;

var itemPopUpDiv = null;
var gsDivName = "";
var gTimeoutVar = 0;
var _nPUWImageOffsetTop  = 0;
var _nPUWImageOffsetLeft = -40;

var _bDebugModePUW = false;  // debug mode flag (verbose error reporting)

// ---------------------------------------------------------------------------------------------------------------------------------------------
// this form calls the function automatically, i.e., calls javascript function inline when via
// Response.Write "<script language=""javascript"" type=""text/javascript"" src=""/javascript/popUpWindow.js#custid/" & vCustID & """></script>"
/* if not commented-out, gets auto-called with passed-in params
(function()
{
	var rc = popUpWindow();
	
	return(rc);
})()
//}
*/

//function popUpWindow(vLink, sName, custID)
function popUpWindow()
{
	var rc = false;
	
	try
	{
		//alert("in popUpWindow()");
		
		if(!window.focus)
		{
			rc = true;
		}
		else
		{
			var sHref;
			
			if(typeof(_vLink) == 'string')
			{
				sHref = _vLink;
			}
			else
			{
				sHref = _vLink.href;
			}
			
			//+++TEST CODE+++sHref = sHref + "?a=0"
			
			var sPc = ""
			if(sHref.indexOf("?") >= 0)
			{
				sPc = "&";
			}
			else
			{
				sPc = "?";
			}
			
			sHref = sHref + sPc + "CustID=" + getScriptUrlParams()["custid"];
			
			_puwin = window.open(sHref, _sName, 'top=200,left=200,width=400,height=300,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
			_puwin.moveTo(200, 200);
			_puwin.focus();
			
			_timedFocus("_puwin", 2000);
			_timedClose("_puwin", 120000); //30000 = 30 seconds
			
			rc = true;
		}
	}
	catch(e)
	{
		if(_bDebugModePUW)
		{
			if(e.description.length != 0) { alert("popUpWindow() : Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
	
	return(rc);
}

function popEmbedVideo(sLink, lWidth, lHeight)
{
	var rc = false;
	
	try
	{
		//alert("in popEmbedVideo()");
		
		if(!window.focus)
		{
			rc = true;
		}
		else
		{
			var sPc = ""
			if(_vVidLink.indexOf("?") >= 0)
			{
				sPc = "&";
			}
			else
			{
				sPc = "?";
			}
				
			_puvidwin = window.open(_vVidLink + sPc + "emblnk=" + sLink, _sVidName, 'top=200,left=200,width=' + lWidth + ',height=' + lHeight + ',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
			_puvidwin.moveTo(400, 300);
			_puvidwin.focus();

			_timedFocus("_puvidwin", 500);
			_timedClose("_puvidwin", 60000); //30000 = 30 seconds
				
			rc = true; //when called via <a> onclick, return false; prevents navigation attempt to href
		}
	}
	catch(e)
	{
		if(_bDebugModePUW)
		{
			if(e.description.length != 0) { alert("popEmbedVideo() : Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
	
	return(rc);
}

function toggleEmbedVideo(elemTog, elemId)
{
	var rc = true;
	
	try
	{
		//alert("in toggleEmbedVideo()");
		
		var itemTogElem = document.getElementById(elemId);
		
		if(itemTogElem != null)
		{
			if(itemTogElem.style.display == "none")
			{
				itemTogElem.style.display = "block";
				elemTog.innerHTML = elemTog.innerHTML.replace("See", "Hide");
			}
			else
			{
				itemTogElem.style.display = "none";
				elemTog.innerHTML = elemTog.innerHTML.replace("Hide", "See");
			}
			
			rc = false; //when called via <a> onclick, return false; prevents navigation attempt to href
		}
	}
	catch(e)
	{
		if(_bDebugModePUW)
		{
			if(e.description.length != 0) { alert("toggleEmbedVideo() : Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
	
	return(rc);
}

function popUpDivToggle(sDivName, sDiv_aid, bForceHidden)
{
	var rc = false;
	
	try
	{
		gsDivName = sDivName;

		document.onclick = null;
		
		if(gsDivName != null && gsDivName.length > 0)
		{
			//alert("in popUpDivToggle()");
			
			//alert(gsDivName);
			//alert(sDiv_aid);

			var iDiv_aid = null;
			if(sDiv_aid != null && sDiv_aid.length > 0)
			{
				iDiv_aid = document.getElementById(sDiv_aid);
			}
			//alert(iDiv_aid);
			
			//setTimeout("gTimeoutVar = gTimeoutVar + 1;", 10);
			
			itemPopUpDiv = document.getElementById(gsDivName);
			
			if(itemPopUpDiv != null)
			{
				if(iDiv_aid != null)
				{
					if(!bForceHidden)
					{
						setOffsetsPUW(iDiv_aid, itemPopUpDiv);
					}
				}
				
				if(_isIE)
				{
					// toggle visibility
					//alert(itemPopUpDiv.style.visibility);
					if((itemPopUpDiv.style.visibility == 'hidden') && !bForceHidden)
					{
						itemPopUpDiv.style.visibility = 'visible';
						
						setTimeout("document.onclick = function() {popUpDivToggle(gsDivName, null, true);}", 5);
					}
					else
					{
						itemPopUpDiv.style.visibility = 'hidden';
					}
				}
				else
				{
					// toggle visibility
					//alert(itemPopUpDiv.style.getPropertyValue('visibility'));
					if((itemPopUpDiv.style.getPropertyValue('visibility') == 'hidden') && !bForceHidden)
					{
						itemPopUpDiv.style.setProperty('visibility', '', null);
						
						setTimeout("document.onclick = function() {popUpDivToggle(gsDivName, null, true);}", 5);
					}
					else
					{
						itemPopUpDiv.style.setProperty('visibility', 'hidden', null);
					}
				}	

				rc = true;
			}
			else
			{
				if(_bDebugModePUW)
				{
					//alert("popUpDivToggle() - document.getElementById(" + gsDivName + ") failed --- Cannot process.");
				}
			}
		}
		else
		{
			if(_bDebugModePUW)
			{
				alert("popUpDivToggle() - Required passed in sDivName is invalid or missing --- Cannot process.");
			}
		}
	}
	catch(e)
	{
		if(_bDebugModePUW)
		{
			if(e.description.length != 0) { alert("popUpDivToggle() : Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
	
	return(rc);
}

function _timedFocus(spWin, msvar)
{
	var t1 = setTimeout(spWin + ".focus()", msvar);
}

function _timedClose(spWin, msvar)
{
	var t1 = setTimeout(spWin + ".close()", msvar);
}

function _startTimedCount(msvar)
{
	document.getElementById('counter').value = _counter;
	
	_counter = _counter + 1;
	
	// recursive call to self
	_timerObj = setTimeout("_startTimedCount()", msvar);
}

function _stopTimedCount()
{
	clearTimeout(_timerObj);
	_counter = 0;
}

function setOffsetsPUW(itemPUWOffsetElem, itemPUWDiv)
{
	try
	{
		if(itemPUWOffsetElem != null && itemPUWDiv != null)
		{
			var end = calculateOffsetLeftPUW(itemPUWOffsetElem);
			var top = calculateOffsetTopPUW(itemPUWOffsetElem);
    
			//itemPUWDiv.style.border = "none"; //"black 1px solid";
			itemPUWDiv.style.left = end + itemPUWOffsetElem.offsetWidth + 15;
			
			var pw = itemPUWOffsetElem.style.left;
			pw = Number(pw.replace(/px/gi, ""));
			var pl = Number(itemPUWDiv.style.width.replace(/px/gi, ""));
			pw = pw + pl
			
			var dw = document.body.clientWidth; //.offsetWidth .scrollWidth;
			
			if(pw > dw)
			{
				itemPUWDiv.style.left = (end - (pl + 10)) + "px";
			}
			
			itemPUWDiv.style.top = top + "px";
		}
		else
		{
			if(_bDebugModePUW)
			{
				alert("PopUpWindow.js::setOffsetsPUW() - Required passed in itemPUWOffsetElem and/or itemPUWDiv invalid or missing --- Cannot process.");
			}
		}
	}
	catch(e)
	{
		if(_bDebugModePUW)
		{
			if(e.description.length != 0) { alert("setOffsetsPUW() : Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
}
        
function calculateOffsetTopPUW(field)
{
	return calculateOffsetPUW(field, "offsetTop") + _nPUWImageOffsetTop;
}

function calculateOffsetLeftPUW(field)
{
	return calculateOffsetPUW(field, "offsetLeft") + _nPUWImageOffsetLeft;
}

function calculateOffsetPUW(field, attr)
{
	var offset = 0;
  
	try
	{
		while(field)
		{
			offset += field[attr]; 
			field = field.offsetParent;
		}
	}
	catch(e)
	{
		if(_bDebugModePUW)
		{
			if(e.description.length != 0) { alert("calculateOffsetPUW() : Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
  
	return offset;
}

//<script type="text/javascript" src="http://www.foo.com/myscript.js#obj1/ABC/obj2/XYZ"></script>
//var myParams = getScriptUrlParams();
//alert(myParams["obj1"]);
//alert(myParams["obj2"]);
function getScriptUrlParams()
{
	var urlParams = [];
	
	try
	{
		var scriptTags = document.getElementsByTagName("script");
	
		// This code is assumed to be in src file so the "src" attribute
		// is guaranteed to be present... no error-checking is needed
		var urlFrags    = scriptTags[scriptTags.length-1].src.split("#");
		var urlParamRaw = [];
	
		if(urlFrags.length > 1)
		{
		    urlParamRaw = urlFrags[1].split("/");
		    
		    if(urlParamRaw.length >= 2)
		    {
		       	for(var param=0; param<urlParamRaw.length; param+=2)
		       	{
		       		urlParams[urlParamRaw[param]] = (urlParamRaw.length >= param + 1 ? unescape(urlParamRaw[param+1]) : null);
		       	}
   		    }
   		}
	}
	catch(e)
	{
		if(e.description.length != 0) { alert("getScriptUrlParams() : Exception caught [" + e.description + "]"); }
	}
	finally
	{
	}
   	 
	return(urlParams);
}

