var menus = new Menus();

function Menus() {
var timeOn = null;
var menuDatas = new Array();
var currentMenu = new Array();

this.addMenus = function (level,props) {
var menuData = new MenuData(level,props);
for(i = 2; i<arguments.length; i++) {
menuDatas[arguments[i]] = menuData;
$("#"+arguments[i]).mouseover(function() {
showMenu(this);
}).mouseout(function() {
hideAllMenusDelay();
});
}
};

function MenuData(level, props) {
this.level = level;
if (props) {
this.hover = props.hover ? props.hover : "defaultHover";
this.vertical = props.vertical!=false;
this.top = props.top ? props.top : 0;
this.left = props.left ? props.left : 0;
this.bottom = props.bottom ? props.bottom : 0;
this.right = props.right ? props.right : 0;
this.justify = props.justify ? props.justify : "left";
} else {
this.hover = "defaultHover";
this.vertical = true;
this.top = 0;
this.left = 0;
this.bottom = 0;
this.right = 0;
this.justify = "left";
}
}

function showMenu(menu) {
var menuData = getMenuData(menu);
if (currentMenu[menuData.level] && currentMenu[menuData.level] != menu) {
menus.hideAllMenusFrom(menuData.level);
}

if (timeOn != null) {
clearTimeout(timeOn);
timeOn = null;
}

var subMenu = document.getElementById(getSubMenuName(menu));
if (subMenu) {
var subMenuStyle = subMenu.style;
var browser = getInnerSize();
var scollPos = getScrollPosition();
var menuPos = findPosition(menu);
var menuSize = getObjectSize(menu);
var subMenuW;
var subMenuH;
var loopcounter = 0;

menuSize.width += menuData.left + menuData.right;
menuSize.height += menuData.top + menuData.bottom;
menuPos.left += -menuData.left;
menuPos.top += -menuData.top;

if (menuData.vertical) {
do {
loopcounter++;

var x = menuPos.left;
subMenuW = getObjectWidth(subMenu);
var right = x + subMenuW;

if ((browser.width + scollPos.left < right) || menuData.justify == "right") {
var x2 = menuSize.width - subMenuW + menuData.left;
if (x + x2 >= 0) {
x += x2;
}
}

subMenuStyle.left = x+'px';

} while (subMenuW != getObjectWidth(subMenu)  && loopcounter < 5);

var y = menuPos.top;
subMenuH = getObjectHeight(subMenu);
var bottom = y + menuSize.height + subMenuH ;

if (browser.height + scollPos.top < bottom) {
y -= subMenuH;
} else {
y += menuSize.height;
}

if (y < scollPos.top) {
y = scollPos.top;
}

subMenuStyle.top = y+'px';
} else {
do {
loopcounter++;

var x = menuPos.left;
subMenuW = getObjectWidth(subMenu);
var right = x + subMenuW + menuSize.width;

if (browser.width + scollPos.left < right || menuData.justify == "right") {
x -= subMenuW + menuData.left;
} else {
x += menuSize.width;
}
subMenuStyle.left = x+'px';

} while (subMenuW != getObjectWidth(subMenu)  && loopcounter < 5);

var y = menuPos.top;
subMenuH = getObjectHeight(subMenu);
var bottom = y + menuSize.height + subMenuH;

if (browser.height + scollPos.top < bottom) {
y += menuSize.height - (subMenuH - 1);
}

if (y < scollPos.top) {
y = scollPos.top;
}
subMenuStyle.top = y+'px';

}
subMenuStyle.visibility="visible";

}
$(menu).addClass(getMenuData(menu).hover);
currentMenu[menuData.level] = menu;
}

function hideMenu(menu) {
$(menu).removeClass(getMenuData(menu).hover);
var subMenu = document.getElementById(getSubMenuName(menu));
if (subMenu) {
subMenu.style.visibility="hidden";
}
}

function hideAllMenusDelay() {
timeOn = setTimeout("menus.hideAllMenusFrom(0)",600)
}

this.hideAllMenusFrom = function (startLevel) {
for(i = startLevel; currentMenu[i]; i++) {
hideMenu(currentMenu[i]);
}
}

function getSubMenuName(menu) {
return menu.id + 'SubMenu';
}

function getMenuData(menu) {
return menuDatas[menu.id];
}

}
var _SR_;
if(_SR_ != null) _SR_.notify("menus.js");
