//HOUSING STATISTICS NETWORK JAVASCRIPT

//FUNCTIONS
//setHomeSettings()
//hoverMenuButton(id)
//offMenuButton(id)
//clickMenuButton(id)
//hoverHomePanel(id)
//offHomePanel(id)
//addSubMenu(id)
//openSubMenu(id)
//clickHomePanel(id)
//clickSubButton(id){
//clickHomePanel(id)
//offSubButton(id)
//clickSubButton(id)
//deselectSubMenuButtons(parent)
//closeSubMenu(id)
//closeSubDivs(parent)
//newPageUrl(page)
//checkUrlHash()
//updateTitle(section, subTopic)
//cnvrt2Upper(str)
//initUrlPoll(hash)
//linkToForum(topicID)
//loadForumPosts()
//printForumPosts()
//hoverPost()
//offPost()
//clickPost()
//viewNews(#)
//hoverViewNews(#)
//offViewNews(#)

function setHomeSettings(){
//alert("onloading");
//set button colour so dom recognizes 
var idObj = document.getElementById("homebutton");
idObj.style.color = "ivory";
idObj.style.backgroundColor = "darkslateblue";
idObj.style.borderColor = "lightslategray";

//switch home div on
document.getElementById("homediv").className = "displayon";
}



function removeDomChilds(domParent){
//pass the parent name here, and it will delete all child nodes.
//alert("It passed this way");
	if ( domParent.hasChildNodes() )
	{
		//alert(domParent);
		while(domParent.childNodes.length >=1)
		{
			//alert("removing child");
			domParent.removeChild(domParent.firstChild);
		}
		
	}
}


function hoverMenuButton(id){
//alert("id = " + id);

//changed button ids to include button cos the bookmark hash in url was jumping to button ids
//so need to make ids different to sections shown in url hash t prevent from happening.
id = id + "button";

//hover here before clause negates hover over selected, in this case the home button
changeHoverTitle(id);

var idObj = document.getElementById(id);
idColor = idObj.style.color;
//alert("colour is " + idColor);

//Need to allow for browser versions, some read colours differently
if(idColor == "ivory" || idColor == "rgb(255, 255, 240)" || idColor == "#fffff0"){
	//Cancel if this link was selected by a click

	return;
}

idObj.style.color = "darkslategray";
idObj.style.backgroundColor = "lavender";
idObj.style.borderColor = "lightslategray";

//alert("hovering");

}

function offMenuButton(id){
//alert("off hover");
id = id + "button";

var idObj = document.getElementById(id);

idColor = idObj.style.color;
//alert("colour is " + idColor);

//Need to allow for browser versions, some read colours differently, not worth repeating this method
if(idColor == "ivory" || idColor == "rgb(255, 255, 240)" || idColor == "#fffff0"){
	//Cancel if this link was selected by a click

	return;
}

idObj.style.color="black";
idObj.style.backgroundColor = "ivory";
idObj.style.borderColor = "darkslategray";

}




