/*

	/global/nav.js
	
*/

function Global_load() {
	Menu_init();
	TextSize_init();
	QuickLinks_init();
}

function FindPhysician_init() {
	var btnFind = document.getElementById("btnFind");
	if (btnFind) btnFind.onclick = FindPhysician_submit;

	var txtFirst = document.getElementById("First");
	if (txtFirst) txtFirst.onkeydown = FindPhysician_keydown;

	var txtLast = document.getElementById("Last");
	if (txtLast) txtLast.onkeydown = FindPhysician_keydown;
}

function FindPhysician_submit() {
	var strFirst = GetValue("First");
	var strLast = GetValue("Last");
	var intSpecialtyId = GetOption("Specialty");
	var strSpecialty = GetOptionText("Specialty");
	var strGender = GetOption("Gender");

	if (strFirst.length > 0 || strLast.length > 0 || strSpecialty.length > 0) {
		strHref = "";
		strHref += AddVariable(strHref, "first", strFirst);
		strHref += AddVariable(strHref, "last", strLast);
		strHref += AddVariable(strHref, "sid", intSpecialtyId);
		strHref += AddVariable(strHref, "specialty", GetAlias(strSpecialty));
		strHref += AddVariable(strHref, "gender", strGender);
		strHref += "&search=true";
		location.href = strHref;
		//location.href = "?first=" + escape(strFirst) + "&last=" + escape(strLast) + "&sid=" + intSpecialtyId + "&specialty=" + GetAlias(strSpecialty) + "&gender=" + escape(strGender) + "&search=true";
	} else {
		alert("Please specify part of a name or a specialty to see a list of physicians.");
	}
}

function AddVariable(strHref, strName, strValue) {
	if (strValue.length > 0) {
		return (strHref.length > 0) ? "&" + strName + "=" + escape(strValue) : "?" + strName + "=" + escape(strValue)
	} else {
		return "";
	}
}

function GetValue(strTextId) {
	return document.getElementById(strTextId).value;
}

function GetOption(strSelectId) {
	var lstSelect = document.getElementById(strSelectId);
	return lstSelect.options[lstSelect.selectedIndex].value;
}

function GetOptionText(strSelectId) {
	var lstSelect = document.getElementById(strSelectId);
	return lstSelect.options[lstSelect.selectedIndex].text;
}

function FindPhysician_keydown(e) {
	var strKeyCode = (window.event) ? window.event.keyCode : e.which;
	if (strKeyCode == keyEnter) {
		FindPhysician_submit();
	}
}

function GetAlias(strTitle) {
	return strTitle.replace(/[ ]*&[ ]*/g, "-and-").replace(/[\s\.\/]+/g, "-").replace(/[^\w\-]+/g, "").replace(/[\-]+/g, "-").replace(/-$/g, "").toLowerCase();
}

/* Home Banner */

var m_intBannerSpeed = 11; // seconds
var m_objRotate = [];
var m_objFade = [];
var m_intBannerPace = 25;
var m_intBannerMove;
var m_intBannerTimer;
var m_intFadePercent = 100;
var m_intFadeSpeed = 10;
var m_intFadeRate = 1;
var m_objReadMore;

function Banner_load() {
	var col = document.getElementById("BannerList").childNodes;
	if (col && col.length > 1) {
		m_intBannerTimer = window.setInterval(Banner_change, m_intBannerSpeed * 1000);
		m_objRotate[0] = document.getElementById("Rotate0");
		m_objRotate[1] = document.getElementById("Rotate1");
		m_objFade[0] = document.getElementById("Fade0");
		m_objFade[1] = document.getElementById("Fade1");
		m_objReadMore = document.getElementById("ReadMore");
		document.getElementById("Next").onclick = NextBanner;
		//m_objTempBanner = document.getElementById("Rotating");
		//m_objBanner = m_objTempBanner.childNodes[0];
		for (var i = 0; i < col.length; i++) {
			m_arrBanner[i] = [];
			m_arrBanner[i][0] = new Image();
			m_arrBanner[i][0].src = col[i].childNodes[0].src;
			m_arrBanner[i][1] = new Image();
			m_arrBanner[i][1].src = col[i].childNodes[1].src;
			m_arrBanner[i][2] = col[i].childNodes[2].innerHTML;
		}
		m_objReadMore.style.display = (m_arrBanner[m_intBanner][2].length == 0) ? "none" : "inline-block";
	}
}

