// new popup object
popup = new Object();
popup.links = new Array();
popup.linkPrefix = "popupDef";
popup.defPrefix = "formDef";
popup.offsetX = 6;
popup.offsetY = 0;
popup.defPosX = 0;
popup.defPosX = 0;
popup.displayTime = 0;
popup.hideTime = 200;
popup.displayTimer = '';
popup.hideTimer = '';
popup.currentDef = '';
popup.currentRefArray = new Array();
popup.defaultHeight = 170;
popup.shortHeight = 170;
popup.defaultWidth = 250;
popup.currentHeight = 0;
popup.titleArray = ["","","","","","","Health Factors*","",""]

var _PopupsDefsLoaded = false;
function startPopupDefs()
{	
    //alert('in popup def');
	// Don't initializing popups twice
	//if (_PopupsDefsLoaded==true) return true;

	// Add all of the popup divs to the scrolling container div
	elem = document.getElementById("innerContainer")
	elem.innerHTML = formPopup.DEFS;		
	
	// For each link that has the correct ID prefix, add the appropriate methods and properties
	//alert('doc lenght='+document.links.length);
	for( i = 0; i < document.links.length; i++){	
	    //alert('link id='+document.links[i].id);
		if( document.links[i].id.indexOf(popup.linkPrefix) == 0 ){
				
			var ele = document.links[i];			
			ele.arrpopupList = createPopupDefList(document.links[i].id);		
			ele.onclick = displayPopupDef;			
			//alert('arrpopup=' + ele.arrpopupList)
		}
	}
	
	document.getElementById(popup.defPrefix + "1").style.display = 'none';	 

	// Set flag
	_PopupsDefsLoaded = true;
	
	return true;
}

function createPopupDefList(strpopupID) {
	// popup lists are extracted from the links ID attribute, which takes the form "popup 1,2,3,4"  	
	var arrRefList = strpopupID.split('|');	
	var arrpopupList = arrRefList[1].split(',');	
	return arrpopupList
	 
}

function displayPopupDef(e)
{	
	hideFormDefinition();
	 
	/* used to capture the event in IE or mozilla */
	e = e || window.event;		
	setCurrentRefArray(this.arrpopupList);
	var elemTitle = document.getElementById("popupDefTitle");
	var elemHeader = document.getElementById("popupDefClose");
	var elemControls = document.getElementById("popupDefControls");
	var intDefID = parseInt(this.arrpopupList[0]) - 1;
	/* change the default X, Y, height, width for popupdef8 - favorites popup */
	if (intDefID == 7) {
		popup.offsetX = -500;
		popup.offsetY = -100;
		popup.shortHeight = 250;
		popup.defaultHeight = 250;
		popup.defaultWidth = 400;
		//elemHeader.style.display = 'none';
		elemTitle.style.display = 'none';
		elemControls.style.width = String(popup.defaultWidth) + 'px';
	} else {
		popup.offsetX = 6;
		popup.offsetY = 0;
		popup.shortHeight = 170;
		popup.defaultHeight = 170;
		popup.defaultWidth = 250;
		//elemHeader.style.display = 'none';
		elemTitle.style.display = 'none';
		elemControls.style.width = String(popup.defaultWidth) + 'px';
	}
	setCurrentPos(e.clientX, e.clientY);		
	popup.displayTimer = setTimeout('showFormDefinition()', popup.displayTime);	
	
	// not needed for this project
	elemTitle.innerHTML = popup.titleArray[intDefID];
	
	return true;
}

function hidePopupDef(e)
{
	
	/* used to capture the event in IE or mozilla */
	e = e || window.event;
	popup.hideTimer = setTimeout('hideFormDefinition()', popup.displayTime);
	return true;
}

function setCurrentRefArray(arrpopups) 
{	
	// Store the array containing the links popups	
	popup.currentRefArray = arrpopups
	return true;
}

