//  *************************************************************************************************************
//	FUNCTIONS TO POSITION, DISPLAY, AND POPULATE THE DROP-DOWN MENUs
//  *************************************************************************************************************
function writeNavElements() {
	var s = '<div id="dd_menu"></div>';
		s += '<iframe id="dd_menu_shim" src="javascript:false;" scrolling="no" frameborder="0" style="position:absolute; top:0px; left:0px; visibility:hidden;"></iframe>';
	
	document.write(s);
}

function hideImgs() {
	return false	
}

function clearHide() {
	clearTimeout($DD_TIMER);
}

function startDDHide() {
	if (document.getElementById("dd_menu").style.visibility == 'visible') {
		$DD_TIMER = setTimeout('hideDD()', 500);
	}
	else
		hideDD();
}

function hideDD() {		
	if ($DD_OVER != $NAV_SELECTED)
		tabnavRollOver(document.getElementById($DD_OVER));
	
	$DD_OVER = null;
	
	document.getElementById("dd_menu").style.visibility = 'hidden';
	document.getElementById("dd_menu_shim").style.visibility = 'hidden';
	
	clearHide();
}

function showDD(domRef, menuWidth) {	
	// reset the dd menu
	if ($DD_OVER != null)
		hideDD();
	
	// clear the timeout
	clearHide();
		
	$DD_OVER = domRef.id;
	
	// change the tab
	if ($DD_OVER != $NAV_SELECTED)
		tabnavRollOver(document.getElementById(domRef.id));
	
	var dd = document.getElementById('dd_menu');
	var ddi = document.getElementById('dd_menu_shim');
	var dd_pos = getDDPosition(domRef);
	
	// set the positioning
	dd.style.left = dd_pos._x + "px";
	dd.style.top = dd_pos._y + "px";
	
	// set the width
	dd.style.width = menuWidth + "px";

	
	// determine if there are any elements in the array
	var linkList = concatNavElements(domRef.id);
	
	if (linkList != false) {
		// replace the innerHTML
		dd.innerHTML = linkList;
		
		dd.style.visibility = "visible";
		
		// now resize/reposition the iframe
		ddi.style.left = dd_pos._x + "px";
		ddi.style.top = dd_pos._y + "px"; 
		ddi.style.width = dd.offsetWidth;
		ddi.style.height = dd.offsetHeight;
		ddi.style.visibility = "visible";
	}
}

function getDDPosition(domRef) {
	var navPosObj = getPosition(domRef);
	var xOffset = -4;
	var yOffset = 9;
	
	// catch any exceptions to the standard offsets
	switch (domRef.id) {
		case "nav_solutions":
			xOffset = -1;
			break;
	}
	
	var dd_x = navPosObj._x + xOffset;
	var dd_y = navPosObj._y + domRef.height + yOffset;
	
	return {_x:dd_x, _y:dd_y};
}

function getPosition(domRef) {
	var xPos = 0;
	var yPos = 0;
	var domObj = domRef;
	
	while (domObj.tagName.toLowerCase() != 'body') {
		xPos += domObj.offsetLeft;
		yPos += domObj.offsetTop;
		
		domObj = domObj.offsetParent;	
	}
	
	return {_x:xPos, _y:yPos}
}

function concatNavElements(elementID) {
	var navArrayObj;
	var i = 0;
	var str = "";
	
	// get the nav array object that corresponds to the current element
	var i = 0;
	var loopLength = navArray.length;
	for (var i=0; i < loopLength; i++) {
		var _item = navArray[i];
		if (elementID.indexOf(_item.label) != -1) {
			navArrayObj = _item;
			break;
		}
		
	}
	
	// now that we have that, loop through the elements and create the string
	var a1 = '<a href="';
	var a2 = '" onmouseover="clearHide();" onmouseout="startDDHide();">';
	var a3 = '</a>';
	
	loopLength = navArrayObj.navElements.length;
	
	// if there are no sub nav items, return false
	if (loopLength < 1)
		return false
	
	for(i=0; i < loopLength;i++) {
		var _item = navArrayObj.navElements[i];
		var a_open = a1 + _item.url;
		
		if (i == 0)
			a_open += '" class="first';
		else if (i == loopLength-1)
			a_open += '" class="last';
		
		// JWP 7.1.6 - Added the ability to process segments within a menu
		if (_item.label.indexOf("XX-") > -1)
		{
			a_open += '" style="color: black; background-color: #E3ECF7';
			a_open += a2 + '<b>';
			a_open += _item.label.replace(/XX-/,"") + '</b>';
			str += a_open + a3;
		}
		else        // no segments, so process as normal
		{
			a_open += a2;
			str += a_open + _item.label + a3;
		}
	}
	
	return str
}

