// sets the class name for the menu item in focus
function setMenuItemClass(id)
	{
	// reset class names for the menu item <span>s
	var spans = document.getElementById('floater').getElementsByTagName('span');
	for (var i = 0; i < spans.length; i++)
		{
		spans[i].className = '';
		}
	
	// set the class name for the menu item in focus
	document.getElementById('tab-menu-'+id).className = 'focus';
	}


// sets the class name of the <h2> for the tab in focus
function setHeadingClass(id)
	{
	var headings = document.getElementById('tab-'+id).getElementsByTagName('h2');
	var heading = headings[0];
	heading.setAttribute('title', 'These options let you show and hide various parts of the right-hand panel.');
	heading.className = 'focus';
	
	if (id == 'nav')
		{
		heading.innerHTML = 'Navigation';
		}
	}


// hides and shows the <div>s and <hr>s
function hideHTML(toShow)
	{
	var toHide = document.getElementById('tabs').firstChild;
	while(toHide)
		{
		if ((toHide.nodeName.toLowerCase() == 'div') || (toHide.nodeName.toLowerCase() == 'hr'))
			{
			toHide.style.display = 'none';
			}
		toHide = toHide.nextSibling;
		} 
	
	// re-show the div in focus
	document.getElementById('tab-'+toShow).style.display = 'block';
	}


function setCookies(id)
	{
	var date;
	var now;
	var today;
	var expiredate;
	var expire;
	
	// if looking at the b-marks then set cookie
	if (id == 'bmarks')
		{
		date = new Date();
		now = date.getTime() / 1000;
		today = new Date();
		expiredate = new Date();
		expire = expiredate.setTime(today.getTime() + 1000*60*60*24*365);
		expire = expiredate.toGMTString();
		document.cookie = "last_bmarks_view="+now+"; expires="+expire+"; path=/; domain=1976design.com";
		}
	
	// if looking at the b-roll then set cookie
	if ((id == 'broll') && (newItemsBroll == 'yes'))
		{
		date = new Date();
		now = date.getTime() / 1000;
		today = new Date();
		expiredate = new Date();
		expire = expiredate.setTime(today.getTime() + 1000*60*60*24*365);
		expire = expiredate.toGMTString();
		document.cookie = "last_broll_view="+now+"; expires="+expire+"; path=/; domain=1976design.com";
		}
	}

// runs everything needed for each tab-swap
function setMenu(id)
	{
	hideHTML(id);
	setHeadingClass(id);
	setMenuItemClass(id);
	setCookies(id);
	}


// creates a menu item and sets the event listener for it
function createItem(sID, sText, sTitle)
	{
	var item = document.createElement('span');
	item.setAttribute('id', 'tab-menu-' + sID);
	item.setAttribute('title', sTitle);
	item.appendChild(document.createTextNode(sText));
	addEvent(item, 'click', function(){setMenu(sID);});
	return item;
	}


// creates the menu bar and sets the intial value for the tab to display
function createMenuBar()
	{
	// set text and title for the navigation menu item
	var navText = 'nv';
	var navTitle = 'View the main navigation menu';
	
	// set text and title for the blogmarks menu item
	var bmarksText = (newItemsBmark == "yes") ? 'bm*' : 'bm';
	var bmarksTitle = (newItemsBmark == "yes") ? 'View the blogmarks - new links have been added since your last visit' : 'View the blogmarks - a collection of misc links I thought you might like';

	// set text and title for the blogroll menu item
	var brollText = (newItemsBroll == "yes") ? 'br*' : 'br';
	var brollTitle = (newItemsBroll == "yes") ? 'View the blogroll - sites have been updated since your last visit' : 'View the blogroll - a selection of blogs I read on a regular basis';
	
	// create each menu item
	var itemNav = createItem("nav", navText, navTitle);
	var itemBmarks = createItem("bmarks", bmarksText, bmarksTitle);
	var itemBroll = createItem("broll", brollText, brollTitle);
	
	// create the containing menuBar div
	var menuBar = document.createElement("div");
	menuBar.setAttribute("id", "floater");
	
	// add menu items into the containing menuBar div
	menuBar.appendChild(itemNav);
	menuBar.appendChild(document.createTextNode('|'));
	menuBar.appendChild(itemBmarks);
	menuBar.appendChild(document.createTextNode('|'));
	menuBar.appendChild(itemBroll);
	document.getElementById('right').appendChild(menuBar);
	
	// set the default instance of the menuBar/tabs
	setMenu('nav');
	
	// the first time the page is loaded, highlight the navigation options with a little arrow on the default tab
	var headings = document.getElementById('tab-nav').getElementsByTagName('h2');
	var heading = headings[0];	
	heading.innerHTML += ' &#8594;';
	}

createMenuBar();