/* Script: Ensure.js | Author:Omar AL Zabir - http://msmvps.com/blogs/omar | License:Copyright (C) 2008 Omar AL Zabir - http://msmvps.com/blogs/omar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
(function(){window.ensure=function(data,callback,scope){if(typeof Prototype=="undefined"){return alert("Prototype library not found.");}if(typeof data.test!="undefined"){var test=function(){return data.test;};if(typeof data.test=="string"){test=function(){return !(eval("typeof "+data.test)=="undefined"&&document.getElementById(data.test)===null);};}else{if(typeof data.test=="function"){test=data.test;}}if(test()===false||typeof test()=="undefined"||test()===null){ensExec=new ensureExecutor(data,callback,scope);}else{callback();}}else{ensExec=new ensureExecutor(data,callback,scope);}};window.ensureExecutor=function(data,callback,scope){this.data=this.clone(data);this.callback=(typeof scope=="undefined"||null===scope?callback:this.delegate(callback,scope));this.loadStack=[];if(data.js&&data.js.constructor!=Array){this.data.js=[data.js];}if(data.html&&data.html.constructor!=Array){this.data.html=[data.html];}if(data.css&&data.css.constructor!=Array){this.data.css=[data.css];}if(typeof(data.js)=="undefined"){this.data.js=[];}if(typeof(data.html)=="undefined"){this.data.html=[];}if(typeof(data.css)=="undefined"){this.data.css=[];}this.init();this.load();};window.ensureExecutor.prototype={init:function(){if(typeof Prototype!="undefined"){this.getJS=HttpLibrary.loadJavascript_Prototype;this.httpGet=HttpLibrary.httpGet_Prototype;}else{throw"Prototype framework not found";}},getJS:function(data){},httpGet:function(url,callback){},load:function(){this.loadJavascripts(this.delegate(function(){this.loadCSS(this.delegate(function(){this.loadHtml(this.delegate(function(){this.callback();}));}));}));},loadJavascripts:function(complete){var scriptsToLoad=this.data.js.length;if(0===scriptsToLoad){return complete();}this.forEach(this.data.js,function(href){if(HttpLibrary.isUrlLoaded(href)||this.isTagLoaded("script","src",href)){scriptsToLoad--;}else{this.getJS({url:href,success:this.delegate(function(content){scriptsToLoad--;HttpLibrary.registerUrl(href);}),error:this.delegate(function(msg){scriptsToLoad--;if(typeof this.data.error=="function"){this.data.error(href,msg);}})});}});this.until({test:function(){return scriptsToLoad===0;},delay:50,callback:this.delegate(function(){complete();})});},loadCSS:function(complete){if(0===this.data.css.length){return complete();}var head=HttpLibrary.getHead();this.forEach(this.data.css,function(href){if(HttpLibrary.isUrlLoaded(href)||this.isTagLoaded("link","href",href)){}else{var self=this;try{(function(href,head){var link=document.createElement("link");link.setAttribute("href",href);link.setAttribute("rel","Stylesheet");link.setAttribute("type","text/css");head.appendChild(link);HttpLibrary.registerUrl(href);}).apply(window,[href,head]);}catch(e){if(typeof self.data.error=="function"){self.data.error(href,e.message);}}}});complete();},loadHtml:function(complete){var htmlToDownload=this.data.html.length;if(0===htmlToDownload){return complete();}this.forEach(this.data.html,function(href){if(HttpLibrary.isUrlLoaded(href)){htmlToDownload--;}else{this.httpGet({url:href,success:this.delegate(function(content){htmlToDownload--;HttpLibrary.registerUrl(href);var parent=(this.data.parent||document.body.appendChild(document.createElement("div")));if(typeof parent=="string"){parent=document.getElementById(parent);}parent.innerHTML=content;}),error:this.delegate(function(msg){htmlToDownload--;if(typeof this.data.error=="function"){this.data.error(href,msg);}})});}});this.until({test:function(){return htmlToDownload===0;},delay:50,callback:this.delegate(function(){complete();})});},clone:function(obj){var cloned={};for(var p in obj){var x=obj[p];if(typeof x=="object"){if(x.constructor==Array){var a=[];for(var i=0;i<x.length;i++){a.push(x[i]);}cloned[p]=a;}else{cloned[p]=this.clone(x);}}else{cloned[p]=x;}}return cloned;},forEach:function(arr,callback){var self=this;for(var i=0;i<arr.length;i++){callback.apply(self,[arr[i]]);}},delegate:function(func,obj){var context=obj||this;return function(){func.apply(context,arguments);};},until:function(o){if(o.test()===true){o.callback();}else{window.setTimeout(this.delegate(function(){this.until(o);}),o.delay||50);}},isTagLoaded:function(tagName,attName,value){var tag=document.createElement(tagName);tag[attName]=value;var tagFound=false;var tags=document.getElementsByTagName(tagName);this.forEach(tags,function(t){if(tag[attName]===t[attName]){tagFound=true;return false;}});return tagFound;}};var userAgent=navigator.userAgent.toLowerCase();var HttpLibrary={browser:{version:(userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1],safari:/webkit/.test(userAgent),opera:/opera/.test(userAgent),msie:/msie/.test(userAgent)&&!/opera/.test(userAgent),mozilla:/mozilla/.test(userAgent)&&!/(compatible|webkit)/.test(userAgent)},loadedUrls:{},isUrlLoaded:function(url){return HttpLibrary.loadedUrls[url]===true;},unregisterUrl:function(url){HttpLibrary.loadedUrls[url]=false;},registerUrl:function(url){HttpLibrary.loadedUrls[url]=true;},createScriptTag:function(url,success,error){var scriptTag=document.createElement("script");scriptTag.setAttribute("type","text/javascript");scriptTag.setAttribute("src",url);scriptTag.onload=scriptTag.onreadystatechange=function(){if((!this.readyState||this.readyState=="loaded"||this.readyState=="complete")){success();}};scriptTag.onerror=function(){error(data.url+" failed to load");};var head=HttpLibrary.getHead();head.appendChild(scriptTag);},getHead:function(){return document.getElementsByTagName("head")[0]||document.documentElement;},globalEval:function(data){var script=document.createElement("script");script.type="text/javascript";if(HttpLibrary.browser.msie){script.text=data;}else{script.appendChild(document.createTextNode(data));}var head=HttpLibrary.getHead();head.appendChild(script);},loadJavascript_Prototype:function(data){if(HttpLibrary.browser.safari){var params={url:data.url,success:function(content){HttpLibrary.globalEval(content);data.success(content);},error:data.error};HttpLibrary.httpGet_Prototype(params);}else{HttpLibrary.createScriptTag(data.url,data.success,data.error);}},httpGet_Prototype:function(data){new Ajax.Request(data.url,{method:"get",evalJS:false,onSuccess:function(transport,json){data.success(transport.responseText||"");},onFailure:data.error});}};})();
