// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

function hasIndexOf(theString, theIndex) {
	if(theString.indexOf(theIndex) == -1) {
		return false;
	}
	return true;
} 
function addClass(item, classToAdd) {
	if(!hasClass(item, classToAdd)) {
		item.className += (" " + classToAdd);
	}
}

function removeClass(item, classToRemove) {
	if(hasIndexOf(item.className, classToRemove)) {
		var theClass = item.className;
		var classes = theClass.split(" ");
		for(var i = 0; i < classes.length; i++) {
			if(classes[i] == classToRemove || classes[i] == " ") {
				classes.splice(i, 1);
			}
		}
		item.className = classes.join(" ");
	}
}
function hasClass(item, classToSearch) {
	if(hasIndexOf(item.className, classToSearch)) {
		var theClass = item.className;
		var classes = theClass.split(" ");
		for(var i = 0; i < classes.length; i++) {
			if(classes[i] == classToSearch) {
				return true;
			}
		}
	}
	return false;
}
function getFileName(theString) {
	if(hasIndexOf(theString, "/")) {
		var firstpos = theString.lastIndexOf("/") + 1;
		var lastpos = theString.length;
		return theString.substring(firstpos, lastpos);	
	}
}
function getQSValue(getThisVar) {
	//you can pass a string as a 2nd Param to search that instead of the pages URL
	var toReturn = "";
	//kick out if theres no Query String
	var theValueToCheck = "";
	
	if(!arguments[1]) {
		theValueToCheck = document.URL;
	}else {
		theValueToCheck = arguments[1];
	}
	if(!hasIndexOf(theValueToCheck, "?")) {return false;}
	var QS = theValueToCheck.substring(theValueToCheck.indexOf("?") + 1);
	var variables = QS.split("&");
	for(var i = 0; i < variables.length; i++) {
		if(variables[i].split("=")[0] == getThisVar) {
			//if we find it the return its value
			toReturn = variables[i].split("=")[1];
			return toReturn;
		}
	}
	//if all else fails return false
	return false;
}

/*
 *  Give it a DOM object and let it strip out
 *  those pesky #text nodes from mozilla for you
 */
function stripEmptyTextNodes(element) {
	for(var i = 0; i < element.childNodes.length; i++) {
		alert(element.childNodes[i].nodeName);
		if(element.childNodes[i].nodeName == "#text") {
			element.removeChild(element.childNodes[i]);
			i--;
		}
	}
	return element;
}
/*
 *  Do a simple getElementById and strip out the #text
 *  nodes while your at it, pass it an Object or a String
 *  of an ID
 */
function grabStrippedElement(ele) {
	if(typeof ele == "string") {
		return stripEmptyTextNodes(document.getElementById(ele));
	}else {
		return stripEmptyTextNodes(ele);
	}
}
function grabElement(ele) {
	if(typeof ele == "string") {
		return document.getElementById(ele);
	}else {
		return ele;
	}
}
function gE(ele) {
	return grabElement(ele);
}
function grabElesByTag(eleType) {
	if(arguments.length < 2) {
		return document.getElementsByTagName(eleType);
	}else {
		return arguments[1].getElementsByTagName(eleType);
	}
}
function gET(eleType) {
	if(arguments.length < 2) {
		return document.getElementsByTagName(eleType);
	}else {
		return arguments[1].getElementsByTagName(eleType);
	}
}
function grabEleByNameAndClass(name, className, element) {
	var collectionWithClass = new Array();
	var searchElement = grabElement(element);
	var collection = grabElesByTag(name, searchElement);
	for(var i=0;i<collection.length;i++) {
		if(hasClass(collection[i], className)) {
			collectionWithClass.push(collection[i]);
		}
	}
	return collectionWithClass;
}
function grabElesByNameAndClass(name, className, element) {
	return grabEleByNameAndClass(name, className, element);
}
function grabElesByTagAndClass(name, className, element) {
	return grabEleByNameAndClass(name, className, element);
}
function gETAC(name, className, element) {
	return grabEleByNameAndClass(name, className, element);
}
var __isNS__ = false;
if(document.getElementById && !document.all) {
	__isNS__ = true;
}

