var Featherweight = function() {
	// Set up Featherweight namespace
	
	// Credits to Dustin Diaz for the meat of this implementation of Prototype's '$'.
	var _getElementsById = function(els) {
		var elements = [];
		var i = els.length;
		while(i--) {
		var element = els[i];
			if (typeof element == 'string') {
				element = document.getElementById(element);
			}
			elements.push(element);
		}
		return elements;
	}

	var _getElementsByClassName = function(className, parentNode, tag) {
		var elements = [];
		if ( parentNode == null )
			parentNode = document;
		if ( tag == null )
			tag = '*';
		var els = parentNode.getElementsByTagName(tag);
		var i = els.length;
		var pattern = new RegExp('(^|\\\\s)'+className+'(\\\\s|$)');
		while(i--) {
			if ( pattern.test(els[i].className) ) {
				elements.push(els[i]);
			}
		}
		
		return elements;
	}
	
	function _collection(els) {
		this.elements = els;
	}
	
	_collection.prototype.items = function(index) {
		return this.elements[index];
	}

	_collection.prototype.each = function(f_ptr) {
		var i = this.elements.length;
		while(i--) {
			f_ptr.call(this, this.elements[i]);
		}
	}

	_collection.prototype.on = function(e, f_ptr) {	
		this.each(function(el) {Featherweight.Core.addEvent.call(this, el, e, f_ptr);});
	}

	window.$ = function() {
		var args = arguments;
    	return new _collection(_getElementsById(args));
	}

	window.$$ = function(className, parentNode, tag) {
    	return new _collection(_getElementsByClassName(className, parentNode, tag));
	}

	var Core = {
		/*Browser: { // Straight from Prototype
			IE: !!(window.attachEvent && !window.opera),
			Opera: !!window.opera,
			WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
			Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
			MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
		},*/
		addEvent: function (object,e,callback) {
			if(window.addEventListener){ // Mozilla, Netscape, Firefox
				object.addEventListener(e, callback, false);
			} else { // IE
				object.attachEvent('on' + e, callback);
			}
		},
		getTarget: function (e) {
		    e = e || window.event;				// W3 || IE
		   	return e.target || e.srcElement;	// W3 || IE
		},
	}
	
	return {
		Core: Core,

		toString: function() { 
			return 'Framework Name';
		}
	};
}();

Featherweight.Loader = function(){
	var scripts = Array();
	var callbacks = Array();
	
	function _include_file(path) {
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = path;
		document.getElementsByTagName('head').item(0).appendChild(script);
	}
	
	return {
		Load: function(path, callback) {
			_include_file(path);

			scripts.push(path);
			callbacks.push(callback);

			window.setTimeout('Featherweight.Loader.check(\''+path+'\');',20);
		},
		
		check: function(path) {		
			var i = -1;
			while(i++ < scripts.length) {
				if(scripts[i] == path) {
					if(callbacks[i]) {
						callbacks[i]();
					}
					return true;
				}
			}
			window.setTimeout('Featherweight.Loader.check(\''+path+'\')',20);
			return false;
		},
		status: function() {
			return scripts.length;
		}
	}
}();

/*Featherweight.Ajax = function() {
	var _create_request = function(url, callback) {
		var httpRequest;
	
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			httpRequest = new XMLHttpRequest();
			//if (httpRequest.overrideMimeType) {
			//	httpRequest.overrideMimeType('text/xml');
			//}
		}
		else if (window.ActiveXObject) { // IE
			if(!(httpRequest = new ActiveXObject("Msxml2.XMLHTTP"))) {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		if (!httpRequest) {
			throw('Cannot create an XHR instance');
			return false;
		}
	
		httpRequest.onreadystatechange = function() { 
			if(httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {			
					if(callback) { // set callback function
						callback(httpRequest);
					}
				} else {
					throw('There was a problem with the request.');
				}
			}
		};
		
		return httpRequest;
	}
	return {
		Get: function(url,data,callback) {
			req = _create_request(url,callback);
			req.open('get', url + '?' + data, true);
			req.send(null);
		},
		Post: function(url,data,callback) {
			req = _create_request(url,callback);
			req.open('post',url,true);
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			req.send(data);
		}
	}
}();*/

Featherweight.Interface = {
	is_loaded: true,
	lazy: function(callback) {
		Featherweight.Loader.Load('scripts/interface.js',callback);
	},
	image_grow: function() {
		var args = arguments;
		Featherweight.Interface.lazy(function() { Featherweight.Interface.image_grow.apply(Featherweight.Interface, args); });
	},
}
