$.extend(
{
  //  Tickertape
  moveTicker: function(ticker)
  {
    var tp = $(ticker),
        ti = tp.parent().prevAll("div").length,
        tpl = tp.position().left,
        tpw = 200 + parseInt(tp.attr('totalwidth')),
        dt = (18 + ti) / 1.5,
        t = (tpl + tpw) * dt;
    tp.animate(
    {
      'left': -tpw
    },
    {
      'duration': t,
      'easing': 'linear',
      'queue': true,
      'complete': function()
      {
        $.tickerStart(ticker);
      }
    });
  },

  tickerStart: function(ticker)
  {
    var w = 200 + $(window).width();
    $(ticker).css(
    {
      'left': w
    });
    $.moveTicker(ticker);
  },
  
  tickerInit: function()
  {
    $("#tickertape ul").each(function()
    {
      if (!$(this).hasClass("rollin"))
      {
        $(this).addClass("rollin").attr('totalwidth',function()
        {
          var wi = 0;
          $(this).find("li").each(function()
          {
            wi += $(this).width();
          });
          return wi;
        });
        
        $(this).find("a").mouseenter(function()
        {
          $(this).parent().parent().stop();
        }).mouseleave(function()
        {
          $.moveTicker($(this).parent().parent());
        });
        
        $.tickerStart($(this));
      }
    });
  },

  //  Mobile versions.
  mDetect: function()
  {
    var ua = navigator.userAgent.toLowerCase(),
        iOs = ((ua.indexOf("iphone") > -1) || (ua.indexOf("ipad") > -1)) ? "ios" : "",
        gOs = (ua.indexOf("android") > -1) ? "android" : "";
    $("html").addClass(iOs).addClass(gOs);
  }
});


$(function() {
  $.mDetect();
  $.tickerInit();
});
