// CREATE AIS NAMESPACE OBJECT //
var AIS; if (!AIS) AIS = {};
if (!AIS.menu) AIS.menu = {};

function $(el) {
	if (typeof el == "string") return document.getElementById(el);
	return el;
}

// DEFINE CORE AIS FUNCTIONS //
AIS.addToWindowOnload = function(newFunction) {
// Unobtrusive Javascript avoids adding onload to <body>
// for compatibility with other js
// but using window.onload in an external .js file could easily lead to 
// window.onload being used several times, each overwriting the last

	var previousOnload = window.onload;
	if (typeof previousOnload != 'function') {
			window.onload = newFunction;
	} else {
		window.onload = function() {
			previousOnload();
			newFunction();	
		}
	}
}

// DEFINE ONE LEVEL TREE MENU FUNCTIONS //
AIS.menu.init = function() {
	if (document.getElementById) {
	// INIT MENU
		var nav = document.getElementById(AIS.menu.id);	
		var ul_array = nav.getElementsByTagName('ul'); // any ul inside nav are submenus
		var cookies = document.cookie;		
		if (cookies) {
			// cookie exists: so when looping thru submenus make each one open or closed to match AIS.menu.array
			// so fill AIS.menu.array from cookie			
			AIS.menu.loadCookie();			
		} else {
			// cookie does not exist:
			// so make an empty array and save it in cookie
			
			
			AIS.menu.array = new AIS.menu.EmptyArray(ul_array.length);
			AIS.menu.saveCookie();
		}	
		// loop thru all submenus
		for (var i=0; i< ul_array.length; i++) {
			var i_ul_el = ul_array[i];
			var i_li = i_ul_el.parentNode; // will be parent LI
			i_li.pnum = i;
			var i_a_first = i_li.getElementsByTagName('a')[0];
			
			//i_li.style.cursor='n-resize';
			
			if (AIS.menu.array[i]==1) {
				i_a_first.className="open";
				i_ul_el.style.display = 'block';
				AIS.menu.current_open = i;
			} else {
				i_a_first.className="closed";
				i_ul_el.style.display = 'none';
			}
			i_a_first.onclick=function(){AIS.menu.setDisplay(this, true);}
			i_a_first.onmouseover=function() {
				this.className+=" hover";
			}
			i_a_first.onmouseout=function() {
				this.className=this.className.replace(new RegExp(" hover\\b"), "");
			}
		}
	
	// INIT flashScriptsLinkBox
	// if this page contains flashFooter then attach onclick to button
	// x>0 ? x*y : -x*y; // test ? doThis : elseDoThis ;
		var flashScriptsLinkBox = document.getElementById('flashScriptsLinkBox');	
		if (flashScriptsLinkBox) {
		//alert('found flashScriptsLinkBox');
			AIS.flashScripts = document.getElementById('flashScripts');	
			
			//alert(flashScriptsLinkBox +' '+ typeof flashScriptsLinkBox );
			AIS.showScript = document.getElementById('showScript');
			AIS.hideScript = document.getElementById('hideScript');
			
			//var flashScriptsLinkBoxA = flashScriptsLinkBox.getElementsByTagName('a')[0];
			//flashScriptsLinkBoxA.onclick=function() {
			//	if (AIS.flashScripts.style.display == 'block') {
			//		AIS.flashScripts.style.display = 'none';	
			//	} else {
			//		AIS.flashScripts.style.display = 'block';
			//	}
			//}
			
			// IE7 will not see pseudo classes if href attribute is absent, so this couple of functions fix that
			//flashScriptsLinkBoxA.onmouseover=function()	{this.style.textDecoration='underline';}
			//flashScriptsLinkBoxA.onmouseout=function()	{this.style.textDecoration='none';}
		
		
			var fSLinkBoxArray = flashScriptsLinkBox.getElementsByTagName('a');
			for (var f=0; f< fSLinkBoxArray.length; f++) {
				f_a_first = fSLinkBoxArray[f];
				f_a_first.onclick=function() {
					if (AIS.flashScripts.style.display == 'block') {
						AIS.flashScripts.style.display = 'none';
						AIS.showScript.style.display = 'inline';
						AIS.hideScript.style.display = 'none';	
					} else {
						AIS.flashScripts.style.display = 'block';
						AIS.showScript.style.display = 'none';
						AIS.hideScript.style.display = 'inline';
					}
				}
				// IE7 will not see pseudo classes if href attribute is absent, so this couple of functions fix that
				f_a_first.onmouseover=	function()	{this.style.textDecoration='underline';}
				f_a_first.onmouseout=	function()	{this.style.textDecoration='none';}
			}
		}
	}
	
//AIS.addToWindowOnload(prepareTextBoxForMultiLineTestOnKeyUp("txtYourComments", 500));
	
}