function clickMenuButton(id, polling){
//Due to back button hash solution, id is also hash section title, 
//but buttons and divs have uniquely different ids

//alert("click menu button was clicked "+ id);
clickButtonId = id + "button";

//unclick previously selected link, by looping array of links and returning selected link to default colors.
var buttonArray = new Array("home", "about", "future", "launch", "needs", "aims", "steergroup", "forum", "uksa", "events", "links", "register", "contact");
arrayCount = buttonArray.length - 1;
//alert(arrayCount); 
var i;
for(i=0; i<=arrayCount; i++){
	
	var section = buttonArray[i];
	var buttonId = section + "button";

	//adjust id to find link buttons
	var buttonColor = document.getElementById(buttonId).style.color;
	//alert(buttonColor);

	if(buttonColor == "ivory" || buttonColor == "rgb(255, 255, 240)" || buttonColor == "#fffff0"){
		var otherObj = document.getElementById(buttonId);
		//alert("link was selected " + buttonColor);
		
		//return button to inactive state
		otherObj.style.color="black";
		otherObj.style.backgroundColor = "ivory";
		otherObj.style.borderColor = "darkslategray";

		//Check for submenus in current selection, pretty cludgy all this tho
		//Just making sure evrything is switched off for each section, subtopic etc
		switch (id)
		{
		case "steergroup":
			//Flip submenu buttons to default colours
			deselectSubMenuButtons(id);
			break;
		case "launch":
			// flip submenu buttons to default colours
			deselectSubMenuButtons(id);
			break;
		case "needs":
			// flip submenu buttons to default colours
			deselectSubMenuButtons(id);
			break;
		case "uksa":
			// flip submenu buttons to default colours
			deselectSubMenuButtons(id);
			break;
			
		}
		//If any main index button is clicked, basically gotta make sure no sub divs are switched on
		//regardless of topic selected
		closeSubDivs("steergroup");
		closeSubDivs("launch");
		closeSubDivs("needs");
		closeSubDivs("uksa");
		//alert("it got this far");
		
		//switch off the topic's div display, change id from ..link to ...div
		
		var divId = section + "div";
		//alert(divId);
		//document.getElementById(divId).style.display = "none";
		document.getElementById(divId).className = "displayoff";
	}

}

//change selected button style to selected state
var idObj = document.getElementById(clickButtonId);
idObj.style.color="ivory";
idObj.style.backgroundColor = "darkslateblue";

//naming convention currently allows adding div to button name to get related div id
var selDiv = id + "div";

//switch on display of selected dic element
//document.getElementById(selDiv).style.display = "block";

document.getElementById(selDiv).className = "displayon";

//Ensure subtopic menus are now closed, but close a selected one
if(id != "launch"){closeSubMenu("launch");}
if(id != "needs"){closeSubMenu("needs");}
if(id != "steergroup"){closeSubMenu("steergroup");}
if(id != "uksa"){closeSubMenu("uksa");}

//Finally, show submenu if selection has one
if(id=="steergroup" || id=="launch" || id=="needs" || id=="uksa")
{
	openSubMenu(id);
}

if(polling == "false")
{
	//Set new url. 
	newPageUrl(id);
}

}//clickMenuButton



function openSubMenu(id){
id=id+"tr";
document.getElementById(id).className = "displayon";

}


function hoverSubButton(id){
//change style of panel on mouseover to reflect hover, also link to side menu.
var idObj = document.getElementById(id);
var buttonColor = idObj.style.color;
if(buttonColor == "ivory" || buttonColor == "rgb(255, 255, 240)" || buttonColor == "#fffff0")
{
	//Do nothing, exit function
	//exit;
}
else
{
//go ahead change bg color to hover
idObj.style.backgroundColor = "lavender";
idObj.style.color = "darkslategray";
//idObj.style.borderColor = "lightslategray";
}
}

function offSubButton(id){
//change style of panel on mouseout to reflect cursor leaving panel, also link to side menu
var idObj = document.getElementById(id);
var buttonColor = idObj.style.color;
if(buttonColor == "ivory" || buttonColor == "rgb(255, 255, 240)" || buttonColor == "#fffff0")
{
	//Do nothing, exit function
	//exit;
}
else
{

idObj.style.backgroundColor = "ivory";
idObj.style.color = "black";
//idObj.style.borderColor = "gainsboro";
}

}

