
function stripWeirdPHPChars(){
	var body = document.getElementsByTagName('body')[0];
	var childNodes = body.childNodes;
	for( var c = 0; c < childNodes.length; c++ ){
		if( childNodes[c].nodeType == 3 ){
			body.removeChild( childNodes[c] );
		}
	}
}

function contents( _elem ){
	return (_elem.innerText !== undefined && _elem.innerText !== '') ? _elem.innerText : _elem.innerHTML;
}

function change_title( _newName ){
	document.title = _newName;
}

Array.prototype.contains = function( _item ){
	for( var i = 0; i < this.length; i++ ){
		if( this[i] == _item ){
			return true;
		}
	}
	return false;
};

String.prototype.capWords = function( ){
	return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};

String.prototype.trim = function( ){
	return this.replace(/^\s+|\s+$/g,"");
};

String.prototype.spaceToUnderscore = function( ){
	return this.replace(/ /g, "_");
};

// REFERENCING ELEMENTS - SHORTHAND ///////////////////////////////////////////

function elem(){
	switch( arguments.length ){
		case 1: return document.getElementById( arguments[0] );
		case 2: return elems( arguments[0], arguments[1] )[0];
		case 3: return elems( arguments[0], arguments[1] )[arguments[2]];
	} return "elem() takes 1, 2 or 3 arguments";
}

function elems(){
	switch(arguments[0]){
		case 'c': return document.getElementsByClassName( arguments[1] );
		case 'n': return document.getElementsByName( arguments[1] );
		case 't': return document.getElementsByTagName( arguments[1] );
	} return "elems() takes 1, 2 or 3 arguments";
}

function byId(){
	return ( arguments.length == 1 )
		? document.getElementById( arguments[0] )
		: "byId() takes one argument"; }

function byName(){
	return ( arguments.length == 1 )
		? document.getElementsByName( arguments[0] )
		: "byName() takes one argument"; }

function byClass(){
	return ( arguments.length == 1 )
		? document.getElementsByClassName( arguments[0] )
		: "byClass() takes one argument"; }

function byTag(){
	return ( arguments.length == 1 )
		? document.getElementsByTagName( arguments[0] )
		: "byTag() takes one argument"; }



// DEBUG TOOLS ////////////////////////////////////////////////////////////////

function alertObj(){
	var report = "";
	for( i in arguments[0] ){ report += (i+" : "/*+arguments[0][i]+"\n"*/); }
	alert(report);
}
function alertIn(){
	var report = "";
	for( i in arguments[0] ){ report += (i+", "); }
	report = report.split(", ").sort();
	var i;
	for( i = 0; i < report.length; i+=150 ){
		var upper = Math.min(i+150);
		alert( report.slice(i,i+150) );
	}
}

// ADDING ELEMENTS TO THE DOM /////////////////////////////////////////////////

function addCSSRule( _newRule ){
	var numRules;
	if( document.styleSheets[0].cssRules ){
		try{ numRules = document.styleSheets[0].cssRules.length; }
		catch(e){ numRules = 0; }
		document.styleSheets[0].insertRule( _newRule, numRules );
	}
	else { // IE
		var ieRule = _newRule.split('{');
		ieRule[1] = ieRule[1].slice(0, ieRule[1].indexOf('}') );
		document.styleSheets[0].addRule( ieRule[0], ieRule[1] );
	}
}

function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;
 
    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';
 
    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';
 
    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
 
    document.body.appendChild(scr);
 
    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;
 
    // Remove the scrolling div from the doc
    document.body.removeChild(
    document.body.lastChild);
 
    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}

var centeredElements = {
	scrWid: null,
	elems:  new Array(),
	add:    function( ){ this.elems.push( arguments[0] ); }
}

function scrollbar_adjust(){
	var body = byTag('body')[0];
	var left = Math.ceil( body.clientWidth/2 );
	var marginLeft = 0;
	if ( body.clientHeight < body.scrollHeight ){
		marginLeft += Math.ceil(centeredElements.scrWid/2);
		if ( body.clientWidth % 2 == 1 ){ left -= 1; }
	}
	for( var n = 0; n < centeredElements.elems.length; n++ ){
		var m = marginLeft + -Math.round( centeredElements.elems[n].clientWidth/2 );
		centeredElements.elems[n].style.left = left + "px";
		centeredElements.elems[n].style.marginLeft = m + "px";
		if( body.clientWidth < centeredElements.elems[n].clientWidth ){
			centeredElements.elems[n].style.marginLeft = -left + "px";
		}
	}
}