/*
 * object used to add onload functions for the body onload event
 * this way we can keep JS that needs to run onload unobtrusive
 *
 * usage: windowObject.addLoadFunction([pass function], [pass function]);
 * you can pass 1 or more functions, the number does not matter

	var WindowObject = function(win) {
		this._win = win;
		this.stack = [];
	};
	WindowObject.prototype = {
		"runFuncs": function() {
			for(var index = 0; index < this.stack.length; index++) {
				this.stack[index]();
			}
		},
		"addLoadFunction": function() {
			for(var index = 0; index < arguments.length; index++) {
				this.stack.push(arguments[index]);
			}
		}
	};
	var windowObject = new WindowObject(this);
	function runOnLoad() {
		windowObject.runFuncs();
	}
	window.onload = runOnLoad;
 */
var windowObject = {
	onload: [],
	loaded: function()
	{
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		for (i = 0;i < windowObject.onload.length;i++) windowObject.onload[i]();
	},
	addLoadFunction: function(fireThis)
	{
		for(var index = 0; index < arguments.length; index++) {
			this.onload.push(arguments[index]);
		}
		if (document.addEventListener) 
			document.addEventListener("DOMContentLoaded", windowObject.loaded, null);
		if (/KHTML|WebKit/i.test(navigator.userAgent))
		{ 
			var _timer = setInterval(function()
			{
				if (/loaded|complete/.test(document.readyState))
				{
					clearInterval(_timer);
					delete _timer;
					windowObject.loaded();
				}
			}, 10);
		}
		/*@cc_on @*/
		/*@if (@_win32)
		var proto = "src='javascript:void(0)'";
		if (location.protocol == "https:") proto = "src=//0";
		document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
		    if (this.readyState == "complete") {
		        windowObject.loaded();
		    }
		};
		/*@end @*/
	   window.onload = windowObject.loaded;
	}
};
function newXMLHttpRequest() {
  var xmlreq = false;
  if (window.XMLHttpRequest) {
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
      }
    }
  }
  return xmlreq;
}

function registerCallBack(req, callOnResponse) {
  var callBackFunction = getReadyStateHandler(req, callOnResponse);
  //req is the XMLHTTPRequest object that was made by the client
  req.onreadystatechange = callBackFunction;
}
function sendReq(req, method, action, isAsynchronous, requestHeaderInfo, requestHeaderType, sendData ) {
  req.open(method, action, isAsynchronous);
  req.setRequestHeader(requestHeaderInfo, requestHeaderType);
  req.send(sendData);
}

function sendPostReq(url, data, callBackFunction) {
	var isAsynchronous = true;
	if(arguments[3] != undefined && (arguments[3] == false || arguments[3] == 'false')) {
		isAsynchronous = false;
	}
	var myReq = new newXMLHttpRequest();
	myReq.RESPONSE_TYPE = getResponseType(arguments[4]);
	registerCallBack(myReq, callBackFunction); 
	sendReq(myReq, "POST", url, isAsynchronous, "Content-Type", "application/x-www-form-urlencoded", data);
}
var RESPONSE_TYPE;
function sendGetReq(url, callBackFunction) {
	var isAsynchronous = true;
	if(arguments[2] != undefined && (arguments[2] == false || arguments[2] == 'false')) {
		isAsynchronous = false;
	}
	var myReq = new newXMLHttpRequest();

	RESPONSE_TYPE = getResponseType(arguments[3]);
	
	registerCallBack(myReq, callBackFunction);
	sendReq(myReq, "GET", url, isAsynchronous, "Content-Type", "application/x-www-form-urlencoded", null);
}

function getResponseType(arg) {
	if(!arg) { return 'text'; }
	if(arg.toLowerCase() == 'xml') {
		return 'xml';
	}
	return 'text';

}

