//č
// save

function mnu_cat_dialog_(show,dialog_name,e){
  menu_creator_links_add_select_links_C();
  mnu_cat_dialog(show,dialog_name,e);
  //alert (sel_cat_id);         
}
function mnu_cat_dialog(show,dialog_name,e){
  //alert (sel_cat_id);
  if (cat_on_reguest){return false;}
  if (show)
  {
    if (menu_cat_on_reguest){return false;}
    show_hide_div('menu_creator_links_add',!show)
    if (!e) {var e=window.event}
      
      //alert (document.documentElement.scrollLeft);
      right = e.clientX;
      bottom = e.clientY;
      
      cat_stock_add_dialog_id = document.getElementById(dialog_name);
      cat_stock_add_dialog_id.style.position = 'absolute';
      //alert (menu_creator_links_add_id.parentNode.nodeName);
      cat_stock_add_dialog_id.style.left = (document.documentElement.scrollLeft + menu_dialog_left + 30) + 'px';
      cat_stock_add_dialog_id.style.top = (document.documentElement.scrollTop + bottom - 10) + 'px';
      
    //show_object(dialog_name);
    show_div(dialog_name);
    menu_cat_on_reguest = true;
  }
  else
  {
    hide_div(dialog_name);
    show_hide_div('menu_creator_links_add',!show)
    menu_cat_on_reguest = false; 
  }
//Enable_El();
return true;         
}

function Menu_cat_ok(){
  Set_Element_Value('links_C',sel_cat_id); 
  mnu_cat_dialog(false,'menu_creator_cat',0);        
}

function Menu_cat_cancel(){
  //alert ("Menu_cat_cancel - nedokoncena");
  mnu_cat_dialog(false,'menu_creator_cat',0);         
}

  function menu_show(id,folow){
    if (folow){return true;}
    ul_id_ = 'ul_'+id;
    ul_id = document.getElementById(ul_id_);
    if (ul_id == null){return true;}

    supp_id_ = 'supp_'+id;
    supp_id = document.getElementById(supp_id_);
    if (supp_id != null){supp_id.style.display = 'block';}
  
    ul_id.style.display = 'block';
    
    a_id_ = 'a_'+id;
    a_id = document.getElementById(a_id_);
    a_id.onclick = function() { return menu_hide(id); };
    
    a_t_id_ = 'a_t_'+id;
    a_t_id_ = document.getElementById(a_t_id_);
    if (a_t_id_ != null){a_t_id_.onclick = function() { return menu_hide(id); };}
    
    //return false;
    img_id_ = 'img_'+id;
    img_id = document.getElementById(img_id_);
    if (img_id == null){return false;}
    img_id.src = "menus/img/minus.gif";
    img_id.alt = "-";
    return false;         
  }
  
  function menu_hide(id,folow){
    if (folow){return true;}
    ul_id_ = 'ul_'+id;
    ul_id = document.getElementById(ul_id_);
    if (ul_id == null){return true;}
  
    supp_id_ = 'supp_'+id;
    supp_id = document.getElementById(supp_id_);
    if (supp_id != null){supp_id.style.display = 'none';}
    
    ul_id.style.display = 'none';
    
    a_id_ = 'a_'+id;
    a_id = document.getElementById(a_id_);   
    a_id.onclick = function() { return menu_show(id) };
    
    a_t_id_ = 'a_t_'+id;
    a_t_id_ = document.getElementById(a_t_id_);
    if (a_t_id_ != null){a_t_id_.onclick = function() { return menu_show(id); };}
  
    //return false;
    img_id_ = 'img_'+id;
    img_id = document.getElementById(img_id_);
    if (img_id == null){return false;}
    img_id.src = "menus/img/plus.gif";
    img_id.alt = "+";
    return false;            
  }

  function on_menu_changed(){
    menu_changed = true;            
  }
  
  
  function menu_save_yes(){
    document.frm_menu.menu_action.value = 'save';
    document.frm_menu.submit();             
  }
  function menu_save(){
    if (on_reguest){return;}
    menu_save_yes();             
  }
  
  function menu_save_no(){
    document.frm_menu.submit();          
  }
  function menu_save_cancel(){
    document.frm_menu.menu_action.value = '';
    hide_div('mnu_creator_dlg');
    hide_div('menu_save_div');
    on_reguest = false;          
  }

