
$(function() {
  var newsDelay = 3500;

  var $panel = $("#scrollingNews ul");
  var $navigator = $("#scrollingNews .navigator");

  var interval = null;

  var animLock = 0;
  var gotoQueued = null;

  // selects a particular news by index (0 to n-1)
  function showNews(idx, queue) {
    queue = queue || false;

    if (animLock > 0) {
      if (queue) {
        console.debug("showNews(): currently transition-locked, queuing change");
        gotoQueued = idx;
      }
      else {
        console.debug("showNews(): currently transition-locked, discarding event");
      }
      return;
    }

    // get the current active news index and the total news number
    var curr_idx = $panel.find("li:visible").prevAll().length;
    var total = $panel.find("li").length;
    //console.debug("showNews(): current active news idx is " + curr_idx + " out of " + total);

    if (idx == "next") {
      idx = (curr_idx + 1 < total ? curr_idx + 1 : 0);
    }
    else if (idx == "prev") {
      idx = (curr_idx > 0 ? curr_idx - 1 : total - 1);
    }

    if ((idx < 0) || (idx >= total)) {
      console.warn("showNews(): Invalid idx " + idx + " out of range");
      return;
    }

    console.debug("showNews(): [idx=" + idx + "] starting fade in / fade out effects");

    animLock = 1;

    $navigator.find("span a").removeClass("active").eq(idx).addClass("active");

    $panel.find("li:visible").fadeOut("slow");

    $panel.find("li").eq(idx).fadeIn("slow", function() {
      console.debug("showNews(): fade in finished");
      animLock = 0;
      if (gotoQueued != null) {
        console.debug("showNews(): recovering queued action " + gotoQueued)
        var q = gotoQueued;
        gotoQueued = null;
        showNews(q, false);
      }
    });
  }

  var total_news = $panel.find("li").length;

  function generate_navigator(cnt) {
    var curr_idx = $panel.find("li:visible").prevAll().length;
    var html = "";
    for (var i = 0; i < cnt; i++) {
      html += "<a href=\"#goto," + i + "\"" +
              (i == curr_idx ? " class=\"active\"" : "") + ">" +
              (i + 1) + "</a>";
    }
    if (html != "")
      $navigator.find("span").append(html);
  }
  if (total_news > 1)
    generate_navigator(total_news);

  // handle clicks
  $("#scrollingNews .navigator a").live("click", function() {
    console.log("Clicked " + $(this).attr("href"));
    var toks = $(this).attr("href").split(",");

    if (toks[0] == "#prev")
      showNews("prev", true);
    else if (toks[0] == "#next")
      showNews("next", true);
    else if (toks[0] == "#goto")
      showNews(parseInt(toks[1]), true);
    else
      return;

    return false;
  });

  // news timer management
  if (total_news > 1) {
    console.info("News: enabled rotation between " + total_news + " news");

    var interval_cb = function() {
      showNews("next", false);
    };
    interval = window.setInterval(interval_cb, newsDelay);

    // halt scrolling when mouse is over
    $("#scrollingNews").mouseover(function() {
      if (interval) {
        console.debug("News: mouse over news area, stopping timer");
        window.clearInterval(interval);
        interval = null;
      }
    });
    $("#scrollingNews").mouseout(function() {
      if (!interval) {
        console.debug("News: mouse left news area, restarting timer");
        interval = window.setInterval(interval_cb, newsDelay);
      }
    });
  }
  else if (total_news == 1) {
    $navigator.find("a").hide();
  }
  else
    console.info("News: timer disabled, no news available");

});