function clickSubButton(id, polling){
//deselect previously selected link, by looping array of links and returning selected link to default colors.
//currently only steer group has sub menus, but may need to add switch here later.
//Need to find parent topic, by isolating the first 2 characters of the sub-topic id
var parent = id.substring(2,0);
//alert("parent: " + parent);

//Array of subtopics ids, set using parent topic id
var subButtonArray = new Array();
var section;
switch(parent)
{
case "sg"://steergroup
	subButtonArray = Array("sgrole", "sgmembers", "sgmeeting", "sgminutes");
	section = "steergroup";
	break;

case "ls"://launchseminar
	subButtonArray = Array("lssummary", "lspresentations", "lsprofiles", "lsagenda", "lsattendance", "lssuggestions");
	section = "launch";
	break;
case "ns"://launchseminar
	subButtonArray = Array("nsprelim", "nsagenda", "nsprofiles", "nsreport", "nspresentations", "nsattendance");
	section = "needs";
	break;
case "sa"://uksa
	subButtonArray = Array("saprogram", "saengagement");
	section = "uksa";
	
	break;


}
var arrayCount = subButtonArray.length - 1;
//alert("id: " + id +", array looks like this: " + subButtonArray + ", array count = " + arrayCount); 

//Loop through sub topics, return style status to default for the previously selected button, 
//and switch off the display of that topic
var i;
for(i=0; i<=arrayCount; i++)
{
	var buttonId = subButtonArray[i];

	//adjust id to find link buttons
	var buttonColor = document.getElementById(buttonId).style.color;

	if(buttonColor == "ivory" || buttonColor == "rgb(255, 255, 240)" || buttonColor == "#fffff0")
	{
		var otherObj = document.getElementById(buttonId);
		//alert("link was selected " + buttonColor);
		
		//return button to inactive state
		otherObj.style.color="black";
		otherObj.style.backgroundColor = "ivory";
		otherObj.style.borderColor = "gainsboro";

		//switch off the topic's div display, change id from ..link to ...div
		
		var divId = buttonId + "div";
		//alert("Old div id : " + divId);
		//document.getElementById(divId).style.display = "none";
		document.getElementById(divId).className = "displayoff";
	}


}	

//Make sure parent divs are not displayed
document.getElementById("steergroupdiv").className = "displayoff";
document.getElementById("launchdiv").className = "displayoff";
document.getElementById("needsdiv").className = "displayoff";
document.getElementById("uksadiv").className = "displayoff";

	//alert("button colour is " + buttonColor);

//Set style status for newly selected topic and switch on the div containing the topic
var idObj = document.getElementById(id);
idObj.style.backgroundColor = "darkslateblue";
idObj.style.color = "ivory";

//naming convention currently allows adding div to button name to get related div id
var selDiv = id + "div";

//switch on display of selected dic element
//document.getElementById(selDiv).style.display = "block";


//Switch on new section
document.getElementById(selDiv).className = "displayon";

//remove sub id tag, first two letters of each subtopic button id.
id = id.substring(2);
//alert(id);

//Only update url on valid click, not back button or refresh/ bookmark
if(polling == "false"){
	var page = section + "/" + id;
	newPageUrl(page);
}

//alert("hello?");

}//clickSubButton


function deselectSubMenuButtons(parent){
var subButtonArray = new Array();
//alert("parent: " + parent);

switch(parent)
{
case "steergroup"://steergroup
	subButtonArray = Array("sgrole", "sgmembers", "sgmeeting", "sgminutes");
	break;
case "launch"://launchseminar
	subButtonArray = Array("lssummary", "lspresentations", "lsprofiles", "lsagenda", "lsattendance", "lssuggestions");
	break;
case "needs"://launchseminar
	subButtonArray = Array("nsprelim", "nsagenda", "nsprofiles", "nsreport", "nspresentations", "nsattendance");
	break;
case "uksa"://uksa
	subButtonArray = Array("saprogram", "saengagement");
	break;
}

var arrayCount = subButtonArray.length - 1;

var i;
for(i=0; i<=arrayCount; i++)
{
	var buttonId = subButtonArray[i];
	//alert("buttomn ID: " + buttonId);

	document.getElementById(buttonId).style.color = "black";
	document.getElementById(buttonId).style.backgroundColor = "ivory";
}

}

