$(function(){
  
  var menu = new Object();
  //menu - array of all page tag-tree 
  $('*[tag]').each(function(n){
      //params - array of each tag-tree 
      var params = $(this).attr('tag') + "::";
      //tag-values of all parents
      $(this).parents('*[tag]').each(function(){
         params += $(this).attr('tag')+"~";
      }); 
      menu[n] = params;
  }); 
  
  $.post('/ajax/contextmenu.php',menu,function(data){
        $('body').append(data);
    
        $('*[tag]').each(function(n){
            
            var elemName = $(this).attr('tag');   
            
            $('*[tag]:eq('+n+')').jeegoocontext(elemName, {
            livequery: true,
            widthOverflowOffset: 0,
            heightOverflowOffset: 1,
            submenuLeftOffset: -4,
            submenuTopOffset: -5,
            onSelect: function(e, context){
              
              switch ($(this).attr('event')){
                
                case 'css_manager':
                //open script window
                var newWin = window.open(
                    '/admin/virtual_'+$(this).attr('object_id')+'/css_manager/css_manager.php?key='+$(this).attr('id'), 
                    "Настройка интерфейса" , 
                    "left=200,top=200,resizable=yes,scrollbars=yes"
                    );
                newWin.focus(); 
                newWin.onbeforeunload = function(){
                     parent.window.location.reload(); 
                }
                break;
                
                case 'banner':
                //open script window
                var newWin = window.open(
                    '/admin/virtual_'+$(this).attr('object_id')+'/s_banner_adm/s_banner_adm.php', 
                    "Баннер" , 
                    "left=200,top=200,resizable=yes,scrollbars=yes"
                    );
                newWin.focus(); 
                newWin.onbeforeunload = function(){
                     parent.window.location.reload(); 
                }
                break;      
                
                case 'ref_list':
                //open script window
                var newWin = window.open(
                    '/admin/virtual_'+$(this).attr('object_id')+'/reference_list/reference_list.php', 
                    "Спецпредложения" , 
                    "left=200,top=200,resizable=yes,scrollbars=yes"
                    );
                newWin.focus(); 
                newWin.onbeforeunload = function(){
                     parent.window.location.reload(); 
                }
                break; 
                           
                case 'executor':
                $.ajax({
                  url: '/menu/exe.php?cmd='+$(this).attr('filename'),
                  success: function(data){
                    if (data=='ok')
                      window.location = window.location;
                    else
                      alert(data);
                  }
                });
                break;   
                
                case 'editor':
                //open script window
                var newWin = window.open(
                    $(this).attr('filename')+"?pos="+$(this).attr('id'), 
                    "Редактор" , 
                    "left=200,top=200,resizable=yes,scrollbars=yes"
                    );    
                newWin.focus(); 
                newWin.onbeforeunload = function(){
                  parent.window.location.reload(); 
                }
                break;
                
                case 'script':
                $.ajax({
                  url: $(this).attr('filename'),
                  success: function(data){
                    window.location = window.location;
                  }
                });
                break;
                
                default:
                  return false;
                  
              }

            },
            onHover: function(e, context){},
            onShow: function(e, context){},
            onHide: function(e, context){}
            });                                        
        });
          
  });

});

