/**
 * Sidebar UL
 *
 * @author     Takashi Mizohata <takashi@firstbornmultimedia.com>
 * @copyright  2008 firstborn
 * @license    firstborn internal (pending)
 */

var SidebarUL = function (elm)
{
  this.list = elm;

  if (typeof(SidebarUL.suffix) == 'undefined')
  {
    SidebarUL.suffix = {};
    SidebarUL.suffix.open = '-open';
    SidebarUL.suffix.close = '-close';
    SidebarUL.suffix.anime = '-anime';
    SidebarUL.suffix.animeR = '-animeR';

    SidebarUL.regexp = SidebarUL.compileRegexp(SidebarUL.suffix);
  }
  var p = $(elm).parent();
  this.header = p.children('h4').get(0);
  this.triangle = $('img', p).get(0);
  this.path = SidebarUL.parsePath(this.triangle.src);
  this.isOpen = false;
}

// INSTANCE MEMBER ________________________________________________________

SidebarUL.prototype.toggle = function (ev)
{
  if (this.isOpen)
  {
    this.hide();
  }
  else
  {
    this.show();
  }
}

SidebarUL.prototype.show = function ()
{
  $(this.list).slideDown();
  this.isOpen = true;
  this.setImageAnime();
}

SidebarUL.prototype.hide = function ()
{
  $(this.list).slideUp();
  this.isOpen = false;
  this.setImageAnimeR();
}

SidebarUL.prototype.setImageOpen = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + SidebarUL.suffix.open + '.' + this.path.ext;
}

SidebarUL.prototype.setImageClose = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + SidebarUL.suffix.close + '.' + this.path.ext;
}

SidebarUL.prototype.setImageAnime = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + SidebarUL.suffix.anime + '.' + this.path.ext;
}

SidebarUL.prototype.setImageAnimeR = function ()
{
  this.triangle.src = this.path.dirname + '/' + this.path.basename + SidebarUL.suffix.animeR + '.' + this.path.ext;
}

// STATIC MEMBER __________________________________________________________

SidebarUL.parsePath = function (str)
{
  var result = {};
  var arr_dirs = str.split('/');
  result.filename = arr_dirs.pop();
  if (result.filename.match(SidebarUL.regexp) != null)
  {
    var arr_file = (result.filename.replace(SidebarUL.regexp, '')).split('.');
  }
  else
  {
    var arr_file = result.filename.split('.');
  }
  result.dirname = arr_dirs.join('/');
  result.ext = arr_file.pop();
  result.basename = arr_file.join('.');
  return result;
}

SidebarUL.compileRegexp = function (obj)
{
  var str = '';
  for (var i in obj)
  {
    str += obj[i] + '|';
  }
  str = str.substring(0, (str.length - 1));
  return new RegExp(str);
}

// DISPATCH EVENT _________________________________________________________

SidebarUL.dispatch = function (ev)
{
  switch (ev.data.type)
  {
    case 'CLICK':
      SidebarUL.dispatchClick(ev);
    break;

    default:
      //throw new Error('event type not defined.');
    break;
  }
}

// CLICK __________________________________________________________________

SidebarUL.dispatchClick = function (ev)
{
  //console.log('SidebarUL::dispatchClick: ' + ev.target.tagName);

  var target;
  switch ((ev.target.tagName).toLowerCase())
  {
    case 'h4':
    case 'img':
      target = $('ul', $(ev.target).parents().filter('div').get(0)).get(0);
    break;

    default:
      throw new Error('Unindentifiable trigger element: ' + ev.target.tagName);
    break;
  }

  var ul = (Controller.getInstance()).dictionary.get(target);
  ul.toggle(ev);
}
