var ticker = {

    contents : document.getElementById('ticker_contents'),
    offset : 0,
    timeout : null,
    firstPass : true,

    start : function() {
  $(function() {
    $("div#ticker").smoothDivScroll({autoScroll: "always", autoScrollDirection: "endlessloopright", autoScrollStep: 1, autoScrollInterval: 30});
    
    $("div#ticker").bind("mouseover", function(){
      $("div#ticker").smoothDivScroll("stopAutoScroll");
    });
    
    $("div#ticker").bind("mouseout", function(){
      $("div#ticker").smoothDivScroll("startAutoScroll");
    });
    
    
  });
    },

    stop : function() {
      $("div#ticker").smoothDivScroll("stopAutoScroll");
    },

    loadData : function() {
      var xmlHttp = getAjaxObject();

      xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
          ticker.processData(json_parse(xmlHttp.responseText));
        }
      };

      xmlHttp.open("GET", "list_json.php?listid=703", true);
      xmlHttp.send(null);
      
    },

    processData : function(data) {
      this.stop();
      
      var contents = '<span class="name"><img src="http://img.eve-markets.net/img/eve_market_icon.png" alt="EVE Markets" \/>Top 100 Index<\/span>';
      contents += ' <span class="price price_' + ((data.index.change > 0) ? 'up' : (data.index.change < 0) ? 'down' : 'nochange');
      contents += '">' + data.index.value + ' ';
      
      if (Math.abs(data.index.change) > 0.009) {
        contents += ((data.index.change > 0) ? '+' : '') + data.index.change;
      }
      else {
        contents += '=';
      }
      contents += '<\/span> ';
      
      for (var i = 0; i < data.items.length; i++) {
        var item = data.items[i];
        contents += '<span class="name" id="ticker_type' + item.typeID + '"><img src="http://img.eve-markets.net/' + item.graphic + '" style="width:16px; height:16px;" /><a href="detail.php?typeid=' + item.typeID + '">' + item.typeName + '<\/a><\/span>';
        contents += ' <span class="price price_' + ((item.percent_change > 0) ? 'up' : (item.percent_change < 0) ? 'down' : 'nochange');
        contents += '">' + item.median_price_display + ' ';
        if (Math.abs(item.percent_change) > 0.009) {
          contents += ((item.change > 0) ? '+' : '') + item.change.toFixed(2) + ' ' + item.percent_change + '%';
        }
        else {
          contents += '=';
        }
        contents += '<\/span> '; 
      }

      this.contents.innerHTML = contents;

      this.start();
      $("div#ticker").smoothDivScroll("startAutoScroll");
      window.setTimeout("ticker.loadData()", 300000); // 5 minutes
    }
    
};


if(document.getElementById("ticker_wrapper").style.display != "none") {
  ticker.loadData();
}
