/**
 * This simple javascript file provides methods to persist the selected tab in a dojo tab container.
 * 
 * $Id: dis.js,v 1.4 2010/07/16 16:17:50 grb Exp $
 */

function ShabMessagesDisplay(initialMessages, language, nbRows) {
    URL_PARAMETERS = "lang=" + language;
    if( nbRows != null) URL_PARAMETERS += "&rows=" + nbRows;
    
    var URL_JSON  = "/obligations/ajax/shab-data.json?" + URL_PARAMETERS;
    
    var URL_INCL  = "/obligations/disclosure/major_shareholders_results_" + language+".html?id=";
    var URL_PRINT = "/obligations/disclosure/major_shareholders_print_"+ language+".html?id=";
    
    var INTERVAL  = "1200000"; // 20min in miliseconds
        
    var TABLE     = "disTable";
    var DIALOG    = "disDialog";
    var PANE      = "disDialogPane";
    var DISP      = "disDisp";    

    // public variable
    this.PUB_DIALOG = DIALOG; 

    // private variables 
    var _messages = initialMessages;
    var _timer = null;
    var _currentId;

    /* PUBLIC */
    
    this.start = function() {
        _updateMessages(); // 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) { 
        _currentMessage = i;
                
        var title; 
        title = _messages[i].DATE;
        title += " ";
        if (language  == "de") {
           title += "Offenlegung von Beteiligungen für ";
         } else if (language == "fr") {
           title += "Publication des participations pour ";
         } else {
           title += "Disclosure of Shareholdings for ";
         }
        title += _messages[i].ISSUER;
        
        var pane = dijit.byId(PANE);
        pane.href = URL_INCL + _messages[i].MSG_NUMBER; 
        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 + _messages[_currentMessage].MSG_NUMBER;
      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 */
    
    /**
     * Display data after update from server
     */
    function _updateMessages() {
		var table = dojo.byId(TABLE);
	    
	    var nrRows = table.rows.length;
	    for (i=0; i<nrRows; i++) {
	       table.deleteRow(0);
	    }      
	    
	    for (i=0; i<_messages.length; i++) {
	       var row = table.insertRow(i);
	       var cell = row.insertCell(0);
	       
	       var html = '<div class="abstract">';
	         html += '<div class="abstract-date">'+_messages[i].DATE + '</div>';
	         html += '<a onclick="'+DISP+'.displayDialog(' + i + ')" '; 
	         html += 'href="javascript:void(0)">';
	         if (language  == "de") {
	           html += "Offenlegung von Beteiligungen für ";
	         } else if (language == "fr") {
	           html += "Publication des participations pour ";
	         } else {
	           html += "Disclosure of Shareholdings for ";
	         }
	         html += _messages[i].ISSUER;
	         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() != _messages.toString()) {
                  _messages = response;
                  _updateMessages();
                }
            },      // end load

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