/*
	### MINIKIT ###
	MochiKit is so big.
	I only need a few pieces.
*/

function createEl(tagName, className, attrs, kid){
	var el = document.createElement(tagName.toUpperCase());
	if(className){
		el.className = className;
	}
	if(attrs){
		for(attrName in attrs){
			el.setAttribute(attrName, attrs[attrName]);
		}
	}
	if(kid){
		if(typeof(kid) == "string")
			kid = document.createTextNode(kid);
		el.appendChild(kid);
	}
	return el;
}

function getElementsByTagAndClassName(tagName, className, /* optional */parent) {
    if (typeof(tagName) == 'undefined' || tagName == null) {
        tagName = '*';
    }
    if (typeof(parent) == 'undefined' || parent == null) {
        parent = document;
    }
    parent = self.getElement(parent);
    var children = parent.getElementsByTagName(tagName) || document.all;
    if (typeof(className) == 'undefined' || className == null) {
        return children;
    }

    var elements = [];
    for (var i = 0; i < children.length; i++) {
        var child = children[i];
        var classNames = child.className.split(' ');
        for (var j = 0; j < classNames.length; j++) {
            if (classNames[j] == className) {
                elements.push(child);
                break;
            }
        }
    }
    return elements;
}

function computedStyle(htmlElement, cssProperty, mozillaEquivalentCSS) {
    if (arguments.length == 2) {
        mozillaEquivalentCSS = cssProperty;
    } 
    var el = getElement(htmlElement);
    if (!el || el == document) {
        return undefined;
    }
    if (el.currentStyle) {
        return el.currentStyle[cssProperty];
    }
    if (typeof(document.defaultView) == 'undefined') {
        return undefined;
    }
    if (document.defaultView == null) {
        return undefined;
    }
    var style = document.defaultView.getComputedStyle(el, null);
    if (typeof(style) == "undefined" || style == null) {
        return undefined;
    }
    return style.getPropertyValue(mozillaEquivalentCSS);
}

function getElement(id) {
	return ((typeof(id) == "string") ? document.getElementById(id) : id);
}

function getEvent() { 
        var list = []; 
        /* 
        this extends the array instance to allow 
        for string comparison of functions 
        */ 
        list.hasFunction = function(val) { 
                for (var i = 0; i < this.length; i++) { 
                        if(this[i].toString() == val.toString()) return true; 
                } 
                return false; 
        } 

        /* 
        this is the actual function that is called when the event is fired. 
        if any of the listeners return false, then false is returned, 
        otherwise true 
        */ 
        var result = function(event) { 
                var finalResult = true; 
                for(var i = 0; i != list.length; i++) { 
                        var evtResult = list[i](event); 
                        if(evtResult == false) finalResult = false; 
                } 
                return finalResult; 
        } 

        /* 
        this is the function on the event that adds a listener 
        */ 
        result.addListener = function(f) { 
                if(f == null) return; 
                if(list.hasFunction(f)) return; 
                list.push(f); 
        } 

        /* 
        this is a debug function - feel free to remove 
        usage example: window.onload.list(); 
        */ 
        result.list = function() { 
                var log = ""; 
                for(var i = list.length-1; i >= 0; i--) { 
                        log += "<pre>"+list[i]+"</pre><hr/>"; 
                } 
                var wnd = window.open("", "Event dump"); 
                wnd.document.write(log); 
        } 

        /* 
        this is a semaphore to ensure that we play nice with other code 
        */ 
        result.__EVT_LIST = true; 

        return result; 

}

function stopEvent(e){
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function addEvent(obj, evt, func) { 
	var oldFunc = obj[evt]; 
	if (typeof oldFunc != 'function') { 
		obj[evt] = getEvent(); 
		obj[evt].addListener(func); 
	} else { 
		if(oldFunc.__EVT_LIST) { 
			obj[evt].addListener(func); 
		} else { 
			obj[evt] = getEvent(); 
			obj[evt].addListener(oldFunc); 
			obj[evt].addListener(func); 
		} 
	} 

} 

function addLoadEvent(func) { 
	addEvent(window, "onload", func); 
}

function getTarget(e) {
    var targ;
    if (e && e.target)
      targ = e.target;
    if (window.event && window.event.srcElement)
      targ = window.event.srcElement;
    if (!targ)
      return;
    if (targ.nodeType == 3) {
      targ = targ.parentNode; // Fix for Safari
    }
	return targ;
}

//css manipulation
function classSwap(el, c1, c2){
	if(classCheck(el,c1)){
		el.className=el.className.replace(c1,c2);
	}else{
		classAdd(el, c1);
	}
}

function classAdd(el, c1){
	if(!classCheck(el,c1)){el.className+=el.className?' '+c1:c1;}
}

function classRemove(el, c1){
    var rep=el.className.match(' '+c1)?' '+c1:c1;
    el.className=el.className.replace(rep,'');	
}

function classCheck(el, c1){
    return new RegExp('\\b'+c1+'\\b').test(el.className);
}

//cookies
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


var notWhitespace = /\S/;
function cleanWhitespace(node) {
	for (var x = 0; x < node.childNodes.length; x++) {
		var childNode = node.childNodes[x]
		if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
			// that is, if it a whitespace text node
			node.removeChild(node.childNodes[x])
			x--
		}
		if (childNode.nodeType == 1) {
			// elements can have text child nodes of their own
			cleanWhitespace(childNode)
		}
	}
}

