// JavaScript Document

var myMenu;
window.onload = function() {
  	myMenu = new SDMenu("my_menu");
  	myMenu.init();
	loadAdverts();
	activateHoverMe();
}

isIE6 = /msie|MSIE 6/.test(navigator.userAgent);

function flashFix(o){document.write(o)}

function initTextCounter(field, countfield, maxlimit){
  	if (field.value.length > maxlimit) {field.value = field.value.substring(0, maxlimit)} //if too long...trim it!
  	else {countfield.value = maxlimit - field.value.length} //otherwise, update 'characters left' counter
}

function validateEmailForm(){
  	response = []; cancelaction = false
  	if (emailform.fname.value == "") {response[0] = "First Name"}
  	if (emailform.lname.value == "") {response[1] = "Last Name"}
  	if (emailform.senders_email.value == "") {response[2] = "E-mail"}
  	str = "To continue please complete the following fields:\n\n"
  	for (var i=0; i<response.length; i++) {if (response[i]!=null) {str+="*"+response[i]+"\n"; cancelaction = true}}
  	if (cancelaction==true) {alert(str); event.returnValue = false; return}
}

function loadAdverts() {
	var div = document.getElementById('adverts');
	var adverts = [], advertStr = '';
	adverts[0] = '<img src="http://www.nhs.uk/img/header/choices-logo.gif" height="67%" width="67%" style="padding-left:10px;" /><p><a href="http://www.nhs.uk/Pages/HomePage.aspx" target="_blank">Click here for information on NHS Healthcare.</a></p>';
	//adverts[1] = '<img src="http://www.pectus.co.uk/imgs/pectuslogo.jpg" height="67%" width="67%" /><p><a href="http://www.pectus.co.uk/" target="_blank">Click here for information on treating Pectus conditions.</a></p>';
	for (var i = 0, len = adverts.length; i < len; i++) advertStr += adverts[i];
	div.innerHTML = advertStr;
}

function expandPub() {
	document.getElementById('keypub').style.display = 'block';
}

function activateHoverMe() {
	var keywords = ['pancreatic','biliary','oesophagus','ulcer','inflammation','polyp','ileum','cancer','bile ducts','pancreas','gallstones']
	var meaning = ['The pancreas is a gland near the stomach which secretes a fluid to help with food digestion and also the hormone insulin which helps the body process glucose (or sugars).', 'The biliary system involves the bile ducts, which are a number of long tube-like structures in the body that carry bile, a digestive juice secreted by the liver and stored in the gallbladder which aids in the digestion of fats.', 'A muscular tube through which food passes from the pharynx, which connects the oesophagus to the mouth and nose, to the stomach.', 'An ucler is an open sore which can occur on the skin or internally, and is often caused by an initial abrasion of some sort, but generally maintained by an inflammation and/or an infection.', 'Quite simply, a response of body tissues to injury or irritation; characterized by pain and swelling and redness and heat.', 'A polyp is an abnormal growth of tissue projecting from a mucous membrane, the lining of any body cavity that may come into contact with the exterior of the body.', 'The small bowel. It joins the colon, or large bowelm and its major functions are digestion and absorption of nutrients.', 'A cancer is a malignant growth, or tumour, and is caused by abnormal and uncontrolled cell division; it may spread to other parts of the body through the lymphatic system or the blood stream.', 'A number of long tube-like structures in the body that carry bile, a digestive juice secreted by the liver and stored in the gallbladder which aids in the digestion of fats.', 'The pancreas is a gland near the stomach which secretes a fluid to help with food digestion and also the hormone insulin which helps the body process glucose (or sugars).', 'A small, hard object, in the shape of a pebble, that sometimes forms in the gallbladder or bile duct; composed of cholesterol, bile pigments and calcium salts.'];
	var a = document.getElementsByTagName('A');
	var rightedge = f_clientWidth() - 530;
	var hovermes = [];
	for (var i = 0, len = a.length; i < len; i++) if (a[i].className == 'hoverme_wrapper') hovermes.push(a[i])
	for (i = 0, len = hovermes.length; i < len; i++) {
		var keyword = hovermes[i].innerHTML;
		for (var n = 0, nlen = keywords.length; n < nlen; n++) {
			if (keywords[n] == keyword.toLowerCase() || keywords[n]+'s' == keyword.toLowerCase()) {var k = meaning[n]; break}
			else k = 0
		}
		
		var hoverleft = hovermes[i].offsetLeft;
		var hovertop = hovermes[i].offsetTop+20;
		if (isIE6) {hoverleft -= 20; hovertop -=50};
		if (k!=0) {			
			hovermes[i].left = (hoverleft > rightedge) ? rightedge : hoverleft;
			hovermes[i].top = hovertop;
			hovermes[i].str = k;
			hovermes[i].onmouseover = function() {HoverMeClassHover(this.left, this.top, this.str)};
			hovermes[i].onmouseout = function() {HoverMeClassWrapper()};
		}
	}
}


function HoverMeClassHover(left, top, str) {
	var ele = document.getElementById('hoverme_box');
	ele.style.left = left;
	ele.style.top = top;
	ele.innerHTML = str;
	ele.className = 'show';
}

function HoverMeClassWrapper() {
	document.getElementById('hoverme_box').className = 'hide';
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
