var addEvent = function(obj,evt,fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evt,fn,false);
	} else if (obj.attachEvent) {
		obj.attachEvent('on'+evt,fn);
	}
};



// Open outgoing links in a new window
addEvent(window, 'load', function () {
	var anchors = document.getElementsByTagName('a');
	var areas = document.getElementsByTagName('area');
	var programRef = 'gnher';
	
	for (var i = 0; i < areas.length; i += 1) {
		if (!areas[i].href.match(programRef)) {
			areas[i].onclick = function () {
				window.open(this.href);
				return false;
			};
		}
	}
	
	for (var i = 0; i < anchors.length; i += 1) {
        var href = anchors[i].href;
		if ((!href.match(programRef) && !href.match('welcome')) && !href.match('javascript') || href.match('pdf')) {
			anchors[i].onclick = function () {
				window.open(this.href);
				return false;
			};
		}
	}
});


// Print page
addEvent(window, 'load', function () {
	var printBtn = document.getElementById('print-page');
	var printTerms = document.getElementById('print-terms');
	
	var printPage = function () {
		var newWin = window.open('', 'Print', 'width=850,height=850,resizable,scrollbars');
		newWin.document.write('<title>Print Window</title>');
		newWin.document.write('<link rel="stylesheet" type="text/css" href="css/print.css">');

		var script = '<script type="text/javascript">' +
					 'window.onload = function () {' +
						'var disable = function (obj) {' +
							'for (key in obj) {' +
								'var nodeList = document.getElementsByTagName(key);' +
								'for (var i = 0; i < nodeList.length; i += 1 ) {' +
									'obj[key](nodeList[i]);' +
								'}' +
							'}' +
						'};' +
						'disable({' +
							'"a" : function (node) { node.removeAttribute("href"); },' +
							'"area" : function (node) { node.removeAttribute("href"); },' +
							'"form" : function (node) { node.onsubmit = function () { return false; }; },' +
							'"input" : function (node) { node.disabled = true; },' +
							'"select" : function (node) { node.disabled = true; }' +
						'});' +
					 '};' +
					 '</script>';

		// Disable links by removing the href attributes from anchor and area tags
		newWin.document.write(script);

		// Add the contents to the document
		newWin.document.write(document.getElementById('wrap').innerHTML);

		newWin.document.close();
		newWin.focus();
		setTimeout(function() {newWin.print()}, 500);
	};
	
	if (printBtn && !printTerms) {
		printBtn.onclick = function () {
			printPage(true);
			return false;
		};	
	} else if (printTerms) {
		var printURL = 'gnher_web_priv_CAprint.htm';
		printBtn.onclick = printTerms.onclick = function () {
			var printWindow = window.open(printURL, 'Print', 'width=850,height=850,resizable,scrollbars');
			return false;
		};
	}
});


// Text size
addEvent(window, 'load', function () {
	var textBtn = document.getElementById('type-size');
	var sizeMenu = document.getElementById('size-menu');
	var utilityMenu = document.getElementById('utilities');
	var sizeLinks;
	var textInc = 0;
	var i;
	var timer;
	
	if (sizeMenu) {
		sizeLinks = sizeMenu.getElementsByTagName('a');
		
		textBtn.onmouseover = function () {
			clearTimeout(timer);
			sizeMenu.style.display = 'block';
		};

		textBtn.onmouseout = function () {
			timer = setTimeout(function () {
				sizeMenu.style.display = 'none';
			}, 300);
		};

		sizeMenu.onmouseout = function () {
			sizeMenu.style.display = 'none';
		}

		for (i = 0; i < sizeLinks.length; i += 1) {
			(function () {
				var t = textInc;
				sizeLinks[i].onclick = function () {
					document.body.style.fontSize = 1 + '.' + t + 'em';
					sizeMenu.className = t === 0 ? 'level-1' : 'level-' + t;
					return false;
				};
			}());

			textInc += 2;
		}
	}
});


// Email page
addEvent(window, 'load', function () {
	var email = document.getElementById('email-page');
	
	if (email) {
		email.onclick = function () {
			window.open('gnher_web_emailForm.jsp', 'email', 'width=520,height=530,resizable');
			return false;
		};	
	}
});


// Tell a Friend email preview
addEvent(window, 'load', function () {
	var previewBtn = document.getElementById('preview-email');
	
	if (previewBtn) {
		previewBtn.onclick = function () {
			window.open(this.href, 'Preview', 'width=800,height=800,resizable,scrollbars');
			return false;
		};
	}
});

addEvent(window, 'load', function () {
	var closeBtn = document.getElementById('close');
	
	if (closeBtn) {
		closeBtn.onclick = function () {
			window.close();
			return false;
		} 
	}
});



// Glossary terms
function getMouseCoords(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	return [posx, posy];
}

