var ApplicationName = '';
var spDaTa = [];
var AppMappingDT = [];
var arrSPNames = [];
var arrFields = [];
var objTicker = [];
var SortedDT = [];
var arrMenuTitles = [];
var fieldName = '';
(function () {
    $(document).ready(function () {
        try {
            GetAppData();
            
        } catch (er) { }
    });
})();


function GetAppData() {
    try {


        $(document).find("#FinIQTicker, tr#trFinIQTicker").remove();
        if (localStorage.getItem('TickerSpeed') == null) {
            localStorage.setItem('TickerSpeed', 25);
        }
        var ApplicationName = getUrlParameter('menustr');
        if (ApplicationName.split("-").length >= 2) {
            ApplicationName = ApplicationName.split("-")[1].trim();
        } else {
            return true;
        }
        AppMappingDT = [];
        $.ajax({
            type: "POST",
            url: "../FinIQ_Ticker/FinIQ_Ticker_Setup.aspx/Get_Ticker_Data",
            data: JSON.stringify({ "Flag": "SPName", "param1": "", "param2": "", "param3": "", "param4": ApplicationName }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: function (data) {
                if (data.d == null || data.d == "") {
                    $(document).find("#FinIQTicker, tr#trFinIQTicker").remove();
                    return true;
                } else {
                    AppMappingDT = jQuery.parseJSON(data.d);
                    arrSPNames = Get_UniqueID(AppMappingDT, "SPName");
                    $(arrSPNames).each(function (k) {
                        Get_SPData($(arrSPNames)[k]);
                        spDaTa = jQuery.grep(window.AppMappingDT, function (n, index) {
                            return (n.SPName == $(arrSPNames)[k]);
                        });
                        if (spDaTa.length > 0) {
                            pnlFinIQTicker = $('<div id="FinIQTicker"></div>');
                            $(pnlFinIQTicker).append($('<ul></ul>'));

                            $(pnlFinIQTicker).append($('<span id="spTickerSettings" title="Settings">&#x2600;</span>'));

                            pnlTickerSetting = $('<div id="pnlTickerSetting"></div>');
                            $(pnlTickerSetting).append($('<span id="spTicker_OnOff" title="24">Ticker On</span>'));
                            $(pnlTickerSetting).append($('<span id="spTickerSlow" title="Slow">Speed Up</span>'));
                            $(pnlTickerSetting).append($('<span id="spTickerFast" title="Fast">Speed Down</span>'));
                            
                            $(pnlFinIQTicker).append(pnlTickerSetting);
                             
                           $(document).find('table.FinIQ_lblFormName').prepend( $('<tr id="trFinIQTicker"><td colspan="2"></td></tr>'));
                             $('#trFinIQTicker td').append(pnlFinIQTicker); 
                             $('.FinIQ_lblFormName:not(:empty)').css({ 'padding-top': '0px' });
                            if (localStorage.getItem('FinIQTicker') == null) {
                                speed = Number(localStorage.getItem('TickerSpeed'));
                                $(document).find('#FinIQTicker ul').show().css({ 'animation-duration': speed + 's' });
                                $('#FinIQTicker').width('100%');
                                $('#spTicker_OnOff').text('Ticker Off');
                            } else {
                                $(document).find('#FinIQTicker ul').hide();
                                $('#FinIQTicker').width('16px');
                                $('#spTicker_OnOff').text('Ticker On');
                            }
                            $('#spTickerSettings').on('click', function () {
                                $(pnlTickerSetting).toggle();
                            });
                            $('#spTickerFast').on('click', function () {
                                if (localStorage.getItem('TickerSpeed') != null) {
                                    speed = Number(localStorage.getItem('TickerSpeed'));
                                    if (speed < 99) {
                                        speed = Number(speed + 1);
                                    } else {
                                        speed = 24;
                                    }
                                    localStorage.setItem('TickerSpeed', speed);
                                    $(document).find('#FinIQTicker ul').show().css({ 'animation-duration': speed + 's' });
                                }
                            });
                            $('#spTickerSlow').on('click', function () {
                                if (localStorage.getItem('TickerSpeed') != null) {
                                    speed = Number(localStorage.getItem('TickerSpeed'));
                                    if (speed > 0) {
                                        speed = Number(speed - 1);
                                    } else {
                                        speed = 24;
                                    }
                                    localStorage.setItem('TickerSpeed', speed);
                                    $(document).find('#FinIQTicker ul').show().css({ 'animation-duration': speed + 's' });
                                }
                            });
                            $('#FinIQTicker #spTicker_OnOff').on('click', function () {
                                try {
                                    $(document).find('#FinIQTicker ul').toggle();

                                    if ($(document).find('#FinIQTicker ul').is(':visible')) {
                                        localStorage.removeItem('FinIQTicker');
                                        $('#FinIQTicker').width('100%');
                                        GetAppData();                                         
                                        $('#spTicker_OnOff').text('Ticker Off');

                                    } else {
                                        localStorage.setItem('FinIQTicker', 'N');
                                        $('#FinIQTicker').width('16px');
                                        $('#spTicker_OnOff').text('Ticker On');
                                    }
                                } catch (er) {
                                    console.log(er.message);
                                }
                            }).attr('title', 'Hide Ticker');
                        }
                        arrFields = Get_UniqueID(spDaTa, "FieldName");
                        PlotTicker(AppMappingDT, SortedDT, arrFields);
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(document).find("#FinIQTicker").remove();
                console.log("Error while creating list " + XMLHttpRequest.responseText);
            }
        });
    } catch (er) {
        console.log(er.message);
        $(document).find("#FinIQTicker").remove();
    }
}


function PlotTicker(AppMappingDT, SortedDT, arrFields) {
    try {
        $.each(SortedDT, function (a, b) {
            var li = $('<li></li>');
            var span = '';
            $.each(arrFields, function (i) {
                if (span != '') {
                    span = span + " : ";
                }
                span = span + '<span style="' + spDaTa[i].CSSClass + '">' + b[$(arrFields)[i]] + '</span>';
            });
            $(li).append($(span)).css({ "color": window.AppMappingDT[0].Background });
            $(document).find("#FinIQTicker ul").append($(li));
        });
    } catch (er) {
        console.log(er.message);
    }
}
function Get_SPData(SPName) {
    try {
        SortedDT = [];
        $.ajax({
            type: "POST",
            url: "../FinIQ_Ticker/FinIQ_Ticker_Setup.aspx/Get_SP_Data",
            data: JSON.stringify({ "SPName": SPName }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (dt) {
                SortedDT = jQuery.parseJSON(dt.d);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("Error while creating list " + XMLHttpRequest.responseText);
            }
        });
    } catch (er) {
        console.log(er.message);
    }
}

function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};

function Get_UniqueID(oDT, columnID) {
    arrTemp = [];
    var lookup = {};
    for (var ob, i = 0; ob = oDT[i++];) {
        var name = ob[columnID];
        if (!(name in lookup)) {
            lookup[name] = 1;
            arrTemp.push(name);
        }
    }
    return arrTemp;
}


function GetPageTitles() {
    

}