function expand_all() {
  if ($j('.categories').is(':hidden')) {
    $j('.categories').show();
    $j('#expander').html('Collapse All');
    $j.cookie('_industries_state', 'expanded', {expires: 7, path: '/'})
  } else {
    $j('.categories').hide();
    $j('#expander').html('Expand All');
    $j.cookie('_industries_state', 'collapsed', {expires: 7, path: '/'})
  }
  set_displayed_groups_cookie();
}

function set_displayed_groups_cookie() {
  all_groups = $j('.group-name');
  groups_shown = new Array();
  for (key in all_groups) {
    for (attribute in all_groups[key].attributes) {
      if (all_groups[key].attributes[attribute]) {
        if (all_groups[key].attributes[attribute].name == 'group_id') {
          group_id = all_groups[key].attributes[attribute].value;
          if (! $j('#'+group_id).is(':hidden') ) {
            groups_shown.push(group_id);
          
          }
        }
      }
    }
  }
  cookie_data = groups_shown.join(',');
  $j.cookie('_groups_shown',cookie_data,{expires: 7, path: '/'});
}

function set_my_categories() {
  $j('#all-cats-table').hide();
  $j('#my-cats-table').show();
  $j('#all-behavioral-segments').hide();
  $j('#my-behavioral-segments').show();
  $j.cookie('_cats_displayed', 'mine', {expires: 7, path: '/'});
}
function set_all_categories() {
  $j('#my-cats-table').hide();
  $j('#all-cats-table').show();
  $j('#my-behavioral-segments').hide();
  $j('#all-behavioral-segments').show();
  $j.cookie('_cats_displayed', 'all', {expires: 7, path: '/'});
}
function toggle_group() {
  var group_id = '';
  var second_group_id_array;
  for (attribute in this.attributes) {
    if (this.attributes[attribute]) {
      if (this.attributes[attribute].name == 'group_id') {
        group_id = this.attributes[attribute].value;
        second_group_id_array = group_id.split('-');
      }
    }
  }
  if (second_group_id_array.length > 1) {
    second_group_id = second_group_id_array[1];
  } else {
    second_group_id = 'my-' + group_id;
  }
  if ($j('#'+group_id ).is(':hidden')) {
    $j('#'+group_id).show();
    $j('#'+second_group_id).show();
  } else {
    $j('#'+group_id).hide();
    $j('#'+second_group_id).hide();
  }
  if ($j('.categories').is(':hidden')) {
    $j('#expander').html('Expand All');
    $j.cookie('_industries_state', 'collapsed', {expires: 7, path: '/'})
  } else {
    $j('#expander').html('Collapse All');
    $j.cookie('_industries_state', 'expanded', {expires: 7, path: '/'})
  }
  set_displayed_groups_cookie();
}
function show_group(group_id){
  if ($j('#'+group_id).is(':hidden')) {
    $j('#'+group_id).show();
  }
}
function set_page_state() {
  industries_state = $j.cookie('_industries_state');
  show_cats = $j.cookie('_cats_displayed');
  groups_to_show = $j.cookie('_groups_shown');
  groups_shown = (groups_to_show) ? groups_to_show.split(','):Array(0)
  if (industries_state) {
    if (industries_state == 'expanded') {
      expand_all();
    }  
  }
  if (show_cats) {
    if (show_cats == 'mine') {
      set_my_categories();
    }
  }
  for ( x=0; x<groups_shown.length; x++) {
    show_group(groups_shown[x]);
  }
}
function show_no_access_popup() {
  if (popup_popped == false) {
    $j('#category-no-access-popup').dialog({
      bgiframe:true,
      draggable:false,
      modal:true,
      resizable:false,
      width:450,
      height:230,
      zIndex:'9999'
    });
    popup_popped = true;
  } else {
    $j('#category-no-access-popup').dialog('open');
    popup_popped = true;        
  }
}
