﻿

//window.onresize = function() { buildLayout(); }




$(document).ready(start);


function start() {
//    $.preloadCssImages();

    buildLayout();
    textColumns();
    tableSorting();
    rowRollover();
    setToolTips();
    drillDownTableButtons();
    definitionHeight()
    $("#ulNews").marquee({ pauseOnHover: true, fxEasingShow: "linear", yScroll: "bottom" });


    
    $('#divWrapper').removeClass("hide");
}
function drillDownTableButtons() {
    if ($.querystring({ id: "pg" }) == 6137 || $.querystring({ id: "pg" }) == 6795 || $.querystring({ id: "pg" }) == 6826 || $.querystring({ id: "pg" }) == 6926 || $.querystring({ id: "pg" }) == 6902 || $.querystring({ id: "pg" }) == 6431 || $.querystring({ id: "pg" }) == 6810 || $.querystring({ id: "pg" }) == 6910 || $.querystring({ id: "pg" }) == 6885) {
        $(".no-js").removeClass("no-js");
        $(".no-lpad").removeClass("no-lpad");

        var str = "";
        if ($.querystring({ id: "pg" }) == 6137) {
            str = 551;
            str += "#" + $("#ctl00_ContentPlaceHolder1_CubeClientName").val();
            str += "#" + $("#ctl00_ContentPlaceHolder1_txtGeoBM").val();
            str += "#" + "[Dim Commodity].[Group Name].&[ROWID]"
            str += "#" + $("#ctl00_ContentPlaceHolder1_ddlBenchmark option:selected").text();
        }
        if ($.querystring({ id: "pg" }) == 6795) {
           
            str = 582;
            str += "#" + $("#ctl00_ContentPlaceHolder1_CubeClientName").val();
            str += "#" + $("#ctl00_ContentPlaceHolder1_txtGeoBM").val();
            str += "#" + $("#ctl00_ContentPlaceHolder1_lboxIndustrySectors option:selected").val();
            str += "#[Dim Sex].[Sex Key].&[GENDERID]";

         
            
            str += "#" + $("#ctl00_ContentPlaceHolder1_rbIndustryYear input:checked").val();
            str += "#" + $("#ctl00_ContentPlaceHolder1_txtIndBM").val();
            str += "#" + "[Dim Study Field].[Study Field Category].&[ROWID]";

            if ($("#ctl00_ContentPlaceHolder1_rbIndBM").attr("checked")) {
            str += "#" + $("#ctl00_ContentPlaceHolder1_ddlIndustryExp option:selected").text();
            } else {
            str += "#" + $("#ctl00_ContentPlaceHolder1_ddlBenchmark option:selected").text();
            }
            
            str += "#CATID#GENDERNAME";
        }
        if ($.querystring({ id: "pg" }) == 6826 || $.querystring({ id: "pg" }) == 6926 || $.querystring({ id: "pg" }) == 6902 ) {

            str = 583;
            str += "#" + $("#ctl00_ContentPlaceHolder1_CubeClientName").val();
            str += "#" + $("#ctl00_ContentPlaceHolder1_txtGeoBM").val();
            str += "#[Dim Sex].[Sex Key].&[GENDERID]";
            str += "#" + "[Dim Study Field].[Study Field Category].&[ROWID]";


            if ($("#ctl00_ContentPlaceHolder1_rbIndBM").attr("checked")) {
                str += "#" + $("#ctl00_ContentPlaceHolder1_ddlIndustryExp option:selected").text();
            } else {
                str += "#" + $("#ctl00_ContentPlaceHolder1_ddlBenchmark option:selected").text();
            }

            str += "#CATID#GENDERNAME";
        }
        if ($.querystring({ id: "pg" }) == 6431) {

            str = 583;
            str += "#" + $("#ctl00_ContentPlaceHolder1_CubeClientName").val();
            str += "#" + $("#ctl00_ContentPlaceHolder1_txtGeoBM").val();
            str += "#[Dim Sex].[Sex Key].&[GENDERID]";
            str += "#" + "[Dim Study Field].[Study Field Category].&[ROWID]";


            if ($("#ctl00_ContentPlaceHolder1_rbIndBM").attr("checked")) {
                str += "#" + $("#ctl00_ContentPlaceHolder1_ddlIndustryExp option:selected").text();
            } else {
                str += "#" + $("#ctl00_ContentPlaceHolder1_ddlBenchmark option:selected").text();
            }

            str += "#CATID#GENDERNAME";
        }
        if ($.querystring({ id: "pg" }) == 6810 || $.querystring({ id: "pg" }) == 6910 || $.querystring({ id: "pg" }) == 6885) {

            str = 585;
            str += "#" + $("#ctl00_ContentPlaceHolder1_CubeClientName").val();
            str += "#" + $("#ctl00_ContentPlaceHolder1_txtGeoBM").val();
            str += "#[Dim Sex].[Sex Key].&[GENDERID]";
            str += "#" + "[Dim Occupation].[Hierarchy].[Division].&[ROWID]";


            if ($("#ctl00_ContentPlaceHolder1_rbIndBM").attr("checked")) {
                str += "#" + $("#ctl00_ContentPlaceHolder1_ddlIndustryExp option:selected").text();
            } else {
                str += "#" + $("#ctl00_ContentPlaceHolder1_ddlBenchmark option:selected").text();
            }

            str += "#CATID#GENDERNAME";
        }
        
        var rowid;
        
        $.each($(".td-indfocus-button"), function() {
            rowid = "-1";
            rowid = $(this).text();

            if (rowid == "-1") {
                rowid = $(this).attr("id");
            }
            if (rowid == "14" || rowid == "70" || rowid == "71") {
                $(this).parent().addClass("no-detail");
            }
            else if (rowid == "93" || rowid == "Total") {
                $(this).parent().addClass("no-detail");
            }
            else {
            var catid = $(this).next(".td-text").text();
           
           var sex = $(this).parent().find(".td-gender").find("span").html();
                var sexid = "";
                if (sex == "Males") {
                    sexid = "1";
                }
                else if (sex == "Females") {
                    sexid = "2";
                }
                else if (sex == "Persons") {
                    sexid = "3";
                }
                
                
                $(this).parent().attr("id", str.replace('ROWID', rowid).replace('GENDERID', sexid).replace("GENDERNAME", sex).replace("CATID", catid));


            }
        });


        $(".tbl-focus").detailsRow('tableDrillDown.aspx', { data: { "id": "id" },
            headerIndicator: " ",
            onLoad: function() {
                if ($(this).attr("id") == "14" || $(this).attr("id") == "70" || $(this).attr("id") == "71") {
                    $(this).parent().addClass("no-detail");
                }

                    if ($(this).attr("id") == "93" || $(this).attr("id") == "Total") {
                        $(this).parent().addClass("no-detail");

                    }


                $(this).parent().prev("tr").addClass("selected");
                rowRolloverDetailed();
                $.each($(".tbl-detailed-data .td-num:contains('%')"), function() {

                    $(this).text($(this).text().replace("%", ""));
                });

                $(".tbl-detailed-data .td-num:empty").text("--");
                $("#trLoading").remove();
            }
        });

        $(".tbl-focus .plus").attr("title", "Click for further detail");
        $(".tbl-focus tfoot .plus").html("&nbsp;").removeAttr("title");
        $(".tbl-focus .no-detail .plus").html("&nbsp;").attr("title", "No further detail available");
    

    }

    
    }


