//Dikkat : Bu dosyada bir değişiklik yapılması halinde,
//lütfen en son versiyonunu ilgili kütüphane ana dizinine yapıştırınız.

/** DOM Utilities Module
 *	Requires : None
 *	Includes :
 *		make
 *		maker
 *		getElementsUltimate
 *		getElem
 *		computedStyle
 */

var DOMUtils = {};
 
//make : creates an element with specified attributes and children.
//attrs : object (attribute=>value)
//children : array if there are more than one, object if there is one, string if there is one and it is a text node
//children array can be passed as second parameter instead of attributes object, in that case, any third parameter is ignored
DOMUtils.make = function( tagName, attrs, children )
{
	if (arguments.length == 2 && (attrs instanceof Array || typeof attrs == "string")){
		childs = attrs;
		attrs = null;
	}
	
	var e = document.createElement(tagName);
	
	if (!attrs){
		for (var p in attrs) e.setAttribute(p, attrs[p]);
	}
	
	if (children != null){
		if (children instanceof Array){
			var child;
			for (var i = 0; i < children.length; i++){
				child = children[i];
				if (typeof child == "string") child = document.createTextNode(child);
				e.appendChild(child);
			}
		}else if (typeof children == "string"){
			e.appendChild(document.createTextNode(children));
		}else e.appendChild(children);
	}
	
	return e;
}

DOMUtils.maker = function( tagName )
{
	return function (attrs, children){
		if (attributes.length == 1) return make(tagName, attrs);
		else return make(tagName, attrs, children);
	};
}

DOMUtils.getElementsUltimate = function( someClass, tagName, container )
{
	if (!container) container = document;
	else if (typeof container == 'string') container = document.getElementById(container);
	if (!tagName)
	{
		if (document.getElementsByTagName("15-").length) tagName = "15-"; //For old browsers
		else if (document.getElementsByTagName("*").length) tagName = "*";
	}
	var all = container.getElementsByTagName(tagName);
	
	if ( !someClass ) return all;
	
	var items = [];
	for ( var i = 0; i < all.length; i++ ) if ( isMember( all[i], someClass ) ) items.push( all[i] );
	return items;
	
	function isMember( element, someClass )
	{
		var myClass = element.className;
		
		if (myClass == someClass) return true;
		
		var whitespace = /\s+/;
        if ( !whitespace.test( myClass ) ) return false;
		
        var c = myClass.split( whitespace );
		
        for( var i = 0; i < c.length; i++ ) {
            if ( c[i] == someClass ) return true;
        }
		
        return false;
	}
}

//getElem : if elem is an object returns elem itself, if it is a string return the element whose id is elem
//for any other type return false
DOMUtils.getElem = function( elem )
{
	if (typeof elem == "string") return document.getElementById(elem);
	else if (typeof elem == "object") return elem;
	else return false;
}

DOMUtils.computedStyle = function( elem, pseudoElem )
{
	if (!pseudoElem) pseudoElem = null;
	if (elem.currentStyle) return elem.currentStyle;
	else if (window.getComputedStyle) return getComputedStyle(elem, pseudoElem);
	else return false;
}