//Copyright 2010 Steve Barraclough & Trinex Internet Solutions Inc.
//Author Steve Barraclough
//Trinex tJS
var browsername = navigator.appName;
function $(i) {
    return document.getElementById(i)
}
var timerId; 
var timers = [];
var tJS = {
	
	year: function() {
		return (new Date()).getFullYear();
	},
	now: function() {
		return (new Date()).getTime();
	},
	browser: {
		//check for IE
		ie: (function() { if (browsername == "Microsoft Internet Explorer") { return true; } else { return false; }})()
	},
	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;
			}
		}
	
	}, //util
	
	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+")"); } },
		overflowx: { attribute: 'overflow-x', unit: 'px', calc: function(i) {return i;} },
		overflowy: { attribute: 'overflow-y', unit: 'px', calc: function(i) {return i;} }
	}, //css
	
	fx: {

		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;
			//return p && p > -10000 ? p : 0;
		},

		stop: function() {
			clearTimeout(timerId);
			timerId = null;
			tJS.fx.dequeue();
		},
		
		dequeue: function() {
			while (timers.length) {
				clearTimeout(timers.shift());
			}
		},
		
		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, fx;
				
				params = parameters;
				prop = proptype;
				startTime = tJS.now();
				steptime = 10;
				fx = tJS.fx;
				
				//Parameters
				if (prop == undefined) { prop = 'width'; }
				//IE Opacity fix
				if (prop == "opacity" && tJS.browser.ie) {
					prop = 'filter';
				}
				unit = tJS.css[prop].unit;
				current = 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 {
					//defaults
					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;
				}
				
				//alert(start);
				fx.prototype = (function() {
					t = tJS.now();
					n = t - startTime;
					this.state = n / timeout;
					if (t >= timeout + startTime) {
						//done
						clearTimeout(timers.shift());
						el.style[prop] = tJS.css[prop].calc(end)+unit;
						//run callback
						if (next != null) {next();}
					} else {
						tmp = tJS.easing[easing](this.state, n, 0, 1, timeout);
						now = start + ((end - start) * tmp);
						el.style[prop] = tJS.css[prop].calc(now)+unit;
						timerId = setTimeout(arguments.callee, steptime);
						timers.push([timerId]);
					}
				})();

			} //obj
		
		} //animate	
	} //fx
}
tJS.prototype = tJS;
var _ = tJS.prototype;





