/*
 *
 */
function Browser()
{		
	this.userAgent = "" ;
	this.browser = "" ;
	this.versionText = "" ;
	this.platform = "" ;
	this.version = this.mayorVersion = null ;
	this.netscape = false ;
	this.explorer = false ;
	this.konqueror = false ;
	this.opera = false ;
	this.safary = false ;
	this.conqueror = false ;
	this.windows = this.linux = this.mac = false ;
	
	var i, iPos, userAgentLowerCase ;
	
	this.userAgent = navigator.userAgent ;
	
	// Browser

	var browsers = [["msie", "explorer"],
						 ["safari", "safari"],
						 ["opera", "opera"],
						 ["netscape", "netscape"], 
						 ["mozilla", "netscape"],
						 ["omniweb", null],
						 ["webtv", null],
						 ["icab", null],
						 ["konqueror", "konqueror"],
						 ["compatible", "netscape"],
						 ] ;
	
	userAgentLowerCase = this.userAgent.toLowerCase();
	
	for (i=0; i<browsers.length; i++)
	{		
		iPos = userAgentLowerCase.indexOf(browsers[i][0]) ;
		if (iPos >= 0)
		{
			this.browser = browsers[i][1] ;
			if (browsers[i][1] != null)
				eval("this." + browsers[i][1] + " = true; ");
			break ;
		}
	}
	
	if (this.netscape)
		this.browserName = "Netscape Navigator" ;
	
	// Version

	this.versionText = navigator.appVersion;

	if (this.explorer) 
	{
		iPos = userAgentLowerCase.indexOf("msie");
		if (iPos > 0)
		{
			this.version = parseFloat(userAgentLowerCase.substring(iPos+"msie ".length));
		}
	}
	else if (this.opera)
	{
		iPos = userAgentLowerCase.indexOf("opera");
		if (iPos > 0)
			this.version = parseFloat(userAgentLowerCase.substring(iPos+"opera ".length));
	}
	else
	{ 
		this.version = parseFloat(this.versionText);
	}
	
	this.mayorVersion = Math.floor(this.version);	
	this.minorVersion = 0 ;

	var sTmp = "" + this.version;
	iPos = sTmp.indexOf(".");
	if ((iPos > 0) && (iPos < sTmp.length-1))
		this.minorVersion = parseInt(sTmp.substring(iPos+1));
	
	// Operating system
	
	if (this.konqueror)
	{
		this.platform = "Linux" ;
		this.linux = true ;
	}
	else
	{					
		var os = [["win32", "windows"],
					 ["windows", "windows"],
					 ["linux", "linux"],
					 ["mac", "mac"]
					 ["x11", "linux"],
					 ["win", "windows"]] ;
				
		iPos = -1 ;		
		for (i=0; ((i<os.length) && (iPos < 0)); i++)
		{			
			iPos = userAgentLowerCase.indexOf(os[i][0]) ;
			if (iPos >= 0)
			{
				this.platform = os[i][1];
				if (os[i][1] != null)
					eval("this." + os[i][1] + " = true; ");
			}
		}
	}
	
	
	//------------------------------------------------------------------
	this.toString = function() { return "browser: " + this.browser + " v" + this.mayorVersion + "." + this.minorVersion + 
												", platform: " + this.platform ; } ;
					
								
	//-------------------------------------------------------
	this.getWindowSize = function() 
	{ 
		var w, h ;
		
		w = h = 0 ;
	
		if (self.innerHeight) // all except Explorer
		{
			w = self.innerWidth;
			h = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}

		/*
		if(!isNaN(parseInt(window.innerWidth)))
		{
			//Non-IE
			w = window.innerWidth;
			h = window.innerHeight;
		} 
		else if (document.documentElement &&
		   	  (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			//IE 6+ in 'standards compliant mode'
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} 
		else if(document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
		{
			//IE 4 compatible
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
		*/
	
		return new CSS_Coordinates(w,h) ; 
	} 
	
	//-------------------------------------------------------
	this.getDocumentSize = function() 
	{ 
		var w, h ;
		
		w = h = 0 ;
		
		var test1 = document.body.scrollHeight;
		var test2 = document.body.offsetHeight
		
		if (test1 > test2) // all but Explorer Mac
		{
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
		else // Explorer Mac;
		     //would also work in Explorer 6 Strict, Mozilla and Safari
		{
			w = document.body.offsetWidth;
			h = document.body.offsetHeight;
		}

		/*
		if (document.documentElement &&
		   	  (document.documentElement.width || document.documentElement.height))
		{
			w = document.documentElement.width;
			h = document.documentElement.height;
		} 
		else if(document.body && ( document.body.offsetWidth || document.body.offsetHeight ) ) 
		{
			w = document.body.offsetWidth;
			h = document.body.offsetHeight;
		}
		*/
		
		return new CSS_Coordinates(w,h) ; 
	} 
	
	//---------------------------------------------------------
	
	this.getScrollPosition = function() 
	{ 
		var x, y ;
		x = y = 0 ;
	
		if (self.pageYOffset) // all except Explorer
		{
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}

		/*
		if(parseInt(window.pageYOffset) != NaN) 
		{
		 //Netscape compliant
		 x = window.pageXOffset;
		 y = window.pageYOffset;
		} 
		else if ((document.body && (document.body.scrollLeft || document.body.scrollTop)))
		{
			//DOM compliant
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		} 
		else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
		{
			//IE6 standards compliant mode
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		*/
		
		return new CSS_Coordinates(x, y); 
	}
}

var BROWSER = new Browser();

/*
 *
 */
function CSS_getElementById(elemID)
{
	var elem ;
	
	elem = null ;
	if (document.getElementById)
	{
		elem = document.getElementById(elemID);
		if (!elem) 
			elem = null ;
	}
	else if (document.all)
	{
		elem = document.all[elemID];
		if (!elem) 
			elem = null ;
	}
			
	return elem;
}

/*
 *
 */
function CSS_getElementByParentAndId(parentNode, elemId)
{
	if (parentNode.id = elemId)
		return parentNode;
	
	var i, elem;
	
	
	if (parentNode.childNodes)
	{
		for (i=0; i<parentNode.childNodes.length; i++)
		{
			elem = CSS_getElementByParentAndId(parentNode.childNodes[i], elemId);
			if (elem != null)
				return elem;
		}
	}
		
	return null;
}

/*
 *
 */
function CSS_getChildsByTag(parentElem, tag)
{
	var elems = new Array();
	if ((parentElem) && (parentElem.childNodes))
	{
		var i;
		for (i=0; i<parentElem.childNodes.length; i++)
			if (parentElem.childNodes[i].nodeName.toLowerCase() == tag.toLowerCase())
				elems[elems.length] = parentElem.childNodes[i];
	}
	return elems;
}

/*
 *
 */
function CSS_getElementsByTag(parentElem, tag)
{
	var elems ;
	
	elems = null ;
	if (parentElem == null)
	{
		if (document.getElementsByTagName)
			elems = document.getElementsByTagName(tag);
	}
	else 
	{
		if (parentElem.getElementsByTagName)
			elems = parentElem.getElementsByTagName(tag);
	}	
	if (!elems) 
		elems = null ;
			
	return elems;
}

/*
 *
 */
function CSS_getStyle(elem)
{
	if (elem.style) 
		return elem.style;
	else if (elem.currentStyle) 
		return elem.currentStyle; 
	else if (document.defaultView && document.defaultView.getComputedStyle) 
		return document.defaultView.getComputedStyle(elem, null);
	else 
		return null;
}

/*
 *
 */
function CSS_Coordinates(x,y)
{
	this.x = x ;
	this.y = y ;
	this.toString = function() { return x + "," + y; };
}

/*
 *
 */

// SIEMPRE DEVUELVE LAS COORDENADAS ABSOLUTAS. NO UTILIZA ESTILOS

function CSS_getElementCoordinates(elem, bRelative)
{		
	var x = 0, y = 0, elemTmp;
	
	elemTmp = elem ;
	while (elemTmp)
	{
		if (elemTmp.x)
		{
			x += elemTmp.x;
			y += elemTmp.y;
		}
		else if (elemTmp.offsetLeft)
		{
			x += elemTmp.offsetLeft;
			y += elemTmp.offsetTop;
		}
		
		elemTmp = elemTmp.offsetParent ;
	}
	
	if (bRelative)
	{
		var parentCoordinates = CSS_getElementCoordinates(elem.parentNode, false);
		x -= parentCoordinates.x ;
		y -= parentCoordinates.y ;
	}
	
	return new CSS_Coordinates(x,y);
}

/*
 *
 */
function CSS_getElementSize(elem)
{		
	var x = 0, y = 0;
	
	if (elem.offsetWidth)
	{
		x += parseInt(elem.offsetWidth);
		y += parseInt(elem.offsetHeight);
	}
	else if (elem.width)
	{
		x += parseInt(elem.width);
		y += parseInt(elem.height);
	}
	
	return new CSS_Coordinates(x,y);
}

/*
 *
 */
function CSS_setElementSize(elem, size)
{			
	if (elem.offsetWidth)
	{
		elem.offsetWidth = size.x ;
		elem.offsetHeight = size.y ;
	}
	else if (elem.width)
	{
		elem.width = size.x ;
		elem.height = size.y ;
	}
}

/*
 *
 */
function CSS_moveElementTo(elem, x, y, speed)
{
	CSS_moveElement(elem, x, y, null, null, speed);
}

/*
 *
 */
function CSS_moveElementBy(elem, dX, dY, speed)
{
	CSS_moveElement(elem, null, null, dX, dY, speed);
}

/*
 *
 */
function CSS_ElementOnMovement(elemID, x, y, dx, dy, speed, onEndFn) 
{
	this.elemID = elemID ;
	this.DAEMON_INTERVAL = 80 ; // ms
	this.onEndFn = onEndFn ;
	
	// COORDENATES
	
	var elem = CSS_getElementById(this.elemID);
		
	var pos = CSS_getElementCoordinates(elem, true);
	
	this.startingX = pos.x ;
	this.startingY = pos.y ;
	
	if ((x != null) && (y != null))
	{
		this.finalX = x ;
		this.finalY = y ;
		
		dx = x - this.startingX ;
		dy = y - this.startingY ;
	}
	else if ((dx != null) && (dy != null))
	{
		this.finalX = this.startingX + dx ;
		this.finalY = this.startingY + dy ;
	}
	else
	{
		this.finalX = this.startingX ;
		this.finalY = this.startingY ;
	}
	
	this.currentX = this.startingX ;
	this.currentY = this.startingY ;
	
	// SPEED
		
	var time ;
	
	// alert("indicated speed: " + speed + ", dx: " + dx + " dy: " + dy);
	speed = ((1.0 * speed * this.DAEMON_INTERVAL) / 1000.0);	// PIXELS TO MOVE EACH DAEMON CALL (FOR THE GREATEST AXIS)

	this.speedX = 0 ;
	this.speedY = 0 ;
	
	if (Math.abs(dx) >= Math.abs(dy))
	{
		this.speedX = speed ;
		if (dx < 0)
			this.speedX *= -1.0 ;

		// RECARCULATE Y SPEED
		
		if (dx != 0)
		{
			time = Math.abs(dx / speed) ;
			this.speedY = dy / time ;
		}
		else
			this.speedY = 0 ;
	}
	else
	{
		this.speedY = speed ;
		if (dy < 0)
			this.speedY *= -1.0 ;
				
		// RECALCULATE X SPEED
		
		if (dy != 0)
		{
			time = Math.abs(dy / speed) ;
			this.speedX = dx / time ;
		}
		else
			this.speedX = 0 ;
	}
	
	// alert("real speed: " + speed + " (" + this.speedX + " " + this.speedY + ")");
	// alert("starting and final: (" + this.startingX + "," + this.startingY + ") - (" + this.finalX + "," + this.finalY + ")");
	
	var thisObj = this ;
	
	//-------------------------
	this.move = function() 
	{
		var end = true ;

		// alert(thisObj.currentX + " " + thisObj.currentY + " " + thisObj.speedX + " " + thisObj.speedY);
					
		if (thisObj.speedX != 0)
		{
			if (thisObj.currentX < thisObj.finalX)
			{
				thisObj.currentX += thisObj.speedX ;
				if (thisObj.currentX >= thisObj.finalX)
					thisObj.currentX = thisObj.finalX ;
				else
					end = false ;
			}
			else
			{
				thisObj.currentX += thisObj.speedX ;
				if (thisObj.currentX <= thisObj.finalX)
					thisObj.currentX = thisObj.finalX ;
				else
					end = false ;
			}	
		}

		if (thisObj.speedY != 0)
		{
			if (thisObj.currentY < thisObj.finalY)
			{
				thisObj.currentY += thisObj.speedY ;
				if (thisObj.currentY >= thisObj.finalY)
					thisObj.currentY = thisObj.finalY ;
				else
					end = false ;
			}
			else
			{
				thisObj.currentY += thisObj.speedY ;
				if (thisObj.currentY <= thisObj.finalY)
					thisObj.currentY = thisObj.finalY ;
				else
					end = false ;
			}	
		}

		CSS_moveElement(CSS_getElementById(thisObj.elemID), parseInt(thisObj.currentX), parseInt(thisObj.currentY), null, null);
		
		if (end)
		{
			if (thisObj.onEndFn) setTimeout(thisObj.onEndFn, thisObj.DAEMON_INTERVAL);
		}
		else
			setTimeout(thisObj.move, thisObj.DAEMON_INTERVAL);
	}
}

/*
 *
 */
		
function CSS_moveElement(elem, x, y, dX, dY)
{
	if (!elem)
		return;
		
	var style = CSS_getStyle(elem);
	if (style)
	{
		if (BROWSER.explorer)
		{
			if (isNaN(x))
			{
				style.pixelTop += dY;
 				style.pixelLeft += dX;
 			}
 			else
 			{
				style.pixelTop = y;
 				style.pixelLeft = x;				
 			}
		}
		else
		{
			if (isNaN(x))
			{
				if (style.left == null) style.left = "0px";
				if (style.top == null) style.top = "0px";
					
				style.left = parseInt(style.left.replace("px", "")) + "px" + dX ;
				style.top = parseInt(style.top.replace("px", "")) + "px" + dY ;
			}
			else
			{
				style.left = x + "px" ;
				style.top = y + "px" ;
			}
		}	
	}
}

/*
 *
 */

function CSS_centerElement(elemID)
{
	var elem = CSS_getElementById(elemID);
	if (elem)
	{
		var w = BROWSER.getWindowSize();
		var s = BROWSER.getScrollPosition();
		var e = CSS_getElementSize(elem);
		
		CSS_moveElementTo(elem, ((w.x-e.x)/2)+s.x, ((w.y-e.y)/2)+s.y);
	}
}

/*
 *
 */
function CSS_getStyleProperty(elem, property) 
{
	if (elem.style[property]) 
	{
		return elem.style[property];
	} 
	else if (elem.currentStyle) 
	{
		return elem.currentStyle[property];
	} 
	else if (document.defaultView && document.defaultView.getComputedStyle) 
	{
		var style = document.defaultView.getComputedStyle(elem, null);
		return style.getPropertyValue(property)
	} 
	else 
	{
		return null;
	}
}

/*
 *
 */
function CSS_setStyleProperties(elem, properties) 
{
	var i ;
	
	if (elem.style)
		for (i=0; i<properties.length ; i++)
			try { elem.style[properties[i][0]] = properties[i][1]; } catch(ex) {} ;
}

/*
 *
 */
function CSS_generateID(prefix, sufix)
{
	var id ;

	do
	{
		id = prefix + Math.floor(Math.random()*10000000) + sufix ;
	}
	while (document.getElementById(id) != null);

	return id ;
}

/*
 *
 */
 
function CSS_toXML(node, deep)
{
	var sXML = "\n<" + node.nodeName ;
	
	var i;
	
	if (isNaN(parseInt(deep)))
		deep = 1;	
	
	var attributes = node.attributes ;
	if (attributes)
	{
		for (i=0; i<attributes.length; i++)
			sXML += " " + attributes[i].name + "='" + String_replaceSubstring(attributes[i].value, "'", "&quot;") + "'";
	}
	
	sXML += ">";
	
	if (deep < 32)
	{
		if (node.nodeType == XML_NODE_TYPE_ELEMENT)
		{
			var childs = node.childNodes ;
			if (childs != null)
			{
				if ((childs.length == 1) && (childs[0].nodeName == "#text"))
					sXML += childs[0].nodeValue;
				else if ((childs.length == 1) && (childs[0].nodeName == "#cdata-section"))
					sXML += childs[0].nodeValue;
				else
				{
					for (i=0; i<childs.length; i++)
						sXML += CSS_toXML(childs[i], deep+1);
				}	
			}
		}
		else
		{		
			sXML += node.nodeValue;
		}
	}
	
	sXML += "</" + node.nodeName + ">";
					
	return sXML;
}

/******************************************************************************
 *
 */
 
function CSS_removeChildNode(node, bCascade)
{
	var i;
	
	if ((bCascade) && (node.childNodes))
	{
		for (i=0; i<node.childNodes.length; i++)
			CSS_removeChildNode(node.childNodes[i], bCascade);
	}

	node.parentNode.removeChild(node, false);
}

/******************************************************************************
 *
 */
 
function CSS_renameAllChilds(elem, prefix)
{
	var i ;
	
	if (elem.childs)
	{
		for (i=0; i<elem.childs.length; i++)
		{
			elem.childs[i].id = prefix + elem.childs[i].id;
			elem.childs[i].name = prefix + elem.childs[i].name;
			CSS_renameAllChilds(elem.childs[i], prefix);
		}
	}
}
				
				