var menu3_access;
var menu3_last_active;
var old_bg;
function menu3( new_settings, new_uniq )
{	// default configuration
	this.conf =
	[
		// level 0
		{			'close_by': 'visibility',
			'expand_delay': 0,
			'collapse_delay': 300,
			'left': -2,
			'top': -5,
			'drop_style': 'vert', // can be vert, hor or line
			'align': 'left', // can be left, right or center
			'valign': 'bottom', // can be top or bottom
			'effects': 0,
			'shade': 1,
			'shade_left': -1,
			'shade_top': -1,
			'shade_color': '#000000'
		},
		// level 1
		{			'close_by': 'visibility',
			'expand_delay': 0,
			'collapse_delay': 300,
			'left': -60,
			'top': 6,
			'drop_style': 'hor', // can be vert or hor or line
			'align': 'right', // can be left, right
			'valign': 'top', // can be vert, hor or line
			'effects': 0,
			'shade': 1,
			'shade_left': -1,
			'shade_top': -1,
			'shade_color': '#000000'
		}
	];

	this.uniq_conf =
	{
	    "spec_hor_left":
	    {	    	'close_by': 'visibility',
			'expand_delay': 0,
			'collapse_delay': 300,
			'left': 35,
			'top': 6,
			'drop_style': 'hor', // can be vert or hor or line
			'align': 'left', // can be left, right
			'valign': 'top', // can be vert, hor or line
			'effects': 0,
			'shade': 1,
			'shade_left': -1,
			'shade_top': -1,
			'shade_color': '#000000'	    },
	    "spec_vert_right":
	    {
	    	'close_by': 'visibility',
			'expand_delay': 0,
			'collapse_delay': 300,
			'left': +2,
			'top': -5,
			'drop_style': 'vert', // can be vert, hor or line
			'align': 'right', // can be left, right or center
			'valign': 'bottom', // can be top or bottom
			'effects': 0,
			'shade': 1,
			'shade_left': -1,
			'shade_top': -1,
			'shade_color': '#2a73ad'
	    }
	};
	// set new menu configuration
	// if needed    if( new_settings )
    {    	this.conf = new_settings;    }

    // set new menu unique configuration
	// if needed
    if( new_uniq )
    {
    	this.uniq_conf = new_uniq;
    }

    // list of the opened items
    this.o_pool = new Array(10);
    // opened level
    this.o_level = 0;

    // assigh methods and event handlers
    this.expand      = menu3_expand;
	this.collapse    = menu3_collapse;
	this.standby     = menu3_standby;

	this.onmouseover = menu3_onmouseover;
	this.onmouseout  = menu3_onmouseout;

	this.getx = menu3_getx;
	this.gety = menu3_gety;

	this.getlevel = menu3_getlevel;

	this.inArray = menu3_inArray;

	this.t_collapse = null;
	this.t_expand = null;

    // save instance
	menu3_access = this;
}

