var Retailers = {

    setUpCountryArray : function() {
        this.countryArray = $$('#retailers .leftNav ul li a.countryTitle');
        Retailers.currentCountry = this.countryArray[0].getNext();
        this.setUpCountryArrayEvents();
        this.setUpCities()
    },

    setUpCountryArrayEvents : function() {

        this.countryArray.each(function(item,index) {

            item.addEvent('click', function() {

                Retailers.onCountryClick(index);

            });

        });

    },

    setUpMapArray : function() {

        this.mapArray = $$('#retailer-map a');
        this.setUpMapArrayEvents();

    },

    setUpMapArrayEvents : function() {

        this.mapArray.each(function(item,index) {

            item.addEvent('click', function() {

                Retailers.onCountryClick(index);

            });

        });

    },

    onCountryClick : function (index) {

        Retailers.previousCountry = Retailers.currentCountry;
        Retailers.currentCountry = Retailers.countryArray[index].getNext();
        this.closeAllCountries();

    },

    highLightCountry : function(highlight, item) {

        if ( highlight || highlight==undefined ) {
            Retailers.currentCountry.getPrevious().setStyle('color','#ffffff');
        } else if ( item != undefined) {
            item.setStyle('color','#9E9E9E');
        }
        
    },

    closeAllCountries : function () {

        var otherCountries = this.countryArray.filter(function (item) {
            return item.innerHTML != Retailers.currentCountry.getPrevious().innerHTML;     
        });

        otherCountries.each(function(item,index) {

            Retailers.close(item.getNext());
            Retailers.highLightCountry(false,item);
            Retailers.cityObjects[index].reset();

        });

        if ( Retailers.isOpen(Retailers.currentCountry) ) {
            Retailers.close(Retailers.currentCountry);
        } else {
            Retailers.open(Retailers.currentCountry);
        }

        this.highLightCountry();

    },

    close : function (elm) {
        if ( this.isOpen(elm)) elm.setStyle('display','none');
    },

    open : function(elm) {
        elm.setStyle('display','block');
    },

    isOpen : function (elm) {
        return elm.getStyle('display') == 'block';
    },

    setUpCities : function() {

        this.cityObjects= [];

        this.countryArray.each(function ( item , index ) {

            Retailers.cityObjects[index] = new City({
                theParent : Retailers.countryArray[index].getNext()
            });

        });        

    },

    init : function () {

        this.setUpCountryArray();
        this.setUpMapArray();

    }

};

var City = new Class({

    Implements: [Options,Events],

    options : {

        theParent : undefined,
        index : 0

    },

    initialize : function (options) {

        this.setOptions(options);
        this.cityNames =  this.options.theParent.getElements('a.cityTitle');
        this.cityDetails = this.options.theParent.getElements('div.cityDetails');
        this.setEvents();
        this.index = this.options.index ;


    },

    setEvents : function () {

        var cityObj = this;

        this.cityNames.each( function ( item, index) {

            item.addEvent('click', function () {

                cityObj.index = index;
                cityObj.switchCity();

            });

        });

    },

    switchCity : function () {

        this.cityDetails.each( function (item) {

            Retailers.close(item);

        });

        Retailers.open(this.cityDetails[this.index]);

    },

    reset : function() {

        this.index = this.options.index;
        this.switchCity();

    }

});



window.addEvent('domready', function() {
    Retailers.init();
});


        
