/*	
CASH UI Tools core class
more information/downloads available at: http://uitools.cashmusic.org

distributed under the BSD license
Copyright (c) 2009, CASH Music
All rights reserved.
*/
var CUIDebug=new Class({debug:0,cuiCore:false,setCUICore:function(a){this.cuiCore=a;this.debug=this.cuiCore.debug;},debugMsg:function(b,d){if(this.debug){var a="",c;b=b+0;if(this.cuiCore&&this.name){a="["+this.name+"] ";}c=["CUI Error: ","CUI Warning: ","CUI Notice: "];console.log(c[b]+a+d);}},debugLoadMsg:function(){if(this.debug){var a="CUI Module Loaded: "+this.name+" (v"+this.version+")",b=this.listAllOptions();if(b){a+="\nOptions: "+b;}console.log(a);}},listAllOptions:function(){if(!this.donotdebugoptions){var b="",a="",c="";$H(this.options).each(function(e,d){if(typeof(e)=="string"){c="'";}b+=a+d+" = "+c+e+c;a=", ";c="";});if(b){return b;}else{return false;}}else{return false;}}});var CUICore=new Class({Implements:[Options,Events,CUIDebug],name:"CASH User Interface Tools",version:0.9,options:{debug:0,autoBoot:0,timeout:16},initialize:function(a){this.setOptions(a);
this.modules=$H();this.commonCache=$H();this.injectedFiles=[];this.debug=0;this.timeout=this.options.timeout*1000;this.documenthead=$$("head")[0];if(document.id("cui_core")){this.libpath=document.id("cui_core").getProperty("src").replace("cui_core.js","");}this.defineLibrary();if(this.options.autoBoot){this.bootstrap();}if(this.options.debug&&typeof(console)!="undefined"){this.debug=1;var b=this.listAllOptions();if(this.libpath){console.log("CASH UI Tools (v"+this.version+") loaded.\nPath: '"+this.libpath+"'\nMooTools version: "+MooTools.version+"\nOptions: "+b);}else{console.log("CASH UI Tools loaded with errors. Version: "+this.version+' Path unknown. Please add id="cui_core" to script declaration\nOptions: '+b);}}},injectScript:function(a,c){var b;if(c){a=this.libpath+a;}if(this.injectedFiles.indexOf(a)==-1){b=new Element("script",{type:"text/javascript",src:a}).injectInside(this.documenthead);
this.injectedFiles.push(a);}},htmlContentChanged:function(a){this.allModulesAutoAttach(a);this.fireEvent("htmlChanged",a);},storeModule:function(c,b,g,f,e,a){var d={path:c,dependencies:g,autoLaunch:f,attach:e,relativePath:a,pointer:null,options:null};this.modules.set(b,d);},loadModule:function(a){var c=this.modules.get(a),b;if(c){if(c.pointer===null){if(c.dependencies){b=$A(c.dependencies.split(","));b.each(function(d){if(this.modules.get(d).pointer===null){this.loadModule(d);}}.bind(this));this.loadAfterDependencies(a,b,0);}else{this.injectScript(c.path,c.relativePath);}}else{this.debugMsg(2,"requested module ('"+a+"') already loaded");return false;}}else{this.debugMsg(0,"requested module ('"+a+"') is not defined, cannot load");return false;}},loadAfterDependencies:function(c,h,f){var b=h.length,g=0,a,e,d;
h.each(function(i){if(this.modules.get(i).pointer){g++;}}.bind(this));if(g<b){if(f<this.timeout){a=f+100;e=[c,h,a];this.loadAfterDependencies.delay(100,this,e);}else{this.debugMsg(0,"requested module ('"+c+"') could not load, dependency loading exceeded timeout");}}else{d=this.modules.get(c);this.injectScript(d.path,d.relativePath);}},getModule:function(b){var a=this.modules.get(b);if(a){return a.pointer;}else{return null;}},getModuleOptions:function(b){var a=this.modules.get(b);if(a){return a.options;}else{return null;}},setModuleOptions:function(a,b){var c=this.modules.get(a);if(c){c.options=b;}this.modules.set(a,c);},setModulePointer:function(b,a){var c=this.modules.get(b);if(c){c.pointer=a;}this.modules.set(b,c);},moduleCallback:function(a){if(typeof(a)=="object"){if(this.modules.has(a.name)){this.setModulePointer(a.name,a);
}else{this.storeModule(0,a.name,0);this.setModulePointer(a.name,a);}this.fireEvent("moduleLoad",a.name);a.debugLoadMsg();this.moduleAutoAttach(a.name);}else{this.debugMsg(0,"moduleCallback() must be provided with an object");}},moduleAutoAttach:function(b,d){var c=this.modules.get(b),a,e;if(c.autoLaunch&&c.attach&&typeof(c.pointer.attachToElement)=="function"){a=$A(c.autoLaunch.split(","));e=$$(a);if(e.length>0){if(!d){$$(a).each(function(f){c.pointer.attachToElement(f);});}else{a.each(function(f){document.id(d).getElements(f).each(function(g){c.pointer.attachToElement(g);});});}}}},allModulesAutoAttach:function(a){this.modules.each(function(c,b){if(c.autoLaunch&&c.pointer){if(!a){this.moduleAutoAttach(b);}else{this.moduleAutoAttach(b,a);}}}.bind(this));},registerModule:function(c,b){c.implement(new CUIDebug());
var a=this.getModuleOptions(b),d;if(a){d=new c(a);}else{d=new c();}d.setCUICore(this);this.moduleCallback(d);},bootstrap:function(){var b=[],a;this.modules.each(function(d,c){if(d.autoLaunch&&!d.pointer){a=$A(d.autoLaunch.split(","));if($$(a).length>0){this.loadModule(c);b.push(c);}}}.bind(this));if(b.length>0){this.debugMsg(2,"boot started, attempting to load necessary modules ("+b.join(", ")+")");}this.checkBootStatus(b,0);},checkBootStatus:function(a,d){if(a.length==0){this.fireEvent("bootComplete",true);}else{var f=a.length,e=0,b,c;a.each(function(g){if(this.modules.get(g).pointer){e++;}}.bind(this));if(e<f){if(d<(this.timeout+2000)){b=d+100;c=[a,b];this.checkBootStatus.delay(100,this,c);}else{this.fireEvent("bootComplete",false);this.debugMsg(1,"boot failure, could not load all modules");
}}else{this.fireEvent("bootComplete",true);this.debugMsg(2,"boot successfully completed");}}},defineLibrary:function(){this.storeModule("enhancements/cui_anchor.js","linkexternal",0,"a.external",1,1);this.storeModule("enhancements/cui_anchor.js","linkpopup",0,"a.popup",1,1);this.storeModule("enhancements/cui_anchor.js","linkinside",0,"a.cui_linkinside",1,1);this.storeModule("enhancements/cui_anchor.js","drawer",0,"a.cui_drawertoggle",1,1);this.storeModule("utility/cui_utility.js","utility",0,0,0,1);this.storeModule("media/cui_overlay.js","overlay",0,0,0,1);this.storeModule("media/cui_imagebox.js","imagebox","utility,overlay","a.cui_imagebox,div.cui_imagebox",1,1);this.storeModule("media/cui_moviebox.js","moviebox","utility,overlay","a[href$=.mov],a[href$=.mp4],a[href$=.MOV],a[href$=.MP4],a[href^=http://www.youtube.com/watch?v],a[href^=http://youtube.com/watch?v],a[href^=http://vimeo.com/],a[href^=http://www.vimeo.com/],a[href^=http://video.google.com/videoplay?docid],a[href^=http://myspacetv.com/index.cfm?fuseaction=vids.individual&videoid],a[href^=http://vids.myspace.com/index.cfm?fuseaction=vids.individual&videoid]",1,1);
},clearAutoLoad:function(a){var b=this.modules.get(a);b.autoLaunch=0;},addToAutoLoad:function(a,c){var b=this.modules.get(a);if(b.autoLaunch){b.autoLaunch+=(","+c);}else{b.autoLaunch=c;}}});