function closeSubDivs(parent){
//Loop through sub divs of topic, and switch display off with classname, cludging here
var subButtonArray = new Array();
//alert("close sub divs:" + parent);

switch(parent)
{
case "steergroup"://steergroup
	subButtonArray = Array("sgrolediv", "sgmembersdiv", "sgmeetingdiv", "sgminutesdiv");
	break;

case "launch"://launchseminar
	subButtonArray = Array("lssummarydiv", "lspresentationsdiv", "lsprofilesdiv", "lsagendadiv", "lsattendancediv", "lssuggestionsdiv");
	break;

case "needs"://launchseminar
	subButtonArray = Array("nsprelimdiv", "nsagendadiv", "nsprofilesdiv", "nsreportdiv", "nspresentationsdiv", "nsattendancediv");
	break;

case "uksa"://uksa
	subButtonArray = Array("saprogramdiv", "saengagementdiv");
	break;
}

var arrayCount = subButtonArray.length - 1;


var i;
for(i=0; i<=arrayCount; i++)
{
	var buttonId = subButtonArray[i];
	document.getElementById(buttonId).className = "displayoff";
	//alert("button ID: " + buttonId);

}

}


function closeSubMenu(id){
//switch display off for the tr element containing the table of the sub menu
//alert("close sub menu:" + id);

var trId = id + "tr";
document.getElementById(trId).className = "displayoff";

//return all sub-topic buttons style to default settings
deselectSubMenuButtons(id);
}

function changeHoverTitle(id){
//changes text and background colour of top feature on page, to show a little info on what can be found there.
var bgColor; var title; var text;

//set variables by switch, apply changes after.

//alert("changing hover title, id: " + id);

switch(id)
{
case "homebutton":
	bgColor = "";
	title = "The HSN Website";
	text = "The HSN is an open organisation affiliated to the RSS Statistics Users Forum. It was set up to drive improvements in the data and statistics available.";
	break;
case "aboutbutton":
	bgColor = "";
	title = "About HSN";
	text = "The website exists to raise the profile of Housing Statistics, stimulate dialogue and alert users to HSN and other events.";
	break;
case "aimsbutton":
	bgColor = "";
	title = "Aims & Objectives";
	text = "Concise and clear aims and objectives were formally adopted at the Launch Seminar.";
	break;
case "futurebutton":
	bgColor = "";
	title = "Future Seminars";
	text = "Planning is already underway; the list of potential subjects is now available. Influence the seminar priorities by giving your views in the HSN Internet Forum.";
	break;
case "launchbutton":
	bgColor = "";
	title = "Launch Seminar";
	text = "The Network was formally inaugurated on the 26th Feb 2010 at it's first event, the Launch Seminar.";
	break;
case "needsbutton":
	bgColor = "";
	title = "Housing Needs";
	text = "The second HSN seminar will be held at the RSS on the 6th October 2010. Attendance is free, register today to reserve a place.";
	break;
case "steergroupbutton":
	bgColor = "";
	title = "Steering Group";
	text = "The Steering Group members are volunteers from the widest possible range of users and providers of housing statistics, including the devolved administrations.";
	break;
case "forumbutton":
	bgColor = "";
	title = "Internet Forum";
	text = "An internet forum has been set up to encourage public dialogue on housing statistics issues. Register now to have your say.";
	break;
case "registerbutton":
	bgColor = "";
	title = "Register";
	text = "Registration for the contact list can be done via a form, and shouldn't take more than a minute.";
	break;
case "uksabutton":
	bgColor = "";
	title = "UKSA";
	text = "The HSN aims to keep users up to date with UK Statistics Authority activities which relate to housing statistics.";
	break;
case "eventsbutton":
	bgColor = "";
	title = "Events";
	text = "The HSN plans to hold at least two events per year. This section also lists other events related to housing statistics.";
	break;
case "linksbutton":
	bgColor = "";
	title = "Links";
	text = "A comprehensive list of links to HSN affiliated organisations, seminar presentations and journals, documents etc.";
	break;
case "contactbutton":
	bgColor = "";
	title = "Contact";
	text = "Contact details for founding members of HSN.";
	break;
}

fade1.text = title;
fade2.text = text;
fade1.initFadeColorOut();
fade2.initFadeColorOut();


//document.getElementById("hometitlebody").style.backgroundColor = "rgb(25,25,24)";

//document.getElementById("hometitle").innerHTML = title;

//document.getElementById("hometitlespan").innerHTML = text;

}

