<?php

function _digett_block_secondary_links_configure($block=array()){
  $menu = menu_get_menu();
  $pid = variable_get('menu_primary_menu',0);
  $options = array(0=>'Default');
  if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {
    foreach ($menu['visible'][$pid]['children'] as $mid) {
      $options[$mid] = $menu['visible'][$mid]['title'];
    }
  } 
  $form = array();
  $form['digett_block_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => digett_block_variable_get($block,'title',''),
  );
  foreach(node_get_types() as $type=>$name){
    $form['digett_block_'.$type] = array(
      '#type' => 'select',
      '#title' => $name,
      '#options' => $options,
      '#default_value' => digett_block_variable_get($block,$type,0),
    );
  }
  return $form;
}

function _digett_block_secondary_links_view($block=array()){
  if(arg(0)=='node' && is_numeric(arg(1))){
    $node = node_load(arg(1));
    $mid = digett_block_variable_get($block,$node->type,0);
  }
  if(!$mid){      
    $trail = _menu_get_active_trail();
    /* $trail == array(menu, level1 nav item, level2 nav item) */
    if(($trail[0]==variable_get('menu_primary_menu',0)) &&
      isset($trail[1]) && ($trail[1] > 0)){
      $mid = $trail[1];
    }
  }
  
  _digett_block_section($mid);
  if($mid){
    $return['content'] = theme('menu_tree',$mid);
  }else{
    $return['content'] = '';
  }
  $return['subject'] = digett_block_variable_get($block,'title','');
  
  return $return;  
}

