// ÑÊÐÈÏÒ ÏÅÐÅÊËÞ×ÅÍÈß ÑËÎÅÂ ÌÅÍÞØÊÈ
function selectPart(part){
	document.getElementById("ext_men_part_" + selectedPart).style.display = "none";
	document.getElementById("eml_" + selectedPart).className = "categoryUnselected";
	document.getElementById("ext_men_part_" + part).style.display = "block";
	document.getElementById("eml_" + part).className = "category_selected";
	selectedPart = part;
}
// ÑÊÐÈÏÒ ÓÏÐÀÂËÅÍÈß ÑËÎÅÌ ÌÅÍÞØÊÈ
var registredLayers = new Array();

function extMenu(layer_id)
{
	this.id				= layer_id;
	
	this.fTop			= 300;
	this.fLeft			= 100;
	this.lTop			= 300;
	this.lLeft			= 100;
	this.cTop			= 300;
	this.cLeft			= 100;
	this.lWidth			= 300;
	this.lHeight		= 300;
	this.cWidth			= 0;
	this.cHeight		= 0;
	this.fWidth			= 0;
	this.fHeight		= 0;
	this.cStep			= 0;
	this.cont_display	= "none";
	this.tl_visibility	= "hidden";
	
	this.step_timeout	= 30;
	this.step_count		= 8;
	this.state			= "closed"; // closed || running || opened
		
	this.showTimer		= 0;
	this.hideTimer		= 0;
	
	this.register		= false;
	this.realElement	= null;
	
	this.restoreFromObj = function(obj)
	{
		for(prop in obj){
			eval("this."+prop+" = obj."+prop);
		}
	}
	
	this.saveObj = function()
	{
		obj = registredLayers[this.cryptName(layer_id)]
		for(prop in obj){
			eval("obj."+prop+" = this."+prop);
		}
	}
	
	this.cryptName = function(nameStr)
	{
		var sum = 0;
		for(i = 0; i < nameStr.length;i++){
			sum += nameStr.charCodeAt(i);
		}
		return sum;
	}
	
	this.attachLayer =  function (layer_id)
	{
		registredLayers[this.cryptName(layer_id)] = this;
		this.realElement = document.getElementById(layer_id);
		this.register = true;
	}
	
	if(!this.register && layer_id){
		this.attachLayer(layer_id);
	}
	
	this.setParams = function(fTop, fLeft, fWidth, fHeight, lTop, lLeft, lWidth, lHeight, step_timeout, step_count)
	{
		this.fTop		= this.cTop		= fTop;
		this.fLeft		= this.cLeft	= fLeft;
		this.fWidth		= this.cWidth	= fWidth;
		this.fHeight	= this.cHeight	= fHeight;
		this.lTop		= lTop;
		this.lLeft		= lLeft;
		this.lWidth		= lWidth;
		this.lHeight	= lHeight;
		this.step_timeout	= step_timeout;
		this.step_count		= step_count;
		this.resetRealElement;
	}
	
	this.show = function(show, layer_id)
	{
		if(!this.register){			
			this.restoreFromObj(registredLayers[this.cryptName(layer_id)]);
		}
		show = (show) ? true : false;
		if(show && this.state == "opened") return false;
		if(show && this.hideTimer > 0) {clearTimeout(this.hideTimer)}
		if(!show && this.showTimer> 0 ) {clearTimeout(this.showTimer)}
		
		var d_width	 = Math.floor(( (show ? this.lWidth : this.fWidth) - this.cWidth) / (this.step_count - this.cStep));
		var d_height = Math.floor(( (show ? this.lHeight : this.fHeight) - this.cHeight) / (this.step_count - this.cStep));
		var d_top	 = Math.floor(( (show ? this.lTop : this.fTop) - this.cTop) / (this.step_count - this.cStep));
		var d_left	 = Math.floor(( (show ? this.lLeft : this.fLeft) - this.cLeft) / (this.step_count - this.cStep));
		
		this.cTop	+= d_top;
		this.cLeft	+= d_left;
		this.cWidth	+= d_width;
		this.cHeight+= d_height; 
		this.cStep++;
		
		this.cont_display = (this.cStep == this.step_count && show) ? "block" : "none";
		this.tl_visibility = (this.cStep == this.step_count && !show) ? "hidden" : "visible";
		
		this.resetRealElement();
		if(this.cStep < this.step_count){
			t_timer = setTimeout("outShow(" + show + ", '" + this.id + "')", this.step_timeout);
			if(show) {this.showTimer = t_timer} else{this.hideTimer = t_timer}
			this.state = "running";
		}
		else{
			this.cStep = 0;
			this.state = (show) ? "opened" : "closed";
			this.hideTimer = this.showTimer = 0;
		}
		this.saveObj();
	}
		
	this.resetRealElement = function()
	{
		if(this.realElement){
			with(this.realElement.style)
			{
				top = this.cTop;
				left = this.cLeft;
				width = this.cWidth;
				height = this.cHeight;
				visibility = this.tl_visibility;
			}
			// åñëè åñòü êîíòåéíåðíàÿ ÷àñòü
			if(document.getElementById(this.realElement.id+"_cont")){
				document.getElementById(this.realElement.id+"_cont").style.display = this.cont_display;
			}
		}
	}
}