//delete
function menu_selection_changed(id){
  if (delete_menu_reguest){    
    // zaskrtneme puvodni radio
    rb_id_str = 'rb_'+sel_li_id;
    rb_id = document.getElementById(rb_id_str);
    rb_id.checked = true;  
    return false;
  }
    sel_li_id = id;
    document.frm_menu.menu_id.value = sel_li_id;       
  }
  
  function menu_delete()
  {
    document.frm_menu.menu_action.value = 'del';
    if (sel_li_id == 0){return}
    if (on_reguest){return;}
    if (is_sys_menu(sel_li_id))
    {
      return;
    }
    if (menu_changed)
    {
      show_div('menu_save_div');
      show_div('mnu_creator_dlg');
      on_reguest = true;
      return;  
    }
    if (!last_li_id == 0){menu_delete_no();}
    
    li_id_str = 'li_'+sel_li_id;
    li_id = document.getElementById(li_id_str);
    li_id.style.backgroundColor = '#FF0000';
    last_li_id = sel_li_id; 
    show_div('menu_delete_div');
    show_div('mnu_creator_dlg');
    delete_menu_reguest = true; 
    on_reguest = true;     
  }
  
  function menu_delete_yes(){
    document.frm_menu.submit();             
  }
  function menu_delete_no(){
    hide_div('mnu_creator_dlg');
    hide_div('menu_delete_div'); 
    li_last_id_str = 'li_'+last_li_id;
    li_last_id = document.getElementById(li_last_id_str); 
    li_last_id.style.backgroundColor = '';
    delete_menu_reguest = false;
    on_reguest = false;          
  }

//insert
 function menu_insert()
  {
    if (on_reguest){return;}
    
    if (Get_Element_Value('sys_insert'+sel_li_id) != "1")
    {
      if (is_sys_menu(sel_li_id)){return;}  
    }
      
    document.frm_menu.menu_action.value = 'insert';
    if (menu_changed)
    {
      show_div('menu_save_div');
      show_div('mnu_creator_dlg');
      on_reguest = true;  
    }
    else 
    {
      document.frm_menu.submit();
    }   
  }

// links

function on_menu_type_changed(menu_type_r){
  menu_type = menu_type_r.value;           
}



function links_dialog(show,id,e){ 
    if (show)
    {
      if (on_reguest){return;}
      if (is_sys_menu(id))
      {
        return;
      }
      Set_Element_Value('request_id',id);
      menu_type = Get_Element_Value('type_'+id);
      //alert (type);
      // N - none
      // C - category
      // D - user dokumet
      // E - extern link
      // S - system
      
      Set_Element_Value('links_C','0');
      Set_Element_Value('links_D','-1');
      Set_Element_Value('links_E','# ');
      
      SetCheckBox('LinkType_E_param1',false);
      
      SetCheckBox('LinkType_'+menu_type,true); 
      if  (menu_type == 'N')
      {       
      
      }else if  (menu_type == 'C')
      {       
        Set_Element_Value('links_C',Get_Element_Value('link'+id));
      }else if  (menu_type == 'D')
      {       
        Set_Element_Value('links_D',Get_Element_Value('link'+id));
      }else if  (menu_type == 'E')
      {       
        Set_Element_Value('links_E',Get_Element_Value('link'+id));
        SetCheckBox('LinkType_E_param1',(Get_Element_Value('links_param1'+id) != 0));
      }
    
      //nastaveni - enable
      SetCheckBox('menu_parameter_enable',(Get_Element_Value('sett_enable'+id) != 0));
      SetCheckBox('menu_parameter_admin',(Get_Element_Value('sett_admin'+id) != 0));
      SetCheckBox('menu_parameter_login',(Get_Element_Value('sett_login'+id) != 0));
      SetCheckBox('menu_parameter_login_req',(Get_Element_Value('sett_login_req'+id) != 0));
      
      
      //sett_enable_id_str = 'sett_enable'+id;
      //sett_enable_id = document.getElementById(sett_enable_id_str);
      //sett_enable_id.value = id;
      
      
         
      if (!e) {var e=window.event}
      
      //alert (document.documentElement.scrollLeft);
      right = e.clientX;
      bottom = e.clientY;
      
      menu_creator_links_add_id = document.getElementById('menu_creator_links_add');
      menu_creator_links_add_id.style.position = 'absolute';
      //alert (menu_creator_links_add_id.parentNode.nodeName);
      menu_dialog_left = right;
      menu_creator_links_add_id.style.left = (document.documentElement.scrollLeft + right + 30) + 'px';
      menu_creator_links_add_id.style.top = (document.documentElement.scrollTop + bottom - 10) + 'px';
      //menu_creator_links_add_id.style.left = '1px';
      //menu_creator_links_add_id.style.top = '1px';
      //menu_creator_links_add_id.style.visibility = "visible";
      menu_parameter_enable_change();
      show_div('menu_creator_links_add');
      on_reguest = true;
    } 
    else
    {
      hide_div('menu_creator_links_add');      
    }         
  }

