function SHExternal()
{
	var content  = document.getElementById( 'external' );
	var elem     = document.getElementById( 'rubric' );
	var selected = elem.options[elem.selectedIndex].value;
	
	if ( selected == '0' )
	{
		Show( 'external' );
	} else {
		Hide( 'external' );
	}
}

function build_position_element( position )
{
	var object = '';
	var html   = '';
	var parent = '';
	
	if ( parent = document.getElementById( 'parent' ) )
	{
		parent = parent.options[parent.selectedIndex].value;
	}
	
	if ( object = document.getElementById( 'position' ) )
	{
		html += '<select name="position" class="edit">';
		
		for ( var i = 0; i < elements.length; i++ )
		{
			if ( elements[i][2] == parent )
			{
				html += '<option value="' + elements[i][3] + '"';
				
				if ( elements[i][3] == position ) 
				{
					html += ' selected';
				}
				
				html += '>[ ' + elements[i][3] + ' ] ' + elements[i][1] + '</option>';					
			}
		}
		
		html += '<option value="last">--- последней ---</option>';
		
		html += '</select>';
		
		object.innerHTML = html;
	}
}

function build_position_menu( position )
{
	var object = '';
	var html   = '';
	var parent = '';
	
	if ( parent = document.getElementById( 'type' ) )
	{
		parent = parent.options[parent.selectedIndex].value;
	}
	
	if ( object = document.getElementById( 'position' ) )
	{
		html += '<select name="position" class="edit">';
		
		for ( var i = 0; i < menues.length; i++ )
		{
			if ( menues[i][2] == parent )
			{
				html += '<option value="' + menues[i][3] + '"';
				
				if ( menues[i][3] == position ) 
				{
					html += ' selected';
				}
				
				html += '>[ ' + menues[i][3] + ' ] ' + menues[i][1] + '</option>';					
			}
		}
		
		html += '<option value="last">--- последней ---</option>';
		
		html += '</select>';
		
		object.innerHTML = html;
	}
}

function collapse_menu( parent )
{
	if ( document.getElementById( 'menu_' + parent ) )
	{			
		if ( document.getElementById( 'menu_' + parent ).innerHTML == '' )
		{
			var html  = '';
			
			if ( parent == '0' )
			{
				for ( var i = 0; i < menues.length; i++ )
				{
					var tmp = tpl;
					
					var up   = ( menues[i][2] == 1 ) ? '<a href="/admin/menu/menu/up/'   + menues[i][0] + '/"><img src="/images/admin/up_blue.gif" align="absmiddle" width="16" height="16" alt="" border="0" /></a>'   : '&nbsp;';
					var down = ( menues[i][3] == 1 ) ? '<a href="/admin/menu/menu/down/' + menues[i][0] + '/"><img src="/images/admin/down_blue.gif" align="absmiddle" width="16" height="16" alt="" border="0" /></a>' : '&nbsp;';
					
					var col  = '<a href="javascript:;" onclick="collapse_menu(\'' + menues[i][0] + '\');"><img src="/images/admin/closed.gif" align="absmiddle" width="16" height="16" title="Развернуть" alt="Развернуть" border="0" vspace="0" hspace="0" id="collapse_' + menues[i][0] + '" /></a>';
					var doc  = '<img src="/images/admin/document.gif" align="absmiddle" width="16" height="16" alt="" border="0" vspace="0" hspace="0" />';
					
					tmp = tmp.replace( '[WHAT]', 'menu' );
					tmp = tmp.replace( '[WHAT]', 'menu' );
					tmp = tmp.replace( '[WHAT]', 'menu' );
					
					tmp = tmp.replace( '[ID]', menues[i][0] );
					tmp = tmp.replace( '[ID]', menues[i][0] );
					tmp = tmp.replace( '[ID]', menues[i][0] );
					tmp = tmp.replace( '[ID]', menues[i][0] );
					
					tmp = tmp.replace( '[PADD]', 5 );
					
					tmp = tmp.replace( '[NAME]', menues[i][1] );
					
					tmp = tmp.replace( '[UP]',   up   );
					tmp = tmp.replace( '[DOWN]', down );
					
					tmp = tmp.replace( '[COLLAPSE]', ( ( hasElementChild( menues[i][0] ) ) ? doc : col ) );
					
					html += tmp;
				}
			} else {
				for ( var i = 0; i < elements.length; i++ )
				{
					if ( elements[i][1] == parent )
					{
						var tmp = tpl;					
						
						var up   = ( elements[i][4] == 1 ) ? '<a href="/admin/menu/element/up/'   + elements[i][0] + '/"><img src="/images/admin/up.gif" align="absmiddle" width="16" height="16" alt="" border="0" /></a>'   : '&nbsp;';
						var down = ( elements[i][5] == 1 ) ? '<a href="/admin/menu/element/down/' + elements[i][0] + '/"><img src="/images/admin/down.gif" align="absmiddle" width="16" height="16" alt="" border="0" /></a>' : '&nbsp;';
						
						var col  = '<a href="javascript:;" onclick="collapse_menu(\'' + elements[i][0] + '\');"><img src="/images/admin/closed.gif" align="absmiddle" width="16" height="16" title="Развернуть" alt="Развернуть" border="0" vspace="0" hspace="0" id="collapse_' + elements[i][0] + '" /></a>';
						var doc  = '<img src="/images/admin/document.gif" align="absmiddle" width="16" height="16" alt="" border="0" vspace="0" hspace="0" />';
						
						tmp = tmp.replace( '[WHAT]', 'element' );
						tmp = tmp.replace( '[WHAT]', 'element' );
						tmp = tmp.replace( '[WHAT]', 'element' );
						
						tmp = tmp.replace( '[ID]', elements[i][0] );
						tmp = tmp.replace( '[ID]', elements[i][0] );
						tmp = tmp.replace( '[ID]', elements[i][0] );
						tmp = tmp.replace( '<span id="menu_[ID]"></span>', '' );
						
						tmp = tmp.replace( '[PADD]', elements[i][3] );
						
						tmp = tmp.replace( '[NAME]', elements[i][2] );
						
						tmp = tmp.replace( '[UP]',   up   );
						tmp = tmp.replace( '[DOWN]', down );
						
						tmp = tmp.replace( '[COLLAPSE]', doc );
						
						html += tmp;
					}
				}
			}
			
			if ( html != '' )
			{
				var collapse = document.getElementById( 'collapse_' + parent );
				
				if ( collapse )
				{
					collapse.src   = '/images/admin/open.gif';
					collapse.title = 'Свернуть';
					collapse.alt   = 'Свернуть';
				}
			}
			
			document.getElementById( 'menu_' + parent ).innerHTML = html;
		} else {
			var collapse = document.getElementById( 'collapse_' + parent );
			
			if ( collapse )
			{
				collapse.src   = '/images/admin/closed.gif';
				collapse.title = 'Развернуть';
				collapse.alt   = 'Развернуть';					
			}
			
			document.getElementById( 'menu_' + parent ).innerHTML = '';
		}
	}	
}

