﻿var FinancialCalendar = {
    UpdateEventsTimer: null,
    CurrentEventType: null, /*I=indicators, H=holidays*/
    CurrentEventsHolidaysVersion: -1,
    CurrentEventsIndicatorVersion: -1,
    Interval: null,
    CachedIndicators: null,
    CachedHolidays: null,
    CurrentEventTypeChanged: false,
    ModuleId: 0,
    ModuleDefID: 0,
    IsTimerRunning: false,
    ToolsAndInfoHandler : null,
    //	_captionInitialized:false,
    
    
    set_Interval: function(interval){
        if (!isNaN(interval)) 
            FinancialCalendar.Interval = interval;
        else 
            FinancialCalendar.Interval = 30000;
    },
    init: function(interval, moduleId, moduleDefID){
        /*timer which update the events asyc*/
        FinancialCalendar.set_Interval(interval);
        FinancialCalendar.IsTimerRunning = false;
        
        
        $.timer(FinancialCalendar.Interval,function(timer)
        {
            if (FinancialCalendar.IsTimerRunning)
                FinancialCalendar.update();
        });
    
        /*
        FinancialCalendar.UpdateEventsTimer = $create(Ajax.Timer, {
            enabled: false,
            id: "UpdateEventsTimer",
            interval: FinancialCalendar.Interval
        }, {
            tick: UpdateEvents_OnTick
        }, null, null);
        */
        
        FinancialCalendar.ModuleId = moduleId;
        FinancialCalendar.moduleDefID = moduleDefID;
        
        /*indicator columns array*/
        var o = new Array(); /*temp array object*/
        o.push(FinancialCalendar.ch_ind_Date);
        o.push(FinancialCalendar.ch_ind_Importance);
        o.push(FinancialCalendar.ch_ind_Time);
        o.push(FinancialCalendar.ch_Ind_Location);
        o.push(FinancialCalendar.ch_ind_IndicatorName);
        o.push(FinancialCalendar.ch_ind_Period);
        o.push(FinancialCalendar.ch_ind_Unit);
        o.push(FinancialCalendar.ch_ind_Forecast);
        o.push(FinancialCalendar.ch_ind_Previous);
        o.push(FinancialCalendar.ch_ind_Actual);
        FinancialCalendar.ColumnsNamesIndicators = o;
        
        o = new Array()
        o.push('ch_ind_Date');
        o.push('ch_ind_Importance');
        o.push('ch_ind_Time');
        o.push('ch_ind_IndicatorName');
        o.push('ch_Ind_Location');
        o.push('ch_ind_Period');
        o.push('ch_ind_Unit');
        o.push('ch_ind_Forecast');
        o.push('ch_ind_Previous');
        o.push('ch_ind_Actual');
        FinancialCalendar.columnsIndicatorKeys = o;
        
        /*holidays columns array*/
        o = new Array()
        o.push(FinancialCalendar.ch_holidaysDate);
        o.push(FinancialCalendar.ch_hol_Location);
        o.push(FinancialCalendar.ch_hol_Name);
        FinancialCalendar.ColumnsNamesHolidays = o;
        
        o = new Array()
        o.push('ch_holidaysDate');
        o.push('ch_hol_Location');
        o.push('ch_hol_Name');
        FinancialCalendar.columnsHolidaysKeys = o;
        
        
        /*start timer*/
        FinancialCalendar.changeCurrentEventType("I");
        $('#aIndicators').removeClass('current-step');
        $('#aHolidays').removeClass('not-current-step');
        $('#aIndicators').addClass('current-step');
        $('#aHolidays').addClass('not-current-step');
        FinancialCalendar.start();
        
       /* 
        function UpdateEvents_OnTick(sender, args){
            FinancialCalendar.update();
        }
        */
        
    },
    
    dispose: function(){
        FinancialCalendar.CachedIndicators = null;
        FinancialCalendar.CachedHolidays = null;
        FinancialCalendar.ColumnsNamesIndicators = null;
        FinancialCalendar.ColumnsNamesHolidays = null;
        
    },
    update: function(){
        FinancialCalendar.stop();
        var localOffset =  new Date().getTimezoneOffset();
        switch (FinancialCalendar.CurrentEventType) {
            case "I":/*indicator*/
                $.get(FinancialCalendar.ToolsAndInfoHandler,
                  {
                    action: "GetFinancialCalendarIndicators",
                    localOffset:localOffset,
                    cultureId:Site.CultureId,
                    version: FinancialCalendar.CurrentEventsIndicatorVersion
                  },
                  FinancialCalendar.OnCallGetFinancialCalendarEventsComplete, //calback function
                  FinancialCalendar.OnCallGetFinancialCalendarEventsError     // onError function
                  );
                  
                break;
            case "H": /*holidays*/
                 $.get(FinancialCalendar.ToolsAndInfoHandler,
                  {
                     action: "GetFinancialCalendarHolidays",
                     localOffset:localOffset,
                     cultureId:Site.CultureId,
                     version: FinancialCalendar.CurrentEventsHolidaysVersion
                  },
                  FinancialCalendar.OnCallGetFinancialCalendarEventsComplete, //calback function
                  FinancialCalendar.OnCallGetFinancialCalendarEventsError     // onError function
                  );
            
                break;
        }
    },
    toggleEventType: function(){
        switch (FinancialCalendar.CurrentEventType) {
            case "I":/*indicator*/
                FinancialCalendar.changeCurrentEventType("H");
                break;
            case "H": /*holidays*/
                FinancialCalendar.changeCurrentEventType("I");
                break;
        }
    },
    changeCurrentEventType: function(newType){
        if (FinancialCalendar.CurrentEventType != newType) {
            FinancialCalendar.CurrentEventTypeChanged = true;
        }
        FinancialCalendar.CurrentEventType = newType;
        
        FinancialCalendar.update();
    },
    updateCaption: function(){
        $('#fcCaption').text(FinancialCalendar.CurrentEventType == "I" ? FinancialCalendar.CaptionIndicators : FinancialCalendar.CaptionHolidays);
    },
    stop: function(){
        FinancialCalendar.IsTimerRunning = false;
        //FinancialCalendar.UpdateEventsTimer.set_enabled(false);
    },
    start: function(){
    FinancialCalendar.IsTimerRunning = true;
    
    },
    showEmptyTableMessage: function(tableContainer){
        tableContainer.html("<div class=\"fc-no-data\">" + FinancialCalendar.NoData + "</div>");
    },
    unload: function(){
        FinancialCalendar.stop();
        FinancialCalendar.dispose();
        
    },
    renderFinancialCalendarEventsTable: function(dataTable, tableContainer){
        function addTextCell(sb, text, dontAddSeperator, tdClasses, style){
            if (!tdClasses) 
                tdClasses = "";
            
            if (FinancialCalendar.CurrentEventType == "I") 
                sb.push("<td class=\"" + tdClasses + " left-border-cell fc-cell \">");
            else 
                sb.push("<td class=\"" + tdClasses + " left-border-cell-offset fc-cell \">");
            
            sb.push(text || "&nbsp");
            
            if (dontAddSeperator) /*buttom dotted line*/ 
                sb.push("</td>");
            
        }
        
        /*gets stringBuilder and rows collection*/
        function renderIndicatorRows(sb, rows){
            var currentDate = null;
            var lastRowBeforeSeperatorLine = false;
            var ltrTdClass = 'td-ltr';
            var centerTdClass = 'td-center';
            
            var columns = FinancialCalendar.ColumnsNamesIndicators;
                        
            for (var r = 0, row; row = rows[r]; r++) {
                //rows[r]['IndicatorDate'] = new Date(rows[r]['IndicatorDate']);//.year, rows[r]['IndicatorDate'].month, rows[r]['IndicatorDate'].day, rows[r]['IndicatorDate'].hour,rows[r]['IndicatorDate'].minute,rows[r]['IndicatorDate'].second);
            
                /*if is a new group of date, but not the first group draw a thick line*/
                if (currentDate!==null && (rows[r]['IndicatorDate'].split(' ')[0] != currentDate.split(' ')[0] && 0 != r)) {
                    sb.push("<tr><td colspan=10><div class=border-top-solid-medium>&nbsp</div></td></tr>");
                }
                lastRowBeforeSeperatorLine = (typeof(rows[r + 1]) != 'undefined' && rows[r]['IndicatorDate'].split(' ')[0] != rows[r + 1]['IndicatorDate'].split(' ')[0]) == true ? true : false;
                
                /*calc local date and time of indicators.*/
                var  dateString , timeString ,dateTimeArray;
                //localDateTimeString = localDateTimeFormating(rows[r]['IndicatorDate']);
                dateTimeArray = (rows[r]['IndicatorDate']).split(' ');
                dateString=dateTimeArray[0];
                timeString=dateTimeArray[1];
               
                /*Indicator Date*/
                sb.push('<tr><td class=date-group>');
                if (currentDate===null ||currentDate.split(' ')[0] != rows[r]['IndicatorDate'].split(' ')[0]) {
                    sb.push(dateString);
                    currentDate = rows[r]['IndicatorDate'];
                }
                sb.push('</td>');
                
                /*Importance*/
                sb.push('<td>');
                
                switch (rows[r]['Importance']) {
                    case 0:
                        break; /*no importance defined*/
                    case 1: /*High*/
                        sb.push("<img class=importance-img src=\'" + Site.url + "images/Puma/HighImportance_8x8.gif'/>");
                        break;
                    case 2: /*Medium*/
                        sb.push("<img class=importance-img src=\'" + Site.url + "images/Puma/MediumImportance_8x8.gif'/>");
                        break;
                    case 3: /*Low*/
                        sb.push("<img class=importance-img src=\'" + Site.url + "images/Puma/LowImportance_8x8.gif'/>");
                        break;
                }
                sb.push('</td>');
                
                /*time*/
                addTextCell(sb, timeString, lastRowBeforeSeperatorLine, ltrTdClass);
                
                /*Location*/
                addTextCell(sb, rows[r]['LocationName'], lastRowBeforeSeperatorLine, ltrTdClass);
                
                /*Indicator name*/
                var description = rows[r]['Description'] ? rows[r]['Description'] : '';
                var desc = description.replace(/\n/gim, '\\n');
                desc = desc.replace(/\r/gim, '\\r');
                sb.push("<td class=\"bubble-event "+ltrTdClass+"\" displayHeader=\"" + rows[r]['IndicatorName'] + "\" displayContent = \"" + desc + "\"  style=\"cursor:pointer;white-space:normal\"><div class=left-border-cell>");
                sb.push(rows[r]['IndicatorName'] || "&nbsp");
                
                if (lastRowBeforeSeperatorLine) /*buttom dotted line*/ 
                    sb.push("</div></td>");
                
                /*Period*/
                addTextCell(sb, rows[r]['PeriodName'], lastRowBeforeSeperatorLine, centerTdClass);
                
                /*Symbol*/
                addTextCell(sb, rows[r]['Symbol'], lastRowBeforeSeperatorLine, centerTdClass);
                
                /*Forecast*/
                addTextCell(sb, rows[r]['Forecast'], lastRowBeforeSeperatorLine, centerTdClass);
                
                /*Forecast*/
                addTextCell(sb, rows[r]['Previous'], lastRowBeforeSeperatorLine, centerTdClass);
                
                /*Actual*/
                addTextCell(sb, rows[r]['Actual'], lastRowBeforeSeperatorLine, centerTdClass);
                
                
                sb.push("</tr>")
                
                if (!lastRowBeforeSeperatorLine) 
                    sb.push("<tr><td style=\"line-height:1px;\">&nbsp</td><td style=\"line-height:1px;\">&nbsp</td><td style=\"line-height:1px;\" colspan= " + (columns.length - 2) + "><div class=border-bottom-dotted></div></td></tr>")
                
            }
        }
        
        /*gets stringBuilder and rows collection*/
        function renderHolidaysRows(sb, rows){
            var currentDate = null;
            var columns = FinancialCalendar.ColumnsNamesHolidays;
            
            
            for (var i = 0, row; row = rows[i]; i++) {
                rows[i]['HolidayDate'] = rows[i]['HolidayDate'].split(' ')[0];
                /*if is a new group of date, but not the first group draw a thick seperator line*/
                if (currentDate!==null && (rows[i]['HolidayDate'] != currentDate && 0 != i)) {
                    sb.push("<tr><td colspan=3><div class=border-top-solid-medium>&nbsp</div></td></tr>");
                }
                
                lastRowBeforeSeperatorLine = (typeof(rows[i + 1]) != 'undefined' && rows[i]['HolidayDate'] != rows[i + 1]['HolidayDate'].split(' ')[0]) == true ? true : false;
                
                /*Holiday Date*/
                sb.push("<tr><td class=date-group>");
                if (currentDate===null || rows[i]['HolidayDate'] != currentDate) {
                    sb.push(rows[i]['HolidayDate']);
                    currentDate = rows[i]['HolidayDate'];
                }
                sb.push('</td>');
                
                /*Location*/
                addTextCell(sb, rows[i]['LocationName'], lastRowBeforeSeperatorLine);
                
                /*HolidayName*/
                
                addTextCell(sb, rows[i]['HolidayName'], lastRowBeforeSeperatorLine, 'holiday-width');
                
                if (!lastRowBeforeSeperatorLine) 
                    sb.push("<tr><td style=\"line-height:1px;\"></td><td style=\"line-height:1px;\" colspan= " + (columns.length - 1) + "><div class=border-bottom-dotted></div></td></tr>")
                
            }
            
        }
        
        function renderHeaders(sb){
            var columns = FinancialCalendar.CurrentEventType == "I" ? FinancialCalendar.ColumnsNamesIndicators : FinancialCalendar.ColumnsNamesHolidays;
            
            
            sb.push("<tr><td colspan=" + columns.length + "><div class=border-top-solid-medium>&nbsp</div></td></tr>");
            sb.push("<tr>");
            
            for (var i = 0, j = columns.length; i < j; i++) {
                if (0 == i) {
                    sb.push("<td class=\"header-offset fc-header\">");
                }
                else {
                    var _class = columns[i] == "" ? "" : "left-border-cell";
                    if (FinancialCalendar.CurrentEventType == "I") 
                        sb.push("<td class=\"" + (columns[i] == "" ? "" : "left-border-cell") + " fc-header\">");
                    else 
                        sb.push("<td class=\"" + (columns[i] == "" ? "" : "left-border-cell-offset") + " fc-header\">");
                }
                
                sb.push(columns[i]); /*header text*/
                sb.push("</td>");
            }
            
            sb.push("</tr>");
            /*underline of headers*/
            sb.push("<tr><td colspan=" + columns.length + "><div class=border-top-solid-thin>&nbsp</div></td></tr>");
            
        }
        
        if (!dataTable.rows || dataTable.rows.length == 0) //if the dataTable is empty.
            return;
        
        /*render table*/
        var sb = new Array();//Sys.StringBuilder();
        
        sb.push("<table cellpadding=0>");
        
        /*render headers*/
        renderHeaders(sb);
        
        /*render rows*/
        if (FinancialCalendar.CurrentEventType == "I") 
            renderIndicatorRows(sb, dataTable.rows);
        else 
            renderHolidaysRows(sb, dataTable.rows);
        
        sb.push("</table>");
        
        /*set render result*/
        
        var sbString= '';
        for (var i=0;i< sb.length; i++)
            sbString += sb[i];
        var newHtmlTable = sbString;
        tableContainer.html(newHtmlTable);
        
        /*description bubble:*/
        $('.bubble-event').each(function(i, el){
            $(el).hover(function(){
                if ($(this).attr('displayContent') && $(this).attr('displayContent') != '') {
                    $('#bubble-header').text($(this).attr('displayHeader'));
                    $('#bubble-context').text($(this).attr('displayContent'));
                    $('#bubble-context').css({'height':56,'overflow':'hidden'});
                    $('#bubble').show().css({
                        'top': $(this).offset().top-111,
                        'left': $(this).offset().left-120
                    });
                }
            }, function(){
                $('#bubble').hide();
            });
        });
        
        /*cache render result*/
        if (FinancialCalendar.CurrentEventType == "I") {
            FinancialCalendar.CachedIndicators = newHtmlTable;
        }
        else {
            FinancialCalendar.CachedHolidays = newHtmlTable;
        }
        
        /*clear string builder*/
        sb = null;
        
    },
    updateVersion: function(newVersion){
        if (FinancialCalendar.CurrentEventType == "I") {
            FinancialCalendar.CurrentEventsIndicatorVersion = newVersion;
        }
        else {
            FinancialCalendar.CurrentEventsHolidaysVersion = newVersion;
        }
        
    },
    /******************************** WS callback handlers *******************************/
    OnCallGetFinancialCalendarEventsError: function(error){
        var data = eval('(' + error + ')');
        //alert(data);
    },
    
    OnCallGetFinancialCalendarEventsComplete: function(result){
        var data = eval('(' + result + ')');
        var container = $('#tblEventsContainer');
        //data.Table = eval('(' + data.Table + ')');
        /* if server returned non empty data */
        if (data && data.Table && data.Table.rows && data.Table.rows.length > 0) {
            FinancialCalendar.updateVersion(data.NewVersion);
            FinancialCalendar.renderFinancialCalendarEventsTable(data.Table, container);
        }
        else /*didnt return rows*/ {
            if (isVersionEquals(data.NewVersion) == true) {
                /*user initated update but no rows returned thus read from cache*/
                if (FinancialCalendar.CurrentEventTypeChanged == true) {
                    if (FinancialCalendar.CurrentEventType == "I") /*indicators*/ {
                        container.html(FinancialCalendar.CachedIndicators);
                    }
                    else /*holidays*/ {
                        container.html(FinancialCalendar.CachedHolidays);
                    }
                }
                
                /*else do nothing: - timer initiated update but there is no update to do*/
            
            }
            /*version has changed but no data returned thus empty table returned*/
            else {
                FinancialCalendar.showEmptyTableMessage(container);
            }
        }
        
        FinancialCalendar.CurrentEventTypeChanged = false;
        
        /*change module caption*/
        FinancialCalendar.updateCaption();
        
        /*restart timer*/
        FinancialCalendar.start();
        
        /*checks if new version from server equals to current version*/
        function isVersionEquals(newVersion){
            if (FinancialCalendar.CurrentEventType == "I") 
                return FinancialCalendar.CurrentEventsIndicatorVersion == newVersion;
            return FinancialCalendar.CurrentEventsHolidaysVersion == newVersion;
        }
    }
}
