﻿//
// Adds a CSS class.
//
function addClass(element, className) {
	if(!hasClass(element, className)) {
		if(element.className) {
			element.className += " " + className;
		} else {
			element.className = className;
		}
	}
}

//
// Removes a CSS class.
//
function removeClass(element, className) {
	if(element.className) {
		var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
		element.className = element.className.replace(regexp, "$2");	
	}
}

//
// Determines if the element has a CSS class with the given name.
//
function hasClass(element, className) {
	var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	return regexp.test(element.className);
}

//
// Finds elements by class name.
//
// tagName - Optional filter.
// startEleemnt - Optional start element.
//
function getElementsByClassName(className, tagName, startElement) {
	if(! tagName) {
		tagName = "*";
	}
	
	if(! startElement) {
		startElement = document.body;
	}
	
	var elements = startElement.getElementsByTagName(tagName);
	var matchingElements = [];

	for(var i = 0; i < elements.length; i++) {
		var element = elements[i];
		
		if(hasClass(element, className)) {
			matchingElements.push(element);
		}
	}
	
	return matchingElements;
}

//
// Gets the top position of the element.
//
function getLeft(element) {
    var curtop = 0;
    
    if (element.offsetParent)
    {
        while (element.offsetParent)
        {
            curtop += element.offsetTop
            element = element.offsetParent;
        }
    } else if (element.y) {
        curtop += element.y;
    }

    return curtop;
}

//
// Gets the left position of the element.
//
function getLeft(element) {
    var curleft = 0;

    if (element.offsetParent)
    {
        while (element.offsetParent)
        {
            curleft += element.offsetLeft
            element = element.offsetParent;
        }
    } else if (obj.x) {
        curleft += element.x;
    }

    return curleft;
}

//
// Represents a set of "x" and "y" coordinates.
//
function Point(x, y) {
	this.X = (x ? x : 0);
	this.Y = (y ? y : 0);
}

Point.prototype = {
	X : 0,
	Y : 0,
	
	//
	// Converts the point into a string representation.
	//
	toString : function() {
		return this.X.toString() + ", " + this.Y.toString();
	}
}

//
// Represents a width and a height.
//
function Size(width, height) {
	this.Width = (width ? width : 0);
	this.Height = (height ? height : 0);
}

Size.prototype = {
	Width : 0,
	Height : 0,
	
	//
	// Converts the size into a string representation.
	//
	toString : function() {
		return this.Width.toString() + ", " + this.Height.toString();
	}
}