function definitionHeight() {

    if ($('#ctl00_btnPrintPage').val() != undefined) {
    if ($('.div-definition.industry').position() != undefined) {
        var lgth = ($('.div-layout').height() - $('.div-definition.industry').position().top);
        lgth = lgth - $('#divFooter').height();
        if (parseInt(lgth) > 200) {
            $('.div-definition.industry').css("height", lgth + "px")
        }
    }
    }


}

function textColumns() {
    if ($('#myjquerypagemenu').attr("id") != undefined) {
        $('.div-description .div-autotext').addClass("columns").removeClass("div-autotext");
    }
    else {
        $('.div-description .div-autotext').addClass("columns colwide").removeClass("div-autotext");
    }
    
    //$('.columns').columnize({columns: 2});
}
function tooltips() {
//    $("a").wTooltip({ className: "tooltip" }); 
//    $(".header").wTooltip({ className: "tooltip" });
}

function buildLayout() {
    $('.div-layout').removeClass("no-javascript");
    if ($('link').attr("rel") == "print") {
        $('#divWrapper').css("width", "775px");
    }
}

function toggleVisible(e,s) {
    
    $('#' + e).toggleClass("hide");
    if ($('#' + e +".hide").length>0) {
    $('#' + s).html("Show more info");
    }
    else {
    $('#' + s).html("Hide more info");
    }
    
    
}

