function removeSpacesAnd(string) {
    return string.replace(' and ', '').split(' ').join('').toLowerCase();
}

function showCountryByName() {
    var shortName = document.getElementById("FindCountry").value;
    shortName = removeSpacesAnd(shortName);
    if ((shortName == 'FindYourCountry') || (shortName == '')) {
        window.location = '/index/ranking';
    }
    else {
        window.location = '/index/country/' + shortName;
    }
}

function showLinkedCountry(event, data, formatted) {
    alert('test');
    var p = data.toString();
    p = p.replace('<a href="', '');
    var url = p.substr(0, p.indexOf('"'));
    p = p.replace('</a>', '');
    p = p.substr(p.indexOf('>') + 1, p.length - p.indexOf('>') - 1);
    //p = replaceSpacesWithPlus(p);
    //$(".navSearch > input.txt").val("");
    window.location = "" + url + '?query=' + p;
    return false;
}


$(document).ready(function () {
    //Autocomplete
    var data = "Afghanistan,Albania,Algeria,Angola,Argentina,Armenia,Australia,Austria,Azerbaijan,Bahamas,Bahrain,Bangladesh,Barbados,Belarus,Belgium,Belize,Benin,Bhutan,Bolivia,Bosnia and Herzegovina,Botswana,Brazil,Bulgaria,Burkina Faso,Burma,Burundi,Cambodia,Cameroon,Canada,Cape Verde,Central African Republic,Chad,Chile,China,Colombia,Comoros,Costa Rica,Cote dIvoire,Croatia,Cuba,Cyprus,Czech Republic,Democratic Republic Congo,Denmark,Djibouti,Dominica,Dominican Republic,Ecuador,Egypt,El Salvador,Equatorial Guinea,Eritrea,Estonia,Ethiopia,Fiji,Finland,France,Gabon,Gambia,Georgia,Germany,Ghana,Greece,Guatemala,Guinea,Guinea Bissau,Guyana,Haiti,Honduras,Hong Kong,Hungary,Iceland,India,Indonesia,Iran,Iraq,Ireland,Israel,Italy,Jamaica,Japan,Jordan,Kazakhstan,Kenya,Kiribati,North Korea,South Korea,Kuwait,Kyrgyz Republic,Laos,Latvia,Lebanon,Lesotho,Liberia,Libya,Liechtenstein,Lithuania,Luxembourg,Macau,Macedonia,Madagascar,Malawi,Malaysia,Maldives,Mali,Malta,Mauritania,Mauritius,Mexico,Micronesia,Moldova,Mongolia,Montenegro,Morocco,Mozambique,Namibia,Nepal,Netherlands,New Zealand,Nicaragua,Niger,Nigeria,Norway,Oman,Pakistan,Panama,Papua New Guinea,Paraguay,Peru,Philippines,Poland,Portugal,Qatar,Republic Congo,Romania,Russia,Rwanda,Saint Lucia,Saint Vincent Grenadines,Sao Tome Principe,Saudi Arabia,Senegal,Serbia,Seychelles,Sierra Leone,Singapore,Slovakia,Slovenia,Solomon Islands,Somalia,South Africa,Spain,Sri Lanka,Sudan,Suriname,Swaziland,Sweden,Switzerland,Syria,Taiwan,Tajikistan,Tanzania,Thailand,Timor Leste,Togo,Tonga,Trinidad and Tobago,Tunisia,Turkey,Turkmenistan,Uganda,Ukraine,United Arab Emirates,United Kingdom,United States,Uruguay,Uzbekistan,Vanuatu,Venezuela,Vietnam,Yemen,Zambia,Zimbabwe".split(",");
    $("#txtCountry").autocomplete('autocompletecountries', { resultsClass: 'jc_results autocomplete' });
    $("#FindCountry").autocomplete('autocompletecountries', { resultsClass: 'ac_results autocomplete' });
    $("#country1").autocomplete(data, { resultsClass: 'ac_graph top autocomplete' });
    $("#country2").autocomplete(data, { resultsClass: 'ac_graph bottom autocomplete' });
    $("#country3").autocomplete(data, { resultsClass: 'ac_graph bottom autocomplete' });
    $("#compare-country").autocomplete(data);

    $("#FindCountry").result(function (event, data, formatted) {
        var p = data.toString();        
        p = p.replace('<a href="', '');
        var url = p.substr(0, p.indexOf('"'));
        p = p.replace('</a>', '');
        p = p.substr(p.indexOf('>') + 1, p.length - p.indexOf('>') - 1);        
        window.location = "" + url;
        return false;
    });

    // $("#FindCountry").result(showLinkedCountry(event, data, formatted));

    $("#FindCountry").keypress(function (event) {
        if (event.keyCode == 13) {
            showCountryByName();
            return false;
        }
    });

    $("#btnSearch").click(showCountryByName);

    // Add utility first/last/alt classes to LIs.
    $("ul li:first-child").addClass("first");
    $("ul li:last-child").addClass("last");
    $("ul li:nth-child(even)").addClass("alt");
    $("table tr:nth-child(even)").addClass("alt");

    // Replace HRs with Divs in IE
    if (jQuery.browser.msie) {
        $("hr").wrap("<div class='hr'></div>");
    }

    // Tab_options and Tabs: Two DIVs that interact using Rels and IDs
    $(".tabs li:first-child").each(function () {
        $(this).addClass("current");
    });

    $(".tab-content").each(function () {
        $(this).children(":not(:first)").hide();
    });

    $(".tabs a").click(function () {

        if ($(this).parent().hasClass("current") == false) {
            var thisTarget = $(this).attr("href");
            $(this).parents(".tabs").find('li.current').removeClass('current');
            $(this).parent().addClass('current');
            $(this).parents(".tabs").next().children(":visible").fadeOut(1, function () {
                $(thisTarget).fadeIn("fast");
            });
        }
        if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
            $(this).blur();
        }
        return false;
    });

    //Reset button inside forms
    $("form .reset").click(function () {
        $(this).parents("form")[0].reset();
        return false;
    });

    $(".tab-controller").click(function () {
        var thisTarget = $(this).attr("href");
        if ($(".tabs a[href='" + thisTarget + "']").parent().hasClass("current") == false) {
            $(".tabs a[href='" + thisTarget + "']").parents(".tabs").find('a.current').removeClass('current');
            $(".tabs a[href='" + thisTarget + "']").parent().addClass('current');

            $(".tab-content").children(":visible").fadeOut(1, function () {
                $(thisTarget).fadeIn("fast");
            });
        }
        return false;
    });

    $(".legend-colors a").click(function () {
        var thisTarget = $(this).attr("class");
        $(".table-data td.highlight").removeClass("highlight");
        $(".table-data ." + thisTarget + "").parents("td").addClass("highlight");
        return false;
    });

    //$(".table-results table").tablesorter({ widgets: ['zebra'] });
    $(".table-results table th").addClass("th-link");

    // add icon to denote external link    
    // $("a").filter(function () {
    //  return this.hostname && this.hostname != location.hostname;
    // }).addClass('external');


    // BACK TO TOP FUNCTIONALITY
    $("#back-top").hide();

    $(function () {
        $('#section').scroll(function () {
            if ($(this).scrollTop() > 300) {
                $('#back-top').fadeIn();
            } else {
                $('#back-top').fadeOut();
            }
        });

        $('#back-top a').click(function () {
            $('#section').animate({
                scrollTop: 0
            }, 800);
            return false;
        });
    });

    // COLORBOX FOR PHOTOS
    $(function () {
        $(".indexphotos").colorbox({ rel: 'photos' });

    });


    // STYLING SELECT FORM ELEMENTS
    //$(function(){
    //$('#emailform select').customStyle();
    //$('#download_form select').customStyle();
    //});

    // EMAIL SIGNUP MODAL
    //$(function($) {
    // Load dialog on click
    $('.emailform').click(function (e) { $('#emailform').modal(); return false; });
    // });

});   //end document ready
