/* 
	Tecknosfera DBNET S.L.
	Element: LIB DHTML
	Programed by: francisco_javier_martinez AT hotmail DOT com
	To : Tecknosfera DBNET S.L.
*/

// Class to create Dynamic elements

function LIB_DHTML_object ()
{
	// Constructor
	this.LIB_DHTML_object = function ()
	{
	
	}

	// Function to set Dynamic attributes on referer Object
	this.setDynamicAttributes = function (objReferer, attributes)
	{
		if (attributes != "" && attributes != null)
		{
			for (attributeCont=0;attributeCont<attributes.length;attributeCont++)
			{
				objReferer.setAttribute(attributes[attributeCont].attributeName, attributes[attributeCont].attributeValue);
			}
		}
		return (objReferer);
	}
	
	// Function to get the Class Object of Referer Object
	/*
		Tested on	IE 5.01 SP2 ---------------> OK
					IE 5.5 SP2 ----------------> OK
					IE 6.0 SP2 ----------------> OK
					Firefox 1 & Firefox 2 -----> OK
					Netscape 8 ----------------> OK
					Opera 8 -------------------> NO    Opera 8 can't acces to document.styleSheets    Documentation ---> http://www.quirksmode.org/dom/w3c_css.html
					Opera 9 -------------------> OK
					Safari --------------------> OK
	*/
	this.foundClassElement = function (objReferer)
	{
		var classFounded = false;
		var contStyleSheets = 0;
		var theClass = null;
		while (classFounded == false && contStyleSheets<document.styleSheets.length)
		{
			contStyleSheets++;
			var contCssRules = 0;
			var theRules = new Array();
			if (document.styleSheets[contStyleSheets].cssRules)
			{
				theRules = document.styleSheets[contStyleSheets].cssRules
			}
			else
			{
				if (document.styleSheets[contStyleSheets].rules)
				{
					theRules = document.styleSheets[contStyleSheets].rules
				}
			}
			while (classFounded == false && contCssRules < theRules.length)
			{
				if (theRules[contCssRules].selectorText.substr(1).toLowerCase() == objReferer.className.toLowerCase())
				{
					classFounded = true;
					theClass = theRules[contCssRules];
				}
				contCssRules++;
			}
		}
		return(theClass);
	}
	
	// function to find a class using the name
	this.foundClass = function (classNameReferer)
	{
		var classFounded = false;
		var contStyleSheets = 0;
		var theClass = null;
		while (classFounded == false && contStyleSheets<document.styleSheets.length)
		{
			contStyleSheets++;
			var contCssRules = 0;
			var theRules = new Array();
			if (document.styleSheets[contStyleSheets].cssRules)
			{
				theRules = document.styleSheets[contStyleSheets].cssRules
			}
			else
			{
				if (document.styleSheets[contStyleSheets].rules)
				{
					theRules = document.styleSheets[contStyleSheets].rules
				}
			}
			while (classFounded == false && contCssRules < theRules.length)
			{
				if (theRules[contCssRules].selectorText.substr(1).toLowerCase() == classNameReferer.toLowerCase())
				{
					classFounded = true;
					theClass = theRules[contCssRules];
				}
				contCssRules++;
			}
		}
		return(theClass);
	}
		
	// Function to create a LINK element
	this.createDynamicLink = function(container, href, linkText, className)
	{
		// Set on the Navigator TD the section to Load
		var tdNavigator = document.getElementById(container);
		var navigatorLink = document.createElement('a');
		navigatorLink.setAttribute('href',href)
		// The CLASS STYLE Atributhe is diferent to MIE and FIREFOX
		if (LIB_navigator.getName() == "Microsoft Internet Explorer")
		{
			navigatorLink.setAttribute('className',className);
		}
		else
		{
			navigatorLink.setAttribute('class',className);
		}
		
		navigatorLink.appendChild(document.createTextNode(linkText));
		return(navigatorLink);
	}
	
	/* ********************************************************************************************************************************************************* */
	// ******************* Include Functions
	/* ********************************************************************************************************************************************************* */
	
	// Function to create a Script include on current HTML page
	this.include = function (includeURL)
	{
		var theInclude = getInclude(includeURL)
		var head = document.getElementsByTagName("head").item(0);
		head.appendChild(theInclude);
	}
	
	// Function to create a Script include on current HTML page. Verify if exists or not exist the script
	this.include_once = function (scriptURL)
	{
		var head = document.getElementsByTagName("head").item(0);
		var contHeadNodes;
			alert (headNode.nodeName.toLowerCase() + "== script && " + headNode.getAttribute("src") + " == " + scriptURL)
		for(contHeadNodes=0; contHeadNodes<head.childNodes.length; contHeadNodes++)
		{
			var headNode = head.childNodes[contHeadNodes];
			// If the actual node is an script node and the name is the same of my Script to include.. SKIP -> return(false)
			if(headNode.nodeName.toLowerCase() == "script" && headNode.getAttribute("src") == scriptURL)
			{
				return (false);
			}
		}
		// If not skeeped, include the file
		LIB_DHTML.include(scriptURL);
	}
	
	// Function to create the correspondent include
	function getInclude (theIncludeURL)
	{
		var theInclude = null;
		var typeOfInclude = theIncludeURL.split(".")[theIncludeURL.split(".").length - 1];
		switch (typeOfInclude)
		{
			case "js":
				theInclude = document.createElement("script");
				theInclude.setAttribute("language","javascript");
				theInclude.setAttribute("type","text/javascript");
				theInclude.setAttribute("src",theIncludeURL);
			break;
			
			case "css":
				theInclude = document.createElement("link");
				theInclude.setAttribute("rel","stylesheet");
				theInclude.setAttribute("type","text/css");
				theInclude.setAttribute("href",theIncludeURL);
			break;
		}
		return (theInclude);
	}
}

var LIB_DHTML = new LIB_DHTML_object();

