'use strict';

var items = {
  '128': [
    {text: 'Critical', id: 101},
    {text: 'Non-Critical', id: 102},
    {text: 'Planned Service Outage', id: 103},
    {text: 'All', id: 100},
    {text: 'Waste Heat', id: 441}
  ],
  '112': [
    {text: 'Critical', id: 72},
    {text: 'Non-Critical', id: 71},
    {text: 'Planned Service Outage', id: 70},
    {text: 'All', id: 73},
    {text: 'Waste Heat', id: 438}
  ],
  '142': [
    {text: 'Critical', id: 148},
    {text: 'Non-Critical', id: 149},
    {text: 'Planned Service Outage', id: 150},
    {text: 'All', id: 147},
    {text: 'Waste Heat', id: 439}
  ],
  '163': [
    {text: 'Critical', id: 244},
    {text: 'Non-Critical', id: 245},
    {text: 'Planned Service Outage', id: 246},
    {text: 'All', id: 243},
    {text: 'Waste Heat', id: 442}
  ],
  '177': [
    {text: 'Critical', id: 291},
    {text: 'Non-Critical', id: 292},
    {text: 'Planned Service Outage', id: 293},
    {text: 'All', id: 290},
    {text: 'Waste Heat', id: 443}
  ],
  '225': [
    {text: 'Critical', id: 385},
    {text: 'Non-Critical', id: 386},
    {text: 'Planned Service Outage', id: 387},
    {text: 'All', id: 384},
    {text: 'Waste Heat', id: 440}
  ]
};

$(function () {
  var regexp = /Itemid=(\d+)/;
  $('a.sublevel:contains(Notices)').each(function () {
    var that, match, itemid, item, img, ul, hidden, parent;
    that = $(this);
    match =  that.attr('href').match(regexp);
    if (match) {
      itemid = match[1];
      item = items[itemid];
      if (item) {
        parent = that.parent();
        img = $("img", parent).first();
        ul = $(document.createElement('ul'));
        ul.css('list-style-image', 'none').css('list-style-type', 'none');
        $(item).each(function () {
          var a, li;
          a = document.createElement('a');
          li = document.createElement('li');
          $(a).attr('href', 'index.php?option=com_content&task=view&id=' + this.id + '&Itemid=' + itemid)
              .addClass('sublevel')
              .text(this.text);
          $(li).append(img.clone())
               .append(a)
               .appendTo(ul);
        });
        hidden = !new RegExp('Itemid=' + itemid).test(document.location.href);
        parent.append(ul);
        if (hidden) {
          ul.hide();
        }
        that.click(function () {
          ul[hidden ? 'show' : 'hide']('fast');
          hidden = !hidden;
        });
        that.attr('href', '#');
      }
    }
  });
});