﻿
function ChangeTheSelectedSymbolsGroup(strNewGroup)
{
    oQs.getNames = true;
    oQs.assetsCategory = strNewGroup;
    oQs.GetQuotes();
    
    var arrOptions = getElementsByClassName('SymbolsSelectedGroupName');
    
    for (var index=0; index < arrOptions.length ; index++)
    {
        arrOptions[index].className = 'SymbolsGroupName';        
    }

    event.srcElement.className = 'SymbolsSelectedGroupName';
}

    function getElementsByClassName(classname, node)
    {
        if(!node) node = document.getElementsByTagName("body")[0];
        var a = [];
        var re = new RegExp('\\b' + classname + '\\b');
        var els = node.getElementsByTagName("*");
        for(var i=0;els.length>i; i++)
             if(re.test(els[i].className))a.push(els[i]);
        return a;
    }
 

function Quotes()
{
    this.url = QDHUrl;
    this.QueryString = null;
    this.QuotesListChanged = false;
    this.Interval = 1000;
    this.QuotesData = new Array();
    this.Clients = new Array();
    this.assetsCategory = "";
    this.getNames = true;
    this.waiting = null;
    if (window.location.search.indexOf('selectedCategory') > -1)
    {
        this.assetsCategory = window.location.search.split('selectedCategory=')[1];
        if (this.assetsCategory.indexOf('&') > -1)
            this.assetsCategory.substr(0 , this.assetsCategory.indexOf('&'));
    }
    

    this.BuildQueryString =  function ()
    {
	    var tempQS = "";
	    var iiSymbol
	    this.QueryString = "selectedCategory=" + this.assetsCategory;
	    if (this.getNames == true)
	    {
	        this.QueryString += "&GetFullAssetsNames=true";
	        this.getNames = false;
	    }
    };
    
    this.GetQuotes = function() 
    {
        if (oQs.waiting != null)
            clearTimeout(oQs.waiting);
            
        oQs.BuildQueryString();

        if (oQs.QueryString != "Symbols=")
            var myAjax = new Ajax.Request(
	        oQs.url,
	        {
	            method: 'get',
	            parameters: oQs.QueryString,
	            onComplete: oQs.QuotesDataArived,
	            onException : oQs.ScheduleNextCall
	        });
    };
    this.ScheduleNextCall = function()
    {
        oQs.waiting = setTimeout(this.GetQuotes , this.Interval);
    };
    
    this.QuotesDataArived = function (originalRequest)
    {
        //debugger;
        //alert(originalRequest);
	    var arrQlines = originalRequest.responseText.split("\r\n");
	    var arrTempQuotes="";
	    var arrQuotes="";
	    for(i=0;i<arrQlines.toArray().length;i++)
	    {
		    arrTempQuotes = arrQlines[i].split("\t");
		    if (arrTempQuotes.length > 2)
		    {
                if (oQs.QuotesData[i] == undefined || oQs.QuotesData[i].Symbol != arrTempQuotes[0])
                {
	                oQs.QuotesData[i] = new Quote();
	                oQs.QuotesData[i].Symbol = arrTempQuotes[0];
                    oQs.getNames = true;
	            }
    	        
	            if(oQs.QuotesData[i].Ask=="")
	                oQs.QuotesData[i].Ask = arrTempQuotes[2];
    		    
    		    
	            oQs.QuotesData[i].Dir = arrTempQuotes[2]-oQs.QuotesData[i].Ask>0?"Up":arrTempQuotes[2]-oQs.QuotesData[i].Ask<0?"Down":"";
	            oQs.QuotesData[i].Ask = arrTempQuotes[2];
	            oQs.QuotesData[i].Bid = arrTempQuotes[1];
    	        
	            if (arrTempQuotes.length > 4)
	            {
	                oQs.QuotesData[i].AssetName      = arrTempQuotes[3];
	                oQs.QuotesData[i].AssetShortName = arrTempQuotes[4];
	            }
	        }
	    }
	    
	     if (arrTempQuotes.length < 2)
	        i--;
	        
	    for (;i < oQs.QuotesData.length ; i++)
	    {
	    
	        oQs.QuotesData[i] = undefined;
	    }
	    oQs.NotifyClients();
	    
	    oQs.ScheduleNextCall();
	    
    };
	this.NotifyClients = function (){
		var i;
		for(i=0;i<oQs.Clients.length;i++){
			oQs.Clients[i].QuotesArived();
		}
	};
	this.GetQuotesBySymbol = function (sSymbol){
		return this.QuotesData[sSymbol];
	};
	this.AddQuote = function (sSymbol){
		this.QuotesData[sSymbol]= new Quote();
	};
	this.RegisterClient = function (oObj,arrSymbols1)
	{
		this.Clients[this.Clients.length] = oObj;
        oQs.getNames = true;
        oQs.GetQuotes();	
	};
};
var oQs  = new Quotes();
//var pe = new PeriodicalExecuter(function(pe) {oQs.GetQuotes()},QDHInterval);