function Banner_change() {
	m_intBanner++;
	if (m_intBanner >= m_arrBanner.length) {
		m_intBanner = 0;
		// window.clearInterval(m_intBannerTimer);
	}

	//m_intBannerMove = m_dimBanner[0];
	//m_objTempBanner.style.backgroundImage = m_objBanner.style.backgroundImage;
	//m_objBanner.style.backgroundPosition = m_intBannerMove + "px center";
	//m_objBanner.style.backgroundImage = "url(" + m_arrBanner[m_intBanner].src + ")";
	
	m_objRotate[0].style.backgroundImage = "url(" + m_arrBanner[m_intBanner][0].src + ")";
	m_objRotate[1].style.backgroundImage = "url(" + m_arrBanner[m_intBanner][1].src + ")";

	//Banner_slide();
	Banner_fade();
}

/*function Banner_slide() {
	if (m_intBannerMove - m_intBannerPace > 0) {
		m_intBannerMove -= m_intBannerPace;
		m_objBanner.style.backgroundPosition = m_intBannerMove + "px center";
		window.setTimeout("Banner_slide()", m_intBannerSpeed);
	} else {
		m_objBanner.style.backgroundPosition = "0px center";
	}
}*/

function Banner_fade() {
	if (m_intFadePercent - m_intFadeRate < 1) {
		m_intFadePercent = 100;
		SetFade();
		m_objFade[0].style.backgroundImage = m_objRotate[0].style.backgroundImage;
		m_objFade[1].style.backgroundImage = m_objRotate[1].style.backgroundImage;
		m_objReadMore.href = m_arrBanner[m_intBanner][2];
		m_objReadMore.style.display = (m_arrBanner[m_intBanner][2].length == 0) ? "none" : "inline-block";
	} else {
		m_intFadePercent -= m_intFadeRate;
		SetFade();
		window.setTimeout("Banner_fade()", m_intFadeSpeed);
	}
}

function SetFade() {
	m_objFade[1].style.opacity = m_intFadePercent / 100;
	m_objFade[1].style.MozOpacity = m_intFadePercent / 100;
	m_objFade[1].style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=" + m_intFadePercent + ")";
}

function NextBanner() {
	window.clearInterval(m_intBannerTimer);
	m_intFadePercent = 0;
	SetFade();
	Banner_change();
	m_intBannerTimer = window.setInterval(Banner_change, m_intBannerSpeed * 1000);
}

function Calendar_load() {
	var lstMonth = document.getElementById("Month");
	if (lstMonth) lstMonth.onchange = Calendar_change;
}

function Calendar_change() {
	var strMonth = GetSelectedMonth();
	var intMonth = strMonth.split("/")[0];
	var intYear = strMonth.split("/")[1];
	var strHref = "?";
	if (intMonth > 0) {
		strHref += "month=" + intMonth + "&year=" + intYear;
	}
	location.href = strHref;
}

function GetSelectedMonth() {
	var lstMonth = document.getElementById("Month");
	return lstMonth.options[lstMonth.selectedIndex].value;
}

/* Menus */

var m_hdlMenuClose;
var m_hdlMenuOpen;
var m_activeMenu;
var m_activeMenuTab;
var m_blnInMenu;
var m_intMenuDelayMs = 500;
var m_intMenuOpenMs = 5;
var m_intMenuCloseMs = 5;
var m_intMenuSpeedMs = 12;
var m_intMenuHeight;
var m_intMenuMinPx = 50;
var m_intMenuTabHeight;
var m_intMenuFrameRight;

