/**
 * Util js object
 * 
 * @version		$Id: util.js 1330 2009-03-06 10:54:33Z joeprongen $
 */
var RCSF_Base_Util =
{
	/*
	 * Swaps all the elements of that classname to another one
	 * 
	 * @param	string	oldClassname	old classname	
	 * @param	string	newClassname	new classname
	 * @param	object	oOptions	
	 * 				string	sElementId	optional if only 1 el is supposed to be switched
	 * 				boolean	bReplace	if you want to replace the full classname
	 * TODO: make more use of Prototype?
	 */
	SwapClassName : function (oldClassname, newClassname, oOptions)
	{
		var divs = document.getElementsByClassName(oldClassname)
	
		if(typeof(oOptions) != 'undefined')
		{
			if(oOptions.sElementId)
			{
				// Replaces the full clanname into the new class
				if(oOptions.bReplace == true)
				{
					$(oOptions.sElementId).className = newClassname;
				}
				else
				{
					// get old full classname and then only replace that part
					s_class = $(oOptions.sElementId).className;
					$(oOptions.sElementId).className = s_class.replace(oldClassname,newClassname);
				}
			}		
		}
		else
		{
			for(i=0; i <divs.length;i++)
			{
				divs[i].className = newClassname;
			}
		}
	}
	
	/**
	 * Get the height of a window
	 * 
	 * @author Joep Rongen <joep@rhinocreations.com>
	 * 
	 * TODO: make more use of prototype
	 */	
	,GetWindowHeight : function ()
	{
		var windowHeight = 0;
		if (typeof(window.innerHeight) == 'number') 
		{
			windowHeight = window.innerHeight;
		}
		else 
		{
			if (document.documentElement && document.documentElement.clientHeight) 
			{
				windowHeight = document.documentElement.clientHeight;
			}
			else 
			{
				if (document.body && document.body.clientHeight) 
				{
					windowHeight = document.body.clientHeight;
				}
			}
		}
		return windowHeight;
	}
	
	
	/**
	 * @author Joep Rongen <joep@rhinocreations.com>
	 *
	 * @return	string	curTime	Current time in hh:mm:ss notation  
	 */
	,GetTime : function ()
	{
		var curDateTime = new Date()
	  	var curHour = curDateTime.getHours()
	  	var curMin = curDateTime.getMinutes()
	  	var curSec = curDateTime.getSeconds()
	  	var curTime = 
	    	((curHour < 10) ? "0" : "") + curHour + ":" 
	    	+ ((curMin < 10) ? "0" : "") + curMin + ":" 
	    	+ ((curSec < 10) ? "0" : "") + curSec 
	  	return curTime;
	}
	
	
	/**
   	 * Removes an element and its child elements
   	 * 
   	 * 
   	 * @param	Object	oEl	Element to remove
   	 */
   	,RemoveElement : function (oEl)
	{
		var a_child_elements = oEl.childElements();
		
		// Remove childs
		for (var i = a_child_elements.length - 1; i > 0; i--)
		{
			//alert(a_child_elements[i].name + ' : ' + a_child_elements[i].id + ' : ' + a_child_elements[i].className);
			RCSF_Base_Util.RemoveElement(a_child_elements[i]);
		}
				
		// Remove element itself
		Element.remove(oEl);
	}	

	
	
	/**
	 * Stores value in object in object recursively
	 * 
	 * @param	Object	oData	Object to store value in
	 * @param	Array	aKey	key(s) location where the value should be stored
	 * @param	mixed	mValue	Value
	 */
	,SetObjectElement : function (oData, aKey, mValue)
	{
		var s_key = aKey.shift();
		if (aKey.length == 0) oData[s_key] = mValue;
		else
		{	
			if (typeof(oData[s_key]) == 'undefined') oData[s_key] = {};
			RCSF_Base_Util.SetObjectElement(oData[s_key], aKey, mValue);
		}
	}	
	
	
	/**
	 * Extracts numeric part of and element id
	 * 
	 * @param	mixed	m element or element id
	 * @return	string	numeric id
	 */
	,ExtractNumericId : function (m)
	{
		if (typeof(m) != 'string') m = m.id;
		return m.split(/[_-]/).last();
	}
	
	/**
	*	External funcition RIPPED from Highslide
	*/
	,getPageSize : function () 
	{
		var d = document, w = window, iebody = d.compatMode && d.compatMode != 'BackCompat' 
			? d.documentElement : d.body;	
		
		var b = d.body;
		var xScroll = (w.innerWidth && w.scrollMaxX) 
				? w.innerWidth + w.scrollMaxX : Math.max(b.scrollWidth, b.offsetWidth),
			yScroll = (w.innerHeight && window.scrollMaxY) 
				? w.innerHeight + w.scrollMaxY : Math.max(b.scrollHeight, b.offsetHeight),
			pageWidth = Prototype.Browser.IE ? iebody.scrollWidth :
				(d.documentElement.clientWidth || self.innerWidth),
	      	pageHeight = Prototype.Browser.IE ? Math.max(iebody.scrollHeight, iebody.clientHeight) : 
				(d.documentElement.clientHeight || self.innerHeight);
		
		var width = Prototype.Browser.IE ? iebody.clientWidth : 
				(d.documentElement.clientWidth || self.innerWidth),
			height = Prototype.Browser.IE ? iebody.clientHeight : self.innerHeight;
		
		return {
			pageWidth: Math.max(pageWidth, xScroll),
			pageHeight: Math.max(pageHeight, yScroll),
			width: width,
			height: height,		
			scrollLeft: Prototype.Browser.IE ? iebody.scrollLeft : pageXOffset,
			scrollTop: Prototype.Browser.IE ? iebody.scrollTop : pageYOffset
		}
	}
}