function menu_creator_links_add_select_links_C(){
  var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
	if (!xmlhttp) {
    alert ('Nepodařilo se zinicializovat objekt "Microsoft.XMLHTTP"');
    return false;	
	} 
  
  //alert ("DSD");
  cat_value = Get_Element_Value('links_C');
  xmlhttp.open('GET', 'cat_manager.php?action=get_stock_by_category&cat_id='+encodeURIComponent(cat_value), false);
  
  xmlhttp.send('');
  stock_value = parseInt(xmlhttp.responseText);
  
  Set_Element_Value('stock',stock_value);
  Set_Element_Value('cat_stock_old_id',stock_value);
  Set_Element_Value('cat_cat_old_id',cat_value);
  //alert (cat_value);
  OnChangeStock();
  xmlhttp = null;        
}
  
  
function menu_parameter_enable_change(){
  Enable_CheckBox('menu_parameter_admin',GetCheckBox('menu_parameter_enable'));
  //Enable_CheckBox('menu_parameter_login',GetCheckBox('menu_parameter_enable'));
  //Enable_CheckBox('menu_parameter_login_req',!GetCheckBox('menu_parameter_enable'));
  menu_parameter_admin_change();         
}
function menu_parameter_admin_change(){
  Enable_CheckBox('menu_parameter_login',!GetCheckBox('menu_parameter_admin') && GetCheckBox('menu_parameter_enable') ); 
  //Enable_CheckBox('menu_parameter_login_req',!GetCheckBox('menu_parameter_admin') && GetCheckBox('menu_parameter_enable') );
  menu_parameter_login_change();        
}
function menu_parameter_login_change(){ 
  Enable_CheckBox('menu_parameter_login_req',!GetCheckBox('menu_parameter_admin') && GetCheckBox('menu_parameter_login') && GetCheckBox('menu_parameter_enable'));        
}

function Menu_Detail_Ok(){
  id = Get_Element_Value('request_id');
  //alert (id);
  Set_Element_Value('type_'+id,menu_type);
  Set_Element_Value('link'+id,"# ");
  
  Set_Element_Value('sett_enable'+id,GetCheckBox('menu_parameter_enable'));
  Set_Element_Value('sett_admin'+id,GetCheckBox('menu_parameter_admin'));
  Set_Element_Value('sett_login'+id,GetCheckBox('menu_parameter_login'));
  Set_Element_Value('sett_login_req'+id,GetCheckBox('menu_parameter_login_req'));
  Set_Element_Value('links_param1'+id,'0');
  //alert (menu_type);   
  if (menu_type == "N")//none
  {
    //Set_Element_Value('link'+id,"#");     
  }
  else if (menu_type == "C") //category
  {
    Set_Element_Value('link'+id,Get_Element_Value('links_C'));
  }
  else if (menu_type == "D") //category
  {
    Set_Element_Value('link'+id,Get_Element_Value('links_D'));
  }
  else if (menu_type == "E") //category
  {
    Set_Element_Value('link'+id,Get_Element_Value('links_E'));
    Set_Element_Value('links_param1'+id,GetCheckBox('LinkType_E_param1'));
  }
  //s.selectedIndex = 0;
  links_dialog(false,0); 
  on_menu_changed(); 
  on_reguest = false;      
}

