/**************************************************************************/
/**************************************************************************/
/* CSS UTILITIES */
// Copyright 2007-2008 Collaborative Bike Map Project
/**************************************************************************/
/**************************************************************************/

/************************************
// Set CSS style value
************************************/
function setStyle (objid, style, value) {
	var e = document.getElementById(objid);
	if (e != null)
		e.style[style]= value;
}

/************************************
// Set style by object
************************************/
function setStyleByObject (obj, style, value) {
	obj.style[style]= value;
}

/*****************************************************
// Get CSS style value -- See http://www.quirksmode.org/dom/getstyles.html.
*****************************************************/
function getStyle (objid, style) {
	var s;
	var obj = document.getElementById (objid);
	if (obj.currentStyle)
		s = obj.currentStyle[style];
	else if (window.getComputedStyle)
		s = document.defaultView.getComputedStyle(obj,null).getPropertyValue(style);
	return s;
/*	var s = document.getElementById(objid).currentStyle[style]
	// dumpText ("Text = " + s);
	return (s); */
} 

/**************************************************************************
// Determine if element is member of a class.
// Inputs: elmt - Element (string id or object)
//         c - Class name (string)
// Return: true if element elmt is a member of the class c; false otherwise
***************************************************************************/
function hasClass (elmt, c) {
    if (typeof elmt == "string") elmt = document.getElementById (elmt);   // get element id

    // Before doing a regexp search, optimize for a couple of common cases.
    var classes = elmt.className;
    if (!classes) return false;    // Not a member of any classes
    if (classes == c) return true; // Member of just this one class

    // Otherwise, use a regular expression to search for c as a word by itself
    // \b in a regular expression requires a match at a word boundary.
    return elmt.className.search("\\b" + c + "\\b") != -1;
};

/**************************************************************************
// Add class c to the className of element elmt if it is not already there.
// Inputs: elmt - Element (string id or object)
//         c - Class name (string)
***************************************************************************/
function addClass (elmt, c) {
    if (typeof elmt == "string") elmt = document.getElementById (elmt);   // get element id
    if (this.hasClass(elmt, c)) return; // If already a member, do nothing
    if (elmt.className) c = " " + c;  // Whitespace separator, if needed
    elmt.className += c;              // Append the new class to the end
};

/**************************************************************************
// Remove all occurrences (if any) of class c from the className of element elmt
// Inputs: elmt - Element (string id or object)
//         c - Class name (string)
***************************************************************************/
function removeClass (elmt, c) {
    if (typeof elmt == "string") elmt = document.getElementById (elmt);   // get element id
    // Search the className for all occurrences of c and replace with "".
    // \s* matches any number of whitespace characters.
    // "g" makes the regular expression match any number of occurrences
    elmt.className = elmt.className.replace (new RegExp ("\\b"+ c+"\\b\\s*", "g"), "");
};
