/* -----------------------------------------------------------------------------------
 *	Dom 	functions for dom access and manipulation
 *	
 *		Required: none
 *
 * -----------------------------------------------------------------------------------
 *
 *	isRegExp(o)
 *	isArray(a)
 *	isDefined(property)
 *	$(id)										id = string or RegExp
 *	$F(name,elt)
 *
 * 	Dom
 *		getElementsByclassName( className, elt ) 	className = string or RegExp
 *		getParent( elt )
 *		getParentByclassName( className, elt )		className = string or RegExp
 *		setOpacity( elt, v )
 *		findSize( elt )
 *		toCamel(property)
 *		setStyle(el, styleProp, value )
 *		getStyle(el, styleProp )
 *		findPos(obj)
 *		mousePos(event)
 *		target(event)
 *
 * -----------------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------------
 *		Public functions
 * --------------------------------------------------------------------------------- */

function isRegExp(o)
{
	if ( (typeof(o) == "function" || typeof(o) == "object") && o.constructor == RegExp ) {
		return true;
	}
	return false;
}
function isArray(a) 
{
    return typeof(a)=="object" && a.constructor == Array;
}
function isDefined(property) 
{
  return (typeof property != 'undefined');
}

function $(id, elt ) 
{
	if ( !isDefined(elt) ) {
		elt = document;
	}
	if ( isRegExp(id) ) {
		var result = [];
		var all = elt.getElementsByTagName( "*" );
		var i;
		for ( i=0; i < all.length; i++ ) {
			if ( all.item(i).id.match(id) ) {
				result[result.length] = all.item(i);
			}
		}
		if ( result.length == 1 ) {
			return result[0];
		} else {
			if ( result.length === 0 ) {
				return null;
			}
			return result;
		}
	} else {
		return document.getElementById(id);
	}
}

function $F(name, elt)
{
	var found;
	var fields;
	
	if ( !isDefined(elt) ) {
		elt = document;
	}
	
	function findField( inputs, name ) 
	{
		var i;		
		for ( i = 0; i < inputs.length; i++ ) {
			if ( inputs.item(i).name == name ) {
				//console.log( "Found " + name );
				return inputs.item(i);
			}
		}
		return false;
	};
	
	fields = elt.getElementsByTagName( "FORM" );
	
	if ( false !== (found = findField( fields, name )) ) {
		return found;
	}

	fields = elt.getElementsByTagName( "INPUT" );
	
	if ( false !== (found = findField( fields, name )) ) {
		return found;
	}
	
	fields = elt.getElementsByTagName( "TEXTAREA" );
	if ( false !== (found = findField( fields, name )) ) {
		return found;
	}

	fields = elt.getElementsByTagName( "SELECT" );
	if ( false !== (found = findField( fields, name )) ) {
		return found;
	}

	return false;
}

var Dom = {
	getElementsByClassName: function( className, elt )
	{
		if ( !isDefined(elt) ) elt = document;

		var i;
		var result = [];
		var all = elt.getElementsByTagName( "*" );

		if ( isRegExp(className) ) {
			for ( i=0; i < all.length; i++ ) {
				if ( all.item(i).className.match(className) ) {
					result[result.length] = all.item(i);
				}
			}
		} else {
			for ( i=0; i < all.length; i++ ) {
				if ( all.item(i).className == className ) {
					result[result.length] = all.item(i);
				}
			}
		}
		if ( result.length == 1 ) {
			return result[0];
		} else {
			if ( result.length === 0 ) {
				return null;
			}
			return result;
		}
	},
	getParent: function( elt )
	{
		if ( elt.parentElement ) {
	        return elt.parentElement;
		}
	    return elt.parentNode;
	},
	getParentByClassName: function( className, elt )
	{
		var node = elt;

		if ( isRegExp(className) ) {
			while ( node !== null && node != document.body ) {
				if ( node.className.match(className) )
					return node;
				node = Dom.getParent(node);
			}
		} else {
			while ( node !== null && node != document.body ) {
				if ( node.className == className )
					return node;
				node = Dom.getParent(node);
			}		
		}
		/*alert( "no parent wiht className="+className+"found " ); */
		return null;
	},
	setOpacity: function( elt, v )
	{
		//if ( elt.style.filter ) {
			//alert("IE opacity " + elt.id + " alpha(opacity="+Math.floor((v*100))+")");
			elt.style.filter = "alpha(opacity="+Math.floor((v*100))+")";
		//} else {
			elt.style.opacity = v;
		//}
	},
	findSize: function ( elt )
	{
		var h = Dom.getStyle(elt,'height');
		if ( h == "auto" ) {
		    h = parseInt(elt.offsetHeight);
		} else {
		    h = parseInt(h);
		}
		var w = Dom.getStyle(elt,'width');
		if ( w == "auto" ) {
		    w = parseInt(elt.offsetWidth);
		} else {
		    w = parseInt(w);
		}
		return {width: w, height: h };
	},

	/*
	 *	Ripped from somewhere
	 *  
	 *		Convert property name from 'property-name' to 'propertyName'
	 */
	toCamel: function (property) 
	{
		var convert = function(prop) 
		{
			var test = /(-[a-z])/i.exec(prop);
			return prop.replace(RegExp.$1, RegExp.$1.substr(1).toUpperCase());
		};

		while(property.indexOf('-') > -1) {
			property = convert(property);
		}

		return property;
		//return property.replace(/-([a-z])/gi, function(m0, m1) {return m1.toUpperCase()}) // cant use function as 2nd arg yet due to safari bug
	},

	setStyle: function(el,styleProp,value)
	{
		if ( styleProp === "opacity" )
		{
			Dom.setOpacity(el, opacity);
		} else
		{
			el.style[Dom.toCamel(styleProp)] = value;
		}
	},
	getStyle: function (el,styleProp)
	{
		var y;
		var x = el;
		if (x.currentStyle) {
			y = x.currentStyle[Dom.toCamel(styleProp)];
		} else {
	        var style = document.defaultView.getComputedStyle(x,null);
	        if ( style !== null ) {
	            y = style.getPropertyValue(styleProp);
	        } else {
	            return null;
	        }
	    }
		return y;
	},

	/*
	 * 	Original code ripped from
	 * 		www.quirksmode.org/js/contents.html
	 * 		Written by ppk, www.quirksmode.org
	 */

	/*
	 *	return 	element position relative to page
	 *			{left,top}
	 */
	findPos: function (obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while ( (obj = obj.offsetParent) !== null ) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return { left:curleft, top:curtop };
	},

	/* 
	 * 	return 	mouse position relative to page
	 *			{left,top}
	 */
	mousePos: function(event) {
		var posx = 0;
		var posy = 0;
		if (!event) var event = window.event;
		if (event.pageX || event.pageY) {
			posx = event.pageX;
			posy = event.pageY;
		}
		else if (event.clientX || event.clientY) 	{
			posx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return { left:posx, top:posy };
	},

	/*
	 *	return 	target of event
	 *			element
	 */
	target: function(event) {
		var targ;
		if (!event) var event = window.event;
		if (event.target) targ = event.target;
		else if (event.srcElement) targ = event.srcElement;
		if (targ.nodeType == 3) /* defeat Safari bug */
			targ = targ.parentNode;
		return targ;
	}
};


/*
 *	Original code ripped from 
 *		http://cass-hacks.com/articles/code/js_url_encode_decode/
 */
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match !== null && match.length > 1 && match[1] !== '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
		if ( charCode < 16 ) {
			output += '%0' + hexVal.toUpperCase();
		} else {
			output += '%' + hexVal.toUpperCase();
		}
      }
      x++;
    }
  }
  return output;
}


