/*global $ document */
$(document).ready(function () {

  $(function () {
      $.getJSON("GetData/get_ticker_data.aspx", function(json) {

                jJSON["dsplyName"] = (function() {
                    response = {
                        values: [],
                        count: 0
                    };
                    $.each(json.quotes,function(i,item) {
                        if (item.dsplyName != "undefined") {
                            response.count++;
                            response.values[i] = item.dsplyName;
                        }
                    });
                    return response;
                })();

                jJSON["last"] = (function() {
                    response = {
                        values: [],
                        count: 0
                    };
                    $.each(json.quotes,function(i,item) {
                        if (item.last != "undefined") {
                            response.count++;
                            response.values[i] = item.last;
                        }
                    });
                    return response;
                })();
                
                jJSON["nchange"] = (function() {
                    response = {
                        values: [],
                        count: 0
                    };
                    $.each(json.quotes,function(i,item) {
                        if (item.nchange != "undefined") {
                            response.count++;
                            response.values[i] = item.nchange;
                        }
                    });
                    return response;
                })();
                
                jJSON["pchange"] = (function() {
                    response = {
                        values: [],
                        count: 0
                    };
                    $.each(json.quotes,function(i,item) {
                        if (item.pchange != "undefined") {
                            response.count++;
                            response.values[i] = item.pchange;
                        }
                    });
                    return response;
                })();
                
								var showNext, ar;
								ar = new Array("last", "pchange", "nchange");
								ar.current = 0;
								ar.next = function() { 
									ar.current = (ar.current + 1) % ar.length;
									return ar[ar.current];
								}
								
								showNext = function() {
									var e = ar.next();
									$("td.data").remove();

                  $("#data-title").text(e);
                  $.each(jJSON.getValues(e, null), function(i, data){                              
                    if (data < 0) {       
                      $("#display-data").append("<td style='color:red;' class='data'>" + data + "</td>");
                    } 
										else 
										{
                      $("#display-data").append("<td class='data'>" + data + "</td>");
                    }                  
                  });
								}
								showNext();
                $("#display-data").everyTime(5000, "ticker", showNext);    
                
                $.each(jJSON.getValues("dsplyName",null), function(i, data) {
                  $("#display-index").append("<th>" + data + "</th>");            
                });
                  
            });
        });        

       var jJSON = {
            getValues: function(obj,num) {
                return jJSON[obj]["values"].slice(0,((num == null) ? jJSON[obj]["values"].length : num));
            }
        };
});