function Menu_init() {
	var objMenu = document.getElementById("Menu");
	var framePos = GetElementPosition(document.getElementById("Frame"));
	m_intMenuTabHeight = objMenu.offsetHeight;
	m_intMenuFrameRight = framePos.x + framePos.w;
	var cells = objMenu.rows[0].cells;
	for (i = 0; i < cells.length; i++) {
		if (document.getElementById(cells[i].childNodes[0].getAttribute("menu"))) {
			cells[i].childNodes[0].onmouseover = MenuTab_mouseover;
			cells[i].childNodes[0].setAttribute("default", cells[i].childNodes[0].className);
		}
	}
	window.onresize = Window_resize;
}

function Window_resize() {
	Menu_close();
}

function MenuTab_mouseover() {
	var thisMenu = document.getElementById(this.getAttribute("menu"));
	if (m_hdlMenuClose) {
		window.clearTimeout(m_hdlMenuClose);
		m_hdlMenuClose = null;
	}
	if (m_activeMenu && m_activeMenu != thisMenu) {
		m_activeMenu.style.display = "none";
		m_activeMenu.childNodes[0].style.height = "";
	}
	if (m_activeMenuTab) {
		m_activeMenuTab.className = m_activeMenuTab.getAttribute("default");
	}
	if (m_hdlMenuOpen) {
		window.clearTimeout(m_hdlMenuOpen);
		m_hdlMenuOpen = null;
	}
	m_activeMenuTab = this;
	m_activeMenu = thisMenu;
	if (m_activeMenu) {
		m_blnInMenu = true;
		m_activeMenuTab.className = "tmpLinkOn";
		m_activeMenuTab.onmouseout = Menu_mouseout;
		m_hdlMenuOpen = window.setTimeout("Menu_on()", m_intMenuDelayMs / 2);
	}
}

function Menu_mouseover() {
	if (m_hdlMenuOpen) {
		window.clearTimeout(m_hdlMenuOpen);
		m_hdlMenuOpen = null;
	}
	if (m_hdlMenuClose) {
		window.clearTimeout(m_hdlMenuClose);
		m_hdlMenuClose = null;
		m_hdlMenuOpen = window.setTimeout("Menu_open()", m_intMenuOpenMs);
	}
	m_blnInMenu = true;
}

function Menu_mouseout() {
	m_blnInMenu = false;
	m_hdlMenuClose = window.setTimeout("Menu_off()", m_intMenuDelayMs);
}

function Menu_on(strCaller) {
	if (m_blnInMenu) {
		m_activeMenu.childNodes[0].style.height = "";
		m_activeMenu.style.visibility = "hidden";
		m_activeMenu.style.display = "block";
		m_intMenuHeight = m_activeMenu.offsetHeight;
		m_activeMenu.childNodes[0].style.height = "1px";
		m_activeMenu.onmouseover = Menu_mouseover;
		m_activeMenu.onmouseout = Menu_mouseout;
		Menu_position();
		m_activeMenu.style.visibility = "visible";
		m_hdlMenuOpen = window.setTimeout("Menu_open()", m_intMenuOpenMs);
	}
}

function Menu_open() {
	if (m_activeMenu.childNodes[0].offsetHeight >= (m_intMenuHeight - (m_intMenuSpeedMs * 3))) {
		m_activeMenu.childNodes[0].style.height = "";
		m_hdlMenuOpen = null;
	} else if (m_activeMenu.childNodes[0].offsetHeight < (m_intMenuSpeedMs * 8)) {
		m_activeMenu.childNodes[0].style.height = Math.round(m_activeMenu.childNodes[0].offsetHeight + (m_intMenuSpeedMs)) + "px";
		m_hdlMenuOpen = window.setTimeout("Menu_open()", m_intMenuCloseMs);
	} else if (m_activeMenu.childNodes[0].offsetHeight < (m_intMenuSpeedMs * 25)) {
		m_activeMenu.childNodes[0].style.height = Math.round(m_activeMenu.childNodes[0].offsetHeight + (m_intMenuSpeedMs * 2)) + "px";
		m_hdlMenuOpen = window.setTimeout("Menu_open()", m_intMenuCloseMs / 2);
	} else {
		m_activeMenu.childNodes[0].style.height = Math.round(m_activeMenu.childNodes[0].offsetHeight + (m_intMenuSpeedMs * 3)) + "px";
		m_hdlMenuOpen = window.setTimeout("Menu_open()", m_intMenuCloseMs / 3);
	}
}