function Menu_Detail_Storno(){
  links_dialog(false,0); 
  on_reguest = false;      
}

//sys

function is_sys_menu(id){
    if (on_reguest){return;}
    if (id == 0){return;}
    //sys_id_str = 'sys'+id;
    //sys_id = document.getElementById(sys_id_str);
    
    if (Get_Element_Value('sys'+id) == "1")
    {      
      show_sys_dialog();
      return true;      
    }
  
    return false;        
  }

function show_sys_dialog(){
  show_div('menu_sys_div');
  show_div('mnu_creator_dlg'); 
  on_reguest = true;        
}

function hide_sys_dialog(){
  hide_div('mnu_creator_dlg'); 
  hide_div('menu_sys_div');
  on_reguest = false;        
}

function Menu_Insert_Up(){
  
  request_id_val = Get_Element_Value('request_id');
  request_id_item = document.getElementById("li_" + request_id_val);
  

  menu_strom = request_id_item.parentNode;
  
  menu_strom_root = menu_strom.childNodes;
  menu_strom_root_cnt = menu_strom_root.length;
  
  //alert ("li_" + request_id_val);
  
  for (menu_strom_root_index=0;menu_strom_root_index<menu_strom_root_cnt;menu_strom_root_index++) 
  {
    //alert (menu_strom_root[menu_strom_root_index].id);
    if (menu_strom_root[menu_strom_root_index].id == "li_" + request_id_val)
    {
      if (menu_strom_root_index == 1)
      {
        if (menu_strom_root[0].id == "blank") {break;}}
      if (menu_strom_root_index == 0){break;}
      
      div1 = GetDivForMenuMove();
      div1.innerHTML = menu_strom_root[menu_strom_root_index].innerHTML;
      Anime_Menu_Move(menu_strom_root[menu_strom_root_index], menu_strom_root[menu_strom_root_index-1],div1,true);
      if (menu_strom_root_cnt == 2){menu_strom_root_index = 0;}
      removed_Child = menu_strom.removeChild(menu_strom_root[menu_strom_root_index]);
      //alert('3');
      if (menu_strom_root_index == menu_strom_root_cnt - 1)
      {   
        //alert('A');     
        //menu_strom_root_index = menu_strom_root_index - 2;
        menu_strom.insertBefore(removed_Child, menu_strom_root[menu_strom_root_index - 2].nextSibling);
        //menu_strom_root_index = menu_strom_root_index + 2;
      }
      else
      {
      //alert('B');
      menu_strom.insertBefore(removed_Child, menu_strom_root[menu_strom_root_index].previousSibling);}
      if (menu_strom_root_cnt == 2){menu_strom_root_index = 1;}
      //vymenime indexi
      
      
      
      old_index_str = 'poradi_' + request_id_val; 
      old_index = Get_Element_Value('poradi_' + request_id_val);
      new_index_li_str = menu_strom_root[menu_strom_root_index].id;
      new_index_str = new_index_li_str.replace('li_', 'poradi_');
      new_index = Get_Element_Value(new_index_str);
      
      Set_Element_Value(old_index_str,new_index);
      Set_Element_Value(new_index_str,old_index);
      
      //alert('4');
      on_menu_changed();
      break;  
    }
  } 
  //utils_dialog(false,0);
}

