Yourchance = {};

var Yourchance = (window.Yourchance || {});

/**
* function for namespaces - taken from YAHOO.namespace()
*/
Yourchance.namespace = function() {
  var a=arguments, o=null, i, j, d;
  for (i=0; i<a.length; i=i+1) {
	 d=a[i].split(".");
	 o=Yourchance;
	 for (j=(d[0] === "Yourchance") ? 1 : 0; j<d.length; j=j+1) {
		o[d[j]]=o[d[j]] || {};
		o=o[d[j]];
	 }
  }
  return o;
};

/**
 * This class is used to include css or javascript at runtime
 *
 * Example:
 *
 * Maguro.Invoker.invoke(["foo.js", "bar.css"]);
 *
 * @author maguro DataAssist (fh)
 */
Yourchance.Invoker = {

	/**
	 * Hold keys for included files
	 *
	 * @var Array
	 */
	cache: [],

	/**
	 * Invokes the given file. Note that the file extension and content type are the right.
	 * If given event, the event called after finished include of files
	 *
	 * Example:
	 *
	 * Yourchance.Invoker.invoke(["foo.js", "bar.css"], function() {
	 * 	alert("foo.js and bar.css loaded !")
	 * });
	 *
	 * foo.js = javascript
	 * foo.js.jsp => include as javascript
	 * bar.css => stylesheet
	 * bar.css.jsp => incuded as stylesheet
	 *
	 * @param Array files list of files to include
	 * @param Function event custom function, called onloaded files
	 *
	 * @retun void
	 */
	invoke:function(files, event) {

		if (Object.isString(files))
			files = [files];
		
		var amount = files.length;
		var loaded = 0;
		
		files.each(function(file) {
			var element = null;

			if(!this.cache.include(file)) {
				this.cache.push(file);

				if (file.indexOf(".js") > 0) {
					element = document.createElement('script');
					element.setAttribute("src", file);
					element.setAttribute("type", "text/javascript");
				} else if (file.indexOf(".css") > 0) {
					element = document.createElement('link');
					element.setAttribute("href", file);
					element.setAttribute("rel", "stylesheet");
					element.setAttribute("type", "text/css");
				} else {
					return;
				}

				if (event != null) {

					if (element.addEventListener) {
						
						Event.observe(element, 'load', function() {
							loaded++;
							if (loaded == amount) {
								event.call(this);
							}
						}, false);
						
					} else {
						
						element.onreadystatechange = function() {
							if(this.readyState.match(/loaded|complete/)) {
								loaded++;
								if (loaded == amount) {
									event.call(this);
								}
							}
						}
					}
				}

				$$('head').first().appendChild(element);

			} else {
				loaded++;
				if (loaded == amount) {
					if (typeof event == "function")
						event();
				}
			}
		}.bind(this));
	}
};

/**
 * explorer fix to disable background image reloading
 */
try {

	document.execCommand("BackgroundImageCache", false, true);
} catch(error) {}