function Menu_off(strCaller) {
	if (!m_blnInMenu) {
		if (m_activeMenu) {
			m_hdlMenuClose = window.setTimeout("Menu_close()", m_intMenuCloseMs);
		}
	}
}

function Menu_close(blnForce) {
	if (!m_activeMenu) return false;
	var intOffsetHeight = m_activeMenu.childNodes[0].offsetHeight;
	if (intOffsetHeight < ((m_intMenuSpeedMs / 2) + 1) || blnForce) {
		m_activeMenu.style.display = "none";
		m_activeMenu.childNodes[0].style.height = "";
		m_hdlMenuClose = null;
		//window.setTimeout("MenuTab_default()", m_intMenuDelayMs / 5);
		MenuTab_default();
	} else if (intOffsetHeight < m_intMenuMinPx) {
		m_activeMenu.childNodes[0].style.height = Math.round(intOffsetHeight - (m_intMenuSpeedMs / 2)) + "px";
		if (m_activeMenu.childNodes[0].offsetHeight == intOffsetHeight) {
			Menu_close(true);
			return false;
		}
		m_hdlMenuClose = window.setTimeout("Menu_close()", m_intMenuCloseMs * 3);
	} else {
		m_activeMenu.childNodes[0].style.height = Math.round(intOffsetHeight - m_intMenuSpeedMs) + "px";
		m_hdlMenuClose = window.setTimeout("Menu_close()", m_intMenuCloseMs);
	}
}

function Menu_position() {
	var intMenuOffset = (document.all) ? (document.getElementById("Document").offsetWidth % 2) - 1 : -1;
	var pos = GetElementPosition(m_activeMenuTab);
	m_activeMenu.style.top = (pos.y + m_intMenuTabHeight) + "px";
	if (pos.x + m_activeMenu.offsetWidth > m_intMenuFrameRight) {
		m_activeMenu.style.left = pos.x - ((pos.x + m_activeMenu.offsetWidth) - m_intMenuFrameRight + intMenuOffset) + "px";
	} else {
		m_activeMenu.style.left = (pos.x + intMenuOffset) + "px";
	}
}

function MenuTab_default() {
	if (m_activeMenuTab) {
		m_activeMenuTab.className = m_activeMenuTab.getAttribute("default");
	}
}