function tableSorting() {

    


    // add sorting by 1,300,000 (with commas)  
    $.tablesorter.addParser({
        id: "fwnumber",
        is: function(s) {
            return false;
        },
        format: function(s) {
            return $.tablesorter.formatFloat(s.replace(/(\,|\%)/g, ""));
        },
        type: "numeric"
    });

    $.tablesorter.addParser({
        id: "fwpercent",
        is: function(s) {
            return false;
        },
        format: function(s) {
            return $.tablesorter.formatFloat(s.replace(/%/g, ""));
        },
        type: "numeric"
    });

    // only look for value in the comment, eg: <!--text--><IMG>,   
    $.tablesorter.addParser({
        id: "commenttxt",
        is: function(s) {
            return /^<!--([a-z]+)-->/.test($.trim(s));
        },
        format: function(s) {
            return s.replace(/^<!--([a-z]+)-->.*/g, "$1");
        },
        type: "text"
    });



    $.tablesorter.addParser({
        id: "millions",
        is: function(s) {
        return /^[$m,?.]/.test(s);
        },
        format: function(s) {
            return $.tablesorter.formatFloat(s.replace(/(\,|\[a-z]|\$)/g, ""));
        },
        type: "numeric"
    });

    $.tablesorter.addWidget({
        id: "genderLabel",
        format: function(table) {
            if ($(".td-gender .display").length == 1) {
                $(".td-gender span").addClass("nodisplay");
                $(".td-gender:first span").removeClass("nodisplay");
            }

        }
    });

    if ($(".td-gender .display").length == 1) {
        $(".th-gender").removeClass("header").addClass("\{sorter: false\}").click(function() { return false; });
    }

    var myTextExtraction = function(node) {
        if (node.childNodes[0] != undefined) {
            if (node.childNodes[0].childNodes[0] != undefined) {
                return node.childNodes[0].childNodes[0].innerHTML;
            }
            else {
                if (node.childNodes[0].value != undefined) {
                    return node.childNodes[0].value;
                    
                }
                else {
                    //alert(node.childNodes[0].);
                    return node.childNodes[0].innerHTML;
                }
            }
        }
        else {
            
            node.text;
        }


    }
    
    $(".tbl-sort.tbl-gender2").tablesorter({
    headers: {
        0: { sorter: 'text' },
        1: { sorter: false },
        2: { sorter: 'text' },
        3: { sorter: 'fwnumber' },
        4: { sorter: 'fwpercent' },
        5: { sorter: 'fwpercent' },
        6: { sorter: 'fwnumber' },
        7: { sorter: 'fwpercent' },
        8: { sorter: 'fwpercent' },
        9: { sorter: 'fwnumber' }
        }, widgets: ['zebra', 'genderLabel'],
        textExtraction: "complex"
    });
    
    $(".tbl-sort.tbl-gender").tablesorter({
    headers: {
    0: { sorter: 'text' },
    1: { sorter: 'text' },
            2: { sorter: 'fwnumber' },
            3: { sorter: 'fwpercent' },
            4: { sorter: 'fwpercent' },
            5: { sorter: 'fwnumber' },
            6: { sorter: 'fwpercent' },
            7: { sorter: 'fwpercent'},
            8: { sorter: 'fwnumber' }
        }, widgets: ['zebra', 'genderLabel'], 
        textExtraction: "complex"
    });

    
    
    $(".tbl-sort.tbl-normal").tablesorter({
        headers: {
            0: { sorter: 'text' },
            1: { sorter: 'fwnumber' },
            2: { sorter: 'fwpercent' },
            3: { sorter: 'fwpercent' },
            4: { sorter: 'fwnumber' },
            5: { sorter: 'fwpercent' },
            6: { sorter: 'fwpercent' },
            7: { sorter: 'fwnumber' }

        }, widgets: ['zebra'],
        textExtraction: "complex"
    });
    
    $(".tbl-sort.tbl-millions").tablesorter({
    headers: {
    0: { sorter: 'text' },
            1: { sorter: 'millions' },
            2: { sorter: 'fwpercent' },
            3: { sorter: false },
            4: { sorter: 'fwpercent' },
            5: { sorter: 'millions' },
            6: { sorter: 'fwpercent' },
            7: { sorter: 'fwpercent' },
            8: { sorter: 'fwnumber' }
        }, widgets: ['zebra'],
        textExtraction: "simple"
    });

    $(".tbl-sort.tbl-dollars").tablesorter({
    headers: {
            0: { sorter: false },
            1: { sorter: 'text' },
            2: { sorter: 'millions' },
            3: { sorter: 'fwpercent' },
            4: { sorter: 'fwpercent' },
            5: { sorter: 'millions' },
            6: { sorter: 'fwpercent' },
            7: { sorter: 'fwpercent' },
            8: { sorter: 'millions' }
        }, widgets: ['zebra'],
        textExtraction: "simple"
    });
    $(".header").attr("title", "Click to sort");

//    $(".header").each(function() {
//    $(this).html("<span><span>" + $(this).html() + "<\/span><\/span>");
//    });




    $(".tbl-data").bind("sortStart", function() {
        $(".tr-total").each(function() {

            if ($(".tbl-data tfoot").html() == null) {
                $(this).parent().parent().append("<tfoot></tfoot>");
            }
            $(".tbl-data tfoot").append("<tr></tr>");
            $(".tbl-data tfoot tr:last").append($(this).html());
            $(this).empty();

        });
        


    }).bind("sortEnd", function() {

$(".tr-total").remove();




        $(".tbl-detailed-data").remove();

        if ($(".td-gender .display").length > 1) {
            $(".td-gender span").removeClass("nodisplay").addClass("display");
            $(".td-gender span:contains('Males')").parent().removeClass("td-bold").addClass("td-indent");
            $(".td-gender span:contains('Females')").parent().removeClass("td-bold").addClass("td-italic").addClass("td-indent");
        }

        if ($(".td-gender .display").length < 2) {
            $(".td-gender span").removeClass("display").addClass("nodisplay");
            $(".td-gender:first span").removeClass("nodisplay").addClass("display");
        }


        $(".tbl-focus tr.selected").removeClass("selected");
        $(".tbl-focus td.plus").removeAttr("state").text("+");
        var rowid;
        $.each($(".td-indfocus-button"), function() {
            rowid = $(this).attr("id");
            if (rowid == "14" || rowid == "70" || rowid == "71") {
                $(this).parent().addClass("no-detail");
                $(this).text("");
            }
            if ($(this).parent().parent().is("tfoot")) {
                $(this).removeAttr("title");
                $(this).text("");
            }

        });
        $(".tbl-data tbody").removeClass("hide");
    });

}

