﻿$(document).ready(
function () {


    /*This functions is used when Selected Value of Economies dropdown list in Selector control changed*/
    $(".selector").change(function (event) {
        /*Get the Selected Economy and navigate it to the Economy details screen
        * get the URL from  the rel attribute of the Economy dropdown list and Append the selected value to the URL
        */
        //alert($(this).attr("rel") + $(this).val());
        window.location = $(this).attr("rel") + $(this).val();

    });

    $(".selectorTopic").change(function (event) {
        /*Get the Selected Economy and navigate it to the Economy details screen
        * get the URL from  the rel attribute of the Economy dropdown list and 

        Append the selected value to the URL
        */
        window.location = $(this).attr("rel") + $(this).val();
    });


    /*This functions is used when Selected Value of Topics dropdown list in Selector control changed*/
    $(".topics").change(function (event) {
        /*Get the Selected Topic and navigate it to the Topic details screen
        * get the URL from  the rel attribute of the Topic dropdown list and Append the selected value to the URL
        */
        window.location = $(this).attr("rel") + $(this).val();
    });

    $(".selector1").change(function (event) {
        /*alert($(this).attr("eco") + $(this).attr("rel") + $(this).val());*/
        window.location = $(this).attr("eco") + $(this).attr("rel") + $(this).val();
    });

    $(".economy-chooser").hover(function () {
        $(".economy-chooser .explore-data").css({ "display": "block", "position": "absolute", "right": "0", "top": "0" });
        $(".selector").mouseleave(function (event) { event.stopPropagation(); });
    }, function () {
        $(".economy-chooser .explore-data").css({ "display": "none" });
        $(".selector").mouseleave(function (event) { event.stopPropagation(); });
    });

    $(".printexcel").hover(function () {
        $(".js #country-data .breakdown").css({ "z-index": "-1" });
    }, function () {
        $(".js #country-data .breakdown").css({ "z-index": "10" });
    });


    function attachClickEvent() {
        /*Loop thru each Hyper link to Unbind and Bind the click events*/
        $(".TopicDataHeader").each(function () {
            /*First unbind the existing click events*/
            $(this).unbind('click');
            /*Bind the new click event to the Anchor tag*/
            $(this).bind('click', indicatorDataHeader_Click);
        });
    }

    $(".TopicDataHeader").click(indicatorDataHeader_Click);

    function indicatorDataHeader_Click(event) {
        //alert("hi 0");
        event.preventDefault();
        event.stopPropagation();
        var url = $(this).attr("href");
        $.get(url, function (data) {
            $(".IndicatorData").html(data);
            $(".TopicDataHeader").unbind('click');
            $(".TopicDataHeader").bind('click', indicatorDataHeader_Click);
        });
    }



    //------------- Java Script for Page Tool Drop down & up -------------------//

    /*function toggler(event) {

    event.preventDefault();
    event.stopPropagation();

    alert("hmmmmmmm");

    $(".toggle_container").hide();
    $("h2.expand_heading").toggle(function () {
    $(this).addClass("active");
    }, function () {
    $(this).removeClass("active");
    });
    $("h2.expand_heading").click(function () {
    $(this).next(".toggle_container").slideToggle("slow");
    });
    $(".snShow").toggle(function () {
    alert("Collapsing");
    $(this).addClass("expanded");
    }, function () {
    $(this).removeClass("expanded");
    });
    $(".snShow").click(function () {
    alert("Expanding");
    $(".toggle_container").slideToggle("slow");
    });
    }*/
    $(".toggle_container").hide();
    $(".expand_all").click(function (event) {
        event.preventDefault();
        event.stopPropagation();
        var clsName = $(this)[0].className;
        //alert($(this)[0].className);
        if (clsName.indexOf("expanded") == -1)
            $(this).addClass("expanded");
        else
            $(this).removeClass("expanded");
        //alert($(this)[0].className);
        //alert(clsName.indexOf("expanded"));
        $(".toggle_container").slideToggle("slow");
    });

    //------------- Java Script for opening share urls from pagetools -------------------//

    var url = location.href;

    $(".Linked").click(function () {

        url = location.href;
        window.open('http://www.linkedin.com/shareArticle?mini=true&url='
                + encodeURIComponent(url) + '&title='
                + encodeURIComponent(document.title));
    });

    $(".Dig").click(function () {
        url = location.href;
        window.open('http://digg.com/submit?url=' + encodeURIComponent(url)
                + '&title=' + encodeURIComponent(document.title), '_new');
    });

    $(".facebook").click(function () {
        url = location.href;
        window.open('http://www.facebook.com/share.php?u='
                + encodeURIComponent(url) + '&t='
                + encodeURIComponent(document.title), '_new');
    });

    $(".twitter").click(function () {
        url = location.href;
        window.open('http://twitter.com/share?url=' + encodeURIComponent(url)
                + '&text=' + encodeURIComponent(document.title), '_new');
    });


    $(".Google").click(function () {
        url = location.href;
        window.open('http://www.google.com/buzz/post?url='
                + encodeURIComponent(url) + '&title='
                + encodeURIComponent(document.title), '_new');
    });

    $(".Stumble").click(function () {
        url = location.href;
        window.open('http://www.stumbleupon.com/submit?url='
                + encodeURIComponent(url) + '&title='
                + encodeURIComponent(document.title), '_new');
    });

    $(".Delicious").click(function () {
        url = location.href;
        window.open('http://delicious.com/save?url=' + encodeURIComponent(url)
                + '&title=' + encodeURIComponent(document.title), '_new');
    });

    //    function hi5() {
    //        window.open('http://hi5.com/save?url=' + encodeURIComponent(url)
    //                + '&title=' + encodeURIComponent(document.title), '_new');
    //    }

    //    function renren() {
    //        window.open('http://share.renren.com/share/buttonshare.do?link=' + url
    //                + '&title=' + document.title, '_new');
    //    }


    //    function sina() {
    //        window.open('http://v.t.sina.com.cn/share/share.php?title=' + document.title
    //                + '&url=' + url + '&source=bookmark&appkey=', '_new');
    //    }


    $(".expanderBody").each(function () {

        if (!($(this).hasClass("expanded")))

            $(this).hide();

    });

    $(".expanderHead").click(function () {

        $(this).parent().next(".expanderBody").toggle(500);

        $(this).toggleClass("SectorGroupExpanded SectorGroupCollapsed");

    });

    // Uncomments this Region level details to be displayed on Economy Characteristics page
    /*$("div.indicatorComponentLinkDetails").hide();
    $("a.indicatorComponentLink").click(function () {
        $(".indicatorComponentLinkDetails").toggle();
        $(this).toggleClass("GroupExpanded GroupCollapsed")
    });
    */

});   

function Search(url) {
    url = "http://extsearch.worldbank.org/servlet/SiteSearchServlet?ed=entsurveys";
	//add the parameter on the search query
    var searchText = document.getElementById('txtSearch');
    url += '&q=' + searchText.value;
    //redirect to the search url
    window.location = url;
}

function checkEnterKey(e, submitButtonName) {
    var key;
    //if the browser is IE
    if (window.event)
        key = window.event.keyCode;
    //if its firefox, Opera, etc..
    else
        key = e.which;
    //if the keycode equals the code for the enter key
    if (key == 13) {
        document.getElementById(submitButtonName).click();
        return false;
    }
    return true;
}

$("[id$='btnSearch']").click(function () {
    Search("http://extsearch.worldbank.org/servlet/SiteSearchServlet?ed=entsurveys");
});

$("#txtSearch").keypress(function () {

    return checkEnterKey(event, 'btnSearch');
});