function menu3_expand( player, pid, id, spec_id )
{    if( this.t_collapse )
    {    	return;    }

    if( this.inArray( this.o_pool, pid ) )
    {    	// close all childs after the parent
    	// of the opened element
    	this.collapse( pid );
    }
    else
    {    	// close all
    	// if new branch        this.collapse();    }

    if( !document.getElementById(id) )
    {    	return;    }

    if( this.o_level == 0 )
    {    	document.getElementById(player).style.backgroundImage = "url('" + site_content_url + "/public_stuff/images/m_b22.gif')";
    	menu3_last_active = player;    }

   	// add opened element to pool
   	this.o_pool[this.o_level] = id;
    this.o_level++;

    // open new branch
    // set its position
    var el_x = this.getx( document.getElementById( player ) );
    var el_y = this.gety( document.getElementById( player ) );
    var el_w = $('#'+player).width();
    var el_h = $('#'+player).height();

    // get configuration list
    var cur_conf = (spec_id && this.uniq_conf[spec_id]) ? this.uniq_conf[spec_id] : this.conf[this.o_level-1];

    // if open vertical menu
    if( cur_conf['drop_style'] == 'vert' )
    {    	if( cur_conf['align'] == 'right' )
    	{
    	    var show_x = el_x + el_w + cur_conf['left'] - $('#'+id).width();    	}
    	else if( cur_conf['align'] == 'left' )
    	{    		var show_x = el_x + cur_conf['left'];    	}
    	else
    	// center
    	{            var show_x = el_x + el_w/2 + cur_conf['left'] - ($('#'+id).width()/2);
    	}

    	var show_y = el_y + cur_conf['top'];
    	if( cur_conf['valign'] == 'bottom' )
    	{
    		show_y += el_h;
    	}

    	$('#'+id).css('left',show_x+'px');
        $('#'+id).css('top',show_y+'px');
    }
    // if open horizontal menu
    else if( cur_conf['drop_style'] == 'hor' )
    {    	if( cur_conf['align'] == 'right' )
    	{        	var show_x = el_x + el_w + cur_conf['left'];
        }
        // left
        else
        {        	var show_x = el_x + cur_conf['left'] - $('#'+id).width();        }

    	var show_y = el_y + cur_conf['top'];
    	if( cur_conf['valign'] == 'bottom' )
    	{
    		show_y += el_h;
    	}

    	$('#'+id).css('left',show_x+'px');
        $('#'+id).css('top',show_y+'px');    }
    // line drop-down
    else if( cur_conf['drop_style'] == 'line' )
    {        // do not count position for line drop-down
        // it is static    }

    // apply show method
    if( cur_conf['close_by'] == 'visibility' )
	{
		$('#'+id).css('visibility','visible');
	}
	else if( cur_conf['close_by'] == 'display' )
	{
		$('#'+id).css('display','');
	}
	else
	{
		$('#'+id).css('visibility','visible');
	}

    if( this.o_level > 1 && cur_conf['shade'] == 1 )
    {    	$('#'+id).dropShadow({left: cur_conf['shade_left'], top: cur_conf['shade_top'], color: cur_conf['shade_color']});
    }
}

function menu3_collapse( id )
{	// TODO: find items with spec_id
	// and apply their configuration	clearTimeout( this.t_expand );

	if( !id || !this.inArray( this.o_pool, id ) )
	{		// clear all
		for( i = 0; i < this.o_level; i++ )
		{			$('#'+this.o_pool[i]).removeShadow();			if( this.conf[i]['close_by'] == 'visibility' )
			{				$('#'+this.o_pool[i]).css('visibility','hidden');
			}
			else if( this.conf[i]['close_by'] == 'display' )
			{
				$('#'+this.o_pool[i]).css('display','none');
			}
			else
			{
				$('#'+this.o_pool[i]).css('visibility','hidden');
			}		}

		this.o_level = 0;

        if( menu3_last_active )
        {        	document.getElementById(menu3_last_active).style.backgroundImage = "";
        	menu3_last_active = 0;        }
	}
	else
	{		// clear only the childs of the element		var close_after = 0;
		var new_o_level = 0;		for( i = 0; i < this.o_level; i++ )
		{			if( !close_after && this.o_pool[i] != id )
			{				continue;			}
			else if( !close_after )
			{				new_o_level = i + 1;				close_after = 1;

				// leave selected element opened
				continue;			}

            $('#'+this.o_pool[i]).removeShadow();
			if( this.conf[i]['close_by'] == 'visibility' )
			{
				$('#'+this.o_pool[i]).css('visibility','hidden');
			}
			else if( this.conf[i]['close_by'] == 'display' )
			{
				$('#'+this.o_pool[i]).css('display','none');
			}
			else
			{				$('#'+this.o_pool[i]).css('visibility','hidden');			}
		}

		this.o_level = new_o_level;
	}

	// erase pool data
	// of the closed elements
	for( i = this.o_level; i < this.o_pool.length; i++ )
	{		this.o_pool[i] = '';	}
}

function menu3_onmouseover( player, pid, id, spec_id )
{	if( !spec_id )
	{		spec_id = '';	}
	if( player && pid && id )
	{		clearTimeout(this.t_collapse);
	    this.t_collapse = null;
    	clearTimeout(this.t_expand);
		// open menu only if all parameters was setted		this.t_expand = setTimeout( 'menu3_access.expand("' + player + '","' + pid + '","' + id + '","' + spec_id + '")', this.conf[this.getlevel(id)]['expand_delay'] );	}
	else
	{		// in the other way - wait & do not close anything
		this.standby();	}
}

function menu3_onmouseout( id )
{	clearTimeout(this.t_collapse);
	this.t_collapse = setTimeout( 'menu3_access.collapse()', this.conf[this.getlevel(id)]['collapse_delay'] );
}

function menu3_standby()
{	clearTimeout(this.t_collapse);
	this.t_collapse = null;
}

function menu3_getlevel( id )
{    for( i = 0; i < this.o_pool.length; i++ )
	{
		if( this.o_pool[i] == id )
		{			return i;		}
	}

	return 0;}

function menu3_getx( elem )
{
    return elem.offsetParent ?
               elem.offsetLeft + this.getx( elem.offsetParent ) :
               elem.offsetLeft;
}

function menu3_gety( elem )
{
    return elem.offsetParent ?
               elem.offsetTop + this.gety( elem.offsetParent ) :
               elem.offsetTop;
}

function menu3_inArray( a, e )
{
	for( i = 0; i < a.length; i++ )
	{
		if( a[i] == e )
		{
			return 1;
		}
	}

	return 0;
}

// *OLD CODE, FOR COMPATIBILITY
// for other popups. line layers
var _open_menu_id;
var tvar;
var tvar2;

// used for choose lang dropdown
function MyShowMenuReal( id, ITEMS_OFFSET_LEFT, ITEMS_OFFSET_TOP )
{
	ClearAllTimeout()
    tvar2 = setTimeout( '_MyShowMenuReal("' + id + '","' + ITEMS_OFFSET_LEFT + '","' + ITEMS_OFFSET_TOP + '")', 500 );
}

function _MyShowMenuReal( id, ITEMS_OFFSET_LEFT, ITEMS_OFFSET_TOP )
{	if( _open_menu_id )
	   document.getElementById(_open_menu_id).style.visibility = 'hidden';
	_open_menu_id = id;

	mval = document.getElementById(id);
	mval.style.left = document.getElementById(id+'_container').offsetLeft + ITEMS_OFFSET_LEFT + 'px';
	mval.style.top = document.getElementById(id+'_container').offsetTop + ITEMS_OFFSET_TOP + 'px';
	mval.style.visibility = 'visible';}

function MyCloseMenu(id,tmst)
{	try{
	    clearTimeout(tvar2);
	}
	catch(e){};

    tvar = setTimeout( '_MyCloseMenu("' + id + '")', tmst );
}

function _MyCloseMenu(id)
{
	document.getElementById(id).style.visibility = 'hidden';
}

function ClearAllTimeout()
{
	try{
	    clearTimeout(tvar);
	    clearTimeout(tvar2);
	}
	catch(e){};
}

function LHoverIn( id )
{	old_bg = $("#"+id).css('background-color');	$("#"+id).css('background-color','#c7dce8');}

function LHoverOut( id )
{
	$("#"+id).css('background-color',old_bg);
}

if( top.frames.length > 0 ) top.location.href = self.location;