/**
* RBS additional JavaScript
*/ 
RBS = {
    /*
    *  Scope for Utilities
    */
    utils: {
        /**
        * Returns the available languages from the RBS tags as options. This 
        * method can be used directly as {@link CQ.Selection#optionsProvider} 
        * to draw a language selector.
        * @static
        * @return {Object[]) The language options
        */
        getLanguageOptions : function() {
            try {
                var url = "/etc/tags/rbs/language.infinity.json";
                var json = CQ.HTTP.eval(url);
                var opts = [];
                for (var name in json) {
                    var langObj = json[name];
                    var title = langObj['jcr:title'];
                    if (title) {
                        opts.push({
                            value: name,
                            text: title
                        });
                    }
                }
                opts.sort(function(l1, l2) {
                    if (l1.text < l2.text) {
                        return -1;
                    } else if (l1.text == l2.text) {
                        return 0;
                    } else {
                        return 1;
                    }
                });
                return opts;
            } catch (e) {
                CQ.Log.error("RBS.utils#getLanguageOptions failed: " + e.message);
            }
            return [];
        },
        
        /**
        * Returns the available locations from the RBS tags as options. This 
        * method can be used directly as {@link CQ.Selection#optionsProvider} 
        * to draw a location selector.
        * @static
        * @return {Object[]) The location options
        */
        getCountryOptions : function() {
            try {
                var url = "/etc/tags/rbs/locations - geographic.infinity.json";
                var json = CQ.HTTP.eval(url);
                var opts = [];
                RBS.utils.addLocationNodes(json, "ROOT", opts);
                
                opts.sort(function(l1, l2) {
                    if (l1.text < l2.text) {
                        return -1;
                    } else if (l1.text == l2.text) {
                        return 0;
                    } else {
                        return 1;
                    }
                });
                return opts;
            } catch (e) {
                CQ.Log.error("RBS.utils#getLocationOptions failed: " + e.message);
            }
            return [];
        },
        
        /**
        * Recursively walks a JSON tree of location tags and adds each 
        * location node to the opts argument.
        * @static
        */
        addLocationNodes : function(currentNode, nodeName, opts) {
            for (var p in currentNode) {
                if (currentNode[p]["jcr:title"]) {
                    RBS.utils.addLocationNodes(currentNode[p], p, opts);
                } else if (p == "jcr:title" && RBS.utils.isCountryNode(currentNode)) {
                    opts.push({ 'value': nodeName, 'text': currentNode["jcr:title"]});
                }
            }
        },
        
        /**
        * If a node has a single child which is a leaf node then it is 
        * considered a country node as its child must be a city.
        * @static
        */
        isCountryNode : function (node) {
            var hasChild = false;
            
            for (var p in node) {
                if (node[p]['jcr:title']) {
                    hasChild = true;
                    for (var n in node[p]) {
                        if (node[p][n]['jcr:title']) {
                            // too deep                          
                            return false;
                        }
                    }
                }
            }
            
            return hasChild;
        }
    }
};

