var MAP = {
    init: function() {
        MAP.wwLayer = $('#worldWideLayer');

        MAP.wwMapLink = $('#btWorldwide');

        MAP.wwMapLink.click(function() {
            // show/hide map
			if ($('#worldWideLayer').hasClass('mapReady')) {
				$('#worldWideLayer').removeClass('mapReady')
			}
			else {
				$('#worldWideLayer').addClass('mapReady')
			}
            $('#countrySelect').toggleClass('mapOpen');
            MAP.showContinent(0);
            return false;
        });

        MAP.wwCloseLink = $('#closeWorldLayer');

        MAP.wwCloseLink.click(function() {
            MAP.closeWorldLayer();
            return false;
        });

        MAP.wwContinents = $('#mapWorldmap').children('.continentShape');
        MAP.wwContinentsCountries = $('#wlCountries').children('div.countryListLayer');
        MAP.wwContinentLinks = MAP.wwLayer.find('.continentLink');
        MAP.wwCountryLayerLinks = MAP.wwContinentsCountries.children('ul.countryList').children('li').children('a');
        
        // set up event handlers for image map and continent links
        MAP.wwContinentLinks.each(function() {

            var contId = this.id.split('_')[1];

            $(this).click(function() {
                MAP.showContinent(contId);
                return false;
            });

            MAP.wwContinents.filter('#continent_' + contId).hover(
                function() {
                MAP.hoverContinent(contId);
            },
                function() {
                MAP.hoverContinent(0);
            }
            );

            MAP.wwContinents.filter('#continent_' + contId).click(function() {
                MAP.showContinent(contId);
                return false;
            });
        });

        MAP.wwCountryLayerLinks.each(function() {
            $(this).click(function() {
                // hide all countryMoreLinks and remove selected class from links
                MAP.wwLayer.find('ul.countryMoreLinks').hide();
                MAP.wwLayer.find('a.selectedCountry').removeClass('selectedCountry');
                
                // show/hide this country's links
                $(this).siblings('ul.countryMoreLinks').toggle();
                $(this).toggleClass('selectedCountry');
                return false;
            });
        });
    },

    hoverContinent: function(id) {
        var hoverImg = '/syn/assets/img/map/map' + id + '.gif'
        $('#map_hover').attr('src', hoverImg);
    },

    showContinent: function(id) {
        var mapImage = '/syn/assets/img/map/map' + id + '.gif';
        $('#map_active').attr('src', mapImage);

        // remove selected class from currently selected layer
        MAP.wwContinentsCountries.filter('.selectedLayer').removeClass('selectedLayer');

        // add selected class to new layer
        $('#countries_' + id).addClass('selectedLayer');
    },

    closeWorldLayer: function() {
        $('#worldWideLayer').removeClass('mapReady')
        $('#countrySelect').removeClass('mapOpen');
    }
}