/**
 * ImagePath
 *
 * Image path manupilation object
 *
 * @author     Takashi Mizohata <takashi@firstbornmultimedia.com>
 * @copyright  2008 firstborn
 * @license    firstborn internal (pending)
 */

var ImagePath = function (str)
{
  this.original = str;

  // compile regex once, and cache it.
  if (typeof(ImagePath.suffix) == 'undefined')
  {
    ImagePath.suffix = {};
    ImagePath.suffix.hover = '-hover';
    ImagePath.suffix.selected = '-selected';
  }
  if (typeof(ImagePath.regexp) == 'undefined')
  {
    ImagePath.regexp = ImagePath.compileRegexp(ImagePath.suffix);
  }


  // initialize...
  this.pathinfo = ImagePath.pathinfo(this.original);

  // has to do some image preloading as well.
}


// MEMBER METHODS _________________________________________________________

ImagePath.prototype.selected = function (target)
{
  target.src = this.pathinfo.dirname + '/' + this.pathinfo.basename + ImagePath.suffix.selected + '.' + this.pathinfo.ext;
}

ImagePath.prototype.out = function (target)
{
  target.src = this.pathinfo.dirname + '/' + this.pathinfo.filename;
}


ImagePath.prototype.hover = function (target)
{
  target.src = this.pathinfo.dirname + '/' + this.pathinfo.basename + ImagePath.suffix.hover + '.' + this.pathinfo.ext;
}


// STATIC METHODS _________________________________________________________

ImagePath.pathinfo = function (str)
{
  var result = {};
  var arr_dirs = str.split('/');
  result.filename = arr_dirs.pop();
  if (result.filename.match(ImagePath.regexp) != null)
  {
    var arr_file = (result.filename.replace(ImagePath.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;
}


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