function getReadyStateHandler(req, responseXmlHandler) {
  return function () {
    if (req.readyState == 4) {
      if (req.status == 200) {
        if(RESPONSE_TYPE == 'text') {
	        responseXmlHandler(req.responseText);
	    }else if(RESPONSE_TYPE == 'xml') {
	    	responseXmlHandler(req.responseXML);
	    }else {
	    	responseXmlHandler("bad things happened");
	    }
      } else {
        //alert("I just busted\nHTTP error: "+req.status);
      }
    }
  }
}
function include_css( styles ) {
	//if( ! in_array( styles, $INCLUDED_CSS_STYLES ) ) {
	//	$INCLUDED_CSS_STYLES.push( styles );		
		var styleTag = document.createElement("style");
		styleTag.setAttribute( "type", "text/css" );
		if( styleTag.styleSheet ) { // IE only
			styleTag.styleSheet.cssText = styles;
		} else { // w3c standard
			var stylesNode = document.createTextNode( styles );
			styleTag.appendChild( stylesNode );
		}
		document.getElementsByTagName("head")[0].appendChild( styleTag );
		
	//}
}
var calculateLoadTime = function (returnedElement, callBackString) {
	this.imgTotalList = gET("IMG", returnedElement);
	this.callBackString = callBackString;
	//alert(returnedElement.nodeName);
	/*if(returnedElement.nodeName == "IMG") {
		var temp = new Array();
		temp.push(returnedElement);
		this.imgTotalList = temp;
		//alert(returnedElement.nodeName);
		//alert(this.imgTotalList.length);
	}*/
	//alert(this.imgTotalList.length)
	if(this.imgTotalList.length <= 0) {window[this.callBackString]();}
	this.imgTotalList.counter = 0;
	this.checkLoads();
	this.isDone = false;
	if(this.imgTotalList[0].complete != undefined) {
		this.startCheck();
	}
	this.loadTimer;
}
calculateLoadTime.prototype.checkLoads = function() {
	var noComplete = false;
	for(var j=0;j<this.imgTotalList.length;j++) {
		this.imgTotalList[j].originalList = this.imgTotalList;
		this.imgTotalList[j].totalImages = this.imgTotalList.length;
		var thisRef = this.imgTotalList[j];
		thisRef.callBackString = this.callBackString;
		if(this.imgTotalList[j].complete == undefined) {
			noComplete = true;
			addEvent(this.imgTotalList[j], "load", function() {
				thisRef.originalList.counter++;
				//museDebugger.log( thisRef.originalList.counter + " out of: " + thisRef.totalImages + " loaded" );
				if(thisRef.originalList.counter >= thisRef.totalImages) {
					//museDebugger.log( thisRef.originalList.counter + " should call back now, the total was: " + thisRef.totalImages );
					isDone = true;
					window[thisRef.callBackString]();
				}});
			addEvent(this.imgTotalList[j], "error", function() {
				//museDebugger.log( "onError called" );
				window[thisRef.callBackString]();
				isDone = true;
			});
			addEvent(this.imgTotalList[j], "abort", function() {
				//museDebugger.log( "onAbort called" );
				window[thisRef.callBackString]();
				isDone = true;
			});
		
		}else {
			if(this.imgTotalList[j].complete) {
				//museDebugger.log( "its complete already" );
				//museDebugger.log( thisRef.originalList.counter + " out of: " + thisRef.totalImages + " loaded" );
				thisRef.originalList.counter++;
				if(thisRef.originalList.counter >= thisRef.totalImages) {
					//museDebugger.log( thisRef.originalList.counter + " should call back now, the total was: " + thisRef.totalImages );
					window[this.callBackString]();
					clearTimeout(this.loadTimer);
					this.isDone = true;
					break;
				}
			}
		}
	}
	if(!noComplete) {
		this.startCheck();
	}
}
calculateLoadTime.prototype.startCheck = function() {
	if(!this.isDone) {
		thisRef = this;
		this.loadTimer = setTimeout("thisRef.checkLoads();", 30);
	}else {
		clearTimeout(this.loadTimer);	
	}
}
function checkForLoad() {
/**/
}
var addEvent;
if (document.addEventListener) {
	addEvent = function(element, type, handler) {
		element.addEventListener(type, handler, false);
	};
} else if (document.attachEvent) {
	addEvent = function(element, type, handler) {
		element.attachEvent("on" + type, handler);
	};
} else {
	addEvent = new Function; // not supported
}
function getXPos(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function getYPos(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}
function getHeight(obj) {
	return obj.offsetHeight;
}
function getWidth(obj) {
	var tagWidth = obj.getAttribute("width");
	if(tagWidth) {
		return tagWidth;
	}
	return obj.offsetWidth;
}
function setHeight(obj, newHeight) {
	if(__isNS__) {
		newHeight -= parseInt(obj.style.paddingTop);
		newHeight -= parseInt(obj.style.paddingBottom);
		obj.style.minHeight = newHeight;
	}else {
		obj.style.height = newHeight;
	}
	
}
function setWidth(obj, newWidth) {
	if(__isNS__) {
		newWidth -= parseInt(obj.style.paddingLeft);
		newWidth -= parseInt(obj.style.paddingRight);
	}
	obj.style.width = newWidth + "px";
}
var ScrollingVarsObj = function(ViewContainer, anItem, anItemMarginOffset) {
		if(!anItemMarginOffset) {
			this.anItemMarginOffset = 0;
		}else {
			this.anItemMarginOffset = anItemMarginOffset;
		}
		this.ViewArea = getWidth(ViewContainer);
		this.ScrollItemSize = parseInt(getWidth(anItem)) + parseInt(this.anItemMarginOffset);
		//number of items that can show in the given window 
		this.NumberOfItemsThatCanShow = Math.ceil(this.ViewArea / this.ScrollItemSize);
		this.LowestRoomThatIsShowing = 1;
		// -1 move stuff left 1 for right
		this.direction = -1;
		//the miliseconds to wait until shifting again
		this.timeToCallBack = 15;
		if(document.all) {
			this.timeToCallBack += this.timeToCallBack;
		}
		//this is how much will scroll per call
		this.AmountToMove = 10;
	};

	Array.prototype.______array = '______array';

(function (s) {
    var m = {
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
    };

    s.parseJSON = function () {
        try {
            if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.
                    test(this)) {
                return eval('(' + this + ')');
            }
        } catch (e) {
        }
        throw new SyntaxError("parseJSON");
    };

    s.toJSONString = function () {
        if (/["\\\x00-\x1f]/.test(this)) {
            return '"' + this.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                var c = m[b];
                if (c) {
                    return c;
                }
                c = b.charCodeAt();
                return '\\u00' +
                    Math.floor(c / 16).toString(16) +
                    (c % 16).toString(16);
            }) + '"';
        }
        return '"' + this + '"';
    };
})(String.prototype);