var navIdArray = ["nav_solutions", "nav_products", "nav_services", "nav_support", "nav_partners"];

// stores a reference to the section; null is home
var $NAV_SELECTED = null;
// stores drop down over ref
var $DD_OVER = null;
// reference to the hide timer
var $DD_TIMER;

function addNavObj(nLabel, nURL) {
	this.navElements.push(new makeNavObj(nLabel, nURL));
}

function makeMenuObj(mLabel) {
	this.navElements = new Array();
	this.label = mLabel;
	this.addElement = addNavObj;
}

function makeNavObj(nLabel, nURL) {
	this.label = nLabel;
	this.url = nURL;
}

// you need to keep this variable living because it is used by the ddmenu
var navArray = new Array();

//
// here is a function that will expose this js to the server-side
// ssArray is an array that contains the navigation structure as follows: 
// ssArray[n][0]	String		The label/reference of the menu object.	
// ssArray[n][...]	Object(s)	Any other arguments passed should be objects that have 2 properties:
//									label (String) 
//									URL (String)
//
// example: ssArray[n] = ['solutions', {label:'Broadband 1', url:'#'}]
//
function buildNavigation(ssArray) {
	if (!ssArray.length) {
		alert('There was an error generating the menu system.  Please report this to Telect site personnel.  Thank you!');
	}
		
	var i = 0;
	var loopLength = ssArray.length;
	for (i; i < loopLength; i++) {
		var curItem = ssArray[i];
		var curNavArrayIndex = navArray.length;
		
		// make new menu obj and add to array
		navArray[curNavArrayIndex] = new makeMenuObj(curItem[0]);
		
		// if there are no items to add, then just move on to the next menu item
		if (curItem.length == 1)
			continue
		
		// otherwise, create children
		var j = 1;
		for (j; j < curItem.length; j++) {
			var curChild = curItem[j];
			navArray[curNavArrayIndex].addElement(curChild.label, curChild.url);			
		}
	}
}

//var test_ssArray = new Array();
//	test_ssArray[0] = ['solutions', 
//				  {label:'XX-Telecom', url:'#'},
//				  {label:'Fttx Solutions', url:'#'},
//				  {label:'Central Office', url:'#'},
//				  {label:'Local Loop', url:'#'},
//				  {label:'XX-Enterprise IT', url:'#'},
//				  {label:'Datacenter', url:'#'},
//				  {label:'VoIP', url:'#'},
//				  {label:'Campus Connect', url:'#'},
//				  {label:'XX-Commercial / Residential', url:'#'},
//				  {label:'Last-Mile Fiber', url:'#'},
//				  {label:'Neighborhood Connect', url:'#'},
//				  {label:'CATV', url:'#'}
//				  ];
//	test_ssArray[1] = ['products', 
//				  {label:'Cables, Connectors, Accessories', url:'#'},
//				  {label:'Cable Management Systems', url:'#'},
//				  {label:'Copper Connectivity & Patching Bays/Systems', url:'#'},
//				  {label:'Equipment Cabinets, Racks, & Hardware', url:'#'},
//				  {label:'Media Converters/Extenders', url:'#'},
//				  {label:'Multiplexers', url:'#'},
//				  {label:'Optical Connectivity & Patching Bays/Systems', url:'#'},
//				  {label:'Power Distribution, Protection, & Management', url:'#'}
//				  ];
//	test_ssArray[2] = ['services', 
//				  {label:'Systems Integration', url:'#'},
//				  {label:'Customized Solutions', url:'#'},
//				  {label:'Installations', url:'#'}
//				  ];
//	test_ssArray[3] = ['support', 
//				  {label:'Technical', url:'#'},
//				  {label:'Documents', url:'#'},
//				  {label:'Knowledge Base', url:'#'},
//				  {label:'Tools', url:'#'},
//				  {label:'FAQs', url:'#'},
//				  {label:'Podcasts', url:'#'}
//				  ];
//	test_ssArray[4] = ['partners', 
//				  {label:'Distribution Partners', url:'#'},
//				  {label:'Become a Partner', url:'#'},
//				  {label:'Partner Programs', url:'#'}
//				  ];
//	
// here is where you would make the call to build the menu system and replace the test_ssArray with a server side array
//buildNavigation(test_ssArray);