/*
 * Alles wat ik mis aan prototype
 */
function CE ( name, className ) {
	ret = document.createElement ( name );
	if ( className ) {
		Element.addClassName(ret,className);
	}
	return ret;
} 

function CT ( text ) {
	return document.createTextNode ( text );
} 

function px ( n ) {
	if ( typeof n == 'string' ) {
		if ( /^\d+px$/.test (n) ) {
			ret = n;
		} else {
			ret = px(parseInt(n));
		}
	} else if ( typeof n == 'number' ) {
		ret = '' + n + 'px';
	} else {
		alert ( n + ' (' +(typeof n) +') is not a numeric value suited for px' );
		ret = px(0);
	}
	
	return ret;
}

function unpx ( n ) {
	var m;
	if ( m = n.match  ( /^(\d+)px$/ ) ) {
		return parseInt( m[1] ); 
	}
	return 0;
}

RegExp.quote = function (str) {
	return str.replace (
      /[\/\[\(\\\{\?.+*]/g,
      function ( $0 ) {
         return '\\' + $0
      }
   );
};

Math.roundAt = function ( value, decimals ) {
	var radix = Math.pow ( 10, decimals > 0 ? decimals : 0 );
	return Math.round ( radix * value ) / radix;
};

Math.between = function ( value, minimum, maximum ) {
	return Math.min ( Math.max ( value, minimum ), maximum );
};

function posH (x,y) {
	return {top:px(y),left:px(x)};
}

function sizeH ( w, h ) {
	if ( h < 0 ) h = 0;
	if ( w < 0 ) w = 0;
	return {width:px(w),height:px(h)};
}

Element.setSize = function (e,w,h) {
	return Element.setStyle(e,sizeH(w,h));
};

Element.setPosition = function ( e, x, y ) {
	return Element.setStyle(e,posH(x,y));
};

Element.getWidth = function ( e, withLeftMargin, withRightMargin ) {
	var width = 
		Element.getDimensions ( e ).width
		+ ( withLeftMargin  ? Element.getMargin ( e, 'left' ) : 0) 
		+ ( withRightMargin ? Element.getMargin ( e, 'right' ) : 0);
	return width;
};

Element.getMargin = function ( e, side ) {
	return unpx ( Element.getStyle ( e, 'margin-' + side.toLowerCase () ) );
};

Element.getHeight = function ( e, withTopMargin, withBottomMargin ) {
	var height = 
		Element.getDimensions ( e ).height
		+ (withTopMargin? Element.getMargin ( e, 'top' ) : 0) 
		+ (withBottomMargin? Element.getMargin ( e, 'bottom' ) : 0);
	return height;
};

Element.setOpacity = function ( e, opacity ) {
	if (opacity == 0 && e.style.visibility != "hidden") {
	   e.style.visibility = "hidden";
	} else if (e.style.visibility != "visible") {
	   e.style.visibility = "visible";
	}
	if (window.ActiveXObject) {
		e.style.filter = "alpha(opacity=" + Math.max (0, Math.min (opacity * 100, 100)) + ")";
	}
	e.style.opacity = Math.max (0, Math.min (opacity, 1));
};

_alert = window.alert;
_alertContinue = true;
window.alert = function ( msg ) {
	_alertContinue = _alertContinue && confirm ( msg );
};

function DEBUG ( str ) {
	var d = $('DEBUG');
	s = CE('div');
	s.appendChild ( CT(str) );
	if ( d ) {
		d.insertBefore ( s, d.firstChild );
		while ( d.childNodes.length > 100 ) {
			d.removeChild ( d.lastChild );
		}
	}
};

Element.flipClassName = function ( element, className ) {
	if ( Element.hasClassName ( element, className ) ) {
		Element.removeClassName ( element, className );
	} else {
		Element.addClassName ( element, className );
	}
};


Element.flipSrc = function (element,flip) {
	var re = [], newSrc;
	$A(flip).each ( function ( v, i ) {
		re[i] = new RegExp ( '(^|/)' + RegExp.quote ( v ) + '$' );
	} );
	$A(re).each ( function ( re, i )  {
		if ( re.test ( element.src ) ) {
			newSrc = element.src.replace ( re, '$1' + flip[ i == 0 ? 1 : 0 ] );
		}
	} );
	element.src = newSrc;
} ;

// inner workings are the same as document.getElementsByClassName (prototype.js)
document.getElementsByAttr = function ( attribute, value, parentElement ) {
  var children = ($(parentElement) || document.body).getElementsByTagName('*');
  return $A(children).inject([], function(elements, child) {
    if (
	 		( child.getAttributeNode(attribute) && child.getAttributeNode(attribute).specified)
		&& ( !value || child.getAttribute ( attribute ) == value )   
	 )
      elements.push(child);
    return elements;
  });
};

