﻿$(document).ready(
function () {

    $(".surveyGraph").show();
    $(".obstacleGraph").hide();
    $(".dvSubcutsInfo").hide();


    $("ul.graphs-nav a.tabObstacle").click(function (event) {
        event.stopPropagation();
        event.preventDefault();

        /*Remove the Active class from the Active Tab*/
        $("ul.graphs-nav li").removeClass("tablinks active");

        /*Add the Active class to the Current Tab. 
        So that there will be clear distintion for the user the data being displayed on the screen is related to which tab*/
        $(this).parent("li").addClass("tablinks active");

        //$(".surveyGraph").fadeOut(1);
        //$(".obstacleGraph").fadeIn(1);

        $(".surveyGraph").hide();
        $(".obstacleGraph").show();
    });

    $("ul.graphs-nav a.tabSurvey").click(function (event) {
        event.stopPropagation();
        event.preventDefault();

        /*Remove the Active class from the Active Tab*/
        $("ul.graphs-nav li").removeClass("tablinks active");

        /*Add the Active class to the Current Tab. 
        So that there will be clear distintion for the user the data being displayed on the screen is related to which tab*/
        $(this).parent("li").addClass("tablinks active");

        //$(".obstacleGraph").fadeOut(1);
        //$(".surveyGraph").fadeIn(1);

        $(".obstacleGraph").hide();
        $(".surveyGraph").show();
    });

    $("ul.graphs-nav a#aIFCVerfied").click(function (event) {
        event.stopPropagation();
        var target_offset = $("#notes").offset();
        if (target_offset != null) {
            var target_top = target_offset.top;
            //goto that anchor by setting the body scroll top to anchor top
            $('html, body').animate({ scrollTop: target_top }, 500);
        }

    });
    $(".cutsSelector").change(function (event) {
        event.preventDefault();
        event.stopPropagation();

        var cut = $(this).val();

        if (cut == "group")
            return;

        var tpName = $(".HdTopicName").val(); // $(this).attr("tpName");
        var hashStr;

        if (cut == 'select-a-cut' || cut == 'no-subgroups') {
            cut = '';
        }
        $("#indicator-datagrid").addClass(" loading");

        var qryStr = "?ajax=1"
        if (window.location.hash.indexOf("--") != -1) {
            hashStr = window.location.hash.split("--");
            qryStr = qryStr + "&cut=" + cut + "&topic=" + hashStr[0].substring(1, hashStr[0].length);
        }
        else {
            qryStr = qryStr + "&cut=" + cut + "&topic=" + tpName;
        }

        $.get($(this).attr("rel") + $(this).attr("eco") + qryStr, function (data) {
            $(".IndicatorData").html(data);
            //$(".IndicatorData").hide();
            //$(".IndicatorData").fadeIn(1000);


            $("#indicator-datagrid").removeClass("loading");

            //window.location.hash will have value like this: reg-and-tax--exporter-type
            //split it by -- gives you topic : reg-and-tax and cut: exporter-type
            //replace cut by your new value


            attachClickEvent();
            if (cut == "") {
                $(".dvSubcutsInfo").hide();
            }

            if (cut != '')
                cut = "--" + cut;

            if (window.location.hash.indexOf("--") != -1) {
                window.location.hash = hashStr[0] + cut;
            }
            else {
                window.location.hash = "#" + tpName + cut;
            }


        });
    });


    $(".subCutsExpandedShow").click(subCutsExpander_click());
    $(".subCutsCollapsedShow").click(subCutsExpander_click());
    $(".subCutsExpandAllShow").click(subCutsAllExpander_click());
    $(".subCutsCollapseAllShow").click(subCutsAllExpander_click());


    function subCutsExpander_click(event) {
        if (event == undefined)
            return;

        event.preventDefault();
        event.stopPropagation();
        var subCutsExpandedShow = "subCutsExpandedShow";
        var clsName = $(this)[0].className;
        $(this).removeClass(clsName);
        var clsName1 = $.trim(clsName);
        if (clsName1 == subCutsExpandedShow) {
            $($($($(this).parent().parent("tr.TableItem"))[0]).children().find(".dvSubcutsInfo")[0]).fadeOut(1000);
            $($($($(this).parent().parent("tr.TableItem"))[0]).children().find(".dvIndDesc")[0]).fadeOut(1000);
            $(this).addClass("subCutsCollapsedShow");

        }
        else {
            $($($($(this).parent().parent("tr.TableItem"))[0]).children().find(".dvSubcutsInfo")[0]).fadeIn(1000);
            $($($($(this).parent().parent("tr.TableItem"))[0]).children().find(".dvIndDesc")[0]).fadeIn(1000);
            $(this).addClass("subCutsExpandedShow");

            $("td.noCuts div.dvSubcutsInfo").hide();
        }
    }

    function subCutsAllExpander_click(event) {
        if (event == undefined)
            return;

        event.preventDefault();
        event.stopPropagation();

        $("#indicator-datagrid").addClass("loading");

        var subCutsExpandedShow = "subCutsExpandAllShow";
        var clsName = $(this)[0].className;
        $(this).removeClass(clsName);
        var clsName1 = $.trim(clsName);
        if (clsName1 == subCutsExpandedShow) {
            var newclass = "subCutsCollapsedShow";
            $(".subCutsExpandedShow").each(function () {
                var clsName2 = $(this)[0].className;
                $(this).removeClass(clsName2);
                $(this).addClass(newclass);
            });
            $(".dvSubcutsInfo").fadeOut(1000);
            $(".dvIndDesc").fadeOut(1000);
            $(this).addClass("subCutsCollapseAllShow");
        }
        else {

            var newclass = "subCutsExpandedShow";
            $(".subCutsCollapsedShow").each(function () {
                var clsName2 = $(this)[0].className;
                $(this).removeClass(clsName2);
                $(this).addClass(newclass);
            });
            $(".dvSubcutsInfo").fadeIn(1000);
            $(".dvIndDesc").fadeIn(1000);
            $(this).addClass("subCutsExpandAllShow");

            //alert($("td.noCuts div.dvSubcutsInfo"));
            $("td.noCuts div.dvSubcutsInfo").hide();
        }

        $("#indicator-datagrid").removeClass("loading");
    }


    /*This function is used when Tabs are being clicked on both Economy Details as well as Topic Details screens*/
    $("ul.tab-menu-nav a").click(function (event) {
        event.stopPropagation();
        event.preventDefault();
        var cut = $(".cutsSelector").val();

        if (cut == 'select-a-cut' || cut == 'no-subgroups')
            cut = '';

        $("#indicator-datagrid").addClass(" loading");

        /*Remove the Active class from the Active Tab*/
        $("ul.tab-menu-nav li").removeClass("tablinks active");
        /*Add the Active class to the Current Tab. 
        So that there will be clear distintion for the user the data being displayed on the screen is related to which tab*/
        $(this).parent("li").addClass("tablinks active");
        /*Get the First Query String parameter into a varible*/
        var topicQueryString = $(this).attr("href").split("?")[1];
        /*Get the value from the First query string parameter into a variable
        This values will be used to append to the URL which make the url as user friendly   */
        var topicName = topicQueryString.split("=")[1];

        //        var economy = $(this).attr("href");
        //        var urlEconomy = $(this).attr("href").split("/");
        //        var yearEconomy = urlEconomy[3].split("_");
        //        topicQueryString = yearEconomy[1].split("?");
        //        topicName = topicQueryString[1].split("=")[1];
        //        var urlString = "/" + urlEconomy[1] + "/" + urlEconomy[2] + "/" + topicQueryString[0] + "/" + yearEconomy[0] + "?" + topicQueryString[1];
        /*Get the data passed from the Code Behind page*/
        $.get($(this).attr("href") + "&cut=" + cut + "&ajax=1", function (data) {
            /*$.get($(this).attr("href") + "&cut=" + cut + "&ajax=1", function (data) {*/
            /*Render the received data inside the Div tag where tab-Details class is used. 
            This is the place Data related to the tabs would be shown */
            $(".IndicatorData").html(data);
            //$(".IndicatorData").hide();
            //$(".IndicatorData").fadeIn(1000);
            attachClickEvent();

            if (cut == "" || cut == 'no-subgroups') {
                $(".dvSubcutsInfo").hide();

                window.location.hash = "#" + topicName
            }
            else
                window.location.hash = "#" + topicName + "--" + cut;


            $("#indicator-datagrid").removeClass("loading");
        });
    });


    $(".ExportPrintExcel input.chkAllTopics").click(function () {
        var checkedStatus = $(this).is(":checked");
        if (checkedStatus) {
            $('#btnPrint').removeAttr('disabled');
            $('#btnExcel').removeAttr('disabled');
        }
        else {
            $('#btnPrint').attr('disabled', 'disabled');
            $('#btnExcel').attr('disabled', 'disabled');
        }
        $(".ExportPrintExcel input.chkTopic:checkbox").each(function () {
            $(this).attr("checked", checkedStatus);
        });
    });

    $(".ExportPrintExcel input.chkTopic:checkbox").click(function () {
        var checkedStatus = $(this).is(":checked");
        var flagChecked = false;
        var flagUnchecked = true;
        $(".ExportPrintExcel input.chkTopic:checkbox").each(function () {
            if ($(this).is(":checked")) {
                flagChecked = true;
            }
            else
                flagUnchecked = false;
        });

        if (flagChecked) {
            $('#btnPrint').removeAttr('disabled');
            $('#btnExcel').removeAttr('disabled');
        }
        else {
            $('#btnPrint').attr('disabled', 'disabled');
            $('#btnExcel').attr('disabled', 'disabled');
        }

        $(".ExportPrintExcel input.chkAllTopics").attr("checked", flagUnchecked);
    });

    $(".ExportPrintExcel input.chkAllCuts").click(function () {
        var checkedStatus = $(this).is(":checked");
        $(".ExportPrintExcel input.chkCut:checkbox").each(function () {
            $(this).attr("checked", checkedStatus);
        });
    });

    $(".Print").click(function (event) {
        event.stopPropagation();
        event.preventDefault();
        var url = $(this).attr("rel") + $(this).attr("eco") + "&ajax=1&print=1";
        var topicids = "";
        var cutids = "";
        $(".ExportPrintExcel input.chkTopic:checkbox:checked").each(function () {
            if (topicids == "") {
                topicids = $(this).attr("rel");
            }
            else {
                topicids += "," + $(this).attr("rel");
            }

        });

        if (topicids != "") {
            url += "&topicids=" + topicids;
        }

        $(".ExportPrintExcel input.chkCut:checkbox:checked").each(function () {
            if (cutids == "") {
                cutids = $(this).attr("rel");
            }
            else {
                cutids += "," + $(this).attr("rel");
            }
        });

        if (cutids != "") {
            url += "&cutids=" + cutids;
        }

        $.get(url, function (data) {
            $("#toprint").html(data);
            $("#toprint").printElement({ leaveOpen: false, printMode: 'popup' });
            $("#toprint").html("");
        });

    });

    /*This is used when Export to Excel button has been hit to export the screen data into the Excel spreadsheet*/
    $(".excel").click(function (event) {
        event.stopPropagation();
        event.preventDefault();
        /*
        var url = $(this).attr("rel") + $(this).attr("eco") + "&ajax=1&excel=1";
        var topicids = "";
        var cutids = "";
        $(".ExportPrintExcel input.chkTopic:checkbox:checked").each(function () {
        if (topicids == "") {
        topicids = $(this).attr("rel");
        }
        else {
        topicids += "," + $(this).attr("rel");
        }

        });

        if (topicids != "") {
        url += "&topicids=" + topicids;
        }

        $(".ExportPrintExcel input.chkCut:checkbox:checked").each(function () {
        if (cutids == "") {
        cutids = $(this).attr("rel");
        }
        else {
        cutids += "," + $(this).attr("rel");
        }
        });

        if (cutids != "") {
        url += "&cutids=" + cutids;
        }

        var includeStats = $("input.chkSE").is(":checked");

        url += "&includeStats=" + includeStats;
        */

        var topicids = "";
        var cutids = "";
        $(".ExportPrintExcel input.chkTopic:checkbox:checked").each(function () {
            if (topicids == "") {
                topicids = $(this).attr("rel");
            }
            else {
                topicids += "," + $(this).attr("rel");
            }

        });

        $(".ExportPrintExcel input.chkCut:checkbox:checked").each(function () {
            if (cutids == "") {
                cutids = $(this).attr("rel");
            }
            else {
                cutids += "," + $(this).attr("rel");
            }
        });


        var ischkSENOB = $("input.chkSE").is(":checked");

        var HdEcoDataIds = $(".HdEconomyIds").val();


        var mostRecent = true;

        var url = "/Handlers/CustomQuery.ashx?ajax=1&&excel=1" + "&EconomyIds=" + HdEcoDataIds + "&topicIds=" + topicids + "&cutsIds=" + cutids;
        url = url + "&ischkSENOB=" + ischkSENOB + "&mostRecent=" + mostRecent;


        window.location = url;
        return false;
    });

    if (window.location.hash != null && window.location.hash != "") {

        var hash = window.location.hash.split("#")[1];
        var qryStr = "&ajax=1";
        var topic = "";

        var cut = $(".cutsSelector").val();

        if (hash.indexOf("--") != -1) {
            hashStr = hash.split("--");
            cut = hashStr[1];
            qryStr = qryStr + "&cut=" + hashStr[1]; // +"&topic=" + hashStr[0];
            topic = hashStr[0];
            $(".cutsSelector").val(hashStr[1]);

        }
        else
            topic = hash;

        var tablinkObj = $(".tablink" + topic);

        if (tablinkObj != null) {
            $("ul.tab-menu-nav li").removeClass("tablinks active");
            $(tablinkObj).parent("li").addClass("active");

            $.get($(tablinkObj).attr("href") + qryStr, function (data) {
                $(".IndicatorData").html(data);
                attachClickEvent();
                if (cut == '' || cut == 'no-subgroups') {
                    $(".dvSubcutsInfo").hide();
                };
            });

        }
    }

    function attachClickEvent() {
        /*Loop thru each Hyper link to Unbind and Bind the click events*/
        $(".subCutsExpandAllShow").unbind('click');
        $(".subCutsExpandAllShow").bind('click', subCutsAllExpander_click);

        $(".subCutsCollapseAllShow").unbind('click');
        $(".subCutsCollapseAllShow").bind('click', subCutsAllExpander_click);

        $(".subCutsExpandedShow").unbind('click');
        $(".subCutsExpandedShow").bind('click', subCutsExpander_click);

        $(".subCutsCollapsedShow").unbind('click');
        $(".subCutsCollapsedShow").bind('click', subCutsExpander_click);
    }

    //    $(".TopicDataHeader").click(indicatorDataHeader_Click);

    //    function indicatorDataHeader_Click(event) {
    //        event.preventDefault();
    //        event.stopPropagation();
    //        var url = $(this).attr("href");
    //        $.get(url, function (data) {
    //            $(".IndicatorData").html(data);
    //            $(".TopicDataHeader").unbind('click');
    //            $(".TopicDataHeader").bind('click', indicatorDataHeader_Click);
    //        });
    //    }

});
