
var scriptonfire = {
	
	build: function() {
		return function() {
			this.construct.apply(this, arguments);
		}
	},
	
	debug: function() {
		alert("debug");
	},
	
	object: function()
	{
		return new Object();
	}
	
};

var userAgent = navigator.userAgent.toLowerCase();
scriptonfire.browser = {
	version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
	safari: (userAgent.match(/webkit/) || false),
	opera: (userAgent.match(/opera/) || false),
	msie: (userAgent.match(/msie/) && !userAgent.match(/opera/)),
	mozilla: (userAgent.match(/mozilla/) && !userAgent.match(/compatible|webkit/))
};

function trim(string, chars) {
	return ltrim(rtrim(string, chars), chars);
}
function ltrim(string, chars) {
	chars = chars || "\\s";
	if(string) return string.replace(new RegExp("^[" + chars + "]+", "g"), "");
	return null;
}
function rtrim(string, chars) {
    chars = chars || "\\s";
	if(string) return string.replace(new RegExp("[" + chars + "]+$", "g"), "");
	return null;
}

var $array = {
	
	iterate: function(i) {
		if(!i) return [];
		if(i.toArray) return i.toArray();
		else {
			var results = [];
			for(var j = 0, length = i.length; i < length; i++)
				results.push(i[j]);
			return results;
		}
	},
	
	clean: function(array) {
		if(arguments.length > 1)
			for(var i = 0, _array = []; i < arguments.length; i++)
				arrays.push($array.clean(_array[i]));
			return _array;
		if(array) {
			var $_new = [];
			for(var j = 0; j < array.length; j++) {
				if(array[j] && array[j] != "" && array[j] != null && array[j] != false)
					$_new.push(array[j]);
			}
			return $_new;
		} else return false;
	}
	
}

var $attribute = scriptonfire.build();
$attribute.prototype = {
	
	$objects: [],
	$attributes: [],
	$values: [],
	$tagname: "",
	$parent: {},
	
	construct: function($tagname, $parent)
	{
		this.$tagname = $tagname;
		this.$parent = $parent;
	},
	
	search: function($attribute, $value)
	{
		if($attribute != "")
		{
			this.$attributes.push($attribute);
		}
		if($value != "")
		{
			this.$values.push($value);
		}
	},
	
	checkout: function()
	{
		var $elements = this.$parent.getElementsByTagName(this.$tagname);
		
		for(var $i = 0, $search_attributes = "", $element_attributes = ""; $i < $elements.length; $i++)
		{
			if(this.$attributes.length > 0 && this.$values.length > 0)
			{
				for(var $j = 0; $j < this.$attributes.length; $j++)
				{
					$search_attributes += this.$attributes[$j] + "=" + this.$values[$j] + "&";
					if($elements[$i][this.$attributes[$j]] == this.$values[$j])
					{
						$element_attributes += this.$attributes[$j] + "=" + this.$values[$j] + "&";
					}
					//alert($elements[$i].tagName + " : " + this.$attributes[$j]);
				}
				if(
					$search_attributes != "" &&
					$element_attributes != "" &&
					$search_attributes == $element_attributes
				)
				{
					this.$objects.push($extend($elements[$i], $events));
				}
			}
			else
			{
				//alert($elements[$i].tagName);
				this.$objects.push($extend($elements[$i], $events));
			}
			$search_attributes = "";
			$element_attributes = "";
		}
		
		this.$attributes = [];
		this.$values = [];
		this.$tagname = [];
		
		return this.$objects;
	}
	
}

var $extend = function(destination, source) {
	if(destination.length > 1)
		for(var object in destination)
			for(var property in source)
				destination[object][property] = source[property];
	else
		for(var property in source)
			destination[property] = source[property];
	return destination;
};