function Menu_Insert_Down(){
  request_id_val = Get_Element_Value('request_id');
  request_id_item = document.getElementById("li_" + request_id_val);

  menu_strom = request_id_item.parentNode;
  menu_strom_root = menu_strom.childNodes;
  menu_strom_root_cnt = menu_strom_root.length;
  //alert ("li_" + request_id_val);
  
  for (menu_strom_root_index=0;menu_strom_root_index<menu_strom_root_cnt;menu_strom_root_index++) 
  {
    if (menu_strom_root_index == menu_strom_root_cnt - 1){break;}
    //alert (menu_strom_root[menu_strom_root_index].id);
    if (menu_strom_root[menu_strom_root_index].id == "li_" + request_id_val)
    {
  
      removed_Child = menu_strom.removeChild(menu_strom_root[menu_strom_root_index]);      
      menu_strom.insertBefore(removed_Child, menu_strom_root[menu_strom_root_index].nextSibling);
      
      div1 = GetDivForMenuMove();
      div1.innerHTML = menu_strom_root[menu_strom_root_index+1].innerHTML;
      Anime_Menu_Move(menu_strom_root[menu_strom_root_index], menu_strom_root[menu_strom_root_index+1],div1);
      
      
      //menu_strom_root[menu_strom_root_index].style.visibility = '';
      //menu_strom_root[menu_strom_root_index + 1].style.visibility = '';
      
      //vymenime indexi
      old_index_str = 'poradi_' + request_id_val; 
      old_index = Get_Element_Value('poradi_' + request_id_val);
      new_index_li_str = menu_strom_root[menu_strom_root_index].id;
      new_index_str = new_index_li_str.replace('li_', 'poradi_');
      new_index = Get_Element_Value(new_index_str);
      
      Set_Element_Value(old_index_str,new_index);
      Set_Element_Value(new_index_str,old_index);
      on_menu_changed();
      break;  
    }
  } 
  //utils_dialog(false,0);
}
        
function Menu_Insert_Top(){
  request_id_val = Get_Element_Value('request_id');
  request_id_item = document.getElementById("li_" + request_id_val);
  
  menu_strom = request_id_item.parentNode;
  menu_strom_root = menu_strom.childNodes;
  menu_strom_root_cnt = menu_strom_root.length;
  for (menu_strom_root_index=0;menu_strom_root_index<menu_strom_root_cnt;menu_strom_root_index++) 
  {
    if (menu_strom_root[menu_strom_root_index].id == "li_" + request_id_val)
    {
      if (menu_strom_root_index == 1)
      {
        if (menu_strom_root[0].id == "blank") {break;}}
      if (menu_strom_root_index == 0){break;}
      
      div1 = GetDivForMenuMove();
      div1.innerHTML = menu_strom_root[menu_strom_root_index].innerHTML;
      top_index = 1;
      if (menu_strom_root[0].id == "blank"){top_index = 2;}
      Anime_Menu_Move(request_id_item, menu_strom_root[top_index],div1,true);
      
      old_index_str = 'poradi_' + request_id_val; 
      old_index = Get_Element_Value(old_index_str);
      
      removed_Child = request_id_item; 
      menu_strom.insertBefore(removed_Child, menu_strom_root[top_index]. previousSibling);
            
      for (move_index=menu_strom_root_index;move_index>=0;move_index--)
      {
        if (menu_strom_root[move_index].id != "blank") 
        {        
          old_index_li_str = menu_strom_root[move_index].id;
          old_index_str2 = old_index_li_str.replace('li_', 'poradi_'); 
          old_index2 = Get_Element_Value(old_index_str2);
          Set_Element_Value(old_index_str2,old_index);            
          old_index = old_index2;
          
        }    
      }
      
        
      
      on_menu_changed();
      break;  
    }     
  }          
}

