addEvent(window, 'load', function() {
    var inputs = document.getElementsByTagName('input');
    var input;
    for (var i = 0; (input = inputs[i]); ++i) {
    	if (input.type != 'submit' && input.type != 'button' && input.type != 'checkbox' && input.type != 'radio' && input.type != 'radiobutton') {
	      addEvent(input, 'focus', oninputfocus);
	      addEvent(input, 'blur', oninputblur);
	    }
    }
    var textareas = document.getElementsByTagName('textarea');
    var textarea;
    for (var i = 0; (textarea = textareas[i]); ++i) {
	    addEvent(textarea, 'focus', oninputfocus);
	    addEvent(textarea, 'blur', oninputblur);
    }
		addButtonStates();
});

function oninputfocus(e) {
		source = getSource(e);
		if(source.type!='checkbox' && source.type!='radiobutton') {
			if(source.type=='text' || source.type=="password") {
				addClassName(source, 'text-focus');
			} else {
				addClassName(source, 'focus');
			}
		}
}

function oninputblur(e) {
		source = getSource(e);
		if(source.type=='text') {
			removeClassName(source, 'text-focus');
		} else {
			removeClassName(source, 'focus');
		}
        source.blur();
}

function getSource(e) {
	  /* Cookie-cutter code to find the source of the event */
    if (typeof e == 'undefined') {
        return false;
		//var e = window.event;
    }
    var source;
    if (typeof e.target != 'undefined') {
        source = e.target;
    } else if (typeof e.srcElement != 'undefined') {
        source = e.srcElement;
    } else {
        return;
    }
		return source;
}

function addButtonStates() {
	//var _buttons = getElementsByClass('submit', document, 'INPUT');
	
	
	
	
	var _inputs = document.getElementsByTagName('INPUT');
	for(var _i=0; _i < _inputs.length; _i++) {
		var _input = _inputs[_i];
		if((_input.type=='submit' || _input.type=='button') && checkClassName(_input, 'btn')) {
		
			var _button = _input;
			addEvent(_button, 'mouseover', function(e) {
				_button = getSource(e);
				addClassName(_button, 'hover');
        if(checkClassName(_button, 'important-xs')) addClassName(_button, 'important-xs-hover');
        if(checkClassName(_button, 'important-xl')) addClassName(_button, 'important-xl-hover');
        if(checkClassName(_button, 'important-xxl')) addClassName(_button, 'important-xxl-hover');
        if(checkClassName(_button, 'important')) addClassName(_button, 'important-hover');
        if(checkClassName(_button, 'xs')) addClassName(_button, 'xs-hover');
        if(checkClassName(_button, 'xl')) addClassName(_button, 'xl-hover');
        if(checkClassName(_button, 'xxl')) addClassName(_button, 'xxl-hover');
        if(checkClassName(_button, 'n76')) addClassName(_button, 'n76-hover');
				if(_button.type!= 'text') addClassName(_button, 'btn-hover'); // type check for IE6, somehow event fires when dragging button over text input
			});
			addEvent(_button, 'mouseout', function(e) {
				_button = getSource(e);
				removeClassName(_button, 'hover');
        removeClassName(_button, 'important-xs-hover');
        removeClassName(_button, 'important-xl-hover');
        removeClassName(_button, 'important-xxl-hover');
        removeClassName(_button, 'important-hover');
        removeClassName(_button, 'xs-hover');
        removeClassName(_button, 'xl-hover');
        removeClassName(_button, 'xxl-hover');
        removeClassName(_button, 'n76-hover');
        removeClassName(_button, 'btn-hover');
			});
			addEvent(_button, 'focus', function(e) {
				_button = getSource(e);
				addClassName(_button, 'focus');
        if(checkClassName(_button, 'important-xs')) addClassName(_button, 'important-xs-focus');
        if(checkClassName(_button, 'important-xl')) addClassName(_button, 'important-xl-focus');
        if(checkClassName(_button, 'important-xxl')) addClassName(_button, 'important-xxl-focus');
        if(checkClassName(_button, 'important')) addClassName(_button, 'important-focus');
			});
			addEvent(_button, 'blur', function(e) {
				_button = getSource(e);
				removeClassName(_button, 'focus');
        removeClassName(_button, 'important-xs-focus');
        removeClassName(_button, 'important-xl-focus');
        removeClassName(_button, 'important-xxl-focus');
        removeClassName(_button, 'important-focus');
        removeClassName(_button, 'btn-hover');
			});
		}
	}
}

function addEvent(obj, evType, fn){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, true);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

/* grab Elements from the DOM by className */
function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	var i,j;
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function toggleClassName(_node, _className) {
    if(_node.className.indexOf(_className)!=-1) removeClassName(_node, _className); else addClassName(_node, _className);
}

function hasClassName(_node, _className) {
    return _node.className.indexOf(_className)!=-1;
}