function build_position( position )
{
	var object = '';
	var html   = '';
	var parent = '';
	
	if ( parent = document.getElementById( 'parent_select' ) )
	{
		parent = parent.options[parent.selectedIndex].value;
	}
	
	if ( object = document.getElementById( 'position' ) )
	{
		html += '<select name="position" class="edit">';
		
		html += '<option value="first">--- первой ---</option>';
		
		for ( var i = 0; i < array.length; i++ )
		{
			if ( array[i][1] == parent )
			{
				html += '<option value="' + array[i][3] + '"';
				
				if ( array[i][3] == position ) 
				{
					html += ' selected';
				}
				
				html += '>[ ' + array[i][3] + ' ] ' + array[i][2] + '</option>';					
			}
		}
		
		html += '<option value="last">--- последней ---</option>';
		
		html += '</select>';
		
		object.innerHTML = html;
	}
}

function build_parents( parent )
{
	var object = '';
	var html   = '';
	
	if ( object = document.getElementById( 'parent' ) )
	{
		html += '<select name="parent" id="parent_select" class="edit" onChange="javascript:build_position();">';
		
		html += '<option value="0"' + ( ( parent == 0 ) ? ' selected' : '' ) + '>--- корень ---</option>';
		
		for ( var i = 0; i < array.length; i++ )
		{
			html += '<option value="' + array[i][0] + '"';
			
			if ( array[i][0] == parent ) 
			{
				html += ' selected';
			}
			
			var name = '';
			
			for ( var k = 1; k < array[i][4]; k++ )
			{
				name += '-----';
			}
			
			html += '>' + name + ' ' + array[i][2] + '</option>';
		}
		
		html += '</select>';
		
		object.innerHTML = html;
	}
}