function rowRollover() {

    var rows = $('.tbl-data tr');
    for (var i = 0; i < rows.length; i++) {
        rows[i].onmouseover = function() {
            $(this).addClass('highlight');
                if ($(this).parent().parent().attr("class").match("tbl-width300")) { togglePolygon($(this).children(0).html(), 'show'); }
        }

    

        rows[i].onmouseout = function() {
        $(this).removeClass('highlight');
            if ($(this).parent().parent().attr("class").match("tbl-width300")) { togglePolygon($(this).children(0).html(), 'hide'); } 
    }
    }
}

function rowRolloverDetailed() {
    var rows = $('.tbl-detailed-data tr');
    for (var i = 0; i < rows.length; i++) {
        rows[i].onmouseover = function() {
            $(this).addClass('highlight');
                if ($(this).parent().parent().attr("class").match("tbl-width300")) { togglePolygon($(this).children(0).html(), 'show'); }
        }

    

        rows[i].onmouseout = function() {
        $(this).removeClass('highlight');
            if ($(this).parent().parent().attr("class").match("tbl-width300")) { togglePolygon($(this).children(0).html(), 'hide'); } 
    }
    }
}

function setToolTips() {


    var newsLinks = $('.linkNews');
    var myUrl = "";
    var clientID = $.getQueryString({ id: "id" });
    for (var i = 0; i < newsLinks.length; i++) {

        myUrl = 'ToolTip.aspx?id=' + newsLinks[i].name + '&news=true&clientID=' + clientID;
        $(newsLinks[i]).addClass("others-link");
        $(newsLinks[i]).attr("onclick", "");
        $(newsLinks[i]).qtip({
            content: { text: '<img src="App_Themes/Default/Images/indicator.gif" /> Please wait while fetching info...', prerender: false, url: myUrl },
            show: { when: 'click', delay: 0, solo: true, ready: true },
            hide: { when: { target: $(document.body).children().not($(self)), event: 'mousedown' }, fixed: true, delay: 200 },
            style: { border: { width: 2, radius: 5, color: '#cccccc' },
                tip: { corner: 'topMiddle' }, color: '#333333',
                size: {
                    x: 20, // Be careful that the x and y values refer to coordinates on screen, not height or width.
                    y: 8 // Depending on which corner your tooltip is at, x and y could mean either height or width!
                }
            },
            position: { corner: { target: 'topMiddle', tooltip: 'topMiddle' },
                adjust: { x: 0, y: $(newsLinks[i]).height() }
            }
        }).attr("OnClick", "return false;")
        
        
        
        
        
    }
    
    
    
 
 
 
 

     var buttons = $('.tooltip');
     var myUrl = "";
     var page = $.getQueryString({ id: "pg" });
     for (var i = 0; i < buttons.length; i++) {
         myUrl = 'ToolTip.aspx?id=' + buttons[i].id + '&pg=' + page;
         $(buttons[i]).qtip({
             content: {text:'<img src="App_Themes/Default/Images/indicator.gif" /> Please wait while getting info...', prerender:false, url: myUrl },
             show: { when: 'click', delay: 0, solo: true, ready: true },
             hide: { when: { target: $(document.body).children().not($(self)), event: 'mousedown' }, fixed: true, delay: 200 },
             style: { border: { width: 2, radius: 3, color: '#666666' },
      padding: 0,
      
      tip: 'bottomLeft'
 },
             position: { corner: { target: 'topRight', tooltip: 'bottomLeft' },
                 adjust: { x: -3, y: 5 }
             }
         }).attr("OnClick", "return false;")
     }


     var others = $("td:contains('Other areas')");
     if (others.length > 0) {
     var myUrl = "";
     var industry = $("#ctl00_ContentPlaceHolder1_lboxIndustrySectors").val();
     var clientid = $.getQueryString({ id: "id" });
     myUrl = 'ToolTip.aspx?clientid=' + clientid + '&industry=' + industry + "&pg=" + page; 

     $(others[0]).addClass("others-link");
     $(others[0]).attr("title","Click to view more...");

     $(others[0]).qtip({
         content: { text: '<span class="span-please-wait"><img src="App_Themes/Default/Images/indicator.gif" /> Please wait while fetching info...</span>', prerender: false, url: myUrl},
         show: { when: 'click', delay: 0, solo: true, ready: true },
         hide: { when: { target: $(document.body).children().not($(self)), event: 'mousedown' }, fixed: true, delay: 200 },
         style: { border: { width: 2, radius: 5, color: '#cccccc'},
         tip: { corner: 'bottomMiddle' }, color: '#e5e5e5',
         size: {
             x: 20, // Be careful that the x and y values refer to coordinates on screen, not height or width.
             y: 8 // Depending on which corner your tooltip is at, x and y could mean either height or width!
         }
     },
     position: { corner: { target: 'topLeft', tooltip: 'bottomLeft' },
     adjust: { x: 0, y: $(others[0]).height()}
         }
     })


 }

 var lnkEconomy = $("#lnkEconomyInfo");
 if (lnkEconomy.length > 0) {
     var myUrl = "";
     var clientid = $.getQueryString({ id: "id" });
     myUrl = 'ToolTip.aspx?clientid=' + clientid + '&type=economy'; 
     $(lnkEconomy[0]).attr("title", "click to view more...");
     $(lnkEconomy[0]).addClass("others-link");
     $(lnkEconomy[0]).attr("onclick", "");
     $(lnkEconomy[0]).qtip({
     content: { text: '<img src="App_Themes/Default/Images/indicator.gif" /> Please wait while fetching info...', prerender: false, url: myUrl },
     show: { when: 'click', delay: 0, solo: true, ready: true },
         hide: { when: { target: $(document.body).children().not($(self)), event: 'mousedown' }, fixed: true, delay: 200 },
         style: { border: { width: 2, radius: 5, color: '#ffffff' },
         tip: { corner: 'topMiddle' }, color: '#EDEAE3',
         size: {
             x: 20, // Be careful that the x and y values refer to coordinates on screen, not height or width.
             y: 8 // Depending on which corner your tooltip is at, x and y could mean either height or width!
         }
          },
         position: { corner: { target: 'topMiddle', tooltip: 'topMiddle' },
         adjust: { x: 0, y: $(lnkEconomy[0]).height() }
         }
     })

 }

 
 var lnkResources = $("#lnkResourcesInfo");
 if (lnkResources.length > 0) {
     var myUrl = "";
     var clientid = $.getQueryString({ id: "id" });
     myUrl = 'ToolTip.aspx?clientid=' + clientid + '&type=resource'; 
     $(lnkResources[0]).attr("title", "click to view more...");
     $(lnkResources[0]).addClass("others-link");
     $(lnkResources[0]).attr("onclick", "");

     $(lnkResources[0]).qtip({
         content: { text: '<img src="App_Themes/Default/Images/indicator.gif" /> Please wait while fetching info...', prerender: false, url: myUrl },
         show: { when: 'click', delay: 0, solo: true, ready: true },
         hide: { when: { target: $(document.body).children().not($(self)), event: 'mousedown' }, fixed: true, delay: 200 },
         style: { border: { width: 2, radius: 5, color: '#ffffff'},
         tip: { corner: 'topMiddle' }, color: '#EDEAE3',
         size: {
             x: 20, // Be careful that the x and y values refer to coordinates on screen, not height or width.
             y: 8 // Depending on which corner your tooltip is at, x and y could mean either height or width!
         }

             },
         position: { corner: { target: 'topMiddle', tooltip: 'topMiddle' },
             adjust: { x: 0, y: $(lnkResources[0]).height() }
         }
     })

 }
 
     


     

 }

 
 