﻿(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  this.Cv = function(){};
  
  //Public property. Accessible with "Cv.myPupProp" from everywhere
  Cv.myPupProp = "value";
  
  Cv.namespace = function() {
	var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=Cv;

        for (j=(d[0] == "Cv") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
  }
})();


//###### JavscriptLoader #################################

Cv.JavascriptLoader = Class.create({
	loadedJavascripts : null,
	
	initialize: function() {
		this.loadedJavascripts = new Array();
	},
	
	load : function(path) {
		for (var i = 0; i < this.loadedJavascripts.length; i++) {
			if (this.loadedJavascripts[i].path == path) {
				return;
			}
		}
		
		this.loadedJavascripts[this.loadedJavascripts.length] = new Cv.JavscriptStore(path);
		var myAjax = new Ajax.Request(path + "?rnd=" + Math.random(), { method: 'get',parameters: null, asynchronous:false, onFailure: function() {alert("Error: Could not load modules *" + Namespace + "*. Please refresh your browser window and try again.");}});
		
		eval(myAjax.transport.responseText);
		
		return;
	}
});

if (!window.CvJavascriptLoader)
	var CvJavascriptLoader = new Cv.JavascriptLoader();
	
	
//###### JavscriptStore #################################

Cv.JavscriptStore = Class.create({
	path : null,
		
	initialize: function(path) {
		this.path = path;
	}
});


//###### ResourceLoader #################################

Cv.ResourceLoader = Class.create({
	loadedResources : null,
	
	initialize: function() {
		this.loadedResources = new Array();
	},
	
	load : function(path) {
		for (var i = 0; i < this.loadedResources.length; i++) {
			if (this.loadedResources[i].path == path) {
				return;
			}
		}
		
		this.loadedResources[this.loadedResources.length] = new Cv.ResourceStore(path);
		
		var myAjax = new Ajax.Request(path + "?rnd=" + Math.random(), { method: 'get',parameters: null, asynchronous:false, onFailure: function() {alert("Error: Could not load resource *" + Resource + "*. Please refresh your browser window and try again.");}});
		
		var el = document.createElement("script");
		el.text = myAjax.transport.responseText;
		el.type="text/javascript";
		document.getElementsByTagName("head")[0].appendChild(el);
		
		return;
	}
});

if (!window.CvResourceLoader)
	var CvResourceLoader = new Cv.ResourceLoader();
	
	
//###### ResourceStore #################################

Cv.ResourceStore = Class.create({
	path : null,
		
	initialize: function(path) {
		this.path = path;
	}
});	


//###### CssLoader #################################

Cv.CssLoader = Class.create({
	loadedCss : null,
	
	initialize: function() {
		this.loadedCss = new Array();
	},
	
	load : function(Url) {
		for (var i = 0; i < this.loadedCss.length; i++) {
			if (this.loadedCss[i].Url == Url) {
				return;
			}
		}
		
		CvCssLoader.loadedCss[CvCssLoader.loadedCss.length] = new Cv.CssStore(Url);
		
		var el = document.createElement("link");
		el.href = Url;
		el.type = "text/css";
		el.rel = "stylesheet";
		document.getElementsByTagName("head")[0].appendChild(el);
		
		return;
	}
});

if (!window.CvCssLoader)
	var CvCssLoader = new Cv.CssLoader();
	
//###### CssStore #################################

Cv.CssStore = Class.create({
	Url : null,
		
	initialize: function(Url) {
		this.Url = Url;
	}
});	
