/*
Version 1.3
 */	
	
	//|	Skyspine Vorpal
	//|	Created by Brian Franklin
	//|	http://www.caoine.com
	var vorpal = new function () {
	
		//| Value storage
		this.browser			= null;
		this.version			= null;
		this.os					= null;
		this.ready				= false;
		this.onready			= new Array();
		this.swap				= new Array();
		this.drag				= null;
		this.drops				= new Array();
		this.drops				= new Array();
		this.debugging			= false;
		this.mx					= 0;
		this.my					= 0;
		this.store				= ['vorpal'];
		this.langs				= new Array();

		//| Root the vorpal object
		this.initialize = function () {
			vorpal.environment();
			vorpal.events.attach(document, "domready", function () { if (vorpal.defined($("#vorpal-db2"))) { vorpal.debugging = true; } });
			vorpal.events.attach(document, "mousemove", vorpal.ui.mouse);
			if (vorpal.browser == "ie") {
				document.write("<script type='text/javascript' id='vorpal-dm' defer='defer' src='javascript:void(0);'></script>");
				var t = $("#vorpal-dm");
				t.onreadystatechange = function () {
					if (this.readyState == "complete") {
						vorpal.domready();
					}
				}
			} else if (vorpal.browser == "safari") {
				var t = setInterval(function () {
					if(/loaded|complete/.test(document.readyState)){
						clearInterval(t);
						vorpal.domready();
					}
				}, 10)
			} else {
				window.onload = vorpal.domready;
			}
		};
		
		this.mouseX = function(evt)
		{
			if (!evt) evt = window.event;
			if (evt.pageX)
			return evt.pageX;
			else if (evt.clientX)
			return evt.clientX +(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
			else return 0;
		};
		
		this.mouseY = function (evt) {
			if (!evt) evt = window.event;
			if (evt.pageY)
			return evt.pageY;
			else if (evt.clientY)
			return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
			else return 0;
		};
		
		this.getNum = function (pNum) {
			if(!pNum) return 0;
			if(pNum.indexOf('px') > -1) {
				return parseInt(pNum.substring(0, pNum.indexOf('px')));
			} else {
				return parseInt(pNum);
			}
		};
		
		//| Handle language output
		this.lang = function (s) {
			return this.langs[s];
		};
		
		//| Make a random string
		this.id = function (l) {
			l = l || 10;
			var v = 'abcdefghijklmnopqrstuvwxyz';
			var u = 'vorpal';
			while (vorpal.store.indexOf(u) > -1) {
				u = '';
				for (var i = 0; i < l; i++) {
					var p = Math.floor(Math.random() * v.length);
					u += v.substring(p, p + 1);
				}
			}
			vorpal.store.push(u);
			return u;
		};
	
		//| Check to see if a value is defined
		this.defined = function (s) {
        	if (((typeof s) == 'undefined') || (s == null)) {
				return false; 
        	} else { 
				return true;
        	}
		};
	
		//| Handle anything set to fire when the DOM is ready
		this.domready = function () {
			if (vorpal.ready == false) {
				vorpal.ready = true;
				for (var i = 0; i < vorpal.onready.length; i++) {
					setTimeout(vorpal.onready[i], 0);
				}
				return true;
			}
			return false;
		};
	
		//| Element selector
		this.select = function (s, x) {
			x = x || document;
			if (x == '') { x = document; }

			var v = s.split(' ');
			var o = v.shift();
			var n = false;
			var t = false;
			var i = false;
			var c = false;
			var r = [];
			
			if (o.indexOf(':') > -1) {
				n = o.substring((o.indexOf(':') + 1));
				o = o.substring(0, o.indexOf(':'));
			}
			
			if (o.indexOf('.') > -1) {
				c = o.substring((o.indexOf('.') + 1));
				o = o.substring(0, o.indexOf('.'));
			}
			
			if (o.indexOf('#') > -1) {
				i = o.substring((o.indexOf('#') + 1));
				o = o.substring(0, o.indexOf('#'));
			}
			
			if ((o != false) && (o.length > 0)) {
				t = o;
			}
			
			if ((typeof x != 'object') || (x == document)) {
				x = [x];
			}

			if (i) {
				var z = document.getElementById(i);
				if (typeof z != 'undefined') {
					r = [z];
				} else {
					r = null;
				}
				x = r;
			}
			
			if (t && !c) {
				r = [];
				for (var y in x) {
					if ((typeof x[y] == 'object') && (typeof x[y].getElementsByTagName == 'function')) {
						var z = x[y].getElementsByTagName(t);
						for (var p in z) {
							if (typeof z[p] == 'object') {
								r.push(z[p]);
							}
						}
					}
				}
				x = r;
			}
			
			if (c) {
				r = [];
				if (typeof x == 'object') {
					if (t) {
						for (var j in x) {
							var z = vorpal.get_by_class(x[j], c, t);
							if (typeof z == 'object' || typeof z == 'function') {
								for (var y in z) {
									if (typeof z[y] != 'function') {
										r.push(z[y]);			
									}
								
								}		
							}
						}
					} else {
						for (var j in x) {
							if (typeof x[j] == 'object') {
								var z = vorpal.get_by_class(x[j], c, '*');
								if (typeof z == 'object' || typeof z == 'function') {
									for (var y in z) {
										if (typeof z[y] != 'function') {
											r.push(z[y]);			
										}
									}		
								}
							}
						}
					}
				} else { 
					r = null;
				}
				x = r;
			}
			
			if (n) {
				if ((typeof x == 'object') && (x.length > 0)) {
					switch (n) {
						case 'first':
							x = x[0];
							break;
						case 'last':
							x = x[x.length - 1];
							break;
					}
				}	
			}
			return (v.length > 0) ? vorpal.select(v.join(' '), x) : (x.length == 1) ? x[0]: x;
		
		};
		
		this.has_class = function (s, v) {
			s = vorpal.check(s);
			var c = new RegExp('\\b'+v+'\\b');
			return (typeof s.className != 'undefined') ? (c.test(s.className)) ? true : false : false;
		};
		
		this.add_class = function (s, v) {
			s = vorpal.check(s);
			s.className = (vorpal.has_class(s, v)) ? s.className : (s.className+' '+v);
		};
		
		this.remove_class = function (s, v) {
			s = vorpal.check(s);
			var c = new RegExp('\\b'+v+'\\b');
			s.className = (typeof s.className != 'undefined') ? s.className.replace(c, '') : v;
		};
		
		//|	Because browsers suck
		this.get_by_class = function (s, v, e) {
			e = e || '*';
			var r = [];
			if (typeof s.getElementsByClassName != 'undefined') {
				s = s.getElementsByClassName(v);
				if (e != '*') {
					for (var i = 0; i < s.length; i++) {
						if (s[i].tagName.toLowerCase == e.toLowerCase) r.push(s[i]);
					}
				} else {
					r = s;
				}
			} else {
				if (typeof s.getElementsByTagName != 'undefined') {
					s = s.getElementsByTagName(e);
					for (var i = 0; i < s.length; i++) {
						if (vorpal.has_class(s[i], v)) {
							r.push(s[i]);
						}
					}
				}
			}
			return r;
		};
		
		//| Make sure we have a DOM object
		this.check = function (s) {
			return ((typeof s) != "string") ? s : vorpal.select(s);
		};
		
		//|	Determine whether an element exists inside another
		this.kid = function (p, c) {
			if (p == c) {
				return false; 
			}
			while (c && (c != p)) {
				c = c.parentNode;
			}
			return c == p;
		};
		
		//| Make a datetime stamp for logging
		this.datetime = function () {
			var d = new Date();
			var h = d.getHours();
			var m = d.getMinutes();
			var s = d.getSeconds();
			h = (h < 10) ? "0"+h : h;
			m = (m < 10) ? "0"+m : m;
			s = (s < 10) ? "0"+s : s;
			return h+":"+m+":"+s;
			//return d.getTime();
		};
		
		//| Logger
		this.log = function (s, l) {
			if (vorpal.ready && vorpal.debugging) {
				l = l || "ffffff";
				var e = document.createElement("div");
				e.style.color = "#"+l;
				$("#vorpal-db").appendChild(e);
				e.innerHTML = vorpal.datetime()+": "+s;
				var d = $("#vorpal-db").getElementsByTagName("div");
				while (d.length > 20) {
					$("#vorpal-db").removeChild(d[0]);
				}
			}
		};
	
		//| UI-specific functionality
		this.ui = new function () {
	
			//| Get the computed styles of an element from CSS
			this.style = function (s, v, c){
				c = c || false;
				s = vorpal.check(s);
				var r = '';
				if (document.defaultView && document.defaultView.getComputedStyle){
					r = document.defaultView.getComputedStyle(s, '') || '';
					r = (r) ? r.getPropertyValue(v) : null;
				}
				v = v.replace(/\-(\w)/g, function (x, y){
					return y.toUpperCase();
				});				
				if (s.currentStyle) {
					r = s.currentStyle[v] || null;
				}

				r = r || s.style[v];
				if (c && (typeof r == 'string') && (r.substr((r.length - 2)).toLowerCase() == 'px')) { r = vorpal.integer(r); }
				if (c && (typeof r == 'string') && (r.substr(0, 3).toLowerCase() == 'rgb')) { r = vorpal.hex(r); }
				return r;
			};
	
			//| Cross-browser opacity setter
			this.opacity = function (s, v) {
				s = vorpal.check(s);
				s.style.opacity = v;
				s.style.filter = 'alpha(opacity='+(v * 100)+')';
			};
		
			//| Window dimensions
			this.window = function () {
				if (typeof window.innerWidth != 'undefined') {
					return {"x":window.innerWidth, "y":window.innerHeight};
				} else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {	
					return {"x":document.documentElement.clientWidth, "y":document.documentElement.clientHeight};
				} else {
					return {"x":document.getElementsByTagName('body')[0].clientWidth, "y":document.getElementsByTagName('body')[0].clientHeight};
				}
			};
			
			//| Mouse coordinates
			this.mouse = function (e) {
			/*
				e = e || window.event;
				if (e.pageX || e.pageY) 	{
					vorpal.mx = e.pageX;
					vorpal.my = e.pageY;
				} else if (e.clientX || e.clientY) 	{
					vorpal.mx = (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
					vorpal.my = (e.clientY + document.body.scrollTop + document.documentElement.scrollTop);
				}
				
			*/
				
				var m = new Array();
				m["x"] = 0;//vorpal.mx;
				m["y"] = 0;//vorpal.my;
				return m;
			};
			
			//| Page offsets
			this.offsets = function () {
				if(typeof(window.pageYOffset) == 'number') {
					return {"x":window.pageXOffset, "y":window.pageYOffset};
				} else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
					return {"x":document.body.scrollLeft, "y":document.body.scrollTop};
				} else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
					return {"x":document.documentElement.scrollLeft, "y":document.documentElement.scrollTop};
				}
			};
			
			//| Element coordinates
			this.coords = function (s) {
				s = vorpal.check(s);
				c = s;
				var x = 0;
				var y = 0;
				while (c.offsetParent) {
					x += c.offsetLeft;
					y += c.offsetTop;
					c = c.offsetParent;
				}
				return {"width":s.offsetWidth, "height":s.offsetHeight, "top":y, "left":x};
			};
			
			//| Animate an element
			this.animate = function (e, t, p, d, u) {
				e = vorpal.check(e);
				sd = true;
				u = u || function () {};
				if (d == 'opacity') {
					var m = (100 * e.style[d]);
					p = (p * 100);
				} else {
					var m = vorpal.integer(e.style[d]);
				}
				var a = p - m;
				var b = new Date().getTime();
				var x = b + t;
				if (sd || sk) { setTimeout(function () { sk = sd = false; u(); }, (t+5)); }
				vorpal.ui.animator(e, d, a, m, t, x);
			};

			//| Do the heavy lifting for the animation
			this.animator = function (e, d, a, s, t, x) {
				var c = new Date().getTime();
				var r = Math.max(0, x - c);
				
				if (d == 'opacity') {
					var n = (a - (Math.pow(r, 3) / Math.pow(t, 3)) * a);
					vorpal.ui.opacity(e, ((s + n) / 100));
				} else {
					var n = parseInt(a - (Math.pow(r, 3) / Math.pow(t, 3)) * a);
					e.style[d] = (s + n) + "px";
				}
				if (r > 0) {
					setTimeout(function () { vorpal.ui.animator(e, d, a, s, t, x); }, 10);
				} else {
//					if ((vorpal.integer(e.offsetHeight) == 0) || (vorpal.integer(e.offsetWidth) == 0)) { e.style.display = 'none'; }
				}
			};
			
			//| Sort
			this.drag = function (o) {
				o.e = vorpal.check(o.e);
				o.h = vorpal.check(o.h);
				o.s = o.s || function(){ return; };
				o.d = o.d || function(){ return; };
				o.u = o.u || function(){ return; };
				o.c = o.c || "n";
				o.i = o.i || null;
				
				//| Mousedown function
				var d = function (e) { 
					e = e || window.event;
					e.returnValue = false;
					try { e.preventDefault(); } catch (e) {}
					var c = vorpal.ui.mouse(e);
					var t = vorpal.ui.coords(o.e);
					o.offset = new Array((c["x"] - t["left"]), (c["y"] - t["top"]));
					o.snaps = new Array(c["x"], c["y"]);
					vorpal.drag = o;
				}
				
				//| Mouseup function
				var u = function (e) { 
					e = e || window.event;
					e.returnValue = false;
					try { e.preventDefault(); } catch (e) {}
					if (vorpal.drag) {
						vorpal.drag.u(e);
					}
					if ((vorpal.drag != null) && $(vorpal.drag.p)) {
						vorpal.drag.p.parentNode.removeChild(vorpal.drag.p);
					}
					if (vorpal.drag != null) {
						vorpal.drag.p = null;
						vorpal.drag = null;
					}
				}
				
				//| Mousemove function
				var m = function (e) {
					e = e || window.event;
					e.returnValue = false;
					try { e.preventDefault(); } catch (e) {}
					if (vorpal.drag) {
					
						var c = vorpal.ui.mouse(e);
						var x = c["x"] - vorpal.drag.offset[0];
						var y = c["y"] - vorpal.drag.offset[1];
						var x2 = c["x"] - vorpal.drag.snaps[0];
						var y2 = c["y"] - vorpal.drag.snaps[1];
					
						if ((!vorpal.defined(vorpal.drag.p)) && ((Math.abs(x2) > 5) || (Math.abs(y2) > 5))) {
							vorpal.drag.s(e);
							var t = vorpal.ui.coords(vorpal.drag.e);
							vorpal.drag.p = vorpal.ui.clone(vorpal.drag.e);
							vorpal.drag.p.className = ('dragging '+vorpal.drag.p.className);
							vorpal.drag.p.style.position = "absolute";
							vorpal.drag.p.style.width = t["width"]+"px";
							vorpal.drag.p.style.top = t["top"]+"px";
							vorpal.drag.p.style.left = t["left"]+"px";
							vorpal.drag.p.style.zIndex = 9000;
							vorpal.drag.p.className = 'dragging';
							
						} else if (vorpal.defined(vorpal.drag.p)) {
							
							if (vorpal.drag.c.substr(0, 1) != "h") {
								vorpal.drag.p.style.top = y+"px";
							}
							if (vorpal.drag.c.substr(0, 1) != "v") {
								vorpal.drag.p.style.left = x+"px";
							}
							vorpal.drag.d(e);
							
						}
					}
				}
				
				//| Bind things
				vorpal.events.attach(o.h, "mousedown", d);
				vorpal.events.attach(window, "mouseup", u);
				vorpal.events.attach(window, "mousemove", m);
			};
			
			//| Register a drop target
			this.drop = function (s) {
				s = vorpal.check(s);
				vorpal.drops.push(s);
			};
			
			//| Clone an element
			this.clone = function (s, r, w) {
				r = r || null;
				w = w || null;
				s = vorpal.check(s);
				var c = s.cloneNode(true);
				var f = function (e) {
					if (e.id) {
						if (r && w) {
							e.id = e.id.replace(r, w);
						} else {
							e.id = ("ssui-" + e.id);
						}
					}
					if (typeof e.childNodes != 'undefined') {
						for (var i = 0; i < e.childNodes.length; i++) {
							f(e.childNodes[i]);
						}
					}
				};
				f(c);
				document.body.appendChild(c);
				return c;
			};
		};
		
		//| Event handlers
		this.events = new function () {
		
			//| Add an event
			this.attach = function (e, t, f, b) {
				b = b || false;
				if (t == "domready") {					
					if ((vorpal.browser == "firefox") || ((vorpal.browser == "opera") && (vorpal.version >= 9))) {
						document.addEventListener("DOMContentLoaded", vorpal.domready, b);
					}
					vorpal.onready[vorpal.onready.length] = f;
				} else {
					if (vorpal.browser == "explorer") {
						e = (e != window) ? e : document.body;
						e.attachEvent("on"+t, f);

					} else {
						if (t == 'mouseenter') {
							t = 'mouseover';
							f = this.wrap(f);
						} else if (t == 'mouseleave') {
							t = 'mouseout';
							f = this.wrap(f);
						}
						e.addEventListener(t, f, b);
					}
				}
			};
			
			//| Detach an event
			this.detach = function (e, t, f) {
				if (vorpal.browser == "explorer") {
					e = (e != window) ? e : document.body;
					e.detachEvent("on"+t, f);
				} else {
					e.removeEventListener(t, f, false);
				}
			};
			
			//| Wrapper for enter/leave
			this.wrap = function (f) {
				return function (e) {
					var r = e.relatedTarget;
					if (this == r || vorpal.kid(this, r)) {
						return;
					}
					f.call(this, e);
				}
			};
			
		}();
		
		//| Browser/OS detection
		this.environment = function () {
			var b = new Array(
				{'s': navigator.userAgent,	'f': 'Chrome',		'n': 'Chrome'},
				{'s': navigator.userAgent,	'f': 'OmniWeb',		'n': 'OmniWeb',		'v': 'OmniWeb/'},
				{'s': navigator.vendor,		'f': 'Apple',		'n': 'Safari',		'v': 'Version'},
				{'p': window.opera,								'n': 'Opera'},
				{'s': navigator.vendor,		'f': 'iCab',		'n': 'iCab'},
				{'s': navigator.vendor,		'f': 'KDE',			'n': 'Konqueror'},
				{'s': navigator.userAgent,	'f': 'Firefox',		'n': 'Firefox'},
				{'s': navigator.vendor,		'f': 'Camino',		'n': 'Camino'},
				{'s': navigator.userAgent,	'f': 'Netscape',	'n': 'Netscape'},
				{'s': navigator.userAgent,	'f': 'MSIE',		'n': 'Explorer',	'v': 'MSIE'},
				{'s': navigator.userAgent,	'f': 'Gecko',		'n': 'Mozilla',		'v': 'rv'},
				{'s': navigator.userAgent,	'f': 'Mozilla',		'n': 'Netscape',	'v': 'Mozilla'});
			var o = new Array(
				{'s': navigator.platform,	'f': 'Win',			'n': 'windows'},
				{'s': navigator.platform,	'f': 'Mac',			'n': 'mac'},
				{'s': navigator.platform,	'f': 'Linux',		'n': 'linux'});
			var g = function (d) { 
				for (var i = 0; i < d.length; i++)	{
					var s = d[i].s;
					var p = d[i].p;
					vorpal.swap['v'] = d[i].v || d[i].n;
					if (s) {
						if (s.indexOf(d[i].f) != -1) { return d[i].n; }
					} else if (p) { return d[i].n; }
				}
			};
			var h = function (s) {
				var i = s.indexOf(vorpal.swap['v']);
				if (i == -1) { return; }
				return parseFloat(s.substring(i + vorpal.swap['v'].length + 1));
			};
			vorpal.browser = g(b) || 'unknown';
			vorpal.version = h(navigator.userAgent) || h(navigator.appVersion) || 'unknown';
			vorpal.browser = vorpal.browser.toLowerCase();
			vorpal.os = g(o) || 'unknown';
		};
		
		//| Get an integer from a style string
		this.integer = function (n) {
			if (n == null) { return 0; }
			if (n.indexOf && (n.indexOf('px') > -1)) {
				n = n.substring(0, n.indexOf('px'));
			}
			if (!n && (n !== 0)) { return 0; }
			if ((n < 0) || (n.substring && (n.substring(0, 1) == "-"))) { return (0 - parseInt(n.substring(1))); }
			return parseInt(n);
		};
		
		// Get a hex value from RGB
		this.hex = function (s) {
			s = s.replace(/[^0-9,]/ig, '');
			s = s.split(',');
			var r = vorpal.integer(s[0]).toString(16);
	        var g = vorpal.integer(s[1]).toString(16);
	        var b = vorpal.integer(s[2]).toString(16);
			r = (r.length == 1) ? ('0' + r) : r;
	        g = (g.length == 1) ? ('0' + g) : g;
			b = (b.length == 1) ? ('0' + b) : b;
	        return '#' + r + g + b;
		};
		
		//|	Inject a script into the head and clean it up if desired
		this.inject = function (s, v) {
			v = v || true;
			var d = new Date();
			var i = 'vorpal-sc-'+d.getTime()+'-'+Math.floor(Math.random()*1313);
			var n = document.createElement("script");
			n.setAttribute("src", s);
			n.setAttribute("type", "text/javascript");
			n.id = i;
			document.getElementsByTagName("head")[0].appendChild(n);
			if (v) {
				setTimeout(function(){document.getElementsByTagName("head")[0].removeChild($('#'+i));}, 10000);
			}
		};
		
		//|	The request object for http communications
		this.io = new function () {
		
			this.queue = [];
			this.connections = 0;
			this.max = 3;
			this.rate = 500;
			
			this.interval = setInterval('vorpal.io.cycle();', this.rate);
			
			this.add = function (s, v) {
				if (this.connections < this.max) { 
					this.send(s, v); 
				} else {
					this.queue.push({'s':s, 'v':v});
				}
			};
			
			//|	Handle the queue intelligently
			this.cycle = function () {
				while ((this.queue.length > 0) && (this.connections < this.max)) {
					this.send(this.queue[0].s, this.queue[0].v);
					this.queue.splice(0, 1);
				}
			};
			
			//|	Send the data found in object v to url s
			this.send = function (s, v) {
			
				var connection = window.XMLHttpRequest ? new window.XMLHttpRequest : new window.ActiveXObject("Microsoft.XMLHTTP");	
				this.connections++;
				
				v.vcb = v.vcb || function () {};
				var p = '';
			
				for (var i in v) {
					if (i != 'vcb') {
						p += i+'='+v[i]+'&';
					}
				}
				
				p = p.substring(0, (p.length - 1));
				
				connection.open("POST", s, true);
				connection.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				connection.onreadystatechange = function() {
					if (this.readyState == 4) {
						if (this.status != 404) {
							v.vcb(this.responseText, v);
							vorpal.io.connections--;
						} else {
							v.vcb(false);
							vorpal.io.connections--;
						}
					}
				}
				
				connection.send(p);
			};
			
		}();
		
	}();
	
	if(!Array.indexOf){
	    Array.prototype.indexOf = function(s){
	        for(i = 0; i < this.length; i++){
	            if(this[i] == s){
	                return i;
	            }
	        }
	        return -1;
	    }
	}
	
	$ = vorpal.check;
	vorpal.initialize();