var colorId1 = "hometitlebody";
var textId1 = "hometitle";
var colorIn1 = "255, 255, 240";
var colorOut1 = "119, 136, 153";

var fade1 = new Fade(colorId1, textId1, colorOut1, colorIn1);

var colorId2 = "hometitlespan";
var textId2 = "hometitlespan";
colorIn2 = "255, 255, 255";
colorOut2 = "119, 136, 153";
var fade2 = new Fade(colorId2, textId2, colorOut2, colorIn2);


function Fade(colorID, textID, color, colorTo){
//This object fades from color to colorTo and back, 
//in the middle of the color change it switches to new text for object by id
//can handle any two colors, but not built to handle identical r, g or b values if they occur
//the difference would be zero, so it will crash without some clauses somewhere, i think
//the time delay and increment are hard wired but could become 2 further properties.
//note final default colors are also hard wired, add variables instead for color flexibility

//Works by dividing the difference between each rgb values by the number of iterations of step changes
//desired, at the moment its 20.  
var timerInID;
var timerOutID;
var timerSeg = 20;
var rBit; var gBit; var bBit;
var rNew; var gNew; var bNew;
var counterIn = 0; var counterOut = 0; 
var newRGB;

var me = this;

me.colorID = colorID;
me.textID = textID;
//me.text = text;
me.color = color;
me.colorTo = colorTo;

me.initFadeColorOut = function(){
//catch new calls before last interval finishes
if(counterOut >= 0){clearInterval(timerOutID);}


if(document.getElementById(me.colorID).style.backgroundColor != "rgb(" + color + ")"){ 
//alert(document.getElementById(me.colorID).style.backgroundColor + " == rgb(" + color + ")");
//alert(fade1);
	//overide text input, make sure latest hovered button is there
	//document.getElementById(me.textID).innerHTML = text;

	//return;
}

//alert("Object called. text = " + me.text);
//alert(me.initFadeColorIn);


//split both colours into r,g,b 
colorOutArray = me.color.split(",");
colorInArray = me.colorTo.split(",");
var rOut = colorOutArray[0];
var gOut = colorOutArray[1];
var bOut = colorOutArray[2];
var rIn = colorInArray[0];
var gIn = colorInArray[1];
var bIn = colorInArray[2];

rNew = parseInt(rOut);
gNew = parseInt(gOut);
bNew = parseInt(bOut);

//alert(rNew);

//Work out the differences between them
var rDiff = rOut - rIn;
var gDiff = gOut - gIn;
var bDiff = bOut - bIn;

//divide by timer interval.
rBit = rDiff/timerSeg;
gBit = gDiff/timerSeg;
bBit = bDiff/timerSeg;

//Use set interval to call the colour fade;
timerOutID = setInterval(me.fadeColorOut, 20);

};//initFadeColorOut


me.fadeColorOut = function(){
//timer interval may be floating point but the new rgb numbers must be integers
//alert(timerOut);
//alert("New rgb = " + rNew + "," + gNew + ","+ bNew );

rNew = parseInt(rNew - rBit);
gNew = parseInt(gNew - gBit);
bNew = parseInt(bNew - bBit);



newRGB = "rgb(" + rNew + "," + gNew + ","+ bNew + ")";
//
//alert(newRGB);

//hard wired style selector
switch(me.colorID)
{
case 	"hometitlebody":
	document.getElementById(me.colorID).style.backgroundColor = newRGB;
	break;
case 	"hometitlespan":
	document.getElementById(me.colorID).style.color = newRGB;
	break;
}

if(counterOut >= 20)
{
	clearInterval(timerOutID);
	//timerOutID = 0;
	counterOut = 0;
	var finalRGB = "rgb(" + me.colorTo + ")";
	//alert(finalRGB);

	switch(me.colorID)
	{
	case 	"hometitlebody":
		document.getElementById(me.colorID).style.backgroundColor = finalRGB;
		//alert(me.initFadeColorIn);
		break;
	case 	"hometitlespan":
		document.getElementById(me.colorID).style.color = finalRGB;
		break;
	}

	//Swap info and fade back in
	document.getElementById(me.textID).firstChild.nodeValue = me.text;


	me.initFadeColorIn();


}
counterOut++;


//alert(counterOut);
};//fadeColorOut


me.initFadeColorIn = function(){
//catch new calls before last interval finishes
//alert("fading in now");

if(counterIn >= 0){clearInterval(timerInID);}

//split both colours into r,g,b, this time cos fading back to original color, switch colors here
colorOutArray = me.colorTo.split(",");
colorInArray = me.color.split(",");
var rOut = colorOutArray[0];
var gOut = colorOutArray[1];
var bOut = colorOutArray[2];
var rIn = colorInArray[0];
var gIn = colorInArray[1];
var bIn = colorInArray[2];

rNew = parseInt(rOut);
gNew = parseInt(gOut);
bNew = parseInt(bOut);

//alert(rNew);

//Work out the differences between them
var rDiff = rOut - rIn;
var gDiff = gOut - gIn;
var bDiff = bOut - bIn;

//divide by timer interval.
rBit = rDiff/timerSeg;
gBit = gDiff/timerSeg;
bBit = bDiff/timerSeg;

//Use set interval to call the colour fade;
timerInID = setInterval(me.fadeColorIn, 20);

};//initFadeColorIn



me.fadeColorIn = function(){
//timer interval may be floating point but the new rgb numbers must be integers
//alert(timerIn);
//alert("New rgb = " + rNew + "," + gNew + ","+ bNew );

rNew = parseInt(rNew - rBit);
gNew = parseInt(gNew - gBit);
bNew = parseInt(bNew - bBit);



newRGB = "rgb(" + rNew + "," + gNew + ","+ bNew + ")";
//
//alert(newRGB);
switch(me.colorID)
{
case 	"hometitlebody":
	document.getElementById(me.colorID).style.backgroundColor = newRGB;
	break;
case 	"hometitlespan":
	document.getElementById(me.colorID).style.color = newRGB;
	break;
}


if(counterIn >= 20)
{
	clearInterval(timerInID);
	//timerInID = 0;
	counterIn = 0;
	switch(me.colorID)
	{
	case 	"hometitlebody":
		document.getElementById(me.colorID).style.backgroundColor = "rgb(" + me.color + ")";
		break;
	case 	"hometitlespan":
		document.getElementById(me.colorID).style.color = "rgb(" + me.color + ")";
		break;
	}
	return;

}
counterIn++;


//alert(counterIn);
};//fadeColorOut

}//Fade object


