// hitolog Topics

(function($) {
  $.hitologTopics = function(data, options) {
    return new HitologTopics(data, options);
  };

  $.fn.hitologTopics = function(options) {
    return new HitologTopics(this.get(0), options);
  };

  // constructor
  function HitologTopics(obj, options) {
    var defaults = {
      focusSpeed: 1000,
      focusInterval: 5000,
      items: []
    };
    var options = $.extend(defaults, options);

    this.oBaseElm = $(obj);
    this.options = options;
    this.initialize();
  }

  // class implementation
  HitologTopics.prototype = {
    initialize: function() {
      var self = this;

      // vars
      //this.aoItems = [];
      this.bFirst = true;
      this.nCursor = 0;
      this.bIsMouseOver = false;

      // elements
      this.oLIs = $(this.oBaseElm.children());
      this.nNumLIs = this.oLIs.length;

      for (var i = 0; i < this.nNumLIs; i++) {
        var li = $(this.oLIs[i]);
        li.data("id", i);
        li.click(function() {
          self.moveTo($(this).data("id"));
        });
      }

      this.oFocus = $("#" + this.options.focusId);
      var ulLeft = this.oBaseElm.offset().left;
      var ulTop = this.oBaseElm.offset().top;
      this.oFocus.css({
        left: ulLeft,
        top: ulTop,
        position: "absolute",
        display: "block"
      })
      .dblclick(function() {
        var id = self.nCursor;
        var item = self.options.items[id];
        if (item) {
          window.location.href = item.hito_url;
        }
      });
      this.oContainer = $("#" + this.options.containerId);
      this.oContainer
      .mouseover(function() { self.bIsMouseover = true; })
      .mouseout(function() { self.bIsMouseover = false; })
      ;
      this.oContent = $("#" + this.options.contentId);
      this.oImageAnc = $("#" + this.options.imageId);
      this.oImage = $(this.oImageAnc.children()[0]);
      this.oFullname = $("#" + this.options.fullnameId);
      this.oProfileText = $("#" + this.options.profileTextId);
      this.oNewsList = $("#" + this.options.newsListId);
      this.oNewsItems = this.oNewsList.children();

      // interval
      this._move(true);
      setInterval(function() { self.onInterval(); }, self.options.focusInterval);
    }
    ,
    onInterval: function() {
      if (!this.bIsMouseover) {
        this.nCursor++;
        if (this.nCursor >= this.nNumLIs) {
          this.nCursor = 0;
        }
        this._move(this.nCursor);
      }
    }
    ,
    moveTo: function(id) {
      this.nCursor = id;
      this._move(id);
    }
    ,
    _move: function() {
      var self = this;
      var focusSpeed;
      if (this.bFirst) {
        focusSpeed = 0;
        this.bFirst = false;
      } else {
        focusSpeed = this.options.focusSpeed;
      }
      var cur = $(this.oLIs[this.nCursor]);
      var left = cur.position().left;
      this.oFocus.stop();
      this.oFocus.animate(
        { left: left },
        focusSpeed
      );
      if (focusSpeed == 0) {
        self.onSetContent(self.nCursor);
      } else {
        self.oContent.fadeOut(focusSpeed * 0.2, function() {
          self.onSetContent(self.nCursor);
          self.oContent.fadeIn(focusSpeed * 0.2);
        });
      }
    }
    ,
    onSetContent: function(id) {
      var item = this.options.items[id];
      if (item) {
        this.oImageAnc.attr("href", item.hito_url);
        this.oImage.attr("src", this.options.loadingImageUrl);
        this.oImage.attr("src", item.image_url);
        this.oFullname.text(item.fullname);
        this.oFullname.attr("href", item.hito_url);
        this.oProfileText.text(item.profile_text);
        for (var i = 0; i < this.oNewsItems.length; i++) {
          var li = $(this.oNewsItems[i]);
          var anc = $(li.children()[0]);
          var span = $(li.children()[1]);
          if (i < item.news.length) {
            var news = item.news[i];
            li.removeClass();
            li.addClass(news["class"]);
            anc.html(news.title);
            anc.attr("href", news.url);
            span.text(news.datetime);
          } else {
            li.removeClass();
            anc.text("");
            anc.attr("href", "");
            span.text("");
          }
        }
      }
    }
  }

})(jQuery);

