glow.onDomReady(function(){
    
    var exploreMenu = function(){
	return _exploreMenu.init(exploreMenu.arguments);
    };

    var _exploreMenu = {
        
        init:function(args){
            
            var id = args[0];

            if(id){

                var o = glow.lang.apply({
                    name:"Explore Menu",
                    classes:{
                        tv:"tv",
                        national:"radio-national",
                        nations:"radio-nations",
                        local:"radio-local"
                    }
                },args[1]);

                o.wrapper = glow.dom.get(id);

                if(o.wrapper.length>0){

                    var event = glow.events, heading, r = new Object();

                    // Validate if type has a value of any use to work with.
                    var validate = function(obj){
                        for( type in obj ){
                            var p = obj[type];
                            if (!p.length>0)
                                return false;
                        }
                        return obj;
                    };

                    var create = function(type,o){
                        if(typeof type == "string" && type != "")
                        return {
                            heading:o.wrapper.get("dt."+type),
                            list:o.wrapper.get("dd."+type)
                        }
                        return false;
                    };

                    var addEvent = function(target,heading){
                        return event.addListener(heading,"click",function(){
                            return _exploreMenu.toggle(target,o);
                        });
                    };

                    // Build national object
                    r.nations = create(o.classes.nations,o);
                    r.nations = validate(r.nations);

                    // Build nations object
                    r.national = create(o.classes.national,o);
                    r.national = validate(r.national);

                    // Build nations object
                    r.local= create(o.classes.local,o);
                    r.local = validate(r.local);

                    // Update DOM and attach CTA's
                    if(r.nations){
                        r.nations.heading.addClass("expanded");
                        heading = r.nations.heading.html('<a title="Expand this list to view BBC Nations radio services" href="#">'+r.nations.heading.html()+'</a>');
                        addEvent(r.nations,heading);
                    }

                    // Update DOM and attach CTA's
                    if(r.national){
                        r.national.heading.addClass("expanded");
                        heading = r.national.heading.html('<a title="Expand this list to view BBC National radio services" href="#">'+r.national.heading.html()+'</a>');
                        addEvent(r.national,heading);
                    }
                    // Update DOM and attach CTA's
                    if(r.local){
                        r.local.list.addClass("minify");
                        heading = r.local.heading.html('<a title="Expand this list to view local BBC radio services" href="#">'+r.local.heading.html()+'</a>');
                        addEvent(r.local,heading);
                    }

                    return false;
                }

            }

            return;

        },

        toggle:function(r,o){
            r.list.toggleClass("minify");
            r.heading.toggleClass("expanded");
            return false;
        }

    };

    // init
    exploreMenu("#service-children");

});
