var startTheScript = 0;

var bid = new Array();
var ask = new Array();

var subscript  = 0;
var subscript2 = 0;


YAHOO.util.Event.addListener(window, "load", function() {
    YAHOO.example.Local_XML = new function() {
        this.connectionCallback = {
            success: function(o) {
                var xmlDoc = o.responseXML;
                
                var formatCurrencies = function(elCell, oRecord, oColumn, oData){
                	
                	var currency = oData;
                	
                	
                    // convert string to float first                	
                	var floatingCurrency = parseFloat(currency);
                	

                	
                	//since this is not the first time we're running it
                	
                	var increament  = '<img src="/fileadmin/template/main/images/streaming_quotes/tick_up.gif" />&nbsp;<span style="color:green;font-weight: bolder;">';
                	
                	var decreament = '<img src="/fileadmin/template/main/images/streaming_quotes/tick_down.gif" />&nbsp;<span style="color:red;font-weight: bolder;">';
                	
                	var status = '<span>';
                    
                	
                	
                	
                	
 
                          
                	      if(oColumn.getKey() == 'bid'){	
                            
                            if( floatingCurrency > bid[subscript] && startTheScript){
                            	
                            	status = increament;
                            	
                            } 
                            if (floatingCurrency < bid[subscript] && startTheScript) {
                            	
                            	status = decreament;
                            	
                            }
                             
                            bid[subscript] = floatingCurrency;
                	        subscript++;
                            
                	      } else {
                	      	
                	      	
                	    
                	      	
                	      	if( floatingCurrency > ask[subscript2] && startTheScript){
                            	
                	      		
                	      		
                            	status = increament;
                            	
                            } 
                            if (floatingCurrency < ask[subscript2]&& startTheScript) {
                            	
                            	status = decreament;
                            	
                            }
                	      	
                	      	ask[subscript2] = floatingCurrency;
                	      	subscript2++;
                	      
                	      } 
                              
                	    
 
                	

                	
                	
                	
                	// set each number to have four positions after the decimal point
                	elCell.innerHTML =  status + floatingCurrency; 
                	
                };
                
                
                var formatImage = function(elCell, oRecord, oColumn, oData){
                	
                	                	
                	var currency = oData;
                	
                	var currency1 = currency.substr(0,3).toLowerCase();
                	var currency2 = currency.substr((currency.length - 3),(currency.length - 1) ).toLowerCase();
                	
                	elCell.innerHTML =  '<img src="streaming_quotes/images/' + currency1 +'.gif" /><img src="streaming_quotes/images/' + currency2 +'.gif" />' + currency; 
                }
                
                
                subscript = 0;
                subscript2 = 0;
                

                
                
                var myColumnDefs = [
                    {key:"pair", label:"Currency Pair", formatter: formatImage},
                    {key:"bid", label:"Bid", formatter: formatCurrencies},
                    {key:"ask", label:"Ask", formatter: formatCurrencies},
                    {key:"transactionStamp", label:"Time (EST)"} 
                ];

                this.myDataSource = new YAHOO.util.DataSource(xmlDoc);
                this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML;
                this.myDataSource.responseSchema = {
                    resultNode: "Result",
                    fields: ["pair","bid","ask","transactionStamp"]
                };

                
                
                this.myDataTable = new YAHOO.widget.DataTable("myContainer", myColumnDefs, this.myDataSource);
                
                //set this counter to one after the script was run once
                startTheScript = 1;

            },
            failure: function(o) {

            }
        };
        
		
		
        this.getXML = YAHOO.util.Connect.asyncRequest("GET",
                "/streaming_quotes/proxy.php?dealingDeskId=1001&clientTierId=5&output=xml&cache=" + Math.floor(Math.random()*1000000),
                this.connectionCallback);
    };
});
 
setInterval("YAHOO.util.Connect.asyncRequest('GET', '/streaming_quotes/proxy.php?dealingDeskId=1001&clientTierId=5&output=xml&cache=' +  Math.floor(Math.random()*1000000), YAHOO.example.Local_XML.connectionCallback)",15000); 
