var TWP = window.TWP || {};
TWP.url = window.location.href;
TWP.server = TWP.server || window.location.href.match(/^.*?:\/\/.*?\//)[0];
TWP.debug = window.location.href.match("twpDebug=true") ? true : TWP.debug;
TWP.contextServer = 'http://media3.washingtonpost.com';
TWP.contextServerOverrides = TWP.contextServerOverrides || {
    "http://localhost/":"http://localhost:8080"
};
TWP.contextServer = TWP.server.match("digitalink.com") ? 'http://glasstest.digitalink.com' : TWP.contextServer;
TWP.contextServer = TWP.server.match("washpost.com") ? 'http://glasstest.digitalink.com' : TWP.contextServer;
TWP.contextServer = TWP.contextServerOverrides[TWP.server] ? TWP.contextServerOverrides[TWP.server] : TWP.contextServer;
TWP.contextServer = window.location.href.match("twpContextServer=") ?  window.location.href.match(/twpContextServer=([^&]+)/)[1] : TWP.contextServer;
TWP.eidosBase = TWP.eidosBase ? TWP.eidosBase : window.eidosBase;
/* Make A Change */
TWP.yuiConfig = TWP.yuiConfig || {
    combine:true,
    root:"r=/yui-base/3.3.0/build/",
    comboBase:(TWP.base ? TWP.base : TWP.contextServer) + '/wpost/js/combo?context=wpost&c=true&m=true&'
};
TWP.appInit = function(root, groups) {
    YUI(TWP.yuiConfig).use("node","oop",function(Y){
        if(!groups){
            groups = TWP.yuiConfig.groups;
        }
        root = Y.one(root);
        Y.Object.each(groups,function(group){
            Y.Object.each(group.modules,function(module,key){
                if(!module.disabled){
                    if(module.triggers){
                        Y.Array.each(module.triggers,function(trigger){
                            var triggered = false;
                            if(Y.Lang.isFunction(trigger)){
                                try{
                                    triggered = trigger();
                                }catch(e){
                                    Y.log("Error running trigger: " + e);
                                }
                            }else{
                                try{
                                    triggered = root.one(trigger);
                                }catch(e){
                                    Y.log("Error processing trigger: " + e);
                                }
                            }
                            if (triggered) {
								if(TWP.debug){
									Y.log(module.group+" : "+module.name);
									Y.log(module);
								}
                                var args = [key];
                                if (module.requires) {
                                    Y.Array.each(module.requires,function(s){
                                        args.push(s);
                                    });
                                }
                                args.push(function(innerY){
                                    if (module.init) {
                                        if(Y.Lang.isFunction(trigger)){
                                            try{
                                                module.init(innerY);
                                            }catch(e){
                                                Y.log("Error proccessing init:"+e);
                                            }
                                        }else{
                                            root.all(trigger).each(function(el){
                                                try{
                                                    module.init(innerY, el);
                                                }catch(e){
                                                    Y.log("Error proccessing init:"+e);
                                                }
                                            });
                                        }
                                    }
                                });
                                YUI(TWP.yuiConfig).use.apply(this,args);
                            }
                        });
                    }
                }
            });
        });
    });
}
