(function($) {
    var W = this, D = this.document;
    
    if(!D.getElementById || !D.getElementsByTagName || !D.createElement || !D.createTextNode) {
        return;
    }
    $(D.documentElement || 'html').attr('id', 'js');
    
    var oDispatch = {
        bAvailable: true,
        expandsH : {},
        init: function() {
            var that = this;
            
            $('#country-selector .section').each(function() {
                var sItem = this, oOpener = $(sItem).find('h2'), sId = (oOpener.html().replace(' ', '-')).toLowerCase(), oExpand = $(sItem).find('div.expand');
                
                that.expandsH[sId] = $(oExpand).find('ul').height();
                
                oOpener.click(function() {
                    if(that.bAvailable == true) {
                        that.bAvailable = false;
                        
                        if(that.oCurrent) {
                            that.oCurrent.animate({
                                'height': 0
                            }, 500);
                            
                            if(that.oCurrent[0] == oExpand[0]) {
                                that.oCurrent = null;
                                that.bAvailable = true;
                            } else {
                                oExpand.animate({
                                    'height': that.expandsH[sId]
                                }, 500, function() {
                                    that.oCurrent = oExpand;
                                    that.bAvailable = true;
                                });
                            }
                        } else {
                            oExpand.animate({
                                'height': that.expandsH[sId]
                            }, 500, function() {
                                that.oCurrent = oExpand;
                                that.bAvailable = true;
                            });
                        }
                    }
                });
            });
        }
    };
    
    $(D).ready(function() {
        oDispatch.init();
    });
})(jQuery);
