/**
 * Makes the top navigation bar and handles dynamic submenus
 *
 * @author		Colin Nolan
 * @created		04/05/10
 * @updated		25/03/11
 * @version		v2.0.0
 */
var TopNavigation=function(dataFile){var OVER_REACH=15;var navLinks;var tempOnMouseOver;var subMenu;var menuCell;(function(){try{var navData=XMLHandler.parse(dataFile);navLinks=navData.getElementsByTagName("link")}catch(e){throw e}})();this.makeDynamic=function(navBar){if(!navBar){throw"Could not make the navigation bar dynamic as 'navBar' is not defined"}var dataElements=navLinks;var linkElements=navBar.getElementsByTagName("a");styleTable(navBar);navBar.className="navBar";equalCells(navBar,true);for(var i=0;i<dataElements.length;i++){var navLink=dataElements[i];var text=navLink.getAttribute("name");var options=navLink.getElementsByTagName("option");var found=false;var j=0;while(j<linkElements.length&&!found){if(linkElements[j].innerHTML==text){found=true;var cell=linkElements[j].parentNode;setCellOnmouseover(cell,options,j==linkElements.length-1);linkCellToTag(cell)}j++}}};this.createNavigation=function(){var navBar=create();navBar.className="navBar";return navBar};var create=function(options){var table=document.createElement("table");var tbody=document.createElement("tbody");var tr=document.createElement("tr");var dataElements=!options?navLinks:options;var vertical=!options?false:true;var center=!options?true:false;for(var i=0;i<dataElements.length;i++){var navLink=dataElements[i];if(!navLink.getAttribute("hidden")){var a=document.createElement("a");var td=document.createElement("td");var url,text;if(vertical){text=navLink.childNodes[0].nodeValue;url=navLink.getAttribute("url")}else{text=navLink.getAttribute("name");var options=navLink.getElementsByTagName("option");if(navLink.getAttribute("noclick")){url="javascript:;"}else if(options.length==0){url=navLink.getAttribute("url")}else{url=options[0].getAttribute("url")}setCellOnmouseover(td,options,i==dataElements.length-1)}a.href=url;a.innerHTML=text;td.appendChild(a);linkCellToTag(td);tr.appendChild(td);if(vertical){tbody.appendChild(tr);tr=document.createElement("tr")}}}if(!vertical){tbody.appendChild(tr)}table.appendChild(tbody);equalCells(table,center);styleTable(table);return table};var addSubMenu=function(cell,options,lastCell){if(options.length==0){return false}if(subMenu){removeSubMenu()}tempOnMouseOver=cell.onmouseover;cell.onmouseover=function(){};var position=findPos(cell);var table=cell.parentNode.parentNode.parentNode;var tableCellWidth=table.clientWidth/table.getElementsByTagName("td").length;var cellHeight=table.clientHeight;var cellWidth=tableCellWidth*1.5;var table=create(options);table.style.top=position.y+cellHeight+"px";table.style.left=position.x-1+"px";table.style.position="absolute";table.style.zIndex=2147483647;table.style.width=cellWidth+"px";document.body.appendChild(table);if(lastCell){var leftPos=Number(table.style.left.replace("px",""));leftPos+=2;table.style.left=leftPos+(tableCellWidth-cellWidth)+"px"}table.className="subNavMenu";subMenu=table;menuCell=cell;document.onmousemove=manageMenus};var manageMenus=function(e){var cursor=new Object();if(navigator.appName=="Microsoft Internet Explorer"){cursor.x=event.clientX+document.body.scrollLeft;cursor.y=event.clientY+document.body.scrollTop}else{cursor.x=e.pageX;cursor.y=e.pageY}var limits=new Object();limits.x_min=Number(subMenu.style.left.replace("px",""))-OVER_REACH;limits.x_max=subMenu.clientWidth+limits.x_min+2*OVER_REACH;limits.y_min=Number(subMenu.style.top.replace("px",""))-OVER_REACH;limits.y_max=subMenu.clientHeight+limits.y_min+2*OVER_REACH;if(cursor.x<limits.x_min||cursor.x>limits.x_max||cursor.y<limits.y_min||cursor.y>limits.y_max){var limits=new Object();limits.x_min=findPos(menuCell).x;limits.x_max=menuCell.clientWidth+limits.x_min;limits.y_min=findPos(menuCell).y;limits.y_max=menuCell.clientHeight+limits.y_min;if(cursor.x<limits.x_min||cursor.x>limits.x_max||cursor.y<limits.y_min||cursor.y>limits.y_max){removeSubMenu()}}};var findPos=function(obj){var curleft=curtop=0;do{curleft+=obj.offsetLeft;curtop+=obj.offsetTop}while(obj=obj.offsetParent);return{x:curleft,y:curtop}};var setCellOnmouseover=function(cell,options,lastCell){cell.onmouseover=function(){addSubMenu(cell,options,lastCell)}};var equalCells=function(table,center){var cells=table.getElementsByTagName("td");var size=100/cells.length;for(var i=0;i<cells.length;i++){cells[i].style.width=size+"%";if(center){cells[i].style.textAlign="center"}}};var removeSubMenu=function(){document.body.removeChild(subMenu);document.onmousemove=function(){};menuCell.onmouseover=tempOnMouseOver;subMenu=false;menuCell=false;tempOnMouseOver=false};var styleTable=function(table){table.cellSpacing=0;table.cellPadding=0};var linkCellToTag=function(cell){cell.onclick=function(){window.top.location=this.getElementsByTagName("a")[0].href};cell.style.cursor="pointer"}};