function linkToForum(topicID){
var href = "http://housingstatisticsnetwork.co.uk/forum/viewtopic.php?f=2&t=" + topicID;

window.location.href= href;
}

function hoverPost(id){

document.getElementById(id).style.backgroundColor = "lavender";

}

function offPost(id){

document.getElementById(id).style.backgroundColor = "ivory";

}

function loadForumPosts(){
//creat request to load latest posts
	//alert("Loading posts");
	//request latest posts from forum
	var url = "hsn-latsetposts-ajax.php?";
	request.open("GET", url, true);
	request.onreadystatechange = printForumPosts;
	request.send(null);
}

function printForumPosts(){
if(request.readyState==4)
{
	if(request.status==200)

	{
		var response = request.responseText;
		var responseArray = response.split("#@|@#");

		var postTopicID; var postTopic; var postDate;  
		var postTime; var userName; var postContent;

		for(i=1;i<=5;i++)
		{
		
			//alert(responseArray.length);
			
			var postArray = responseArray[i-1].split("@|@");
			
			//alert(postArray.length);
			postTopicID = postArray[0];
			postTopic = postArray[1];
			postDate = postArray[2];
			postTime = postArray[3];
			userName = postArray[4];
			postContent = postArray[5];
			
			//alert(postTopicID + postTopic + postDate + postTime + userName + postContent );

			
			topicUrl = "http://housingstatisticsnetwork.co.uk/forum/viewtopic.php?f=2&t=" + postTopicID;


			//alert(topicUrl);
			//alert(i);
		

			document.getElementById("posttopic" + i).firstChild.nodeValue = postTopic;

			document.getElementById("posttopic" + i).setAttribute("href", topicUrl);
			document.getElementById("postdate" + i).firstChild.nodeValue = postDate;
			document.getElementById("posttime" + i).firstChild.nodeValue = "  " + postTime;
			document.getElementById("poster" + i).firstChild.nodeValue = userName;
			document.getElementById("post" + i).firstChild.nodeValue = postContent;
			
			

		}

	}
}

}




