var $utils = {};

Array.prototype.contains = function(input){
	for (var i in this) {
		if (this[i] == input) {
			return this[i];
		}
	}
	return null;
};

$utils.instanceOf = function (object, constructorFunction) {
  while (object != null) {
    if (object == constructorFunction.prototype)
     {return true}
	 object = object.__proto__;
  }
  return false;
}

// Correctly handle PNG transparency in Win IE 5.5 or higher.
// http://homepage.ntlworld.com/bobosola. Updated 02-March-2004

$utils.fixPNG = function(img) 
{
	if (navigator.appName == 'Netscape') return;
	var i;
	var imgName = img.src.toUpperCase()
	if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
    {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
    }
}

$utils.px = function(obj, attr, n) {
	 obj['style'][attr] = Math.round(n) + 'px';
}

$utils.abso = function(obj) {
	 obj.style.position = 'absolute';
}

$utils.alpha = function(obj, p) {
	 obj.style.opacity = p;
	  obj.style.filter = 'alpha(opacity='+(p*100)+');';
}

$utils.hide = function(obj) {
	    obj.style.visibility = 'visible';
}

$utils.show = function(obj) {
	    obj.style.visibility = 'hidden';
}

$utils.center = function(obj, parent) {
 var height, width;
 if (!parent) {
   height = GetWindowHeight();
   width = GetWindowWidth();
 } else {
   var oldparentvis = parent.style.visibility;
   var oldparentdisp = parent.style.display;
   parent.style.visibility = 'hidden';
   parent.style.display = '';
   height = parent.offsetHeight;
   width = parent.offsetWidth;
   parent.style.visibility = oldparentvis;
   parent.style.display = oldparentdisp;
 }
 var oldvis = obj.style.visibility;
 var olddisp = obj.style.display;
 obj.style.visibility = 'hidden';
 obj.style.display = '';
 $utils.px(obj, 'top', (height / 2) - (obj.offsetHeight / 2));
 $utils.px(obj, 'left', (width / 2) - (obj.offsetWidth / 2));
 obj.style.visibility = oldvis;
 obj.style.display = olddisp;
}

$utils.bg = function(obj, color) {
	 obj.style.background = color;
}

$utils.toggle = function(obj) {
	if (obj.style.display == 'block' || !obj.style.display)
		obj.style.display = 'none';
	else
		obj.style.display = 'block';
}

$utils.roundTo = function(dec, n) {
	return Math.round((Math.pow(10, dec) * n)) / Math.pow(10, dec);
}

$utils.roundToStr = function(dec, n) {
	rv = Math.round((Math.pow(10, dec) * n)) / Math.pow(10, dec) + '';

	if (dec == 0) {
		return rv;
	}

	lp = rv.lastIndexOf('.');

	if (lp == -1) {
		rv += '.';
		decs = 0;
	} else {
		decs = rv.length - lp - 1;
	}

	while (decs++ < dec) {
		rv += '0';
	}
	return rv;
}

$utils.commify = function(str) {
	var s = str.toString();
	for (;;) {
		var out = s.replace(/^(\d+)(\d{3})/, "$1,$2");
		
		if (out == s) {
			return out;
		} else {
           s = out;
       }
	}
}

$utils.updateSelectElement = function(select,args) {
	select.style.fontSize = args['fontsize'];
	select.onchange = args['onchange'];
	for (var i = 0;i < args['options'].length;i++) {
		var opt = document.createElement('option');
		opt.value=args['options'][i];
		opt.innerHTML = args['optionstext'] ? args['optionstext'][i] : args['options'][i];
		if (opt.value == args['selected']) {
			opt.selected = true;
		}
		select.appendChild(opt);
	//      select.add(opt, null);
  }

	return select;
}


$utils.createSelectElement = function(args) {
	var select=document.createElement('select');
	select.style.fontSize = args['fontsize'];
	select.onchange = args['onchange'];
	for (var i = 0;i < args['options'].length;i++) {
		var opt = document.createElement('option');
		opt.value=args['options'][i];
		opt.innerHTML = args['optionstext'] ? args['optionstext'][i] : args['options'][i];
		if (opt.value == args['selected']) {
			opt.selected = true;
		}
		select.appendChild(opt);
	//      select.add(opt, null);
  }

	return select;
}

$utils.keys = function(obj) {
	var keys = new Array();
	for (var key in obj) {
		keys.push(key);
	};
	return keys;
};

$utils.ucfirst = function(str) {
	return str.substr(0, 1).toUpperCase() + str.substr(1,str.length);
};

$utils.funcname = function(f) {
	var m = f.toString().match(/function (\w*)/);
	return (m && m[1]) ? m[1] : ("anonymous (" + (f.toSource ? f.toSource() : 'unknown') + ")");
}


$utils.stacktrace = function() {
	var s = "";
	var seen = {};
	for (var a = arguments.callee.caller; a !=null; a = a.arguments.callee.caller) {
		s += "->"+$utils.funcname(a) + "\n";
		if (seen[a]) {s+="<*>"; break;}
		seen[a] = 1;
	}
	return s;
}

$utils.getObjectPageXY = function(elem) {
  // Climb tree of parents to get x,y position of this object within the whole page
  // Use this function to calculate an offset to subtract from the mouse event pageX + pageY,
  // when you want to get the coordinates of the mouse relative to a particular object
  var r={"x":0,"y":0};
  while(elem) {
    r.x+=elem.offsetLeft; r.y+=elem.offsetTop; elem=elem.offsetParent;
  }
  return r;
}