function mkEl( ){
	if( arguments.length != 1 ) {
		return null
	}
	var a = arguments[0];
	if( a['id'] == null || a['parent'] == null ) {
		return null
	}
	var tag = "div";
	if( a['tag'] !== undefined ) {
		tag = a['tag'];
	}	
	var newStyle = "";
	var newRule = a['tag'] + "#" + a['id'] + " { ";
	var newEvt = {};
	
	if( a['w'] !== undefined ) {
		newRule += "width:" + parseInt(a['w']) + "px; "; }
	if( a['h'] !== undefined ) {
		newRule += "height:" + parseInt(a['h']) + "px; "; }
	if( a['bg'] !== undefined ) {
		newRule += "background:" + a['bg'] + "; "; }
	if( a['border'] !== undefined ) {
		newRule += "border:" + a['border'] + "; "; }
	if( a['shadow'] !== undefined ) {
		newRule += "-moz-box-shadow:" + a['shadow'] + "; ";
		newRule += "-webkit-box-shadow:" + a['shadow'] + "; ";
		newRule += "box-shadow:" + a['shadow'] + "; "; }
	if( a['roundCorners'] !== undefined ) {
		newRule += "-moz-border-radius:" + a['roundCorners'] + "; ";
		newRule += "-webkit-border-radius:" + a['roundCorners'] + "; ";
		newRule += "border-radius:" + a['roundCorners'] + "; "; }
	if( a['align'] == 'page-center' && a['w'] !== undefined ) {
		newRule += "position:relative; ";
		newRule += "left:50%; ";
		newRule += "margin-left:-"+parseInt( a['w']/2 ,10 )+"px; ";
		if( centeredElements.scrWid == null ){
			centeredElements.scrWid = getScrollerWidth();
		}
		newEvt['target'] = window;
		newEvt['type'] = 'resize';
		newEvt['function'] = scrollbar_adjust;
	}
	if( a['align'] == 'left' || a['align'] == 'right' ) {
		newRule += "float:" + a['align'] + "; ";
	}
	newRule += "}";
	
	addCSSRule( newRule );
	
	var newEl = document.createElement( a['tag'] );
	if( a['id'] !== undefined ){
		newEl.setAttribute( 'id', a['id'] );
		newEl.id = a['id']; //IE
	}
	if( a['class'] !== undefined ){
		newEl.setAttribute( 'class', a['class'] );
		newEl.setAttribute( 'className', a['class'] ); //IE
	}
	newEl.style.cssText = newStyle;
	
	var parent = a['parent'];
	if( a['before'] !== undefined ){
		parent.insertBefore( newEl, a['before'] ); }
	else{
		parent.appendChild( newEl ); }
	
	if( newEvt['target'] !== undefined ){
		addEvent( newEvt['target'], newEvt['type'], newEvt['function'] );
	}
	if( a['align'] == 'page-center' ){
		centeredElements.add( newEl );
		scrollbar_adjust();
	}
	if( a['inner'] != null ){
		newEl.innerText = a['inner'];
	}
	return newEl;
}




function giveAttr( ){
	if( arguments.length != 2 ) {
		return null
	}
	var tgt = arguments[0];
	var a = arguments[1];

	var newStyle = "";
	var newRule = "#" + tgt.id + " { ";
	var newEvt = {};
	
	if( a['w'] !== undefined ) {
		newRule += "width:" + parseInt(a['w']) + "px; "; }
	if( a['h'] !== undefined ) {
		newRule += "height:" + parseInt(a['h']) + "px; "; }
	if( a['bg'] !== undefined ) {
		newRule += "background:" + a['bg'] + "; "; }
	if( a['border'] !== undefined ) {
		newRule += "border:" + a['border'] + "; "; }
	if( a['shadow'] !== undefined ) {
		newRule += "-moz-box-shadow:" + a['shadow'] + "; ";
		newRule += "-webkit-box-shadow:" + a['shadow'] + "; ";
		newRule += "box-shadow:" + a['shadow'] + "; "; }
	if( a['roundCorners'] !== undefined ) {
		newRule += "-moz-border-radius:" + a['roundCorners'] + "; ";
		newRule += "-webkit-border-radius:" + a['roundCorners'] + "; ";
		newRule += "border-radius:" + a['roundCorners'] + "; "; }
	if( a['align'] == 'page-center' && a['w'] !== undefined ) {
		newRule += "position:relative; ";
		newRule += "left:50%; ";
		newRule += "margin-left:-"+parseInt( a['w']/2 ,10 )+"px; ";
		if( centeredElements.scrWid == null ){
			centeredElements.scrWid = getScrollerWidth();
		}
		newEvt['target'] = window;
		newEvt['type'] = 'resize';
		newEvt['function'] = scrollbar_adjust;
	}
	if( a['align'] == 'left' || a['align'] == 'right' ) {
		newRule += "float:" + a['align'] + "; ";
	}
	newRule += "}";
	
	addCSSRule( newRule );
	
	if( newEvt['target'] !== undefined ){
		addEvent( newEvt['target'], newEvt['type'], newEvt['function'] );
	}
	if( a['align'] == 'page-center' ){
		centeredElements.add( tgt );
		scrollbar_adjust();
	}
	if( a['inner'] != null ){
		tgt.innerText = a['inner'];
	}
	return tgt;
}