function GetElementPosition(me) {
	var obj = me;
	var intX = 0;
	var intY = 0;
	var intWidth = obj.offsetWidth;
	var intHeight = obj.offsetHeight;
	while (obj.offsetParent) {
		intX += obj.offsetLeft;
		intY += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return { x: intX, y: intY, w: intWidth, h: intHeight };
}

/* Text Size */

function TextSize_init() {
	var strSize = GetCookie("TextSize");
	if (strSize) {
		var content1 = document.getElementById("Content1");
		var content2 = document.getElementById("Content2");
		if (content1) content1.className = strSize;
		if (content2) content1.className = strSize;
		document.getElementById("TextSize").className = strSize;
	}
}

function Size(intSize) {
	switch (intSize) {
		case 2:
			SetCookie("TextSize", "tmpMedium");
			break;
		case 3:
			SetCookie("TextSize", "tmpLarge");
			break;
		default:
			SetCookie("TextSize", "tmpSmall");
	}
	TextSize_init();
}

function GetCookie(strCookieName) {
	var objCookie = document.cookie;
	var posStart = objCookie.indexOf(strCookieName);
	if (posStart < 0) return null;
	var posEquals = objCookie.indexOf("=", posStart);
	var posEnd = objCookie.indexOf(";", posStart);
	if (posEnd < posEquals) posEnd = objCookie.length;
	var strCookieValue = unescape(objCookie.substring(posEquals + 1, posEnd));
	return strCookieValue;
}

function SetCookie(strCookieName, strCookieValue) {
	var intYearMs = 365 * 24 * 60 * 60 * 1000;
	var datToday = new Date();
	var datExpiration = new Date(datToday.getTime() + intYearMs);
	datExpiration = datExpiration.toGMTString();
	document.cookie = strCookieName + "=" + escape(strCookieValue) + ";expires=" + datExpiration + ";path=/;";
}

/* Ecards */

function Ecard_init() {
	if (document.frmEmail.Ecard) {
		document.getElementById("btnEcardSubmit").onclick = Ecard_submit;
	}
}

function Ecard_submit() {
	if (Ecard_validate()) {
		// fixes IE6 bug
		window.setTimeout("document.frmEmail.submit();", 0);
	}
}

function SelectEcard(obj) {
	document.getElementById("EcardHeader").style.backgroundImage = "url(/res/gallery/" + obj.value + "/md/" + obj.getAttribute("md") + ")";
}

function Ecard_validate() {
	if (document.frmEmail["Sender Name"].value.length < 1) {
		alert("The sender name is required.");
		document.frmEmail["Sender Name"].focus();
		return false;
	}
	if (document.frmEmail["Patient Name"].value.length < 1) {
		alert("The patient name is required.");
		document.frmEmail["Patient Name"].focus();
		return false;
	}
	if (document.frmEmail["Message"].value.length < 1) {
		alert("A message is required.");
		document.frmEmail["Message"].focus();
		return false;
	}
	if (GetRadioValue("EcardType").length == 0) {
		alert("Select an e-card design.");
		return false;
	}
	return true;
}

function GetRadioValue(strName) {
	var radio = document.frmEmail[strName];
	if (radio) {
		var strTemp = radio.length;
		if (radio.length == undefined) {
			return (radio.checked) ? radio.value : "";
		}
		for (var i = 0; i < radio.length; i++) {
			if (radio[i].checked) return radio[i].value;
		}
	}
	return "";
}

/* Videos */

function WatchVideo(strVideoId) {
	var objVideo = document.getElementById(strVideoId);
	var arrTemp = objVideo.getAttribute("aspect").split(":");
	var intAspect = arrTemp[0]/arrTemp[1];
	objVideo.childNodes[0].innerHTML = '<OBJECT ID="MediaPlayer" WIDTH="400" HEIGHT="' + (400 / intAspect) + '" CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" STANDBY="Loading Windows Media Player components..." TYPE="application/x-oleobject"><PARAM NAME="FileName" VALUE="' + objVideo.getAttribute("video") + '"><PARAM name="autostart" VALUE="true"><PARAM name="ShowControls" VALUE="false"><param name="ShowStatusBar" value="false"><PARAM name="ShowDisplay" VALUE="false"><EMBED TYPE="application/x-mplayer2" SRC="' + objVideo.getAttribute("video") + '" NAME="MediaPlayer" WIDTH="400" HEIGHT="' + (400 / intAspect) + '" ShowControls="0" ShowStatusBar="0" ShowDisplay="0" autostart="1"></EMBED></OBJECT>';
}

/* Quick Links */

function QuickLinks_init() {
	document.getElementById("btnQuickLinks").onclick = QuickLinks_select;
	document.getElementById("lstQuickLinks").onchange = QuickLinks_select;
}

function QuickLinks_select() {
	var lst = document.getElementById("lstQuickLinks");
	if (lst.options[lst.selectedIndex].value.length > 0) {
		location.href = lst.options[lst.selectedIndex].value;
	}
}

