1 /**
  2 	Kis JS		Keep It Simple JS Library
  3 	Copyright	Timothy J. Warren
  4 	License		Public Domain
  5 	Version		0.8.0
  6  */
  7 (function (undefined){
  8 
  9 	"use strict";
 10 
 11 	var $_, $, sel;
 12 
 13 
 14 	/**
 15 	 * $_
 16 	 *
 17 	 * Constructor function
 18 	 *
 19 	 * @constuctor
 20 	 * @namespace
 21 	 * @param string selector
 22 	 * @return object
 23 	 */
 24 	$_ = function(s)
 25 	{
 26 		// Have documentElement be default selector, just in case
 27 		if (s === undefined)
 28 		{
 29 			// Defines a "global" selector for that instance
 30 			sel = ($_.el !== undefined)
 31 				? $_.el
 32 				: document.documentElement;
 33 		}
 34 		else
 35 		{
 36 			sel = $(s);
 37 		}
 38 
 39 		// Add the selector to the prototype
 40 		$_.prototype.el = sel;
 41 
 42 		// Use the $_ object as it's own prototype
 43 		var self = Object.create($_);
 44 
 45 		// Give sel to each extension.
 46 		for(var i in self)
 47 		{
 48 			if(typeof self[i] === "object")
 49 			{
 50 				self[i].el = sel;
 51 			}
 52 		}
 53 
 54 		self.el = sel;
 55 
 56 		return self;
 57 	};
 58 
 59 	/**
 60 	 * Simple DOM selector function
 61 	 *
 62 	 * @memberOf $_
 63 	 * @param string selector
 64 	 * @param object context
 65 	 * @return object
 66 	 * @type object
 67 	 */
 68 	$ = function (a, context)
 69 	{
 70 		var x, c;
 71 
 72 		if (typeof a != "string" || a === undefined){ return a;}
 73 
 74 		//Check for a context of a specific element, otherwise, just run on the document
 75 		c  = (context != null && context.nodeType === 1)
 76 			? context
 77 			: document;
 78 
 79 		//Pick the quickest method for each kind of selector
 80 		if (a.match(/^#([\w\-]+$)/))
 81 		{
 82 			return document.getElementById(a.split('#')[1]);
 83 		}
 84 		else
 85 		{
 86 			x = c.querySelectorAll(a);
 87 		}
 88 
 89 		//Return the single object if applicable
 90 		return (x.length === 1) ? x[0] : x;
 91 	};
 92 
 93 	/**
 94 	 * Adds the property `obj` to the $_ object, calling it `name`
 95 	 *
 96 	 * @param string name
 97 	 * @param object obj
 98 	 */
 99 	$_.ext = function(name, obj)
100 	{
101 		obj.el = sel;
102 		$_[name] = obj;
103 	};
104 
105 	/**
106 	 * Iterates over a $_ object, applying a callback to each item
107 	 *
108 	 * @name $_.each
109 	 * @function
110 	 * @param function callback
111 	 */
112 	$_.ext('each', function (callback)
113 	{
114 		if(sel.length !== undefined && sel !== window)
115 		{
116 			[].forEach.call(sel, callback);
117 		}
118 		else
119 		{
120 			callback.call(sel, sel);
121 		}
122 	});
123 
124 	/**
125 	 * Retrieves the type of the passed variable
126 	 *
127 	 * @param mixed obj
128 	 * @return string
129 	 * @type string
130 	 */
131 	$_.type = function(obj)
132 	{
133 		if((function() {return obj && (obj !== this)}).call(obj))
134 		{
135 			//fallback on 'typeof' for truthy primitive values
136 			return (typeof obj).toLowerCase();
137 		}
138 
139 		//Strip x from [object x] and return
140 		return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
141 	};
142 
143 	//Set global variables
144 	$_ = window.$_ = window.$_ || $_;
145 	$_.$ = $;
146 }());
147