﻿$(function () {

    //Load Countries from XML
    LoadCountriesFromXML();

    // handle show and hide overlays

    $('.continent').hover(function () {

        var continent = $(this).attr("rel");

        $(this).addClass("active");
        $(this).children('.over').show();


    }, function () {

        $(this).children(".over").hide();
        $(this).removeClass("active");

    });
});


function LoadCountriesFromXML() {

    $.ajax({
        type: "GET",
        url: "/aam.nsf/Image/grouphomexml/$File/GroupHomePage.xml",
        dataType: "xml",
        success: function (xml) {

            // do Europe

            CreateCountryLinks(xml, 'Europe');

            // do Anmericas

            CreateCountryLinks(xml, 'Americas');

            // do Asia & Austrailia

            CreateCountryLinks(xml, 'AsiaAustralia');

        }
    });

    function CreateCountryLinks(xml, continent) {

        $(xml).find('continent[name=' + continent + ']').each(function () {

            //find the div id=countryLinks for this continent
            var divCountryLinks = $('.continent[rel=' + continent + ']').children('.over').children('.over-mid').children(".listy").children("#countryLinks");

            var totalCountries = $(this).find('country').size();
            var totalCount = Math.ceil(totalCountries / 3);
            var count = parseInt(1);
            var group = parseInt(1);

            var group1 = "";
            var group2 = "";
            var group3 = "";

            $(this).find('country').each(function () {

                var links = "";
                var seperator = "";

                var noOfLanguages = $(this).children("lang").length;
                var langNo = 1;
                var mainCountry = "";

                $(this).find('lang').each(function () {

                    var country = $(this).find("name").text();
                    var url = $(this).find("url").text();
                    var tooltip = "";

                    if (langNo == 1) {
                        mainCountry = country;
                        tooltip = "Go to our " + country + " website";
                    } else {
                        tooltip = "Go to our " + mainCountry + " (" + country + ") website";
                    }

                    if (langNo < noOfLanguages) {
                        seperator = " / ";
                    }
                    else {
                        seperator = "";
                    }

                    links = links + "<a href='" + url + "' title='" + tooltip + "'>" + country + "</a>" + seperator;

                    langNo++;

                });

                langNo = 1;

                if (group == 1) {
                    group1 = group1 + "<li>" + links + "</li>";
                }

                if (group == 2) {
                    group2 = group2 + "<li>" + links + "</li>";
                }

                if (group == 3) {
                    group3 = group3 + "<li>" + links + "</li>";
                }

                count = count + 1;

                if (count == totalCount + 1) {

                    group = group + 1;
                    count = 1;

                }

                links = "";

            });

            $(divCountryLinks).append("<ul class='listy-list'>" + group1 + "</ul>");
            $(divCountryLinks).append("<ul class='listy-list'>" + group2 + "</ul>");
            $(divCountryLinks).append("<ul class='listy-list'>" + group3 + "</ul>");

        });

    }

}