//BOOKMARKS AND BACK BUTTON FUNCTIONS

function newPageUrl(page){
//changes url to match selected page, if new url request is different to old one
window.location.hash = page;
//alert("new page url called " + page);

//add hash to page to match hash check in poll
newHash = "#" + page;
initUrlPoll(newHash);

//Update title
var section; var subTopic;
if(page.indexOf("/")!=-1)
{
hashArray = page.split("/");
section = hashArray[0];
subTopic = hashArray[1];
}
else
{
	section = page;
	subTopic = "";
}
	 
updateTitle(section, subTopic);
}



function checkUrlHash(){
//Grabs url on load and opens page that matches any pages in url
var hash = window.location.hash;
//alert("Check Url called, hash = " + hash);


//New variable selPage, remove # symbol at this point using substring
var selPage = hash.substring(1);
var hashArray = "";
//If hash contains slash, split and grab first in array, if not just take hash.
if(hash.indexOf("/")!=-1)
{
hashArray = selPage.split("/");
selPage = hashArray[0];
var subTopic = hashArray[1];
}
//alert("Selected Page =" + selPage);


switch (selPage)
{
case "":
	//just carry on as normal to home page
	//alert("to default home page");
	//setHomeSettings();
	clickMenuButton("home", "true");
	break;

case "home": 
	//just carry on as normal to home page
	//alert("to bookmarked home page");
	//setHomeSettings();
	clickMenuButton("home", "true");
	break;

case "launch":
	//Set the main buttons, set the section and check for and set the sub-topic if required.

	clickMenuButton(selPage, "true");
	if(hash.indexOf("/") != -1)
	{
		//use the subtopicfunction.
		subTopicId = "ls" + subTopic;
		clickSubButton(subTopicId, "true");
		//alert("Checking url, launch subtopic: " + subTopic);

	}
	break;

case "needs":
	//Set the main buttons, set the section and check for and set the sub-topic if required.

	clickMenuButton(selPage, "true");
	if(hash.indexOf("/") != -1)
	{
		//use the subtopicfunction.
		subTopicId = "ns" + subTopic;
		clickSubButton(subTopicId, "true");
		//alert("Checking url, launch subtopic: " + subTopic);

	}
	break;

case "steergroup":
	//Set the main buttons, set the section and check for and set the sub-topic if required.

	clickMenuButton(selPage, "true");
	if(hash.indexOf("/") != -1)
	{
		//use the subtopicfunction.
		subTopicId = "sg" + subTopic;
		clickSubButton(subTopicId, "true");
		//alert("Checking url, launch subtopic: " + subTopic);

	}

	break;

case "uksa":
	//Set the main buttons, set the section and check for and set the sub-topic if required.

	clickMenuButton(selPage, "true");
	if(hash.indexOf("/") != -1)
	{
		//use the subtopicfunction.
		subTopicId = "sa" + subTopic;
		clickSubButton(subTopicId, "true");
		//alert("Checking url, launch subtopic: " + subTopic);

	}

	break;


default:
	//All other sections, until they recieve sub-topics, will default through here.
	//alert("Checking url, and gone thru default switch. Sel Page: " + selPage);
	clickMenuButton(selPage, "true");

	break;

}


}//checkUrl()


