/////////////////////////////////////////////////////    
// Прототип cайта компании "Философт"
//
// М. Острогорский (misha@philosoft.ru)
//
// Обслуживание страницы
//
// Привычны руки к топорам
/////////////////////////////////////////////////////


// Иерархическое меню
// - - - - - - - - - -

// Инициализирует меню, возвращает развернутую структуру меню
function init_tree_menu( menu_name, tma )
{

 var tms = new Array();

 var preciders = new Array();

 for( i = 1; i < tma.length; i++ ) 
 {
  tms[i] = new Array();
  tms[i]["level"] = tma[i];
  
  if( i == tma.length )
   tms[i]["leaf"] = true;
  else
   tms[i]["leaf"] = tma[i] >= tma[i+1];
  
  if( !tms[i]["leaf"] )
   tms[i]["opened"] = false;

  tms[i]["children"] = new Array();
  tms[i]["number_of_children"] = 0;
  
  var parent = 0;
  
  if( tma[i] > 1)
  {
   if( tma[i] > tma[i-1] )
    preciders[tma[i-1]] = i - 1;
   
   tms[i]["visible"] = false;   
   
   parent = preciders[tma[i]-1];
   tms[i]["parent"] = parent;   
   tms[parent]["number_of_children"]++;
   tms[parent]["children"][tms[parent]["number_of_children"]] = i;    
   
   //alert( i + " " + ( tms[parent]["number_of_children"] )); 
  }
  else
  {
   tms[i]["visible"] = true;
   tms[i]["parent"] = 0;
   preciders[tma[i]] = 0;
  }
  
 }

 //alert(  ( tms[9]["children"] )); 
 
 return tms;
}

// Рекурсивное отображение пунктов (служебная функция для tree_menu_flip)
function tree_menu_showrec( menu, menu_name, item )

{
 for( var i = 1; i <= menu[item]["number_of_children"]; i++ )
 { 
  var itemrow = document.getElementById( menu_name + menu[item]["children"][i] );
                  
  if( itemrow )             
  {
   itemrow.style.display = "";                              
   
   var icon = document.getElementById( "menuicon" + menu_name + menu[item]["children"][i] );
   
   if( icon )      
    if( icon.src.indexOf( "galka-opened" ) != -1 )
     tree_menu_showrec( menu, menu_name, menu[item]["children"][i] );   
    
  } 
 }
}

// Открывает или закрывает пункт-книжку
function tree_menu_flip( menu, menu_name, item )
{
 
 // alert( menu.length);
 var row = document.getElementById( menu_name + item );
 
 if( row )
 {
  var icon = document.getElementById( "menuicon" + menu_name + item );
  if( icon )
  { 
   if( icon.src.indexOf( "galka-yellow" ) != -1 ) 
   {
    icon.src = "layout/galka-opened.png";
    var open = true;
   }
   else
   {
    icon.src = "layout/galka-yellow.png";
    var open = false;
   } 
   
   if( open )   
    tree_menu_showrec( menu, menu_name, item );   
   else
   {
    var i = item;
    while( i < menu.length )
    {     
     i++;
     
     if( menu[i]["level"] <= menu[item]["level"] )
      break;     
     
     var itemrow = document.getElementById( menu_name + i );
                  
     if( itemrow )             
      itemrow.style.display = "none";                              
    }
    
   }
   
  }  
  
  
 }
   
}


// Большая красная кнопка
// - - - - - - - - - - - -

// Отпускает большую красную кнопку
function brbrelease( url, targetid )
{
 var button=document.getElementById( targetid );
  
 button.src="layout/brb-normal.png";
 
 location=url;
}

// Нажимает большую красную кнопку
function brbpress( url, targetid )
{ 
 var button=document.getElementById( targetid ); 
 button.src="layout/brb-pressed.png"; 
 releasecode="brbrelease('" + url + "', '" + targetid + "');"; 
 //alert(releasecode);
 tmp = window.setTimeout( releasecode, 250 );       
}



