/*
NvTaskMgmt Main Javascript
*/

// debug pattern

if (typeof DEBUG === 'undefined') DEBUG = false; // will be removed


window.addEvent('domready', function() {
    var statusMonitorBtn = $('btn_ShowStatusMonitor');
    if ( statusMonitorBtn ) {
        var oStatusMonitor = new statusMonitor;
        oStatusMonitor.addEvent(statusMonitorBtn);
        
    }
});


var statusMonitor = new Class({
    
    timer: 0,
    timerClock: 0,
    xhrData: 0,
    addEvent: function (statusMonitorBtn)
    {
        statusMonitorBtn.addEvent('click', function(event){
            // windows size calculation
            
            var title = 'Status Monitor';
            var self = this;
            this.getData($('txt_ShowStatusMonitor').value);
        }.bind(this));
    },
    popUpFunction: function (width, height, self) {
        DEBUG && console.log('popUpFunction ');
       // this.popUpFunction('Status Monitor', '<div id=\'tabl\'></div>', availWidth-30, availHeigth-100, self);
        new LightFace({ 
            title: this.xhrData.overlay.title, 
            width: width,
            height: height,
            content: this.xhrData.overlay.content,
            buttons: [
                {
                    title: this.xhrData.buttons.close,
                    event: function() { 
                        this.close(); 
                        this.getBox().destroy();
                        clearInterval(self.timer);
                        self.timer=0;
                    }
                },
                {
                    title: this.xhrData.buttons.reload,
                    event: function() { 
                        self.loadGrid();
                    }
                },
                {
                    title: this.xhrData.buttons.ReduceFontsize,
                    event: function() { 
                        self.resizeText(-1);
                    }
                },
                {
                    title: this.xhrData.buttons.IncreaseFontsize,
                    event: function() { 
                    	self.resizeText(1);
                    }
                }
            ]
        }).open();
        if ( typeof this.xhrData.grid.content!='undefined' ) {
            this.showGrid(this.xhrData.grid);
            this.timer = this.loadGrid.periodical(5000, this);
        }
        else {
            this.showClock();
            this.timerClock = this.showClock.periodical(1000);
            this.timer = this.loadGrid.periodical(5000, this);
        }
        
        this.resizeText(this.xhrData.aConfig.fontsize);
    },
    
    getData: function (areaname) {
        
        var oData = new Request({
            url: 'index.php?module=task_list&action=ajax_statusmonitor&a=' + $time()+$random(1, 10000),
            method: 'get',
            noCache: true,
            onSuccess: function(responseText){
              this.xhrData = JSON.decode(responseText);
              DEBUG && console.log(this.xhrData);
              var self = this;
              var w = window,
                d = document,
                e = d.documentElement,
                g = d.getElementsByTagName('body')[0],
                availWidth = w.innerWidth || e.clientWidth || g.clientWidth,
                availHeigth = w.innerHeight|| e.clientHeight|| g.clientHeight;
              DEBUG && console.log('Heigth: '+availHeigth+' Width '+availWidth);
              
              this.popUpFunction(availWidth-40, availHeigth-100, self);
          }.bind(this)
        }).send('areaname='+areaname);
    },
    
    showGrid: function (gridData) {
        
        DEBUG && console.log(this.xhrData.grid);
        
        // check if there is grid already
        var gridTable = $$("table.ariaGrid");
        if ( gridTable ) {
            DEBUG && console.log('Grid table already exists');
            gridTable.destroy();
        }
        else {
            DEBUG && console.log('No Grid table!!!');
        }
        
        var container = $('statusMonitorGrid');
        
        var grid = new AccessibleGrid({
            properties: {
                border: 1,
                cellspacing: 3
            },
            gridContainer : container,
            gridname:'Bodydata',
            headers: this.xhrData.grid.headers,
            rows: this.xhrData.grid.content,
            zebra: true
        });
        
        grid.inject(container);
        grid.enableSort();
    },
    
    loadGrid: function() {
        var container = $('statusMonitorGridContainer');
        if ( !container ) {
            DEBUG && console.log('Clear Interval no overlay any more');
            clearInterval(this.timer);
            this.timer=0;
            clearInterval(this.timerClock);
            this.timerClock=0;
            return true;
        }
        
        var oData = new Request({
            url: 'index.php?module=task_list&action=ajax_statusmonitor&a=' + $time()+$random(1, 10000),
            method: 'get',
            noCache: true,
            onSuccess: function(responseText){
                responseText = JSON.decode(responseText);
                this.xhrData.grid = responseText.grid;
                if ( typeof this.xhrData.grid.content!='undefined' ) {
                    this.showGrid(this.xhrData.grid);
                    $('statusMonitorClock').set('html', '');
                    if (this.timer==0) {
                        this.timer = this.loadGrid.periodical(5000, this);
                    }
                    clearInterval(this.timerClock);
                    this.timerClock=0;
                }
                else {
                    this.showClock();
                    if ( this.timerClock==0) {
                        this.timerClock = this.showClock.periodical(1000);
                        this.showGrid(this.xhrData.grid);
                    }
                    if (this.timer==0) {
                        this.timer = this.loadGrid.periodical(5000, this);
                    }
                }
                // this.showGrid(this.xhrData.grid);
                $('lastupdate_date').set('html', this.xhrData.grid.dateTime);
                
          }.bind(this)
        }).send('areaname='+$('txt_ShowStatusMonitor').value+'&bGridOnly=1');
    },
    
    resizeText: function(multiplier) {
        if ( $('statusMonitorGridContainer').style.fontSize == "") {
            $('statusMonitorGridContainer').style.fontSize = "0.8em";
        }
        //$('statusMonitorGrid').style.fontSize = parseFloat($('statusMonitorGrid').style.fontSize) + (multiplier * 0.2) + "em";
        $('statusMonitorGridContainer').style.fontSize = parseFloat($('statusMonitorGridContainer').style.fontSize) + (multiplier * 0.2) + "em";
    },
    
    showClock: function() {
        var container = $('statusMonitorGridContainer');
        if ( !container ) {
            DEBUG && console.log('Clear Interval no clock any more!!!!');
            clearInterval(this.timerClock);
            this.timerClock=0;
            clearInterval(this.timer);
            this.timer=0;
            return true;
        }
        
        oDateTime = new Date;
        var hour   = (oDateTime.getHours() > 9) ?   '' + oDateTime.getHours()   : '0' + oDateTime.getHours();
        var minute = (oDateTime.getMinutes() > 9) ? '' + oDateTime.getMinutes() : '0' + oDateTime.getMinutes();
        var second = (oDateTime.getSeconds() > 9) ? '' + oDateTime.getSeconds() : '0' + oDateTime.getSeconds();
        
        
        var sFormatedTime = hour +':'+ minute +':'+ second;
        $('statusMonitorClock').set('html', '<p>'+sFormatedTime+'</p>');
        // document.getElementById("ur").innerHTML = UR_Indhold;
    },
    showFilled: function (number) 
    {
        return (number > 9) ? "" + number : "0" + number;
    }

});