function Menu_Insert_Bottom(){
  request_id_val = Get_Element_Value('request_id');
  request_id_item = document.getElementById("li_" + request_id_val);
  
  menu_strom = request_id_item.parentNode;
  menu_strom_root = menu_strom.childNodes;
  menu_strom_root_cnt = menu_strom_root.length;
  for (menu_strom_root_index=0;menu_strom_root_index<menu_strom_root_cnt;menu_strom_root_index++) 
  {
    if (menu_strom_root_index == menu_strom_root_cnt - 1){break;}
    if (menu_strom_root[menu_strom_root_index].id == "li_" + request_id_val)
    {
      removed_Child = request_id_item; 
      menu_strom.appendChild(removed_Child);
  
      div1 = GetDivForMenuMove();
      div1.innerHTML = removed_Child.innerHTML;
      Anime_Menu_Move(menu_strom_root[menu_strom_root_index], menu_strom_root[menu_strom_root_cnt-1],div1);
      
      old_index_str = 'poradi_' + request_id_val; 
      old_index = Get_Element_Value(old_index_str);      
      for (move_index=menu_strom_root_index;move_index<menu_strom_root_cnt;move_index++)
      {
        //if (move_index != 0) 
        {        
          old_index_li_str = menu_strom_root[move_index].id;
          old_index_str2 = old_index_li_str.replace('li_', 'poradi_'); 
          old_index2 = Get_Element_Value(old_index_str2);
          Set_Element_Value(old_index_str2,old_index);            
          old_index = old_index2;
        }    
      }
      on_menu_changed();
      break;  
    }     
  }          
}


function Menu_utils_cancel(){
  utils_dialog(false,0);
  return false;           
}

function utils_dialog(show,id,e){ 
    
    if (show)
    {
      if (on_reguest){return;}
    
      Set_Element_Value('request_id',id);
      
      if (!e) {var e=window.event}
      
      //alert (document.documentElement.scrollLeft);
      right = e.clientX;
      bottom = e.clientY;
      
      menu_creator_links_add_id = document.getElementById('menu_creator_utils');
      menu_creator_links_add_id.style.position = 'absolute';
      //alert (menu_creator_links_add_id.parentNode.nodeName);
      menu_creator_links_add_id.style.left = (document.documentElement.scrollLeft + right + 30) + 'px';
      menu_creator_links_add_id.style.top = (document.documentElement.scrollTop + bottom - 10) + 'px';
      
      
      
      show_div('menu_creator_utils');
      on_reguest = true;
    } 
    else
    {
      hide_div('menu_creator_utils');
      on_reguest = false;      
    }         
  }

//proxy = div1
//var srcFrom = menu_strom_root[menu_strom_root_index];
//var srcTo = menu_strom_root[menu_strom_root_index + 1];
function Anime_Menu_Move(srcFrom,srcTo,proxy,top){
  
  var Dom = YAHOO.util.Dom;
      //animate1
        
        
        var proxyid = proxy.id;
        var thisid = (top==true)?srcFrom.id:srcTo.id; 

        // Show the proxy element and animate it to the src element's location
        Dom.setStyle(proxy, "visibility", "");
        Dom.setStyle(thisid, "visibility", "hidden");
        
        Dom.setX(proxy,Dom.getX(srcFrom));
        Dom.setY(proxy,Dom.getY(srcFrom));
        
        
        srcTo_X = Dom.getX(srcTo);
        srcTo_Y = Dom.getY(srcTo);
        var a = new YAHOO.util.Motion(proxy, {points: {to: [srcTo_X,srcTo_Y]}}, 0.2, YAHOO.util.Easing.easeOut )
        // Hide the proxy and show the source element when finished with the animation
        a.onComplete.subscribe(function() {
                Dom.setStyle(proxyid, "visibility", "hidden");
                Dom.setStyle(thisid, "visibility", "");
            });   
        
        a.animate();         
}

function GetDivForMenuMove(){
  
  div1 = document.getElementById('div1_for_Tree_move');
      if (div1 == null)
      {
        div1 = document.createElement('div');div1.id = 'div1_for_Tree_move';  
        div1.style.position   = "absolute";
        div1.style.visibility = "hidden";
        div1.style.border     = "2px solid #aaa";
        div1.style.zIndex     = 999;
        div1.style.height     = "25px";
        div1.style.width      = "250px"; 
      }
    document.body.appendChild(div1);
    return  div1;        
}