function mkImg( _newId, _src, _where ){
	if( document.getElementById(_newId) === undefined ){
		var newImg = document.createElement('img');
		newImg.setAttribute( 'id', _newId );
		newImg.id = _newId; //IE
		newImg.setAttribute( 'src', "img/"+_src );
		newImg.src = "img/"+_src; //IE
		_where.appendChild( newImg );
	}
}

function addEvent( _el, _type, _f ){
	if( window.attachEvent ){
		_el.attachEvent( 'on' + _type, _f );
	} else {
		_el.addEventListener( _type, _f, true );
	}
}

function removeEvent( _el, _type, _f ){
	if( window.detachEvent ){
		_el.detachEvent( 'on' + _type, null );
	} else {
		_el.removeEventListener( _type, _f, true );
	}
}

function makeDraggable( _el ) {
	addEvent( _el, 'mousedown', function(evt){dragPick(evt);} );
	addEvent( _el, 'mousemove', function(evt){dragDrag(evt);} );
	addEvent( _el, 'mouseout', function(evt){dragDrop(evt);} );
	addEvent( _el, 'mouseup', function(evt){dragDrop(evt);} );
}

function dragPick( _evt ){
	_evt.currentTarget.alt = _evt.offsetX+','+_evt.offsetY;
}

function dragDrag( _evt ){
	var el = _evt.currentTarget;
	if( el.alt !== undefined ){
		var xy = el.alt.split(',');
		el.style.left = _evt.x - xy[0];
		el.style.top = _evt.y - xy[1];
	}
}

function dragDrop( _evt ){
	_evt.currentTarget.alt = null;
	
}


// POSITIONING ELEMENTS ///////////////////////////////////////////////////////

function moveElemPolar( _id, _turns, _radius ){
	//alert("move");
	var x = Math.cos( _turns*2*Math.PI ) * _radius;
	var y = Math.sin( _turns*2*Math.PI ) * _radius;
	moveElem( _id, x, y );
}

function moveElem( _id, _x, _y ){
	var el = elem( _id );
	var parent = el.parentNode;
	el.style.position = "absolute";
	
	if( _x == 'left' ){       el.style.left = 0; }
	else if( _x == 'right' ){ el.style.left = parent.clientWidth - el.clientWidth;  }
	else{                     el.style.left = parseInt(parent.clientWidth/2+_x-el.clientWidth/2) + "px"; }
	
	if( _y == 'top' ){         el.style.top = 0; }
	else if( _y == 'bottom' ){ el.style.top = parent.clientHeight - el.clientHeight; }
    else{                      el.style.top = parseInt(parent.clientHeight/2-_y-el.clientHeight/2) + "px"; }
}

function drag( _id ){
	//putImg( _id );
}

function paper( _id, _url, _stretchType ){
	var _stretch;
	switch( _stretchType ){
		case 'contain': _stretch = 'contain'; break;
		case 'cover:': _stretch = 'cover'; break;
		default: _stretch = 'auto';
	}
	var newRule = "#" + _id + " { background-image:url('img/" + _url + "'); ";
	    newRule += "-moz-background-size:" + _stretch + "; -o-background-size:" + _stretch + ";"
		newRule += "-webkit-background-size:" + _stretch + "; background-size:" + _stretch + "; }";
	addCSSRule( newRule );
}

// CALCULATING ////////////////////////////////////////////////////////////////

// includes the element's own margins, excludes the element's own padding and border.
function offsetTop( elem ){
	if( elem.offsetParent == null ){
		return elem.offsetTop;
	}
	return elem.offsetTop + offsetTop( elem.offsetParent );
}

function offsetLeft( elem ){
	if( elem.offsetParent == null ){
		return elem.offsetLeft;
	}
	return elem.offsetLeft + offsetLeft( elem.offsetParent );
}

