var TimeTube = function(formId, queryFieldId, initialDivId, workingDivId, timelineFrameId, recentDivId, featuredDivId) {
  var this_obj = this;

  this.formId = formId;
  this.queryFieldId = queryFieldId;
  this.initialDivId = initialDivId;
  this.workingDivId = workingDivId;
  this.timelineFrameId = timelineFrameId;
  this.recentDivId = recentDivId;
  this.featuredDivId = featuredDivId;
  this.inited = false;
  this.current = null;

  this.queryBuildTL = function (e) {
    if (e) {
      YAHOO.util.Event.stopEvent(e);
    }

    var q = this.form.query.value;
    if (q.replace(/ /g, "") != "") {
      this.query();
    }
  }

  this.query = function () {
    this.switchToElem(this.working, true, null);
    var cb = { success: function(o) { this_obj.afterQuery(o); }, failure: this.failure };
    YAHOO.util.Connect.setForm(this.form, false); 
    YAHOO.util.Connect.asyncRequest("POST", "/public_api/contrib/timetube/timetube.php?mode=query", cb);
  }

  this.afterQuery = function (o) {
    var callback = function () {
      this_obj.loadTimeline(o.responseText);
    }
    this.findRecentSearches();
    this.switchToElem(this.timeline, false, callback);
  }

  this.clickLoadTimeline = function (e, title, src) {
    YAHOO.util.Event.stopEvent(e);
    var callback = function () {
      this_obj.queryField.value = title;
      this_obj.loadTimeline(src);
    }
    this.switchToElem(this.timeline, false, callback);
  }

  this.init = function () {
    this.form = document.getElementById(this.formId);
    this.queryField = document.getElementById(this.queryFieldId);
    this.initial = document.getElementById(this.initialDivId);
    this.working = document.getElementById(this.workingDivId);
    this.timeline = document.getElementById(this.timelineFrameId);
    this.recent = document.getElementById(this.recentDivId);
    this.featured = document.getElementById(this.featuredDivId);
    this.current = this.initial;
    this.inited = true;
    this.findRecentSearches();
    this.findFeatured();
  }

  this.switchToElem = function (elem, fadeIn, afterFadeCallback) {
    var fadeOutAnim = new YAHOO.util.Anim(this.current, { opacity: { to: 0 } }, 0.4);
    var fadeInAnim = null;
    var useFade = navigator.userAgent.toLowerCase().indexOf("msie") < 0 &&
                  navigator.userAgent.toLowerCase().indexOf("firefox") < 0;

    if (fadeIn) {
      fadeInAnim = new YAHOO.util.Anim(elem, { opacity: { to: 1 } }, 1);
    }

    var callback = function() {
      if (this_obj.current != elem) {
	this_obj.current.style.display = "none";
      }
      elem.style.display = "block";
      if (afterFadeCallback != null) {
	afterFadeCallback();
      }
      this_obj.current = elem;
      if (useFade) {
	if (fadeInAnim != null) {
	  fadeInAnim.animate();
	}
      }
      else {
	elem.style.opacity = 1;
      }
    };
    if (useFade) {
      fadeOutAnim.onComplete.subscribe(callback);
      fadeOutAnim.animate();
    }
    else {
      this.current.style.opacity = 0;
      callback();
    }
  }

  this.findRecentSearches = function () {
    var cb = { success: function(o) { this_obj.afterRecent(o); }, failure: this.failure };
    YAHOO.util.Connect.asyncRequest("GET", "/public_api/contrib/timetube/timetube.php?mode=recent", cb);
  }

  this.afterRecent = function (o) {
    this.recent.style.opacity = 0;
    this.recent.innerHTML = o.responseText;
    var fadeInAnim = new YAHOO.util.Anim(this.recent, { opacity: { to: 1 } }, 1);
    fadeInAnim.animate();
  }

  this.findFeatured = function () {
    this.afterFeatured();
  }

  this.afterFeatured = function () {
    this.afterFeatured_r(1, 15);
  }

  this.afterFeatured_r = function (index, max) {
    if (index <= max) {
      var callback = function () {
	this_obj.afterFeatured_r(index + 1, max);
      };
      var elem = document.getElementById("featured_" + index);
      if (elem != null) {
	var fadeInAnim = new YAHOO.util.Anim(elem, { opacity: { to: 1 } }, .1);
	fadeInAnim.onComplete.subscribe(callback);
	fadeInAnim.animate();
      }
    }
  }

  this.loadTimeline = function (src) {
    this.timeline.src = src;
  }

  this.showTimeline = function () {
    if (this.inited) {
      this.fadeInTimeline();
    }
  }

  this.fadeInTimeline = function () {
    this.switchToElem(this.timeline, true, null);
  }

  this.failure = function () {
    console.log("XHR failed");
  }
}