AIS.menu.setDisplay =function(a_el, state) {
	// a_el is an <a>	
	var li = a_el.parentNode; // == li
	var ul = li.getElementsByTagName('ul')[0]; // ul is a sibling of a_el
	var isalreadyvisible = false;
	if (ul.style.display=="block") { isalreadyvisible = true }
	
	if (isalreadyvisible) {
		ul.style.display = 'none';
		a_el.className='closed';		
		AIS.menu.array[li.pnum] = 0;
	} else {
		ul.style.display = 'block';
		a_el.className='open';		
		AIS.menu.array[li.pnum] = 1;		
		
		// was not open before, so another LI may have been and thus other LI must be closed if in mono mode
		if (AIS.menu.mode=="mono") {
		// beware that the last opened LI could be this one, which has just been opened, so test to avoid closing it
			if (AIS.menu.current_open != li.pnum) {
				AIS.menu.array[AIS.menu.current_open] = 0;
				
				var nav = document.getElementById(AIS.menu.id);	
				var ul_array = nav.getElementsByTagName('ul'); // any ul inside nav are submenus
				var open_ul = ul_array[AIS.menu.current_open];
				
				var open_li = open_ul.parentNode; // will be parent LI of open UL
				var open_a_first = open_li.getElementsByTagName('a')[0];
				
				open_a_first.className="closed";
				open_ul.style.display = 'none';
			}
		}
		AIS.menu.current_open = li.pnum;
	}
	AIS.menu.saveCookie();
}



AIS.menu.openMyHz = function () {
	
	var hz = "hz";
	var hz_i = -1;
	
	var nav = document.getElementById(AIS.menu.id);	
	var ul_array = nav.getElementsByTagName('ul'); // any ul inside nav are submenus
	
	// search array for hz
	for (var i=0; i< ul_array.length; i++) {
	
		i_ul = ul_array[i];
		i_li = i_ul.parentNode;
		i_classes = i_li.className;
		
		if (i_classes) { // then this li has a class attribute to be checked
			if (i_classes == hz) {hz_i = i; break;} // quick test to preempt search
			if (i_classes.search("\\b" + hz + "\\b") != -1) {hz_i = i; break;}
		}
	}
	
	
	// test to see if hz was found
	if (hz_i != -1) {
	
		// if ! AIS.menu.mode == "mono" close all others
		if (AIS.menu.mode == "mono") {
			
			for (var j=0; j<ul_array.length; j++) {
				var j_ul = ul_array[j];
				var j_li = j_ul.parentNode; // will be parent LI
				var j_a = j_li.getElementsByTagName('a')[0];
				
				j_ul.style.display = 'none';
				j_a.className='closed';		
				AIS.menu.array[j] = 0;
			}
		}
		
		// open hz
		var hz_ul = ul_array[hz_i];
		var hz_li = hz_ul.parentNode; // will be parent LI
		var hz_a = hz_li.getElementsByTagName('a')[0];
		
		hz_ul.style.display = 'block';
		hz_a.className="open";
		AIS.menu.array[hz_i] = 1;
		AIS.menu.current_open = hz_i;
		AIS.menu.saveCookie();
		
	}		
}

