/* Copyright Claude Needham gxxaxx@gmail.com All rights reserved
	Please refrain from using this script without permission.
	This is how I make my living. 
	If you would like to use the script contact me. We can work something out. 
	You can get a javascript without stealing it (I'll keep the price low).
	And I can live in a house instead of a station wagon.
*/

/*
	var oxxaxxlib = oxxaxxlib || new xxaxxlibo();
	var front = oxxaxxlib.GrabFront("-->", listrawstr);
 */

function xxaxxlibo() {

	this.addLoadEvent = function(func) {
		  var oldonload = window.onload;
		  if (typeof window.onload != 'function') {
		    window.onload = func;
		  } else {
		    window.onload = function() {
		      if (oldonload) {
		        oldonload();
		      }
		      func();
		    }
		  }
	}

	this.mouseRelativeToObject = function(e, DivObj) {
		if (!e) var e = window.event;
	
		var relativeX = 0;
		var relativeY = 0;

		var mousePos = this.mousePosition(e);
		var offsets = this.elementPosition(DivObj);
		relativeX = mousePos[0] - offsets[0];
		relativeY = mousePos[1] - offsets[1];
		return [relativeX, relativeY];
	}

	// http://www.quirksmode.org/js/events_properties.html
	this.mousePosition = function(e) {
		if (!e) var e = window.event; 	// if e exists ff, else use ie window.event var
		var posX = 0;
		var posY = 0;
		if (e.pageX || e.pageY) 	{
			posX = e.pageX;
			posY = e.pageY;
		} else if (e.clientX || e.clientY) 	{
			posX = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posY = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		return [posX, posY];
	}

	// http://www.quirksmode.org/js/findpos.html
	this.elementPosition = function (obj) {
		var curleft = 0;
		var curtop = 0;
		if (obj) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		return [curleft,curtop];
	} 


	this.addEvent = function (object, evName, fnName, cap) {
		if (!object) {
			return;
		}
		// test for IE-ness
		if (object.attachEvent) {
			object.attachEvent("on" + evName, fnName);
		} else if (object.addEventListener) {
			object.addEventListener(evName, fnName, cap);
		}
	}
	this.removeEvent = function (object, evName, fnName, cap) {
		if (!object) {
			return;
		}
		// test for IE-ness
		if (object.detachEvent) {
			object.detachEvent("on" + evName, fnName);
		} else if (object.removeEventListener) {
			object.removeEventListener(evName, fnName, cap);
		}
	}
	this.eventTarget = function (e) {
		if (e.srcElement) {
			return (e.srcElement);
		} else if (e.target) {
			return (e.target);
		} else {
			return null;
		}
	}

	this.GrabDir = function (instring) {
		var tmp;
		var ndx = instring.lastIndexOf("/");
		if (ndx == -1) {
			return "";
		}
		return instring.substring(0,ndx+1);
	}
	this.GrabFile = function (instring) {
		var tmp;
		var ndx = instring.lastIndexOf("/");
		if (ndx == -1) {
			return instring;
		}
		return instring.substring(ndx+1);
	}

	this.GrabFront = function (stag, instring) {
		if (instring == "") {
			return "";
		}
		if (stag == "") {
			return "";
		}
		var tagwhere = instring.indexOf(stag);
		if (tagwhere < 0) {
			return "";
	    	}
		return instring.substring(0, tagwhere);
	}
	this.GrabAfter = function (stag, instring) {
		if (instring == "") {
			return "";
		}
		if (stag == "") {
			return "";
		}
		var tagwhere = instring.indexOf(stag);
		if (tagwhere < 0) {
			return "";
	    	}
		return instring.substring(tagwhere+stag.length);
	}

	// http://robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
	this.getStyle = function (oElm, strCssRule){
		var strValue = "";
		if (!oElm) {
			return "";
		}
		if(document.defaultView && document.defaultView.getComputedStyle){
			strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
		}
		else if(oElm.currentStyle){
			strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
			});
			strValue = oElm.currentStyle[strCssRule];
		}
		return strValue;
	}

	this.getWidth = function (obj) {
		var retval;
		if (obj.currentStyle) {
			retval = obj.currentStyle["width"];
		} else if (obj.offsetWidth) {
			retval = obj.offsetWidth;
		} else if (window.getComputedStyle) {
 			retval = document.defaultView.getComputedStyle(x,"").getPropertyValue("width");
		}
		if (retval == "auto") {
			retval = obj.offsetWidth;
		}
		return retval;
	}
	this.getHeight = function (obj) {
		var retval;
		if (obj.currentStyle) {
			retval = obj.currentStyle["height"];
		} else if (obj.offsetHeight) {
			retval = obj.offsetHeight;
		} else if (window.getComputedStyle) {
	 		retval = document.defaultView.getComputedStyle(x,"").getPropertyValue("height");
		}
		if (retval == "auto") {
			retval = obj.offsetHeight;
		}
		return retval;
	}

	this.is_iphoneish = function () {
		if (navigator && navigator.platform && navigator.platform.match(/^(iPad|iPod|iPhone)$/)) {
			return true;
		} else {
			return false;
		}
	}
}




