﻿$(document).ready(function () {
    $(".selector").change(function (event) {
        if ($(this).val() == "") {
            event.stopPropagation();
            event.preventDefault();
            return;
        }
        if ($(this).hasClass("detailselector"))
            window.location = $(this).attr("rel").replace("val", $(this).val());
        else if ($(this).hasClass("localpartnerselector")) {
            var vals = $(this).val().split('|');
            window.location = $(this).attr("rel").replace("val1", vals[0]).replace("val2", vals[1]);
        }
        else if ($(this).attr("rel") == null)
            window.location = $(this).val();
        else {
            window.location = $(this).attr("rel") + "/" + $(this).val();
        }
    });

    $(".xlDetails").click(function (event) {
        event.stopPropagation();
        event.preventDefault();
        window.location = $(this).attr("rel") + "?ajax=1&excel=1";
        return false;
    });

    $("a.expandcollapse").click(function () {
        $(this).toggleClass("GroupExpanded GroupCollapsed")
        $("." + $(this).attr("rel")).each(function () {
            $(this).parent().toggle();
        });
    });

    $(".alertCss").click(function (event) {
        event.stopPropagation();
        event.preventDefault();
        var url = '/Handlers/Alerts.ashx?mail=' + $("#txtAlert").val();
        $.get(url, function (data) {
            $(".lblResult").html(data == '1' ? $(".csThanks").val() :  $(".csInvalid").val());
        });
    });

    $("#logo").click(function () {
        window.location.href = "/";
    });

    $(".container").each(function () {
        //leadingWhitespace: Is equal to true if the browser inserts content with .innerHTML exactly as provided—specifically, if leading whitespace characters are preserved. (It is currently false in IE 6-8).
        if (!jQuery.support.leadingWhitespace) {
            if ($(this).html() == "")
                $(this).remove();
        }
        else {
            if ($(this).html().trim() == "")
                $(this).remove();
        }
    });

    $(".aHistorical").click(function () {
        window.location = "/Custom-Query/";
    });

    $("#primary-nav-items a").each(function () {
        $(this).parent().removeClass("active");

        if ($(this).attr("href") != "/") {
            var loc = window.location.href + "/";
            if (loc.indexOf($(this).attr("href") + '/') > -1) {
                $(this).parent().addClass("active");
            }
        }
    });

    $("div.indicatorComponentLinkDetails").hide();
    $("a.indicatorComponentLink").click(function () {
        $(this).next().toggle();
        $(this).toggleClass("GroupExpanded GroupCollapsed")
    });


    //the method below gets all images under ul.items and adds a class "photo", this helps the text to float on right and bottom side of the image
    $("ul.items img").each(function () {
        $(this).addClass("photo");
    });

    $(".secondary .container img").each(function () {
        $(this).addClass("photo");
    });

    $(".photolist img").each(function () {
        $(this).addClass("photo");
    });

    $("ul.economy-categories-nav a").click(function (event) {
        event.stopPropagation();
        event.preventDefault();
        $(".active").removeClass("active");
        $(this).parent("li").addClass("active");
        var topicQueryString = $(this).attr("href").split("?")[1];
        var topicName = topicQueryString.split("=")[1];
        $.get($(this).attr("href") + "&ajax=1", function (data) {
            $(".economy-categories").html(data);
            window.location.hash = "#" + topicName;
        });
    });

    if (window.location.hash != null && window.location.hash != "") {
        var hash = window.location.hash.split("#")[1];
        var tablinkObj = $(".tablink" + hash); 
        if (tablinkObj != null && tablinkObj.length > 0) {
            $(".active").removeClass("active");
            $(tablinkObj).parent("li").addClass("active");
            $.get($(tablinkObj).attr("href") + "&ajax=1", function (data) {
                $(".economy-categories").html(data);
            });
        }
	else if($("#" + hash) != null && $("." + hash).length > 0)
	{
		$($("." + hash)[0]).parents(".sub-nav").find("a").each(function () {
        	    $(this).parent().removeClass("active");
	        });
		$($("." + hash)[0]).parent().addClass("active");
		hideAllSubNavDiv();
		$("#" + hash).show();
	}
    }


    $(".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(); });
    });



    hideAllSubNavDiv();
    $(".sub-nav a").click(function () {
        var div = $(this).attr("class");
        if (div == "")
            return;
        $(this).parents(".sub-nav").find("a").each(function () {
            $(this).parent().removeClass("active");
        });
        $(this).parent().addClass("active");
        window.location.hash = "#" + div;
	hideAllSubNavDiv();
        $("#" + div).show();
    });

    function hideAllSubNavDiv() {
        $(".tabContent").each(function () {
            if (!$("." + $(this).attr("id")).parent().is('.active'))
                $(this).hide();
        });
    }

});


function Search(url) {
    //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=rrudb");
});

$("#txtSearch").keypress(function () {

    return checkEnterKey(event, 'btnSearch');
});



