window.addEvent('load', function(){
	var button = $('nav_' + menuSelected);
	highlightMenuItem(button, false);

	buttons.each(function(buttonID){
		var button = $(buttonID);
		button.addEvent('mouseenter', function(){
			highlightMenuItem(this, false);
		    })
		    .addEvent('mouseout', function(){
			    unhighlight(this);
			});
	    });

    });

//tpl.getRemoteTemplate('newTopSubMenuServices');

var services = new Hash();
services.set('Convertible' ,'Convertible Securities');
services.set('Credit', 'LSTA Xtract Bank Debt Review');
services.set('High Yield', 'High Yield Covenant Xtract');
services.set('QIB-Convert', 'Private Credit Agreements');

var tDate = new Date();
var lastInTime = 0;

var openSubMenu = '';

// removed nav_costs
var buttons = new Array('nav_home', 'nav_about', 'nav_services', 'nav_prop', 'nav_contact');

var subMenu;

function highlightMenuItem(button, openMenu){
    if (!button) return;

    buttons.each(function(otherButtonName){
	    var otherButton = $(otherButtonName);

	    if (otherButton != button
		&& otherButton.id != 'nav_' + menuSelected){
		unhighlight(otherButton);
	    }
	});

    if (button.src.match(/_off\.gif/)){
	button.src = button.src.replace("_off.gif", "_on.gif");
    }

    switch(button.id){
    case 'nav_home':
    case 'nav_about':
    case 'nav_prop':
    case 'nav_contact':
	closeSubMenu();
	openSubMenu = '';
	break;
    case 'nav_services':
	if (openMenu){
	    openSubMenu = button.id;
	    inSubMenu();
	    showSubMenu(button, 'newSubService.php');
	}
	break;
    case 'nav_costs':
	if (openMenu){
	    openSubMenu = button.id;
	    inSubMenu();
	    showSubMenu(button, 'newSubPricing.php');
	}
	break;
    }
}

function showSubMenu(button, servicePage){
    var append = true;
	
    if (!subMenu) {
	append = true;
	subMenu = new Element('div', {id: 'topSubMenu', 'class': 'topSubMenu'})
	    .setStyles({position: 'absolute'
		})
	    .addEvent('mouseleave', closeSubMenu);

	var serviceCount = 1;
	services.each(function(label, service){
		subMenu.adopt(new Element('div', {'class': "topSubMenuItem",
				id: "topSubMenu" + serviceCount })
		    .addEvent('mouseover', highlightItem)
		    .addEvent('mouseout', unhighlightItem)
		    .adopt(new Element('img', {id: "topSubMenu" + serviceCount + "Img",
				    'class': "topSubMenuImg",
				    'src': 'newImages/nav2_arrow.gif'
				    })
			)
		    .adopt(new Element('a', { href: servicePage + '?s=' + service })
			   .appendText(label)
			   )
		    )
		    
		    serviceCount++;
	    });
	
    }
    
    
    var top = (getTopPos(button) + 27) + 'px';
    subMenu.setStyles({top: top,
		left: getLeftPos(button),
		visibility: "visible",
		display:  'block'})
	.addEvent('mouseout', checkChild);

    if (append){
      $('box_services').adopt(subMenu);
      $('box_services').addEvent('mouseout', checkChild);
    }
}


function unhighlight(button){
    if (!button) return;

    if (button.id == 'nav_' + menuSelected) return;

    if (button.src.match(/_on\.gif$/)){
	button.src = button.src.replace("_on.gif", "_off.gif");
    }

}

function closeSubMenu(event){
    if (subMenu) {
	subMenu.style.visibility = 'hidden';
	subMenu.style.display = 'none';
    }

    openSubMenu = '';
}

function checkChild(event){
    var match = false;
    if (event.relatedTarget){
	match = matchParent(event.relatedTarget, subMenu);
    }
    else if (event.toElement){
	match = subMenu.contains(event.toElement);
    }

    if (!match){
	closeSubMenu();
    }


}

function matchParent(target, matchTarget){
    if (!target.parentNode) return false;
    if (target.parentNode == matchTarget) return true;
    return matchParent(target.parentNode, matchTarget);
}


function timeCloseSubMenu(){
    //  setTimeout('closeSubMenu()', 0);
}

function inSubMenu(){
    var date = new Date();
    lastInTime = date.getTime();
}

function highlightItem(e){
    new Event(e).stop();

    var img = $(this.id + 'Img');
    if (img) img.style.visibility = 'visible';
  
    this.className='topSubMenuItemHover';
}

function unhighlightItem(e){
    new Event(e).stop();

    var img = document.getElementById(this.id + 'Img');
    if (img) img.style.visibility = 'hidden';

    this.className = 'topSubMenuItem';

    checkCloseMenu(e);
    //  setTimeout('checkMenuOpen()', 1000);
}

function checkCloseMenu(event){
    if (isIE()) return;
  
    var subMenu = document.getElementById('topSubMenu');
    if (childOf(event.relatedTarget, subMenu)){
	//    alert('is child');
    }
    else {
	closeSubMenu();
    }

}

