var show_delay = 500;
var clear_delay = 250;

var extended;
var implemented;
var orig;
var show_timeout;
var clear_timeout;

function toggle_vis(el, force) {
  var content_el = el + '_content';
  var wrapper_el = el + '_wrapper';
  if (!$(content_el)) return;
//  if ((!force) || ((force == 'visible') && (!($(content_el).getHeight() > 0))) || ((force == 'hidden') && ($(content_el).getHeight() > 0))) {
  if ((!force) || ((force == 'visible') && (!$(content_el).visible())) || ((force == 'hidden') && ($(content_el).visible()))) {
//    if ($(content_el).getHeight() > 0) {
//      Effect.SlideUp(wrapper_el, { duration: 1.0 });
//    } else {
//      Effect.SlideDown(wrapper_el, { duration: 1.0 });
//    }
    $(content_el).toggle();
    var indicator_el = el + '_indicator';
    if ($(indicator_el)) {
//      if ($(content_el).getHeight() > 0) {
      if (!$(content_el).visible()) {
        $(indicator_el).innerHTML = '<img src="/images/collapsed.png" class="indicator"/>';
      } else {
        $(indicator_el).innerHTML = '<img src="/images/expanded.png" class="indicator"/>';
      }
    }
  }
}

function select_standard(std_id) {
  if(extended && extended[std_id]) {
    for(var ex in extended[std_id]) {
      if($('chk_' + ex) && !$('chk_' + ex).checked) $('chk_' + ex).click();
    }
  }    
}

function highlight_standard(std_id) {
  show_timeout = setTimeout('do_highlight_standard("' + std_id + '");', show_delay);
}

function do_extended(std_id, done, clear) {
  if(extended && extended[std_id]) {
    if ($('row_' + std_id)) {
      if (!clear) {
        $('row_' + std_id).className = 'extended';
      } else {
        if(orig && orig[std_id]) $('row_' + std_id).className = orig[std_id];
      }
    }
    done[std_id] = std_id;
    if (extended[std_id]) for(var ex in extended[std_id]) {
      if (!done[ex]) {
        do_extended(ex, done, clear);
      }
    }
  }
}

function do_implemented(std_id, done, clear) {
  if(implemented && implemented[std_id]) {
    if ($('row_' + std_id)) {
      if (!clear) {
        $('row_' + std_id).className = 'implemented';
      } else {
        if(orig && orig[std_id]) $('row_' + std_id).className = orig[std_id];
      }
    }
    done[std_id] = std_id;
    if (implemented[std_id]) for(var im in implemented[std_id]) {
      if (!done[im]) {
        do_implemented(im, done, clear);
      }
    }
  }
}

function do_highlight_standard(std_id) {
  do_extended(std_id, {}, false);
  do_implemented(std_id, {}, false);
  if ($('row_' + std_id)) $('row_' + std_id).className = 'result_highlight';
  if ($('midmore_' + std_id)) $('midmore_' + std_id).toggle();
  if ($('more_' + std_id)) $('more_' + std_id).toggle();
  show_timeout = null;
}

function clear_standard(std_id) {
  if(show_timeout) {
    clearTimeout(show_timeout);
    show_timeout = null;
  } else {
    clear_timeout = setTimeout('do_clear_standard("' + std_id + '");', clear_delay);
  }
}

function do_clear_standard(std_id) {
  if(orig) {
    if (orig[std_id] && $('row_' + std_id)) $('row_' + std_id).className = orig[std_id];
    do_extended(std_id, {}, true);
    do_implemented(std_id, {}, true);
  }
  if ($('midmore_' + std_id)) $('midmore_' + std_id).toggle();
  if ($('more_' + std_id)) $('more_' + std_id).toggle();
  clear_timeout = null;
}