addEvent(window, 'load', function () {
	var anchors = document.getElementsByTagName('a');
	var timeout;
	var terms = {
		'adjuvant-care': '<h4>adjuvant care</h4>is defined as additional treatment given after the main treatment plan to decrease the chance of recurrence. Adjuvant therapy for breast cancer can include chemotherapy, hormone therapy, radiation therapy or biological therapy',
		'biologic-therapy': '<h4>biologic therapy</h4>treatment that uses substances derived from living cells',
		'biopsy': '<h4>biopsy</h4>the removal and examination of a sample of tissue for study purposes; this is the only method that can determine if the tissue contains cancer cells',
		'cell': '<h4>cell</h4>the fundamental, structural and functional unit of living organisms',
		'chemotherapy': '<h4>chemotherapy</h4>the treatment of cancer using chemicals or drugs that are destructive to cells and tissues',
		'DNA': '<h4>DNA (deoxyribonucleic acid)</h4>the building blocks of the genetic code, located in the cell nucleus',
		'early-stage-breast-cancer': '<h4>early-stage breast cancer</h4>breast cancer that has not spread beyond the breast or the axillary lymph nodes (under the arms), usually stages 0, I and some stage III',
		'first-line': '<h4>first-line</h4>first therapy for the condition being treated',
		'gene': '<h4>gene</h4>portion of DNA that directs the production of a specific protein',
		'HER2-gene': '<h4><i>HER2</i> gene</h4>a gene that directs the cell to produce HER2 growth factor receptors on the cell\'s surface, which can control the growth and division of the cell',
		'HER2-negative': '<h4>HER2-negative</h4>indicates that a biopsy revealed a normal level of the <i>HER2</i> gene or protein',
		'HER2-positive': '<h4>HER2-positive</h4>indicates that a biopsy revealed abnormally high levels of the <i>HER2</i> gene or protein',
		'HER2-protein': '<h4>HER2 protein (receptor)</h4>also called the HER2 receptor; a cell-surface protein that helps control normal cell growth, cell division and cell survival',
		'HER2-protein-overexpression': '<h4>HER2 protein overexpression</h4>the excess production of HER2 receptors that results from a change in the <i>HER2</i> gene in cancer cells; thought to cause cancer cells to grow and divide more quickly',
		'HER2-receptor': '<h4>HER2 receptor</h4>Human Epidermal growth factor Receptor 2, one of the many proteins on a cell\'s surface that signals the cell to divide and helps control normal cell growth, cell division, and cell survival',
		'hormone-therapy': '<h4>hormone therapy</h4>administering hormones, such as anti-estrogen, to help block hormone uptake in tumors',
		'lymph-node': '<h4>lymph node</h4>small bean-shaped collections of immune system tissue such as lymphocytes; located along lymphatic vessels',
		'metastatic-breast-cancer': '<h4>metastatic breast cancer</h4>breast cancer that has spread to other sites in the body; also referred to as invasive or infiltrating',
		'oncologist': '<h4>oncologist</h4>a doctor who treats patients who have cancer',
		'pathologist': '<h4>pathologist</h4>a doctor who studies and examines tissue samples for signs of cancer or other abnormalities',
		'pathology-report': '<h4>pathology report</h4>a report that describes tissue removed from the body to help understand the nature of a disease',
		'radiation-therapy': '<h4>radiation therapy</h4>the use of high-energy rays or substance particles to kill or shrink cancer cells',
		'recurrence': '<h4>recurrence</h4>cancer that has returned after treatment',
		'stage': '<h4>stage</h4>a method of describing the size and location of a cancer based upon characteristics of the tumor, the lymph nodes and whether it has spread to other organs',
		'surgery': '<h4>surgery</h4>also known as an operation, this is a procedure done to remove or repair a part of the body or to see if disease is present',
		'targeted-therapy': '<h4>targeted therapy</h4>a type of cancer treatment that targets only certain types of cell activities',
		'tumor': '<h4>tumor</h4>an abnormal growth of tissue that does not have normal body function'
	};
	
	for (var i = 0; i < anchors.length; i += 1) {
		if (anchors[i].className.match('glossary-term')) {
			anchors[i].onmouseover = function (e) {
				if (timeout) {
					clearTimeout(timeout);
				}
				var mouseCoords = getMouseCoords(e);
				var box = document.createElement('div');
				box.id = 'tool-tip';
				box.innerHTML = '<div>' + terms[this.href.substring(this.href.indexOf('#') + 1)] + '</div>';
				timeout = setTimeout(function () {
					document.body.appendChild(box);
					box.style.top = (mouseCoords[1] - 20 - box.offsetHeight) + 'px';
					box.style.left = mouseCoords[0] - 46 + 'px';
				}, 300);
			};
			
			anchors[i].onmouseout = function () {
				if (timeout) {
					clearTimeout(timeout);
				}
				setTimeout(function () {
					if (document.getElementById('tool-tip')) {
						document.body.removeChild(document.getElementById('tool-tip'));
					}
				}, 300);
			}
			
			anchors[i].onclick = function () {
				return false;
			}
		}
	}
});
