// Common Javascript for the Elliott Technologies Ltd. site

function toggle(zap) {
  if (document.getElementById) {
    var element = document.getElementById(zap).style;
    var agt = navigator.userAgent.toLowerCase();
    var group = 'table-row-group';
    if (agt.indexOf('msie') != -1 ) {
      group = "block";
    }
    if (element.display == group) {
      element.display = 'none';
    } else {
      element.display = group;
    } 
  return false;
  } else {
  return true;
  }
}

function show_all(base) {
  if (document.getElementById) {
    var agt = navigator.userAgent.toLowerCase();
    var group = 'table-row-group';
    if (agt.indexOf('msie') != -1 ) {
      group = "block";
    }
    for (i = base; (document.getElementById("block" + i) != undefined); i++) {
      var element = document.getElementById("block" + i).style;   
      element.display = group;
    }
  }
  return true;
}

function collapse_all(base) {
  if (document.getElementById) {
    for (i = base; (document.getElementById("block" + i) != undefined); i++) {
      var element = document.getElementById("block" + i).style;   
      element.display = 'none';
    }
  }
  return true;
}

/***
 * Function called on each page load
 */
function onLoad()
{
	// highlightNav();

} // onLoad

/***
 * Sets the cookie
 */
function set_cookie(cName, cVal)
{	
	// Set the cookie
	document.cookie = cName + "=" + cVal + ";";
	
} // set_cookie

/***
 * Gets the value of a cookie
 */
function get_cookie(cName)
{	
	var str = document.cookie;
	// alert(str);
	
	// See if the cookie is present
	if (str.indexOf(cName) != -1)
	{
		// Get the string from just the cookie name onwards
		str = str.substring(str.indexOf(cName)+cName.length);
		// alert(str);
		
		// If cookie empty return blank
		if ((str.length < 1) || (str.substring(0,1) == ';'))
		{
			return '';
		}
		
		// Not empty, 1st char must be = to be valid
		if (str.substring(0,1) != '=')
		{
			return '';
		}
		
		
		// Strip off any other cookie info if present
		if (str.indexOf(';') != -1)
		{
			str = str.substring(0, str.indexOf(';'));
		}
		
		// Strip off 1st char if it's an '='
		if (str.substring(0,1) == '=')
		{
			str = str.substring(1);
		}
		
		// alert(str);
		return str;
	}
		
	// Cookie not present
	return '';
	
} // get_cookie

/***
 * Switches the css styling for the site
 */
function switch_skin()
{
	if (get_cookie('skin') == 'combo')
	{
		// Set the cookie to default
		set_cookie('skin', 'panorama');
		
	} else {
	
		// Set cookie to alternative
		set_cookie('skin', 'combo');
	}
	
} // switch_skin()

/***
 * Gets the value relating to the name from
 * the name-value pair string, str, where
 * pairs are seperated by sep (& by default)
 */
function get_nvp_val(str, name, sep)
{
	alert(sep.type);
	
	// Use the default sep if not valid
	if (sep.type != String)
	{
		sep = '&';
	}
	
	// See if the name exists in the string
	if (str.indexOf(name) != -1)
	{
		
	}

} // get_nvp_val

/***
 * Adds the str class to the el element
 */
function addToClass(el, str)
{
	// Ensure a valid element was passed
	if (el)
	{
		// See if the str is NOT already present
		if (el.className.indexOf(str) == -1)
		{
			if (el.className == '')
			{
				el.className = str;	
			
			} else {
			
				el.className = el.className + ' ' + str;
			}
		} // if present
	
	} // if valid element
	
} // addToClass

/***
 * Removes the str class from the el
 * element if applicable.
 */
function removeFromClass(el, str)
{
	// Ensure a valid element was passed
	if (el)
	{
		// Get the class str
		var classStr = ' '+el.className+' ';
		// alert(classStr);
		
		// See if the str is present
		if (classStr.indexOf(str) != -1)
		{	
			classStr = classStr.replace(str, '').substring(1, -2);
			el.className = classStr;
		
		} // if present
	
	} // if valid element
	
} // removeFromClass

/***
 * Adds the select class to the currently active
 * navigation link
 */
function highlightNav()
{
	// Get the URL path sring (ie. part after domain)
	var path = window.location.pathname;
	
	// Get the nav element links
	var navPane = document.getElementById('navPane');
	var links = navPane.getElementsByTagName('A');
	
	// See if we're at the home page
	if ((path == '/') || (path == '/index.html') || (path == '/home'))
	{
		// Highlight the 1st (ie. 'home page') link
		highlightNavLink(links[0]);
		
	} else { // not the home page
	
		// Get the domain string and length
		var domain = '' + window.location;
		domain = domain.substr(0, domain.indexOf(path));
		var domLen = domain.length;

		// Get the position of the next '/' (ignoring the one at position 0) in the path
		var slashPosn = path.substr(1, path.length-1).indexOf('/');

		// See if a slash was found
		if (slashPosn >= 0)
		{
			// Get the part of the path only up until the next slash
			path = path.substr(0, slashPosn);
		}
		
		// Iterate through the remainder of the links (ie. not the first 'home page' link)
		for (var i=1; i<links.length; i++)
		{
			// Get the link's URL beyond the domain
			var href = links[i].href
			var linkPath = href.substr(domLen, href.length - domLen);
			
			// See if the link matches the current page
			if (linkPath.indexOf(path) == 0)
			{
				// Match therefore highlight the link
				highlightNavLink(links[i]);

				// Stop searching
				break;
			}

		} // eack link

	} // ifelse home page
	
} // highlightNav

/***
 * Adds the select class to the navigation
 * link passed and the UL that contains it
 */
function highlightNavLink(link)
{
	// Match therefore, deal with highlighting
	addToClass(link, 'selected');
	addToClass(link.parentNode, 'selected');
}

function selectNavLink(linkEl)
{
	// See if the link element is part of the nav pane
}

/***
 * Adds (or moves) the highlight nav image to a
 * position based on top & height
 */
function doNavImage(top, height)
{
	// alert('top='+top+' & height='+height);
	
	// Get the nav element links
	navPane = document.getElementById('navPane');
	images = navPane.getElementsByTagName('img');
	
	// Get the position the image should be at
	h = top + (height/2) - 15;
	
	// Check there are no images already
	if (images.length < 1)
	{
		// No images, therefore add it to the HTML
		imgStr = '<img src="code/images/style/circ_black.gif" style="position:absolute;left:220px;top:'+h+'px" />';
		// alert(imgStr);
		navPane.innerHTML = navPane.innerHTML + imgStr;
	} else {
	
		// Image already, therefore amend it (assumed it's the correct one)
		// alert(images[0].getAttribute('style')); // = '<img src="code/images/style/circ_black.gif" style="position:absolute;left:200px;top:'+h+'" />';
		
	} // ifelse no images

} // doNavImage
