// MWBase.js
//
// Mainwave base classes & functions


//
// "CONSTANTS"
//
var USER_AGENT_IE		= document.all && document.getElementById;


//
// GENERAL UTILITIES
//
function elem(id) {
	return document.getElementById(id);
}

function removeElement(id) {
	if (elem(id)) {
		elem(id).parentNode.removeChild(elem(id));
	}
}


function createElementWithID(type, id) {
	var elem = document.createElement(type);
	elem.setAttribute("id", id);
	return elem;
}

function createElementWithText(type, text) {
	var elem = document.createElement(type);
	var textElem = document.createTextNode(text);
	elem.appendChild(textElem);
	return elem;
}

function createImage(width, height, src, title, id) {
	var img = document.createElement("img");
	img.setAttribute("width", width);
	img.setAttribute("height", height);
	img.setAttribute("src", src);
	if (title) { img.setAttribute("title", title); }
	if (id) { img.setAttribute("id", id); }
	return img;
}

function getCenteredCoords(size) {
	var w, h;
	if (document.body.clientWidth) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	else if (document.body.parentNode.clientWidth) {
		w = document.body.parentNode.clientWidth;
		h = document.body.parentNode.clientHeight;
	}
	else {
		return new Coord(20,20);
	}
	return new Coord(
		((w - size.width) / 2),
		((h - size.height) / 2.5) // Slightly higher than centered looks better
	);
}

function addClass(e, className) {
	var classArray = e.className.split(" ");
	for (i = 0; i < classArray.length; i++) {
		if (classArray[i] == className) {
			// It's already in
			return;
		}
	}
	classArray.push(className);
	e.className = classArray.join(" ");
}

function removeClass(e, className) {
	var classArray = e.className.split(" ");
	for (i = 0; i < classArray.length; i++) {
		if (classArray[i] == className) {
			classArray.splice(i, 1); // Remove it
			// Keep going just in case it's there twice.
		}
	}
	e.className = (classArray.length > 0) ? (classArray.join(" ")) : "";
}

//
// OBJECTS
//

function Coord(x, y) {
	// Properties
	this.x = x;
	this.y = y;

	// Methods
	this.Move = _Coord_Move;
}
function _Coord_Move(moveX, moveY) {
	this.x += moveX;
	this.y += moveY;
}

function Size(width, height) {
	// Properties
	this.width = width;
	this.height = height;

	// Methods
	this.Resize = _Size_Resize;
}
function _Size_Resize(addWidth, addHeight) {
	this.width += addWidth;
	this.height += addHeight;
}

function Rectangle(topLeftX, topLeftY, width, height) {
	// Properties
	this.topLeftCoord = new Coord(topLeftX, topLeftY);
	this.size = new Size(width, height);

	// "Inline" methods
	this.Width = function() {return this.size.width;}
	this.Height = function() {return this.size.height;}
	this.Top = function() {return this.topLeftCoord.y;}
	this.Left = function() {return this.topLeftCoord.x;}
	this.Bottom = function() {return this.Top() + this.Height();}
	this.Right = function() {return this.Left() + this.Width();}

	// Methods: getters with "px" suffix
	this.WidthPx = function() {return this.size.width + "px";};
	this.HeightPx = function() {return this.size.height + "px";};
	this.TopPx = function() {return this.topLeftCoord.y + "px";};
	this.LeftPx = function() {return this.topLeftCoord.x + "px";};

	// Methods: utility
	this.Resize = _Rectangle_Resize;
	this.Move = _Rectangle_Move;
}
function _Rectangle_Resize(addWidth, addHeight, anchorToCentre) {
	this.size.Resize(addWidth, addHeight);
	if (anchorToCentre) {
		this.topLeftCoord.Move(addToX * -0.5, addToY * -0.5);
	}
}
function _Rectangle_Move(x, y) {
	this.topLeftCoord.Move(x, y);
}

//
// ELEMENT RECTANGLE FUNCTIONS
//
function getRect(e) {
	return new Rectangle(e.offsetLeft + (USER_AGENT_IE ? e.offsetParent.offsetLeft : 0),
						 e.offsetTop + (USER_AGENT_IE ? e.offsetParent.offsetTop : 0),
						 e.offsetWidth,
						 e.offsetHeight);
}
function setPosXY(e, x, y) {
	e.style.left = x;
	e.style.top = y;
}
function setPos(e, pos) {
	setPosXY(e, pos.x, pos.y);
}
function setRect(e, rect) {
	setPos(e, rect.topLeftCoord);
	e.style.width = rect.WidthPx();
	e.style.height = rect.HeightPx();
}
function setWidth(e, rect) {
	e.style.width = rect.WidthPx();
}


//
// TIMERS
//
var timerArrayIDs = new Array;
var timerArrayCallbacks = new Array;
var timerArrayCounters = new Array;
function startTimer(count, millisecondDelay, callback) {
	// Create a unique ID for this timer
	var id = "" + Math.random();
	timerArrayCallbacks[id] = callback;
	timerArrayCounters[id] = count;
	timerArrayIDs[id] = setInterval("timerTick("+id+")", millisecondDelay);
}
function cancelTimer(id) {
	if (timerArrayIDs[id]) {
		clearInterval(timerArrayIDs[id]);
	}
}
function timerTick(id) {
	if ((timerArrayCounters[id])-- == 0) {
		clearInterval(timerArrayIDs[id]);
	}
	else {
		// Call the callback, passing the current counter value
		timerArrayCallbacks[id](timerArrayCounters[id]);
	}
}


//
// STRING FUNCTIONS
//
function unescapeHTML(html) {
	var div = document.createElement('div');
    div.innerHTML = stripTags(html);
    return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
}
function stripTags(html) {
   return html.replace(/<\/?[^>]+>/gi, '');
}