// íåîáõîäèìûå âíåøíèå ôóíêöèè

function outShow(show, id)
{
	var o_menu = new extMenu();
	o_menu.show(show, id);
}

function getRegistredLayer(id)
{
	var resLayer = new extMenu();
	resLayer.restoreFromObj(registredLayers[resLayer.cryptName(id)]);
	return resLayer;
}

function mouseOutEvent(el, evt)
{
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	toObj = (evt.toElement) ? evt.toElement : ((evt.relatedTarget) ? evt.relatedTarget : null);
	if(!isAncestor(el, toObj))
	{
		lr = getRegistredLayer(el.id);
		lr.hideTimer = setTimeout("outShow(false,'" + el.id + "')", 500);
		lr.saveObj();
	}
	
}

function mouseOverEvent(el, evt)
{
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	fromObj = (evt.fromElement) ? evt.fromElement : ((evt.relatedTarget) ? evt.relatedTarget : null);
	if(!isAncestor(el, fromObj))
	{
		lr = getRegistredLayer(el.id);
		if(lr.cStep == 0){
			clearTimeout(lr.hideTimer);
			lr.hideTimer = 0;
			lr.saveObj();
		}
	}
		
}

function isAncestor(anc, chld)
{
	var p = chld;
	while ( p && (p = p.parentNode))
	{
		if(p == anc){
			return true;
		}
	}
	return false;
}

function mouseOverDiv(evt, id, offs_x, offs_y, w, h, right, bottom, x_fixed)
{
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	srcObj = (evt.srcElement) ? evt.srcElement : ((evt.target) ? evt.target : null);
	
	while( srcObj.id != ( id + "_head") && srcObj.parentNode )
		srcObj = srcObj.parentNode;
	
	lr = getRegistredLayer(id);
	if((lr.cStep == 0 && lr.hideTimer > 0) || lr.state == "opened")
	{
		clearTimeout(lr.hideTimer);
		lr.hideTimer = 0;
		lr.saveObj();
		return;
	}
	dX = srcObj.offsetLeft + ( right ? srcObj.offsetWidth : 0 );
	dY = srcObj.offsetTop + ( bottom ? srcObj.offsetHeight : 0 );
	p = srcObj;
	while((p = p.offsetParent) && p.nodeName != "BODY")
	{
		dY += p.offsetTop;
		dX += p.offsetLeft;
	}
	if(lr.state == "closed"){
		lr.setParams(dY, dX, ( x_fixed ? w : 0 ), 0, dY + offs_y, dX + offs_x, w, h, 60, 5);	
		lr.showTimer = setTimeout("outShow(true,'" + id + "')", 100);
	}
	else{
		outShow(true,id);
	}
	lr.saveObj();
}

function mouseOutDiv(id)
{
	lr = getRegistredLayer( id );
	if(lr.showTimer > 0 && lr.state == "closed"){
		clearTimeout(lr.showTimer);
	}
	else{
		lr.hideTimer = setTimeout("outShow(false,'" + id + "')", 500);
	}
	lr.saveObj();
}

function mouseOverEye(evt)
{
	mouseOverDiv( evt, "mark_menu_layer", 10, -150, 550, 450, true );
}

function mouseOutEye()
{
	mouseOutDiv( "mark_menu_layer" );
}