String.prototype.trim=function(){
     return this.replace(/^\s+|\s+$/gm,'');
}

var avant = {
	versions : 0.1,
	debugging : false,
	homePage : 'http://www.e-go.co.nz',

	webDir : "", 
	skinDir : "", 
	servicesDir : "", 
	javascriptDir : "",	
	skinImagesDir : "", 
	skinSiteImagesWebDir : "", 
	skinImagesWebDir : "", 
	
	siteRootNode : null,
	
	isIE : false,							// Are we using IE?
	
	Init : function(webDir, skinDir, skinImagesDir, skinSiteImagesWebDir, skinImagesWebDir, servicesDir, javascriptDir) {
		this.webDir = webDir;
		this.skinDir = skinDir;
		this.skinImagesDir = skinImagesDir;
		this.skinSiteImagesWebDir = skinSiteImagesWebDir;
		this.skinImagesWebDir = skinImagesWebDir;
		this.servicesDir = servicesDir;
		this.javascriptDir = javascriptDir;
		
		this.siteRootNode = document.getElementById("ego_rootContainer");
	},
	
	fixEvent : function(event) {
		if (!event) event = window.event
	
		if (event.target) {
			/* Safari Fix */
			if (event.target.nodeType == 3) event.target = event.target.parentNode;
		} else if (event.srcElement) {
			event.target = event.srcElement;
		}
		
		return event;	
	},
	
	map : function(array, func) {
		for (var i = 0, n = array.length; i < n; i++) func(array[i])
	},

    XMLHttpRequest : function(){
    	var xmlHTTP = false;
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		// JScript gives us Conditional compilation, we can cope with old IE versions.
		// and security blocked creation of the objects.
		try {
			xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
			this.isIE = true;
		} catch (e) {
			try {
				xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
				this.isIE = true;
			} catch (E) {
				xmlHTTP = false;
			}
		}
		@end @*/
		
		if (!xmlHTTP && typeof XMLHttpRequest!='undefined') {
			xmlHTTP = new XMLHttpRequest();

			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (xmlHTTP.readyState == null) {
				xmlHTTP.readyState = 1;
				xmlHTTP.addEventListener("load", function () {
						xmlHTTP.readyState = 4;
						if (typeof xmlHTTP.onreadystatechange == "function") xmlHTTP.onreadystatechange();
					}, false);
			}	
		}			
		
		return xmlHTTP;
    },
    
    preloadImages : function() {
		if (document.images) {
			for (var i = 0; i < this.preloadImages.arguments.length; i++) {
				(new Image()).src = this.preloadImages.arguments[i];
			}
	   }
	},
	
	addLoadEvent : function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
	
};

avant.cookieLib = {
	setCookie : function(name, value, expires, path, domain, secure) {
	   var curCookie = name + '=' + escape(value) + ((expires) ? '; expires=' + expires.toGMTString() : '') + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + ((secure) ? '; secure' : '');
	   document.cookie = curCookie;
	},
	
	getCookie : function(name) {
	   var dc = document.cookie;
	   
	   // find beginning of cookie value in document.cookie
	   var prefix = name + "=";
	   var begin = dc.indexOf("; " + prefix);
	   if (begin == -1) {
	      begin = dc.indexOf(prefix);
	      if (begin != 0) return null;
	   }
	   else begin += 2;
	   
	   // find end of cookie value
	   var end = document.cookie.indexOf(";", begin);
	   if (end == -1) end = dc.length;
	   
	   // return cookie value
	   return unescape(dc.substring(begin + prefix.length, end));
	},
	
	deleteCookie : function(name, path, domain) {
	   var value = getCookie(name);
	   if (value != null) document.cookie = name + '=' + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + '; expires=Thu, 01-Jan-70 00:00:01 GMT';
	   return value;
	},
	
	supportsCookies : function(rootPath) {
	   setCookie('checking_for_cookie_support', 'testing123', '', (rootPath != null ? rootPath : ''));
	   if (getCookie('checking_for_cookie_support')) return true;
	   else return false;
	}
};

avant.domLib = {
	deleteElement : function(elementNode) {
		elementNode.parentNode.removeChild(elementNode);
	},

	moveNode : function(node, referenceNode, placeBeforeReference) {
		var parent = node.parentNode;
		parent.removeChild(node);
		
		if (placeBeforeReference && referenceNode) {
			parent.insertBefore(node, referenceNode);
		} else {
			if (referenceNode && referenceNode.nextSibling) parent.insertBefore(node, referenceNode.nextSibling);
			else parent.appendChild(node);
		}
	},

	cloneNode : function(node, placeBeforeNode) {
		var parentNode = node.parentNode;
		var newNode = node.cloneNode(true);
		
		if (placeBeforeNode && node) {
			parentNode.insertBefore(newNode, node);
		} else {
			if (node && node.nextSibling) parentNode.insertBefore(newNode, node.nextSibling);
			else parentNode.appendChild(newNode);
		}
		
		return newNode;
	}	
};

//----------------------------------------------------------------------//
//	Popup functions														//
//		These popup functions are designed with usuability in mind		//
//		I.e. They do not mangle the href attribute of the 'a' element	//
//		They also degrade well when javascript is off and will behave	//
//		like standard popups.											//
//																		//
//		Usuage:															//
//			<a href="www.google.co.nz" target="_blank" 					//
//				onClick="avant.popupLib.link_popup(this); 				//
//							return false;">CLICK ME</a>					//
//																		//
//		The functions can also be used on elements other than links		//
//		<input onClick="raw_popup('www.google.co.nz'); return false;"	//
//																		//
//		Popup Features:													//
//			Both link_popup and raw_popup can be passed a features 		//
//			string. This is the standard html popup string. When		//
//			no custom features are passed the default (_POPUP_FEATURES)	//
//			will be used.												//
//----------------------------------------------------------------------//
avant.popupLib = {
	
	// Default popup features
	_POPUP_FEATURES : Array('location=0, statusbar=0, menubar=0, width=700px,  height=650px'),
	
	//----------------------------------------------------------------------//
	//	raw_popup function													//
	//	@param string path to the popup										//
	//	@param html popup target, defaults to _blank						//
	//	@param features string (see popup functions note)					//
	//	@return the window object											//
	//----------------------------------------------------------------------//
	raw_popup : function(url, target, features) {
	  if (isUndefined(features)) features = _POPUP_FEATURES;
	  if (isUndefined(target))  target = '_blank';
	
	  var theWindow = window.open(url, target, features);
	  theWindow.focus();
	  return theWindow;
	},
	
	//----------------------------------------------------------------------//
	// link_popup function													//
	//	@param string path to the popup										//
	//	@param features string (see popup functions note)					//
	//	@return the window object											//
	//----------------------------------------------------------------------//
	link_popup : function(src, features) {
	    return raw_popup(src.getAttribute('href'),  src.getAttribute('target') || '_blank',  features);
	}
};