(function( window, undefined ) 
{
	var document = window.document;
	var browsername = navigator.appName;
	
	function $(i) {
    	return document.getElementById(i);
	}
	
	var tJS = (function() {
		
		var tJS = function( selector, context ) {
			//alert(selector);
			return new tJS.fn.init( selector, context );
		},
		// Map over tJS in case of overwrite
		_tJS = window.tJS,

		// Map over the $ in case of overwrite
		_$ = window.$,
	
		// A central reference to the root tJS(document)
		roottJS;
	
		tJS.fn = tJS.prototype = {
			init: function( selector ) {
				 //alert(selector);
				 return document.getElementById(selector)
			}
		};
		
		tJS.fn.init.prototype = tJS.fn;
		
		tJS.year = function() {
			return (new Date()).getFullYear();
		}
		
		tJS.now = function() {
			return (new Date()).getTime();
		}
		
		tJS.browser = {
			//check for IE
			ie: (function() { if (browsername == "Microsoft Internet Explorer") { return true; } else { return false; }})(),
			//Return IE Version Number
			ieVersion: function() {
				var ver = -1;
				if (browsername == 'Microsoft Internet Explorer')
				{
					var ua = navigator.userAgent;
					var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
					if (re.exec(ua) != null) {
						ver = parseFloat(RegExp.$1);
					}
				}
				return ver;
			}	
		}

		tJS.util = {
			LTrim: function(s) {
				return s.replace( /^\s*/, "" );
			},
			RTrim: function(s) {
				return s.replace( /\s*$/, "" );
			},
			Trim: function(s) {
				return rtrim(ltrim(s));
			},
			
			//Booleen JSON String Validation
			JSONIsValid: function(str) {
				tmp = /^\s*$/.test(str) ? l : /^[\],:{}\s\u2028\u2029]*$/.test(str.replace(/\\["\\\/bfnrtu]/g, "@").replace(/"[^"\\\n\r\u2028\u2029\x00-\x08\x10-\x1f\x80-\x9f]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]").replace(/(?:^|:|,)(?:[\s\u2028\u2029]*\[)+/g, ""));
				if (tmp) {
					try {
						return eval("("+str+")");
					} catch (c) {
						return false;
					}	
				} else {
					return false;
				}
			},
			
			//Preload Graphics, list full relative paths in arguments
			preloadImages: function() {
			    for(i=0; i<arguments.length; i++) {
			      var arg = arguments[i];
			      self[arg] = new Image();
			      self[arg].src = arg;
			    }
			},
			
			formatEmail: function(name, domain, text) {
				document.write("<a href=\"mailto:"+name+"@"+domain+"\">"+text+"</a>");
			},
			
			setType: function(s) {
				if (typeof s == "string") { 
					//if string assume id
					return $(s); 
				} else if (typeof s == "object") {
					//already element
					return s;
				} else {
					return null;
				}
			}
		}
	
		tJS.css = {
			width: { attribute: 'offsetWidth', unit: 'px', calc: function(i) {return i;} }, 
			height: { attribute: 'offsetHeight', unit: 'px', calc: function(i) {return i;} }, 
			left: { attribute: 'offsetLeft', unit: 'px', calc: function(i) {return i;} },
			top: { attribute: 'offsetTop', unit: 'px', calc: function(i) {return i;} },
			opacity: { attribute: 'opacity', unit: '', calc: function(i) {return (i)/100;} },
			filter: { attribute: 'filter', unit: '', calc: function(i) { return ("alpha(opacity="+i+")"); } }
		}
		tJS.fx = {};
		tJS.fx.prototype = {
			
			timerId: null,
			
			curr: function (elem, prop) {
	            
				var p = prop;
				var attr = tJS.css[p].attribute;
	            if (elem[attr] != "" && elem[attr] != null) {
	                p = elem[attr];
	            } else {
	                p = elem.style[attr];
	            }
	            return p;
				
	        },
			
	        stop: function () {
				clearTimeout(tJS.fx.timerId);
				tJS.fx.timerId = null;
	        },
			
	        animate: function (elem, proptype, timeout, parameters, callback) {
				var el = tJS.util.setType(elem);
				if (el) {
	                var startTime, steptime, percent, easing, next;
	                var start, end, unit, prop, current, timerint;
					var tfanim = this;
					
					this.timers = [];
					this.dequeue = function() {
						while (tfanim.timers.length > 0) {
							clearTimeout(tfanim.timers.shift()[0]);
							tfanim.timers.shift();
			   			}
					}

					params = parameters;
	                prop = proptype;
	                startTime = tJS.now();
	                steptime = 10;
					timerint = 0;
	                if (prop == undefined) {
	                    prop = 'width';
	                }
					
					//Use Filter for opacity on Any IE 8 or less
	                if (prop == "opacity" && (tJS.browser.ieVersion() < 9) && (tJS.browser.ieVersion() > -1)) {
	                   prop = 'filter';
	                }
	                unit = tJS.css[prop].unit;
	                current = tJS.fx.curr(el, prop);
					
	                if (callback != undefined && typeof callback == "function") {
	                    next = callback;
	                } else {
	                    next = null;
	                }
	                if (params != undefined) {
	                    percent = params.percent;
	                    if (percent == undefined) {
	                        percent = null;
	                    }
	                    start = params.start;
						
	                    if (start == undefined) {
	                        start = null;
	                    }
	                    end = params.end;
	                    if (end == undefined) {
	                        end = null;
	                    }
	                    easing = params.easing;
	                    if (easing == undefined) {
	                        easing = 'easeOutQuad';
	                    }
	                } else {
	                    percent = null;
	                    start = null;
	                    end = null;
	                    easing = 'easeOutQuad';
	                }

	                if (percent != null) {
	                    end = parseFloat(current + ((current * percent) / 100));
	                } else {
	                    end = end;
	                }
	                if (start != null) {
	                    start = parseFloat(start);
	                } else {
	                    start = current;
	                }
					
					if (current != end) 
					{
					   (function ARun() {
		                    t = tJS.now();
							n = t - startTime;
							this.prop = prop;
							this.state = n / timeout;
							if (t >= timeout + startTime) {
								el.style[this.prop] = tJS.css[this.prop].calc(end) + unit;
								//Callback
								if (next != null) { next(); }
								//Dequeue
								tfanim.dequeue();
								
		                    } else {
								this.tmp = tJS.easing[easing](this.state, n, 0, 1, timeout);
		                        this.now = start + ((end - start) * this.tmp);
		                        el.style[this.prop] = tJS.css[this.prop].calc(this.now) + unit;
	
								tJS.fx.timerId = setTimeout(ARun, steptime);
								tfanim.timers.push([tJS.fx.timerId]);
		                    }
		                })();
					}
					
					
	            }
	        }
		};
		tJS.fx = tJS.fx.prototype;
		return (window.tJS = window.$ = tJS);
	})();
	
})(window);