function $element(target, $parent)
{
	$elements = [];
	
	if(typeof target != "string")
	{
		return target;
	}
	
	if(typeof target == "string")
	{
		var targets = target.split(";");
		if(targets.length > 1)
		{
			for(var i = 0, elements = []; i < targets.length; i++)
			{
				elements.push($element(trim(targets[i]), $parent));
			}
			return elements;
		}
		else
		{
			$regex = new RegExp(/^([\w_-]*)(?:[:]*([\w_-]*)[=]*)([.#]*)([\w_-]*)/);
			$element_properties = [];
			while(target != "" && target.match($regex))
			{
				$element_properties.push(target.match($regex));
				target = target.replace($regex, "");
			}
			
			$element_attributes = new $attribute(($element_properties[0][1] || "*"), $parent);
			for(var j = 0; j < $element_properties.length; j++)
			{
				$element_attributes.search(
					($element_properties[j][3] || $element_properties[j][2] || ""),
					($element_properties[j][4] || "")
				);
			}
			$elements.push($element_attributes.checkout());
			
		}
	}
	return $elements;
};

Function.prototype.bind = function() {
	var __method = this, __args = $array.iterate(arguments), __object = __args.shift();
	return function() { return __method.apply(object, args.concat($array.iterate(arguments))); }
};

var $events = function() {};
$extend($events, {
	each: function(action, method) {
		for(var i = 0; i < action.length &&
			method.call(action[0],i,action[0]) !== false;
			action[0] = action[++i])
		{}
	}
});

$events.events = [
	"click","dblclick","mousedown",
	"mouseup","mousemove","mouseover",
	"mouseout","change","select",
	"keydown","keypress","keyup","error"
];

$events.each($events.events, function(i,o){
	$events[o] = Object[o] = function(f){
		if(this.length > 1) for(var j = 0; j < this.length; j++) $dom.add_listener(this[j], o, f, false);
		else $dom.add_listener(this, o, f, false);
	};
});

function $(element) {
	
	$traverse = element.split(" ");
	$parent = document;
	
	for(var $elements in $traverse)
	{
		$parent = $element($traverse[$elements], $parent);
	}
	
	alert($parent);
	
	/*
	for(var i = 1; i < $traverse.length; i++)
	{
		$elements = $element(element, $parent);
		for(var j = 0; j < $elements.length; j++)
		{
			
		}
		$parent = (target, $parent);
	}
	
	$elements = $element(element, document);
	if($elements.length > 1) return $extend($elements, Object);
	else return $elements[0];
	*/
}

var $dom = scriptonfire.build();
$dom.prototype = {
	
	construct: function() {
		if(!this.ready) this.add_listener(window, "load", this.initialize, false);
	},
	
	ready: false,
	listeners: false,
	
	initialize: function() {
		this.ready = true;
		
		// examples:
		//
		// target all 'div' elements
		// $("div")
		
		// target all 'div' elements without a class
		// $("div.");
		
		// target all 'div' elements with a class name as 'class'
		// $("div.class");
		
		// target all 'div' elements with an id value of 'elementid'
		// $("div#elementid");
		
		// Add listeners to any object using an object listener, example:
		//
		// target all 'div' elements to react 'onclick' with the specified function
		// $("div").click(scriptonfire.debug);
		
	},
	
	load_listener: function(element, name, observer, useCapture) {
		if(!this.listeners) this.listeners = [];
		if(element.addEventListener) {
			this.listeners.push([element, name, observer, useCapture]);
			element.addEventListener(name, observer, useCapture);
		} else if (element.attachEvent) {
			this.listeners.push([element, name, observer, useCapture]);
			element.attachEvent('on' + name, observer);
		} else {
			// using a browser that does not support the DOM
		}
	},
	
	unload_listeners: function() {
		if(!this.listeners) return;
		for(var i = 0, length = this.listeners.length; i < length; i++) {
			this.remove_listener.apply(this, this.listeners[i]);
			this.listeners[i][0] = null;
		}
		this.listeners = false;
	},
	
	add_listener: function(element, name, observer, useCapture) {
		useCapture = useCapture || false;
		if(name == 'keypress' && (document.attachEvent)) name = 'keydown';
		this.load_listener(element, name, observer, useCapture);
	},
	
	remove_listener: function(element, name, observer, useCapture) {
		useCapture = useCapture || false;
		if(name == 'keypress' && (element.attachEvent)) name = 'keydown';
		if(element.removeEventListener) element.removeEventListener(name, observer, useCapture);
		else if(element.detachEvent) element.detachEvent('on' + name, observer);
	}
	
};

$dom = new $dom();





