if(typeof ScriptHandler=='undefined') { 
Object.extend = function(destination, source, keepExistingTargets) {if(typeof keepExistingTargets=='undefined') keepExistingTargets = false;for (var property in source) {if(!keepExistingTargets)destination[property] = source[property];else if(typeof destination[property]=='undefined') {destination[property] = source[property];}}return destination;};Object.extend(String.prototype, {toQueryParams: function() {var pairs = this.split('?');if(pairs.length==1) return [];pairs = pairs[1].toString().split('&');var _l = pairs.length;var _back = {};for(var i=0; i<_l; i++) {var _splittedParams = pairs[i].split("&");var _lj = _splittedParams.length; for(var j=0; j<_lj; j++) {var _splitted = _splittedParams[j].split("=");_back[_splitted[0]] = _splitted[1];}}return _back;}}); 
var MSIE/*@cc_on =1@*/; 
inject = function(targetWindow, script, options) { 
var _options = {forceReturn:true}; 
if(typeof options!='undefined') Object.extend(_options, options); 
if(!_options.forceReturn && targetWindow.execScript) 
targetWindow.execScript(script); 
else if(MSIE) return targetWindow.eval(script); 
else if(eval.call) return eval.call(targetWindow, script); 
else throw "Cant find any method which can inject you code. Please email your browser-name and version to mb@tecbehind.de. Thank you!"; 
}; 
Function.prototype.createCallback = function(){ 
var args = arguments; 
var method = this; 
return function() { 
return method.apply(window, args); 
}; 
}; 
var ST_COMMON = 0; 
var ST_APP = 1; 
var ST_REGISTRY = 2; 
var ST_STYLE = 3; 
var ST_DEBUG = true; 
ScriptHandler = function(scripts) { 
this.scripts = scripts; 
} 
ScriptHandler.prototype = { 
handleSuccess:function(o){ 
var _l = this.scripts.length; 
for(var i=0; i<_l; i++) { 
var _s = this.scripts[i]; 
if(typeof _s.transaction!='undefined' && _s.transaction.tId==o.tId) { 
_s.content=o.responseText; 
delete this.scripts[i].transaction; 
if(!this.loaded) { 
this.loadedScripts++; 
if(this.loadedScripts==this.scripts.length && this.connectionsTriggered) { 
this.connectionsTriggered = false; 
this.loaded = true; 
if((typeof SH_NO_SINGLETON!='boolean' || !SH_NO_SINGLETON) && typeof window.$SH=='undefined') { 
if(typeof console!='undefined') console.log('ScriptHandler() main-instance not defined, letz use this one'); 
inject(window.top.window, "var $SH = null;", true); 
window.top.window.$SH = this; 
inject(window.top.window, "$SH.inject(self)", true); 
if(window.opener && typeof window.opener!='undefined' && !window.opener.top.window==window.top.window) 
try { 
inject(opener.window.top.window, "var $SH = null;", true); 
opener.window.top.window.$SH = this; 
inject(opener.window.top.window, "$SH.inject(self)", true); 
}catch(e) { 
} 
inject(window.parent.window, "function _dontInjectFiles(){return typeof SH_NO_AUTOINJECT_STYLES=='undefined' ? false : SH_NO_AUTOINJECT_STYLES;}", true); 
var _dontInjectStyles = window.parent.window._dontInjectFiles(); 
if(!_dontInjectStyles) this.injectStyles(window.top.window); 
} 
if(typeof this.fn=='function') this.fn(this); 
}                      
}else{ 
if(typeof _s.callback=='function') { 
_s.callback(o.responseText); 
delete this.scripts[i].callback; 
} 
} 
break; 
} 
} 
}, 
getScripts: function(types) { 
var _debug = function(_a, _s) { 
if(ST_DEBUG) { 
_a.push("/**\r\n\tSCRIPT: "+_s.url+"\r\n**/"); 
if(typeof console!='undefined') _a.push("console.log('Running SCRIPT: "+_s.url+", lines: "+(_s.content.split("\n").length)+"');"); 
}; 
}; 
if(!this.loaded) throw "You cant call getScripts() before they arent completely loaded. Use init() to do that"; 
var _common = false, _app = false, _registry = false; _styles = false; 
if(typeof types=='undefined') { 
_common=true; _app=false; _registry=true; 
}else if(typeof types!='undefined') { 
for(var i=0; i<types.length; i++) 
switch(types[i]) { 
case ST_COMMON: _common=true; break; 
case ST_APP: _app=true; break; 
case ST_REGISTRY: _registry=true; break; 
case ST_STYLE: _styles=true; break; 
default: throw "Script-Type "+types[i]+" isnt a known script-type"; break; 
}; 
if(_styles && (_registry || _app || _common)) throw "It makes no sence to mix stylesheets with javascript, types: "+(types.join(",")); 
}else throw "You cant call ScriptLoader.getScripts() with an argument of type "+typeof types; 
var _a = new Array(); 
var _l = this.scripts.length; 
for(var i=0; i<_l; i++)  { 
var _s = this.scripts[i]; 
if((typeof _s.type=='undefined' && _common) || _styles) { 
if(ST_DEBUG) { 
_a.push("/**\r\n\tSCRIPT: "+_s.url+"\r\n**/"); 
if(typeof console!='undefined' && !_styles) 
_a.push("console.log('Running SCRIPT: "+_s.url+"');"); 
}; 
_a.push(_s.content); 
} else { 
switch(_s.type) { 
case ST_COMMON: if(_common) { _debug(_a, _s); _a.push(_s.content); }; break; 
case ST_APP: if(_app) {_debug(_a, _s); _a.push(_s.content); }; break; 
case ST_REGISTRY: if(_registry) { _debug(_a, _s); _a.push(_s.content); }; break; 
} 
} 
} 
return _a.join(';'); 
}, 
inject: function(targetWindow, addToScripts, types) { 
inject(targetWindow, this.getScripts(types)+(typeof addToScripts=='string' ? ";"+addToScripts : ";")); 
}, 
injectStyles: function(targetWindow) { 
if(typeof targetWindow!='undefined') { 
var _h = document.getElementsByTagName("HEAD")[0]; 
if(typeof _h!='undefined') { 
if(MSIE) { 
_h.appendChild(document.createTextNode('<style type="text/css">'+this.getScripts([ST_STYLE])+'</style>')); 
}else{ 
var _styles = document.createElement("style"); 
_h.appendChild(_styles); 
_styles.type = "text/css"; 
_styles.innerHTML = this.getScripts([ST_STYLE]); 
} 
}else document.write('<style type="text/css">'+this.getScripts([ST_STYLE])+'</style>'); 
}else{ 
targetWindow.$SH.injectStyles(); 
} 
}, 
reloadScript: function(scriptId, fn) { 
var _l = this.scripts.length; 
for(var i=0; i<_l; i++) { 
var _s = this.scripts[i]; 
if(_s.id==scriptId) { 
if(typeof _s.type=='undefined' || _s.type!=ST_REGISTRY) throw "Only scripts of type ST_REGISTRY can be reloaded"; 
this.script[i].callback = fn; 
var callback = { 
success:this.handleSuccess, 
failure:this.handleFailure, 
scope: this 
} 
YAHOO.util.Connect.asyncRequest('GET', _s.url, callback); 
return; 
} 
}; 
throw "The registry-script with the id "+scriptId+" was not found, so i cant reload it"; 
}, 
handleFailure:function(o){ 
throw "Error while loading the script '"+this.scripts[this.currentScript].url+"': "+o.resonseText; 
}, 
init:function(fn) { 
this.fn = fn; 
this.loaded = false; 
this.loadedScripts=0; 
var _self = this; // tricky :) 
var callback = { 
success:_self.handleSuccess, 
failure:_self.handleFailure, 
scope: _self 
}; 
for(var i=0; i<this.scripts.length; i++) 
this.scripts[i].transaction = YAHOO.util.Connect.asyncRequest('GET', this.scripts[i].url, callback); 
this.connectionsTriggered = true; 
} 
}; 
if(typeof SH_NO_SINGLETON!='boolean' || !SH_NO_SINGLETON) { 
// letz check if there is an ScriptHandler which is our main-one 
if(window.top.window!=window && window.top.window.$SH) { 
if(ST_DEBUG && typeof console!='undefined') console.log("ScriptHandler found at top-window"); 
window.$SH = window.top.window.$SH; 
if(typeof SH_NO_AUTOINJECT_STYLES=='boolean' && !SH_NO_AUTOINJECT_STYLES) window.top.window.$SH.injectStyles(window.top.window); 
} else if(window.opener && window.opener.top.window.$SH && window.opener.top.window!=window) { 
if(ST_DEBUG && typeof console!='undefined') console.log("ScriptHandler found at the top-window of the opener"); 
window.$SH = window.opener.top.window.$SH; 
if(typeof SH_NO_AUTOINJECT_STYLES=='boolean' && !SH_NO_AUTOINJECT_STYLES) window.opener.top.window.$SH.injectStyles(window.top.window); 
}; 
} 
} 