var JSON = {
    org: 'http://www.JSON.org',
    copyright: '(c)2005 JSON.org',
    license: 'http://www.crockford.com/JSON/license.html',

    stringify: function (arg) {
        var c, i, l, s = '', v;

        switch (typeof arg) {
        case 'object':
            if (arg) {
                if (arg.______array == '______array') {
                    for (i = 0; i < arg.length; ++i) {
                        v = this.stringify(arg[i]);
                        if (s) {
                            s += ',';
                        }
                        s += v;
                    }
                    return '[' + s + ']';
                } else if (typeof arg.toString != 'undefined') {
                    for (i in arg) {
                        v = arg[i];
                        if (typeof v != 'undefined' && typeof v != 'function') {
                            v = this.stringify(v);
                            if (s) {
                                s += ',';
                            }
                            s += this.stringify(i) + ':' + v;
                        }
                    }
                    return '{' + s + '}';
                }
            }
            return 'null';
        case 'number':
            return isFinite(arg) ? String(arg) : 'null';
        case 'string':
            l = arg.length;
            s = '"';
            for (i = 0; i < l; i += 1) {
                c = arg.charAt(i);
                if (c >= ' ') {
                    if (c == '\\' || c == '"') {
                        s += '\\';
                    }
                    s += c;
                } else {
                    switch (c) {
                        case '\b':
                            s += '\\b';
                            break;
                        case '\f':
                            s += '\\f';
                            break;
                        case '\n':
                            s += '\\n';
                            break;
                        case '\r':
                            s += '\\r';
                            break;
                        case '\t':
                            s += '\\t';
                            break;
                        default:
                            c = c.charCodeAt();
                            s += '\\u00' + Math.floor(c / 16).toString(16) +
                                (c % 16).toString(16);
                    }
                }
            }
            return s + '"';
        case 'boolean':
            return String(arg);
        default:
            return 'null';
        }
    },
    parse: function (text) {
    	//alert(text);
        var at = 0;
        var ch = ' ';

        function error(m) {
            throw {
                name: 'JSONError',
                message: m,
                at: at - 1,
                text: text
            };
        }

        function next() {
            ch = text.charAt(at);
            at += 1;
            return ch;
        }

        function white() {
            while (ch !== '' && ch <= ' ') {
                next();
            }
        }

        function str() {
            var i, s = '', t, u;

            if (ch == '"') {
outer:          while (next()) {
                    if (ch == '"') {
                        next();
                        return s;
                    } else if (ch == '\\') {
                        switch (next()) {
                        case 'b':
                            s += '\b';
                            break;
                        case 'f':
                            s += '\f';
                            break;
                        case 'n':
                            s += '\n';
                            break;
                        case 'r':
                            s += '\r';
                            break;
                        case 't':
                            s += '\t';
                            break;
                        case 'u':
                            u = 0;
                            for (i = 0; i < 4; i += 1) {
                                t = parseInt(next(), 16);
                                if (!isFinite(t)) {
                                    break outer;
                                }
                                u = u * 16 + t;
                            }
                            s += String.fromCharCode(u);
                            break;
                        default:
                            s += ch;
                        }
                    } else {
                        s += ch;
                    }
                }
            }
            error("Bad string");
        }

        function arr() {
            var a = [];

            if (ch == '[') {
                next();
                white();
                if (ch == ']') {
                    next();
                    return a;
                }
                while (ch) {
                    a.push(val());
                    white();
                    if (ch == ']') {
                        next();
                        return a;
                    } else if (ch != ',') {
                        break;
                    }
                    next();
                    white();
                }
            }
            error("Bad array");
        }

        function obj() {
            var k, o = {};

            if (ch == '{') {
                next();
                white();
                if (ch == '}') {
                    next();
                    return o;
                }
                while (ch) {
                    k = str();
                    white();
                    if (ch != ':') {
                        break;
                    }
                    next();
                    o[k] = val();
                    white();
                    if (ch == '}') {
                        next();
                        return o;
                    } else if (ch != ',') {
                        break;
                    }
                    next();
                    white();
                }
            }
            error("Bad object");
        }

        function num() {
            var n = '', v;
            if (ch == '-') {
                n = '-';
                next();
            }
            while (ch >= '0' && ch <= '9') {
                n += ch;
                next();
            }
            if (ch == '.') {
                n += '.';
                while (next() && ch >= '0' && ch <= '9') {
                    n += ch;
                }
            }
            if (ch == 'e' || ch == 'E') {
                n += 'e';
                next();
                if (ch == '-' || ch == '+') {
                    n += ch;
                    next();
                }
                while (ch >= '0' && ch <= '9') {
                    n += ch;
                    next();
                }
            }
            v = +n;
            if (!isFinite(v)) {
                error("Bad number");
            } else {
                return v;
            }
        }

        function word() {
            switch (ch) {
                case 't':
                    if (next() == 'r' && next() == 'u' && next() == 'e') {
                        next();
                        return true;
                    }
                    break;
                case 'f':
                    if (next() == 'a' && next() == 'l' && next() == 's' &&
                            next() == 'e') {
                        next();
                        return false;
                    }
                    break;
                case 'n':
                    if (next() == 'u' && next() == 'l' && next() == 'l') {
                        next();
                        return null;
                    }
                    break;
            }
            error("Syntax error");
        }

        function val() {
            white();
            switch (ch) {
                case '{':
                    return obj();
                case '[':
                    return arr();
                case '"':
                    return str();
                case '-':
                    return num();
                default:
                    return ch >= '0' && ch <= '9' ? num() : word();
            }
        }

        return val();
    }
};
	