function updateTitle(section, subTopic){
//use a switch to alter sub topics where the id isnt sufficient to use
//alert("update title called " + section + " " + subTopic);
switch(section)
{
case "about":
	section = "About HSN";
	break;
case "aims":
	section = "Aims & Objectives";
	break;
case "launch":
	section = "Launch Seminar";
	break;
case "needs":
	section = "Housing Needs Seminar";
	break;
case "steergroup":
	section = "Steering Group";
	break;
case "forum":
	section = "About Internet Forum";
	break;
case "uksa":
	section = "UK Statistics Authority";
	break;
case "future":
	section = "Future Seminars";
	break;
case "":
	section = "";
	break;
default:
//Make first letter capital
section = cnvrt2Upper(section);

}

switch(subTopic)
{
case "summary":
	subTopic = " - Summary";
	break;
case "report":
	subTopic = " - Report";
	break;
case "presentations":
	subTopic = " - Presentations";
	break;
case "profiles":
	subTopic = " - Speaker Profiles";
	break;
case "role":
	subTopic = " - Role Description";
	break;
case "members":
	subTopic = " - Members List";
	break;
case "meeting":
	subTopic = " - Meetings Schedule";
	break;
case "suggestions":
	subTopic = " - Topic Suggestions";
	break;
case "program":
	subTopic = " - Assessments Programme";
	break;
case "engagement":
	subTopic = " - Strengthening User Engagement";
	break;
case "prelim":
	subTopic = " - Preliminary Notice";
	break;
case "":
	subTopic = "";
	break;
default:
//Make first letter capital
subTopic = " - " + cnvrt2Upper(subTopic);

}


//Do same for section
section = cnvrt2Upper(section);


document.title = "The Housing Statistics Network - " + section + subTopic;

}


function cnvrt2Upper(str) {
//Imported function converts first letter to upper case
        return str.toLowerCase().replace(/\b[a-z]/g, cnvrt);
        function cnvrt() {
            return arguments[0].toUpperCase();
        }
}

var pollTimer = "";
function initUrlPoll(hash){
//This gets initialised every url is changed by html event, but picks up back/fwd button browser events
//and uses this to effectively mimick back button behaviour
//First clear the timer if one exists
recentHash = hash;
pollTimer = setInterval(pollHash, 1000);


}


var recentHash = "";
   function pollHash() {
  
     if (window.location.hash==recentHash) {
       return; // Nothing's changed since last polled.
     }
     recentHash = window.location.hash;
  
     // URL has changed, update the UI accordingly.
     checkUrlHash();
  
   }


function viewNews(item){
//Check whether open or not.
//Open the news div, display the content, change style, text and title of span
//Or close it.
//grab element from item, apply hover style
var spanId =  "newsview" + item;
var divId = "newsarticle" + item;
var divObj = document.getElementById(divId);
var spanObj = document.getElementById(spanId);

var openClose = spanObj.firstChild.nodeValue;
//alert("This news item, open close variable is " + openClose);

switch(openClose)
{
	case "View":
		spanObj.firstChild.nodeValue = "Close";
		divObj.style.display = "block";
		break;
	case "Close":
		spanObj.firstChild.nodeValue = "View";
		divObj.style.display = "none";
		break;
	
}
	
}

function hoverViewNews(item){
//grab element from item, apply hover style
var objId = "newsview" + item;
var eIQ = document.getElementById(objId);

eIQ.style.color = "firebrick";
	
}

function offViewNews(item){
//grab element from item, apply hover style
var objId = "newsview" + item;
var eIQ = document.getElementById(objId);

eIQ.style.color = "lightslategray";
	
}
