// -----------------------------------------------------------------------------------------------------------
// Sliding Menu
// Author - Gushan Valentin, e-mail: valentin.gushan@gmail.com
// -----------------------------------------------------------------------------------------------------------

function SlidingMenu (InstanceName, VerticalOffset, HorizontalOffset) {
	this.instanceName= InstanceName
    this.VerticalOffset = VerticalOffset;
    this.HorizontalOffset = HorizontalOffset;
	this.heights = new Array();
	this.widths = new Array();
	this.isOver = new Array();
	this.slideTime = 100;
	this.tick = 20;
	this.slideTimeout = 0;
	this.closeTimeout = 100;
	this.source = new Array();
}

SlidingMenu.prototype.getRightBorderOffset = function getRightBorderOffset()
{
	var x = 0;
	if (self.pageXOffset) // i.e. mozilla or google chrome
	{
		x += self.pageXOffset - 50;
	}
	else if (IE) // IExplorer
	{
		x = document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.clientWidth;
	}

    if (IE) x += document.body.offsetWidth;
	else x += window.innerWidth;
	
	return x + 20;
}
    
// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.getElement = function getElement(objName)
{
	return "document.getElementById('" + objName + "')";
}

// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.getAbsoluteTop = function getAbsoluteTop(o) {
	oTop = o.offsetTop;
	while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
}

// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.getAbsoluteLeft = function getAbsoluteLeft(o) {
	oLeft = o.offsetLeft;
	while(o.offsetParent != null) {
		oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}
	return oLeft;
}

// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.getMenuHeight = function getMenuHeight(menuId)
{
	return eval(this.getElement(menuId)).clientHeight;
}

// -----------------------------------------------------------------------------------------------------------
// 
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.memMenuHeight = function memMenuHeight(menuId)
{
	this.heights[menuId] = eval(this.getElement(menuId)).clientHeight;
}

// -----------------------------------------------------------------------------------------------------------
// 
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.memMenuWidth = function memMenuHeight(menuId)
{
	this.widths[menuId] = eval(this.getElement(menuId)).clientWidth;
}

// -----------------------------------------------------------------------------------------------------------
// изменить высоту менюшки, да так чтобы контент был выровнен по нижнему краю
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.setMenuHeight = function setMenuHeight(menuId, height)
{
	if (isNaN(height)) return;
	eval(this.getElement(menuId) + ".getElementsByTagName('table')[0].style.top = '-" + (this.heights[menuId] - height) + "px'");
	eval(this.getElement(menuId) + ".style.height = '" + height + "px'");
}

SlidingMenu.prototype.getSource = function getSource(menuId)
{
	return this.source[menuId];
}

// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.showMenu = function showMenu(objSrc, menuId)
{
	var left = this.getAbsoluteLeft(objSrc);
	var top = this.getAbsoluteTop(objSrc);
	//
	try { clearTimeout(eval("timerOut" + menuId)); } catch(err) {}
	//
	this.isOver[menuId] = true;
	//
	if (!this.source[menuId]) 
	{
		this.source[menuId] = objSrc;
		this.memMenuHeight(menuId);
		this.memMenuWidth(menuId);
		eval(this.getElement(menuId) + ".getElementsByTagName('table')[0].onmouseover = function() { " + this.instanceName + ".showMenu(" + this.instanceName +".getSource('" + menuId + "'), '" + menuId + "'); }");
		eval(this.getElement(menuId) + ".getElementsByTagName('table')[0].onmouseout = function() { " + this.instanceName + ".hideMenu('" + menuId + "'); }");
		this.setMenuHeight(menuId, 0);
	}
	//
	var x = left + this.HorizontalOffset;
	var width = this.widths[menuId];
	if (x + width > this.getRightBorderOffset()) x = left + objSrc.clientWidth + this.HorizontalOffset + 6 - width;
	//
	eval(this.getElement(menuId) + ".style.left = '" + x + "px'");
	eval(this.getElement(menuId) + ".style.top = '" + (top + this.VerticalOffset) + "px'");
	eval(this.getElement(menuId) + ".style.width = '" + width + "px'");
	eval(this.getElement(menuId) + ".style.zIndex = 100");
	eval(this.getElement(menuId) + ".style.visibility = 'visible'");
	//
	thisObj = this;
	eval("timerIn" + menuId + " = setTimeout(function() { thisObj.slide(menuId, '" + thisObj.instanceName + "'); }, thisObj.slideTimeout);");
}

// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.slide = function slide(menuId, instanceName)
{
	thisObj = eval(instanceName);
	if (!thisObj.isOver[menuId]) return;
	//
	tickCount = thisObj.slideTime / thisObj.tick;
	inc = thisObj.heights[menuId] / tickCount;
	nextHeight = thisObj.getMenuHeight(menuId) + inc;
	if (nextHeight > thisObj.heights[menuId]) nextHeight = thisObj.heights[menuId];
	thisObj.setMenuHeight(menuId, nextHeight);
	if (nextHeight < thisObj.heights[menuId]) 
		eval("timerIn" + menuId + " = setTimeout(function() { thisObj.slide(menuId, '" + instanceName + "'); }, thisObj.tick);");
}

// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.hideMenu = function hideMenu(menuId)
{
	try { clearTimeout(eval("timerIn" + menuId)); }	catch(err) {}
	this.isOver[menuId] = false;
	eval(this.getElement(menuId) + ".style.zIndex = 0");
	thisObj = this;
	eval("timerOut" + menuId + " = setTimeout(function() { thisObj.slideRevert(menuId, '" + thisObj.instanceName + "'); }, this.closeTimeout);");
}

// -----------------------------------------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------------------------------------
SlidingMenu.prototype.slideRevert = function slideRevert(menuId, instanceName)
{
	thisObj = eval(instanceName);
	if (thisObj.isOver[menuId]) return;
	if (!thisObj.heights[menuId]) return;
	//
	tickCount = thisObj.slideTime / thisObj.tick;
	dec = thisObj.heights[menuId] / tickCount;
	nextHeight = thisObj.getMenuHeight(menuId) - dec;
	//
	if (nextHeight < 0) nextHeight = 0;
	thisObj.setMenuHeight(menuId, nextHeight);
	//
	if (nextHeight > 0) 
	{
		eval("timerOut" + menuId + " = setTimeout(function() { thisObj.slideRevert(menuId, '" + instanceName + "'); }, thisObj.tick);");
	} 
	else
	{
		eval(thisObj.getElement(menuId) + ".style.left = '-1000px'");
		eval(thisObj.getElement(menuId) + ".style.top = '-100px'");
		eval(thisObj.getElement(menuId) + ".style.visibility = 'hidden'");
		thisObj.setMenuHeight(menuId, 0);
	}
}