function collapse( parent )
{
	if ( document.getElementById( 'row_' + parent ) )
	{			
		if ( document.getElementById( 'row_' + parent ).innerHTML == '' )
		{
			var html  = '';
			
			for ( var i = 0; i < array.length; i++ )
			{
				if ( array[i][1] == parent )
				{
					var tmp  = tpl;
					var next = i + 1;
					
					var edit = '<a href="/admin/blocks/' + array[i][0] + '/"><img src="/images/admin/edit.gif"   width="16" height="16" title="Редактировать" alt="Редактировать" border="0" align="absmiddle" /></a>';
					var del  = '<a href="/admin/delete/' + array[i][0] + '/"><img src="/images/admin/delete.gif" width="16" height="16" title="Удалить" alt="Удалить" border="0" align="absmiddle" /></a>';

					var up   = ( array[i][6] == 1 ) ? '<a href="/admin/up/'   + array[i][0] + '/"><img src="/images/admin/up.gif" align="absmiddle" width="16" height="16" alt="" border="0" /></a>'   : '&nbsp;';
					var down = ( array[i][7] == 1 ) ? '<a href="/admin/down/' + array[i][0] + '/"><img src="/images/admin/down.gif" align="absmiddle" width="16" height="16" alt="" border="0" /></a>' : '&nbsp;';
					
					var col  = '<a href="javascript:;" onclick="collapse(\'[ID]\');"><img src="/images/admin/closed.gif" align="absmiddle" width="16" height="16" title="Развернуть" alt="Развернуть" border="0" vspace="0" hspace="0" id="collapse_[ID]" /></a>';
					var doc  = '<img src="/images/admin/document.gif" align="absmiddle" width="16" height="16" alt="" border="0" vspace="0" hspace="0" />';
					
					var padding = ( array[i][4] == '1' ) ? 10 : ( ( array[i][4] == '2' ) ? 30 : array[i][4] * 20 );

					var module = ( array[i][5] == '1' ) ? '<a href="/admin/edit/module/' + array[i][0] + '/"><img src="/images/admin/module.gif" align="absmiddle" width="16" height="16" border="0" vspace="0" hspace="0" id="collapse_[ID]" /></a>' : '&nbsp;';
					
					tmp = tmp.replace( '[COLLAPSE]', ( ( hasChild( array[i][0] ) ) ? doc : col ) );
					
					tmp = tmp.replace( '[ID]',     array[i][0] );
					tmp = tmp.replace( '[ID]',     array[i][0] );
					tmp = tmp.replace( '[ID]',     array[i][0] );
					tmp = tmp.replace( '[ID]',     array[i][0] );
					tmp = tmp.replace( '[NAME]',   array[i][2] );
					tmp = tmp.replace( '[EDIT]',   edit        );
					tmp = tmp.replace( '[DELETE]', del         );
					tmp = tmp.replace( '[MODULE]', module      );
					tmp = tmp.replace( '[UP]',     up          );
					tmp = tmp.replace( '[DOWN]',   down        );
					tmp = tmp.replace( '[PADD]',   padding     );
					
					html += tmp;
				}
			}
			
			if ( html != '' )
			{
				var collapse = document.getElementById( 'collapse_' + parent );
				
				if ( collapse )
				{
					collapse.src   = '/images/admin/open.gif';
					collapse.title = 'Свернуть';
					collapse.alt   = 'Свернуть';
				}
			}
			
			document.getElementById( 'row_' + parent ).innerHTML    = html;
		} else {
			var collapse = document.getElementById( 'collapse_' + parent );
			
			if ( collapse )
			{
				collapse.src   = '/images/admin/closed.gif';
				collapse.title = 'Развернуть';
				collapse.alt   = 'Развернуть';					
			}
			
			document.getElementById( 'row_' + parent ).innerHTML    = '';
		}
	}
}

function hasElementChild ( id )
{
	var k = 0;
	
	for ( var i = 0; i < elements.length; i++ )
	{
		( elements[i][1] == id ) ? k++ : '';
	}
	
	return ( ( k > 0 ) ? false : true );		
}

function hasChild ( id )
{
	var k = 0;
	
	for ( var i = 0; i < array.length; i++ )
	{
		( array[i][1] == id ) ? k++ : '';
	}
	
	return ( ( k > 0 ) ? false : true );		
}

function isLast ( parent, position )
{
	for ( var i = 0; i < array.length; i++ )
	{
		if ( array[i][1] == parent && array[i][3] > position )
		{
			return true;
		}
	}
	
	return false;
}

function setColor ( element )
{
	element.style.backgroundColor = "#E9EEF2";
}

function unsetColor ( element )
{
	element.style.backgroundColor = "#FEFEFE";
}

function startList()
{
	if ( document.all && document.getElementById ) {
		navRoot = document.getElementById( "navcontainer" );
		for ( i = 0; i < navRoot.childNodes.length; i++ )
		{
			node = navRoot.childNodes[i];
			if ( node.nodeName == "LI" )
			{
				node.onmouseover = function()
				{
					this.className += " over";
  				}
  				node.onmouseout = function()
  				{
  					this.className = this.className.replace( " over", "" );
   				}
   			}
  		}
 	}
}

function SH( id )
{
	var content = document.getElementById( id );
	
	if ( content.style.display == '' )
	{
		Hide( id );
	} else {
		Show( id );
	}
}

function Show( id )
{
	var content = document.getElementById( id );
	content.style.display = '';
	content.style.visibility = 'visible';	
}

function Show_2( id )
{
	var content = document.getElementById( id );
	content.style.visibility = 'hidden';	
	content.style.display = 'none';
}

function Hide( id )
{
	var content = document.getElementById( id );
	content.style.visibility = 'hidden';	
	content.style.display = 'none';
}

function Hide_2( id )
{
	var content = document.getElementById( id );
	//alert( content.style.display );
	content.style.display = 'none';
	content.style.visibility = 'none';	
	//alert( content.style.display );	
}
