/**
 * ManagementTransactionDisplayer handles the automatic data refresh for 
 * the 'market overview' page.
 * 
 * @param initialTransactions A JSON array of transactions, like
 *  [ {"Date" : "08.08.2008", ....... }, ..., {"Date" : "08.08.2008", ....... } ]
 * @param language Language "en", "de" or "fr"
 */
function ManagementTransactionDisplayer(initialTransactions, language,nbRows,issuer,buySellIndicator,additionalName) {
    URL_PARAMETERS = "lang=" + language;
    if( nbRows != null) URL_PARAMETERS += "&nbRowsToDisplay=" + nbRows;
    if( issuer != null ) URL_PARAMETERS += "&issuer="+issuer;
    if ( buySellIndicator != null) URL_PARAMETERS += "&buySellIndicator="+buySellIndicator;
    var URL_JSON  = "/obligations/ajax/mtrans-data.json?" + URL_PARAMETERS;
    var URL_INCL  = "/obligations/management_transactions/management_incl_" + language+".html?id=";
    var URL_PRINT = "/obligations/management_transactions/management_print_"+ language+".html?id=";
    var INTERVAL  = "120000"; // 2min in miliseconds
    
    if( additionalName == null) additionalName = "";
    var TABLE     = "mTransTable"+additionalName;
    var DIALOG    = "mTransDialog";
    var PANE      = "mTransDialogPane";
    var DISP      = "mTransDisp"+additionalName;    

    // public variable, used on the xsps
    this.PUB_DIALOG = DIALOG; 

    // ------------------------ private variables 
    var _transactions = initialTransactions;
    var _timer = null;
    var _currentId;

    // ------------------------ public methods
    /**
     * Start automatic refresh of management transactions 
     */
    this.start = function() {
        _updateTransactions(); // display inital transaction                
        _timer = setInterval(_refresh, INTERVAL);
    }
   
    this.stop = function() {
        // kill the timer
        clearInterval(_timer);
        _timer = null;
    }    
    
    /**
     * Start automatic refresh of management transactions
     * @param i Index of _transactions[] to display  
     */    
    this.displayDialog = function(i) { 
        _currentTransaction = i;
        /* used for Management Transaction Flake (infocenter) -> there can be many displayers in the same page */
        _currentMTDisplayer = this;
        var title; 
        title = _transactions[i].Date;
        title += " ";
        title += _transactions[i].Issuer;
        
        var pane = dijit.byId(PANE);
        pane.href = URL_INCL + _transactions[i].Id; 
        pane.refresh();

        var dialog = dijit.byId(DIALOG);
        dialog.titleNode.innerHTML = title;
        dialog.show();
    }   
    
    /**
     * Opens a popup for printing the currently active dialog
     */      
    this.printPage = function() {
      var url = URL_PRINT + _transactions[_currentTransaction].Id;;
      var width = 600;
      var height = 280;
      var left = (screen.width - width) / 2;
      var top = (screen.height - height) / 2;
        
      window.open(url, 'popup',
          "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",resizable=1"
        + "directories=0,location=0,menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0");
    } 

    // ------------------------ private methods
    /**
     * Display data after update from server
     */
    function _updateTransactions() {
      var table = dojo.byId(TABLE);
      
      var nrRows = table.rows.length;
      for (i=0; i<nrRows; i++) {
         table.deleteRow(0);
      }      
      
      for (i=0; i<_transactions.length; i++) {
         var row = table.insertRow(i);
         row.className = "row-even";
         /*
          * row.className = (i % 2 == 0)? "row-odd" : "row-even";
          * row.onmouseover = function(evt) {hover(evt, true);}
          * row.onmouseout = function(evt) {hover(evt, false);}
         */    
         
         
         var cell = row.insertCell(0);
         cell.className = "first";
         cell.style.padding="0px";         
         var html = '';
         html += '<div class="abstract" style="padding-top=0;padding-bottom:0;">'; 
         html += '<div class="abstract-date"> ';                  
         html += _transactions[i].Date;                   
         /*
             var _d = new Date();
        	 _d.setTime(_transactions[i].Time);
        	 html += _d.toTimeString();
        	 html += ' '+_transactions[i].Time;
         */         
         html += '</div>';
        
         html += '<a class="abstract-link" onclick="'+DISP+'.displayDialog(' + i + ')" '; 
         html += 'href="javascript:void(0)">';         
         html += _transactions[i].Issuer;
         html += ": <br/>";
         html += _transactions[i].Transaction;
         html += " (";
         html += _transactions[i].Price;
         html += ")";
         html += '</a>';
         html += '</div>';
         cell.innerHTML = html;
       }
    }

    /**
     * Call the server and get the latest management transactions
     */
    function _refresh() {
        dojo.xhrGet( {
            url             : URL_JSON,
            handleAs        : "json-comment-filtered",
            timeout         : 15000,      // in miliseconds
            preventCache    : true,

            // the load function will be called on a successful response
            load: function(response, ioArgs) {
                if (response != null && response.toString() != _transactions.toString()) {
                  _transactions = response;
                  _updateTransactions();
                }
            },      // end load

            error: function(response, ioArgs) {
            }      // end error
        } );
    }               
}


/**
 * Hover a table row
 * @param evt The triggered event on mouseover/out
 * @param doHover true/false
 */
var hover = function(evt, doHover) {
    if (!evt) evt = window.event;
    var src = (evt.target != null)? evt.target : evt.srcElement;
    while(src.tagName.toUpperCase()!='TR' && src!=null) {
        src =  src.parentNode;
    }
    if (src != null) {
      var hover = (doHover)? "-mouseover" : "";
      src.className = ((src.rowIndex) % 2 == 0 ? "row-odd"+hover : "row-even"+hover ); 
    }  
}

