/* 
v1.5
150806 : modified checkflash for better compatability.  
110806 : updated configuresitemenu to normalise link text case 
080806 : updated footer link and bg colours.
080806 : updated submenu code to change BG colour when no submenu found.
080806 : fixed bug matching footer item to menu id
121208 : removed checkflash buggn out in player 10 not needed.

++++++++++++++++++++++++++++++++++++++++++++
Parses included site menu lists for current directory,submenu,page hyperlinks.
Sets new CSS classes / rules to hyperlinks. 
checks if current page should display a submenu, adds submenu hyperlink list to submenu div. 
rearranges order of main menu items so current site directory is first in list. 
moves entire map into footer list for pop up functionality. 
6 : tells flash where to find it's menu referrence fle and loads up flash movie. 

++++++++++++++++++++++++++++++++++++++++++++ 
*/
ConfigureSiteMenu = function(){
	// +++ 1 +++ search map list for current page. 
	var vListArr = document.getElementById('sitedirectory').getElementsByTagName("li");
	var vPageName = GetPageName();
	//alert(vPageName);
	//alert(vListArr)
	for(var n=0; n < vListArr.length; n++){
		var vAnchor = vListArr[n].childNodes[0];
		var vHREF = vAnchor.getAttribute("href");
		//alert(vHREF)
		//ie returns full path as "href" attribute : so parse. 
		var vLinkFileName = vHREF.substr(vHREF.lastIndexOf("/")+1);
		if(vLinkFileName == vPageName){	
			var vCurrPage = vListArr[n]
		}
	}
	if(vCurrPage == null){
		alert("no match for  page " + GetPageName() + " found in site directory");
		}
	// +++ 2 +++ search map list for current submenu, directory and map.
	//CAREFUL, any spaces between the <div id="sitemenu"> and the <?php include will be read as a text node, which breaks the vCurrMapRef, by jove! 
	var vCurrMapRef = document.getElementById('sitedirectory').childNodes[0].id.toLowerCase();
	if(vCurrMapRef == null){
		alert("unable to find the menu ID - remove any spaces between the <div id='sitemenu'> and the <?php include" );
		}
	//alert(vCurrMapRef);
	var vSampler  = vCurrPage.parentNode.parentNode.parentNode.parentNode;
	//alert(vSampler);
	//the page will either be nested four levels below if NOT in a submenu,
	//or nested six levels below if IS in a submenu. so tester checks how deep it is by 
	// investigating the node four above to see if it is the container div tag or not..
	if(vSampler.id =='sitedirectory'){
		//submenu level node. 
		var vCurrSubmenu = vCurrPage.childNodes[0];		
		var vCurrDirectory = vCurrPage.parentNode.parentNode.childNodes[0];
		var vCurrSubList = vCurrPage.getElementsByTagName('ul')[0];
		//check if this node contains a submenu and if so add it to page..
		if(vCurrSubList!= null){
			//must contain a submenu, add it here..
			var vSelectedLink = vCurrSubList.innerHTML.split('<ul>')[0];
			document.getElementById('subMenu').innerHTML =  '<ul><li><a style="color:#A0C804" href="'+vCurrSubmenu.getAttribute('href')+'">'+vCurrSubmenu.innerHTML+'</a></li>'+vCurrSubList.innerHTML+'</ul>';
			}else{
				document.getElementById('subMenu').style.backgroundImage = "none";
				}
	} else{
		//page level node.
		var vCurrSubmenu = vCurrPage.parentNode.parentNode.childNodes[0];
		var vCurrDirectory = vSampler.childNodes[0];
		//since a submenu must exist, add it to the page..
		vCurrPage.childNodes[0].style.color ='#A0C804';
		document.getElementById('subMenu').innerHTML =  '<ul><li><a href="'+vCurrPage.parentNode.parentNode.getElementsByTagName('a')[0].getAttribute('href')+'">'+vCurrPage.parentNode.parentNode.getElementsByTagName('a')[0].innerHTML+'</a></li>'+vCurrPage.parentNode.innerHTML+'</ul>';
	}
	// +++ 3 +++ search footer for current map link, and move it to the front of the list.  
	var aLinks = document.getElementById('sitemenu').getElementsByTagName("li");
	//alert(aLinks);
	for(var n=0; n < aLinks.length; n++){
		var vLink = aLinks[n].childNodes[0].innerHTML.replace(" ","_").toLowerCase();
		//alert(vLink+"       "+vCurrMapRef);
		if(vLink == vCurrMapRef){
			//found the current link.. also means that the text inside the link can be used 
			// move current menu item to front of footer.
			var vFirstHref = aLinks[0].childNodes[0].getAttribute('href');
			var vFirstHyp = aLinks[0].childNodes[0].innerHTML;
			var vCurrHref = aLinks[n].childNodes[0].getAttribute('href');
			var vCurrHyp = aLinks[n].childNodes[0].innerHTML;
			aLinks[0].childNodes[0].setAttribute('href',vCurrHref);
			aLinks[0].childNodes[0].innerHTML= vCurrHyp;
			aLinks[n].childNodes[0].setAttribute('href',vFirstHref);
			aLinks[n].childNodes[0].innerHTML= vFirstHyp;
			var vCurrMap = aLinks[0].childNodes[0];
			var vCurrMapLI = aLinks[0];
		}
	}
	// +++ 4 +++  change css rules for current items.
	vCurrSubmenu.style.color ='#A0C804';
	vCurrDirectory.style.color ='#444444';
	vCurrMap.style.color ='#a0a0a0';
	vCurrMap.className ='currentMenu';
		// +++ 5 +++  bung the lot into the footer.
	var vMapHTML = document.getElementById('sitedirectory').innerHTML;
		//alert(vMapHTML)
		vCurrMapLI.innerHTML = vCurrMapLI.innerHTML + vMapHTML;
		 document.getElementById('sitedirectory').innerHTML = "";
	// +++ 6 +++ load up the flash
	
	vCurrHyp = vCurrHyp.replace(" ","_").toLowerCase();
	var vDirectoryName =  vCurrDirectory.getAttribute('href');
	//ie returns full path as "href" attribute : so parse. 
	vDirectoryName = vDirectoryName.substr(vDirectoryName.lastIndexOf("/")+1);
	//now peel of the suffix.
	vDirectoryName = vDirectoryName.split(".")[0];

	vSubmenuName = vCurrSubmenu.getAttribute('href')
	//ie returns full path as "href" attribute : so parse. 
	vSubmenuName = vSubmenuName.substr(vSubmenuName.lastIndexOf("/")+1);
	//now peel of the suffix.
	vSubmenuName = vSubmenuName.split(".")[0];
	
	//alert(vCurrHyp);
	//alert(vDirectoryName);
	//alert(vSubmenuName);
	
	loadFlash(vCurrHyp,vDirectoryName,vSubmenuName);
	//loadFlash('student_living','the_village','village_life');
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  load SWF
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function loadFlash(pMap,pDirectory,pSubmenu){

		var vPath = 'flash/';
		if (pDirectory == "index" ) {
			vPath = "site/"+vPath;
		} else {
			vPath = "../"+vPath;
		}
		var m="?vMap="+pMap+"&vDirectory="+pDirectory+"&vItem="+pSubmenu;
		var w = 740;
		var h = 108;
		var bgcolor="F9F9F8";	
		document.getElementById('header').innerHTML += ('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'" id="menu" align="middle"><param name="allowscriptAccess" value="sameDomain" /><param name="movie" value="'+vPath+'menu.swf'+m+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#'+bgcolor+'" /><embed src="'+vPath+'menu.swf'+m+'" quality="high" wmode="transparent" bgcolor="#'+bgcolor+'" width="'+w+'" height="'+h+'" name="menu" align="middle" allowscriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');		
	
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  CSS popup list : IE wont set hover selectors for non <a> elements. this fixes it.  
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function UpdateNode(node){
	//if (node.nodeName=="LI") {
				//alert(node.innerHTML);
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			//}
	}

function CSSListFixerIE() {
	if (document.all&&document.getElementById) {
		//update first tier links
		navRoot = document.getElementById("sitemenu").childNodes[0];
	//	for (i=0; i<navRoot.childNodes.length; i++) {
		//	UpdateNode(navRoot.childNodes[i]);
	//	}
		
		
		huffy = navRoot.getElementsByTagName("li");
	//alert(huffy.length)
			for (i=0; i<huffy.length; i++) {
			UpdateNode(huffy[i]);
		}
		//update second tier links	
		//navRoot = document.getElementById("sitemenu").childNodes[0].childNodes[0].childNodes[0].childNodes[0];
		//alert(navRoot.innerHTML);

	//	for (i=0; i<navRoot.childNodes.length; i++) {
	//		UpdateNode(navRoot.childNodes[i]);
	//	}
		
	}
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  pop up a window  
++++++++++++++++++++++++++++++++++++++++++++ 
*/

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
/* 
++++++++++++++++++++++++++++++++++++++++++++
snippet styler
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function StyleSnippet(sDiv){
	var aSnippets = sDiv.getElementsByTagName('p');
		for(s=0;s<aSnippets.length;s+=2){
			
			aSnippets[s].style.backgroundColor = '#f0f0f0';
			
			}
	for(s=1;s<aSnippets.length;s+=2){
			
			aSnippets[s].style.backgroundColor = '#eaeaea';
			
			}
}
/* 
++++++++++++++++++++++++++++++++++++++++++++
  random .bImage div background image selector.   
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function LoadRandomImage(iDiv){
//random image name - either list of image names or generic "bkg_n.jpg" format. 
//using an image name list : 
//var vImageList = ["blueberries.jpg","book.jpg","chopping_board_pot.jpg","coffee_and_cup.jpg", "girl_ponder.jpg","girl_smile_02.jpg", "guy_girl_pc.jpg", "guy_girl_study.jpg", "guy_smile.jpg","highlight.jpg", "table_chairs.jpg",];
//var vRandom = Math.floor(Math.random() * vImageList.length);
//using generic names :
var vRandom = Math.floor(Math.random() * 13);
var vPath = 'url(../images/backgrounds/bkg_'+ vRandom+'.jpg)';
iDiv.style.backgroundImage = vPath;
iDiv.style.backgroundPosition = "bottom";
iDiv.style.backgroundRepeat = "no-repeat";
}
/* 
++++++++++++++++++++++++++++++++++++++++++++
  balance divs  
++++++++++++++++++++++++++++++++++++++++++++ 
*/

function doCompare(a, b) {
    return a - b
}

function BalanceDivs(aRow){
	//aRow contains 1 -4 column elements
	//parse each column for b Div boxes
var aColH = [];
var aStretchDivs = [];
var vStretcher;
for(var r=0;r<aRow.length;r++){
	
	var vCopyMarginAdjust = 0;
	vStretcher = null;
	var aBoxes = [];

	var aContent = aRow[r].getElementsByTagName('div');
	for(var b=0;b<aContent.length;b++){
		if (aContent[b].className.substring(0,1) == "b"){
			aBoxes.push(aContent[b]);
			
			if (aContent[b].className == "bImage"){
				//save element
				 vStretcher =  aContent[b];
				 LoadRandomImage(vStretcher);		 
			}	
			else if (aContent[b].className == "bSpacer"){
				//save element
				 vStretcher =  aContent[b];
			}
			
			else if (aContent[b].className == "bSnippet"){
				StyleSnippet(aContent[b]);	 
			}
			
			else if (aContent[b].className == "bCopy"){
				//the copy div has a bottom margin of 20px for better readability.
				//this amount must be added to all other divs in this col...				
				vCopyMarginAdjust = 20;
			}
		}	
	}
	//now check if vImage was detected in order to choose vImage or last div box as the one to stretch
	 if(vStretcher == null){
	   vStretcher = aBoxes[aBoxes.length-1];
	 }
	//finally add element to be stretched to array.
	aStretchDivs.push(vStretcher);		

	//aBoxes now contains all b elements for this column
	//use the last item to determine column height.
	vColH = aBoxes[aBoxes.length-1].offsetHeight+aBoxes[aBoxes.length-1].offsetTop+vCopyMarginAdjust;
	aColH.push(vColH);
}	
//now we know the col heights in aColH and the elements to stretch, in aStretchDivs.
//find the tallest col by sorting 
aSortH = aColH.slice(0);
aSortH.sort(doCompare);
var vMaxH = aSortH[aSortH.length-1];
for(h=0;h<aStretchDivs.length;h++){
	var vColOffset = vMaxH - aColH[h];
	
	var vBoxHeight = aStretchDivs[h].offsetHeight+vColOffset;
	aStretchDivs[h].style.height = vBoxHeight + 'px';
}
}

function GetDivs(){
	var aContent = document.getElementById('content').getElementsByTagName('div');
	var aColumns = [];
	//separate into columns
	for(i=0; i< aContent.length;i++){
		var vPrefix = aContent[i].className.substring(0,1);
		if (vPrefix == "c"){
			aColumns.push(aContent[i]);
		}
	}
	//keep count 
	var vWidthCount = 0;
	var vCol = 0;
	var aGrid = [];
	//separate into rows
	while(vCol < aColumns.length){ 
		var aRow = [];
		while(vWidthCount < 4){
			var vSuffix = parseInt(aColumns[vCol].className.substring(1));
			aRow.push(aColumns[vCol]);
			vWidthCount +=vSuffix;
			vCol +=1;
		 }
		 vWidthCount = 0;
		 BalanceDivs(aRow);
	}
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  switch element on and off
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function switchElm(elmName){
	document.getElementById(elmName).style.display = "block"
	currentDiv = div_id;
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  get name of current page.
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function GetPageName(){
	var vPageURLObj =  document.location;
	var vPageURL = vPageURLObj.toString();
	var vPageFileName = vPageURL.substr(vPageURL.lastIndexOf("/")+1);
	//ie sometimes ? adds a # to the end of the filename, so parse..
	if(vPageFileName.charAt(vPageFileName.length-1)=="#"){
		var vPageFileName = vPageFileName.substr(0,vPageFileName.length-1);
	};
	
	//vPageFileName = vPageFileName.split(".")[0];//remove file suffix
	return(vPageFileName);
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  show / hide divs  - used for rules / faq / reasons pages etc.
  
++++++++++++++++++++++++++++++++++++++++++++ 
*/
var vVisibleDiv = null;

function switchDiv(headerTXT){
	
	var aReasons = document.getElementById('switcher').getElementsByTagName('h1');
//	alert(aReasons.length);

	for(r=0; r< aReasons.length; r++){
		var vReason = aReasons[r];
	//	alert(aReasons[r]+"    "+ vReason);
		if(vReason.innerHTML == headerTXT){
	//		alert("gotcha");
		if(vVisibleDiv != null){
				vVisibleDiv.style.display = "none";
		}
			vReason.parentNode.style.display = "block";
			vVisibleDiv = vReason.parentNode;
		}
	}
}

/*
++++++++++++++++++++++++++++++++++++++++++++
  run page layout functions   
++++++++++++++++++++++++++++++++++++++++++++ 
*/
var gMap;
var gDirectory;
var gSubmenu;

function runJS(switchRef){
	ConfigureSiteMenu();
	CSSListFixerIE();
	GetDivs();
	//ie win cannot fire two funcs from onload, so .....
	if(switchRef){
		switchDiv(switchRef)
	};
}