AIS.menu.XXXopenMyHz = function () {
// myHeinzsight folder is second in array: array[1]
// so first check it exists
// this is a hack assuming only 2 drop downs

 if (AIS.menu.array[111]) {
 var nav = document.getElementById(AIS.menu.id);
 var ul_array = nav.getElementsByTagName('ul'); // any ul inside nav are submenus

 var ul_el = ul_array[1];
 var li = ul_el.parentNode; // will be parent LI
 var a_first = li.getElementsByTagName('a')[0];

 a_first.className="open";
 ul_el.style.display = 'block';
 AIS.menu.current_open = 1;
 AIS.menu.saveCookie();
 }
}



AIS.menu.saveCookie = function () {
	var max_age = 3600; // 3600:1hr
	var cookie_str = encodeURIComponent(AIS.menu.array);
	//document.cookie = AIS.menu.cookie_name_str+'=' + cookie_str+"; path=/";
	
	document.cookie = AIS.menu.cookie_name_str +'='+ cookie_str + "; max-age=" + max_age + "; path=/";
	
	//alert("saveCookie " + document.cookie);
	// set cookie path / p459
	
	//var dev = document.getElementById('poke');	
	//dev.innerHTML = document.cookie;
	//dev.style.lineHeight = '1em';
	
	//var headerBar = document.getElementById('headerBar');
	//headerBar.style.overflow = 'visible';
}


AIS.menu.loadCookie = function() {
	var cookies = document.cookie;
	if (cookies) {
	
		// cookies is a long string that may contain our cookie aswell as other cookies
		// find the start of our cookie
		var position = cookies.indexOf(AIS.menu.cookie_name_str+'=');
		if(position != 1) {	// then cookie was found
			var name_str_length = AIS.menu.cookie_name_str.length +1; // heinz=
			var start = position + name_str_length;	// start of cookie value
			var end = cookies.indexOf(';', start);	// end of cookie value
			if (end == -1) end = cookies.length;	// incase end is not found
			
			var cookie_str = cookies.substring(start,end);
			
			
			// heinzsight=1; HeinzWizard=RegistrationName=p&RegistrationOrganisation=p&RegistrationPosition=p&RegistrationAddressLine1=p&RegistrationAddressLine2=p&RegistrationAddressLine3=p&RegistrationCounty=p&RegistrationPostCode=p&RegistrationEmail=rat@bat.com

			//var name_str_length = AIS.menu.cookie_name_str.length +1; // heinz=
			//var cookie_str = cookies.substring(name_str_length,cookies.length);
			
			
			var cookie_array_str = decodeURIComponent(cookie_str);
			AIS.menu.array = cookie_array_str.split(",");
			
			
			
			//var dev = document.getElementById('poke');	
			//dev.innerHTML = "pos:"+ position +" "+ document.cookie;
			//dev.style.lineHeight = '1em';
			//var headerBar = document.getElementById('headerBar');
			//headerBar.style.overflow = 'visible';
		
		}
	}
}

AIS.menu.EmptyArray = function(length) {
	if (!length) length = 0;
	// length = length || 0;
	var empty_array = new Array(length);
	for (var i=0; i<length; i++) {
		empty_array[i] = 0;
	}
	return empty_array;
}

AIS.centre = function(el) {
	if (document.getElementById){
		if (el) {
			//test ? doThis : elseDoThis ;
			if (typeof el=="string") {
			var el = document.getElementById(el);
			}
			if (el.height) {
				var height;
				if (window.innerHeight)
					//scua
					height = window.innerHeight;
				else if (document.documentElement && document.documentElement.clientHeight) {
					// IE with Doctype
					height = document.documentElement.clientHeight;
				}
				
				if (height > el.height) {
					var verticalSurplus = height - el.height;
					var marginTop = Math.round(verticalSurplus/2);
					el.style.marginTop = marginTop+"px";
				}
			}
		}
	}
}
    
// DEFINE VARIABLES //
AIS.menu.id = "menu"; // #ID of UL
AIS.menu.mode = "mono"; // mono / multi // only one submenu may be open at one time
AIS.menu.current_open = 0; // used if only one submenu may be open at one time
AIS.menu.cookie_name_str = "heinzsight";
AIS.menu.array = new Array();

AIS.addToWindowOnload(AIS.menu.init);