function setCurrentPos(x, y)
{
	var scrollX = setScrollX();
	var scrollY = setScrollY();
	popup.defPosX = x + popup.offsetX + scrollX;
	popup.defPosY = y + popup.offsetY + scrollY;
	return true;
}

function setScrollX()
{
	x = '';
	if( window.pageXOffset ){
		x = window.pageXOffset
	}else if( document.documentElement.scrollLeft ){
		x = document.documentElement.scrollLeft
	}else if( document.body.scrollLeft ){
		x = document.body.scrollLeft
	}else{
		x = 0;
	}
	return x;
}

function setScrollY()
{
	y = '';
	if( window.pageYOffset ){
		y = window.pageYOffset
	}else if( document.documentElement.scrollTop ){
		y = document.documentElement.scrollTop
	}else if( document.body.scrollTop ){
		y = document.body.scrollTop
	}else{
		y = 0;
	}
	return y;
}


function showFormDefinition()
{	
	if( document.getElementById){	
		
		// Get the popup container
		var eleContainer = document.getElementById("popupDefContainer");		
		
		// Get the IFRAME container used to cover IE form elements (sheesh!)
		var eleIFrame = document.getElementById("shield1");
		
		// Resize height of popup container if only 1 element exists, else use default height	
		//alert('if(popup.currentRefArray.length < 2:' && popup.currentRefArray.length)
		if(popup.currentRefArray.length < 2) {
			popup.currentHeight = popup.shortHeight;
		} else {
			popup.currentHeight = popup.defaultHeight;			
		}
		
		eleContainer.style.height = String(popup.currentHeight) + 'px';
		eleContainer.style.width = String(popup.defaultWidth) + 'px';
		
		// This child element must be dynamically resized for IE
		var eleContainerChild = document.getElementById("innerContainer");
		eleContainerChild.style.height = String(popup.currentHeight - 17) + 'px';
		eleContainerChild.style.width = String(popup.defaultWidth - 5) + 'px';
		
		
		// Display each of the popups divs for this link
		for(i=0; i<popup.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(popup.defPrefix + popup.currentRefArray[i]);
			//alert(popup.defPrefix + popup.currentRefArray[i] + ".style.display = " + eleDef.style.display);
			eleDef.style.display = 'block';
		}
		
		
		// Check if popup div will not be contained within the browser window and make adjustments
		
		// Get window width and height
		if (window.innerWidth != undefined) {
			// For Mozilla
			var win_w = window.innerWidth;
			var win_h = window.innerHeight;
		} else if (document.body.clientWidth != undefined) {
			// For IE  
			var win_w = (document.compatMode=="CSS1Compat")?document.documentElement.clientWidth : document.body.clientWidth; 		
			var win_h = (document.compatMode=="CSS1Compat")?document.documentElement.clientHeight : document.body.clientHeight;
		}
		
		// get vertical scroll position
		var scrollTop = document.body.scrollTop;
		if (scrollTop == 0)
		{
			if (window.pageYOffset)
				scrollTop = window.pageYOffset;
			else
				scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		
		// check if div will be beyond window width
		var refRight = popup.defPosX + popup.defaultWidth + 5;		
		if (refRight > win_w) {			
			popup.defPosX = popup.defPosX - popup.defaultWidth - 20;
		}
		
		// check if div will be beyond window height			
		var refBot = (popup.defPosY - scrollTop) + popup.currentHeight + 5;			
		var strUserAgent = navigator.userAgent;			
		if (strUserAgent.indexOf("Safari") == -1) {
			// If userAgent is NOT Safari (must be IE, or FF)
			var refBot = (popup.defPosY - scrollTop) + popup.currentHeight + 5;
			if (refBot > win_h) {
				popup.defPosY = popup.defPosY - popup.currentHeight  - 5;
			}				
		} else {
			// If userAgent is Safari, make some adjustments			
			win_h += scrollTop;
			if (refBot > win_h) {
				popup.defPosY = popup.defPosY - popup.currentHeight - scrollTop - 5;
			} else {
				popup.defPosY = popup.defPosY - scrollTop;
			}		
		}

		var OffsetX = 0;
		var OffsetY = 0;
		
		// Position and show the definition container
		eleContainer.style.left = (popup.defPosX + OffsetX) +"px";
		eleContainer.style.top = (popup.defPosY + OffsetY) +"px";		
		eleContainer.style.display = 'block';
		
		// Position and show the IFRAME container
		eleIFrame.style.left = (popup.defPosX + OffsetX) + "px";
		eleIFrame.style.top = (popup.defPosY + OffsetY) + "px";		
		eleIFrame.style.display = 'block';
		
	}
	return true;
}

function hideFormDefinition()
{
	 
	if(document.getElementById){
	
		defElem = document.getElementById("innerContainer")
		scrollUp(defElem)
	
		// Hide the popup container
		elem = document.getElementById("popupDefContainer")		
		elem.style.display = 'none';		
		
		// Hide each of the currently shown popup def divs  
		for(i=0; i<popup.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(popup.defPrefix + popup.currentRefArray[i]);			 
			eleDef.style.display = 'none';
		}	
		
		// Hide the Iframe
		var eleIFrame = document.getElementById("shield1");		
		eleIFrame.style.display = 'none';
	}
	
	// Clear the array so that no old items will show up next time
	popup.currentRefArray = new Array();
		
	return true;
}

function scrollUp(elem) 
{
    elem.scrollTop = 0;
}


/**
  This is our definitions object which will hold the HTML for the popup divs
  the rest of this javascript is generated in "controls/footer_js.ascx"
*/
formPopup = new Object();
formPopup.DEFS = '';

formPopup.DEFS += "<div id=\"formDef1\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		Joining CRESTOR.com gives you full access to all the features of this Web site. Choose your favorite pages, save delicious recipes, and more. Your selections will be saved for future visits to CRESTOR.com. Your e-mail address will be your username when you log back in.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef2\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div style=\"font-size: 1.5em;\" class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible. Please call <span class=\"nobr\">800-CRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef3\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible to sign up for the CRESTOR free trial offer. For more information, call <span class=\"nobr\">800-CRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef4\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible to sign up for the CRESTOR patient support program. For more information, call <span class=\"nobr\">800-CRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef5\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you may still be eligible to sign up for other off-line patient support resources. For more information, call <span class=\"nobr\">800-CRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef6\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible for the CRESTOR rebate. For more information, call <span class=\"nobr\">800-CRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef7\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		High blood pressure<br>";
formPopup.DEFS += "		Diabetes<br>";
formPopup.DEFS += "		Age<br>";
formPopup.DEFS += "		Family history<br>";
formPopup.DEFS += "		Smoking<br><br>";
formPopup.DEFS += "		<div class='smallText'>*This is not a complete list. Talk to your doctor about other health factors that make managing cholesterol more important.</div>";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef8\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		<img src=\"/c/images/favorites_popup.gif\" height=\"47\" width=\"237\" border=\"1\" alt=\"\"><br /><br />";
formPopup.DEFS += "		When you find a page that's ";
formPopup.DEFS += "		especially helpful, <b>click the ";
formPopup.DEFS += "		\"Add to Favorites\" box at the top of the page</b>. ";
formPopup.DEFS += "		Your favorite will then appear within \"Your Favorites.\"<br /><br /><a href=\"javascript:void(0);\" onclick=\"hidePopupDef();\"><img src=\"/c/images/but_form_close.gif\" height=\"25\" width=\"70\" border=\"0\" alt=\"Close\"></a>";
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef9\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible to sign up for other off-line segments of the Getting It Right program. To receive program mailings, a free 15-day trial certificate, and \"Check the Facts\" starter kit, call <span class=\"nobr\">800-CRESTOR</span>."
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef10\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		TEST"
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

