function loadMenus() {
    //removeSub();
    //loadMenuCSS();
}

function removeSub() {
    var obj;
    if (document.getElementById('page-main-nav-menu-secondary')) {
        obj = document.getElementById('page-main-nav-menu-secondary');
        obj.parentNode.removeChild(obj);
    }
}

var timers = new Array();
var boxen = new Array();

function menuOn(in_obj) {

    // get rid of all other boxes
    for (var box in boxen) {
        if (box.indexOf('menu_') != -1) {
            if (box != 'menu_' + in_obj.rel && boxen[box] == true) {
                if (document.getElementById(box)) {
                    menuOffComplete(box);
                }
            }
        }
    }

    // if the dropdown has not already been created, create it
    if (!document.getElementById('menu_' + in_obj.rel)) {
        menuCreate(in_obj);
    }

    // clear out all existing timers
    for(var timerID in timers) {
        if (timerID.indexOf('menu_') != -1) clearTimeout(timers[timerID]);
    }
}

function menuCreate(in_obj) {
    var pos = findPos(in_obj);

    var menuDiv = document.createElement('div');
    menuDiv.setAttribute('id','menu_' + in_obj.rel);
    menuDiv.className = 'menuDiv';
    menuDiv.style.left = pos[0] + 'px';
    menuDiv.style.top = (pos[1] + 15) + 'px';
    menuDiv.onmouseover = function () { menuOn(in_obj); }
    menuDiv.onmouseout = function () { menuOff(in_obj); }

    var menuDivUl = document.createElement('ul');
    for(var i=0;i<menu[in_obj.rel].length;i++) {
        var menuDivUlLi = document.createElement('li');
        if (menu[in_obj.rel][i]['disabled'] != undefined) continue;
        //if (menu[in_obj.rel][i]['indent'] != undefined) menuDivUlLi.className = 'menuDivIndent';

        var menuDivUlLiA = document.createElement('a');
        menuDivUlLiA.href = menu[in_obj.rel][i]['link'];
        menuDivUlLiA.title = menu[in_obj.rel][i]['title'];
		menuDivUlLiA.target = menu[in_obj.rel][i]['target'];

        // for tertiary menus
        if (menu[in_obj.rel][i]['rel'] != undefined) {
            menuDivUlLiA.setAttribute('rel', menu[in_obj.rel][i]['rel']);
            menuDivUlLiA.onmouseover = function () { menuTertiaryOn(this); }
            menuDivUlLiA.onmouseout = function () { menuTertiaryOff(this); }
        }

        menuDivUlLiA.appendChild(document.createTextNode(menu[in_obj.rel][i]['title']));
        menuDivUlLi.appendChild(menuDivUlLiA);

        menuDivUl.appendChild(menuDivUlLi);
    }
    menuDiv.appendChild(menuDivUl);
    document.body.appendChild(menuDiv);

    boxen['menu_' + in_obj.rel] = true;
}

function menuOff(in_obj) {
    timers['menu_' + in_obj.rel] = setTimeout('menuOffComplete("menu_' + in_obj.rel+'")',250);
}

function menuOffComplete(in_obj_id) {
    var obj = document.getElementById(in_obj_id);
    obj.parentNode.removeChild(obj);
    boxen[in_obj_id] = null;
}

function menuTertiaryOn(in_obj) {
    // if the dropdown has not already been created, create it
    if (!document.getElementById('menu_' + in_obj.rel)) {
        menuTertiaryCreate(in_obj);
    }
}

function menuTertiaryOff(in_obj) {

}

function menuTertiaryCreate(in_obj) {

    var menuDivUl = document.createElement('ul');
    menuDivUl.id = 'menu_' + in_obj.rel;
    menuDivUl.onmouseover = function () { menuTertiaryOn(in_obj); }
    menuDivUl.onmouseout = function () { menuTertiaryOff(in_obj); }
    for(var i=0;i<menu[in_obj.rel].length;i++) {
        var menuDivUlLi = document.createElement('li');
        if (menu[in_obj.rel][i]['disabled'] != undefined) continue;

        var menuDivUlLiA = document.createElement('a');
        menuDivUlLiA.href = menu[in_obj.rel][i]['link'];
        menuDivUlLiA.title = menu[in_obj.rel][i]['title'];
        menuDivUlLiA.appendChild(document.createTextNode(menu[in_obj.rel][i]['title']));
        menuDivUlLi.appendChild(menuDivUlLiA);
        menuDivUl.appendChild(menuDivUlLi);
    }

    // append the menu after the object we were passed
    in_obj.parentNode.insertBefore(menuDivUl,in_obj.nextSibling);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

document.addEvent(window,'load',loadMenus,false);

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1) return false;
   else return true;
}

// the menu
var menu = new Array();
menu['about'] = new Array();
menu['about'][0] = {'title': 'About Inogen','link': '/About_Inogen/'};
menu['about'][1] = {'title': 'Story of Mae','link': '/Story_of_Mae/'};
menu['about'][2] = {'title': 'Mission/Values','link': '/Mission_Values/'};
menu['about'][3] = {'title': 'Board of Directors','link': '/Board_of_Directors/','rel':'leadership'};
menu['about'][4] = {'title': 'Awards/Recognition','link':'/Awards_Recognition/'};
menu['about'][5] = {'title': 'Press','link':'http://inogenone.com/Press/','target':'_blank'};
menu['about'][6] = {'title': 'Corp. Social Responsibility','link':'Corp__Social_Responsibility','disabled': true};

menu['prod'] = new Array();
menu['prod'][0] = {'title': 'Inogen One','link': '/Products/'};

menu['rd'] = new Array();
menu['rd'][0] = {'title': 'Product Development','link': '/Product_Development/'};
menu['rd'][1] = {'title': 'Outcomes/Clinical Research','link': '/Outcomes_Clinical_Research/'};
menu['rd'][2] = {'title': 'Scientific Advisory Board','link': '/Scientific_Advisory_Board/'};


menu['press'] = new Array();
menu['press'][0] = {'title': 'Backgrounder','link': '/Backgrounder/','disabled': true};
menu['press'][1] = {'title': 'News Releases','link': '/press/'};
menu['press'][2] = {'title': 'Contacts','link': '/Contacts/'};

menu['careers'] = new Array();
menu['careers'][0] = {'title': 'Current Openings','link': 'http://inogen.iapplicants.com/','target':'_blank'};



