if (typeof document.getElementsByClassName != 'function') {
	document.getElementsByClassName = function(class_name) {
	    var docList = this.all || this.getElementsByTagName('*');
	    var matchArray = new Array();

	    var re = new RegExp("(?:^|\\s)"+class_name+"(?:\\s|$)");
	    for (var i = 0; i < docList.length; i++) {
	        if (re.test(docList[i].className) ) {
	            matchArray[matchArray.length] = docList[i];
	        }
	    }
	
		return matchArray;
	}
}

window.selectAll = function(el) {
    var element = (typeof el == "string") ? document.getElementById(el) : el;
      
    unselect();
    
    if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(element);
        window.getSelection().addRange(range);
    }
}

window.unselect = function() {if (document.selection) {document.selection.empty()} else if (window.getSelection) {window.getSelection().removeAllRanges()}}


window.addEvent = function (obj, evType, fn, useCapture) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.attachEvent) {
        return obj.attachEvent('on' + evType, fn);
    } else {
        return false;
    }
};

window.removeEvent = function (obj, evType, fn, useCapture) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.detachEvent) {
        return obj.detachEvent('on' + evType, fn);
    } else {
        return false;
    }
};

window.absolutePosition = function (element) {
    if (typeof element == 'string') { element = document.getElementById(element); }

    var left = 0, top = 0;


    if (!element.offsetParent) { return false; }

    do {
        left += element.offsetLeft;
        top += element.offsetTop;
    } while (element = element.offsetParent);

    return { left: left, top: top };
}

window.fadeTo = function(element, endOpacity, increment, delay, callback, supportIE, skipBadFadeIE) {
    if (typeof element == 'string') { element = document.getElementById('element'); }
    if (!element) { return false; }

    var started = true;

    if (!element.fadeInfo) { element.fadeInfo = {}; started = false }
    if (!element.fadeInfo.currentOpacity) { element.fadeInfo.currentOpacity = (element.style.opacity == '') ? 1 : parseFloat(element.style.opacity) };
    element.fadeInfo.supportIE = supportIE;
    element.fadeInfo.endOpacity = endOpacity;
    element.fadeInfo.increment = increment;
    element.fadeInfo.delay = delay;
    element.fadeInfo.callback = callback;

    if (skipBadFadeIE && !element.fadeInfo.supportIE && (navigator.appName == 'Microsoft Internet Explorer')) {
        element.style.opacity = endOpacity;
        element.style.visibility = (endOpacity == 0) ? 'hidden' : '';
        element.fadeInfo = null;
        if (typeof callback == 'function') { callback(); }
        return false;
    }

    if (!started) { fade(element) }

    function fade(element) {
        if (!element || !element.fadeInfo) { return false; }

        var absStep = Math.abs(element.fadeInfo.increment), currentOpacity = element.fadeInfo.currentOpacity;

        if (Math.abs(element.fadeInfo.endOpacity - currentOpacity) > absStep) {
            setTimeout(function () { fade(element) }, element.fadeInfo.delay);

            element.fadeInfo.currentOpacity = (currentOpacity < element.fadeInfo.endOpacity) ? (currentOpacity + absStep) : (currentOpacity - absStep);
            element.style.opacity = element.fadeInfo.currentOpacity;
            if (element.fadeInfo.supportIE) { element.style.filter = 'alpha(opacity=' + (100 * element.fadeInfo.currentOpacity) + ')'; }
        } else {
            element.style.opacity = element.fadeInfo.endOpacity;

            if (element.fadeInfo.supportIE) {
                if (element.fadeInfo.endOpacity == 1) {
                    if (element.style.removeAttribute) { element.style.removeAttribute('filter'); }
                } else {
                    element.style.filter = 'alpha(opacity=' + (100 * element.fadeInfo.endOpacity) + ')';
                }
            }

            var callback = element.fadeInfo.callback;
            element.fadeInfo = null;

            if (typeof callback == 'function') { callback(); }
        }
    }
}

window.fadeIn = function(element, increment, delay, supportIE, callback) { return fadeTo(element, 1, increment, delay, supportIE, callback);}
window.fadeOut = function (element, increment, delay, supportIE, callback) { return fadeTo(element, 0, increment, delay, supportIE, callback); }

window.changeHeightTo = function(element, endHeight, increment, delay, callback) {
	if (typeof element == 'string') {element = document.getElementById('element');}
	if (!element) {return false;}
	
	var started = true;
	
	if (!element.heightChangeInfo) {element.heightChangeInfo = {}; started = false}	
	if (!element.heightChangeInfo.totalDistance) {element.heightChangeInfo.totalDistance = Math.abs(element.offsetHeight - endHeight)};
	if (!element.heightChangeInfo.currentHeight) {element.heightChangeInfo.currentHeight = element.offsetHeight};
	element.heightChangeInfo.endHeight = endHeight;
	element.heightChangeInfo.increment = increment;
	element.heightChangeInfo.delay = delay;
	element.heightChangeInfo.callback = callback;
	
	if (!started) {changeHeight(element);}
	
	function changeHeight(element) {
		if (!element || !element.heightChangeInfo) {return false;}
		
		var absStep = Math.abs(element.heightChangeInfo.increment) * element.heightChangeInfo.totalDistance, currentHeight = element.heightChangeInfo.currentHeight;

		if (Math.abs(element.heightChangeInfo.endHeight - currentHeight) > absStep) {
			setTimeout(function() {changeHeight(element)}, element.heightChangeInfo.delay);
			
			element.heightChangeInfo.currentHeight = (currentHeight < element.heightChangeInfo.endHeight ) ? (currentHeight + absStep) : (currentHeight - absStep);
			element.style.height = element.heightChangeInfo.currentHeight + 'px';
		} else {
			element.style.height = element.heightChangeInfo.endHeight + 'px';

			var callback = element.heightChangeInfo.callback;
			element.heightChangeInfo = null;
			
			if (typeof callback == 'function') {callback();}
		}	
	}
}

var openDropDown;
addEvent(document, 'mousedown', function (e) {
    if (!openDropDown) { return false; }
    var target = (e && e.target) || (event && event.srcElement);
    if (document.getElementById(openDropDown).droppedDown && !isDescendent(target, openDropDown + '-container')) {
        dropDown(openDropDown);
    }
}, false);

window.isDescendent = function(element, parent) {
    if (typeof element == 'string') { element = document.getElementById(element); }
    if (typeof parent == 'string') { parent = document.getElementById(parent); }

    var currentElement = element;

    do {
        if (currentElement === parent) { return true; }
    } while (currentElement = currentElement.parentNode);
}

window.changeElementTagName = function(element, newTag) {
	var elem = (typeof element == 'string') ? document.getElementById(element) : element;
	var attr = elem.attributes;
	var newElem = document.createElement(newTag);
	
	for (var i = 0; i < attr.length; i ++) {
		if (attr[i].specified) {newElem.setAttribute(attr[i].nodeName, attr[i].nodeValue);}
	}
	
	newElem.innerHTML = elem.innerHTML;
	
	var p = elem.parentNode;
	p.insertBefore(newElem, elem);
	p.removeChild(elem);
	
	return newElem;
}

window.initDropDown = function(dropDownDiv, animationInfo) {
    var dropdowndiv = (typeof dropDownDiv == 'string') ? document.getElementById('dropdown-' + dropDownDiv) : dropDownDiv;
    var dropdownid = dropdowndiv.id, dropdowncontainerdiv = document.getElementById(dropdownid + '-container');
    dropdowndiv.dropDownContainer = dropdowncontainerdiv ;
    dropdowndiv.dropDownAnimation = animationInfo;
    dropdowndiv.className = 'dropdown' + ((dropdowndiv.className == '') ? '' : (' ' + dropdowndiv.className));
	dropdowndiv.setAttribute('style', 'display: none; position: absolute; background: white; border: 1px solid black; overflow: hidden; vertical-align: bottom; z-index: 1000000');
    dropdowncontainerdiv.className = 'dropdown-container' + ((dropdowncontainerdiv.className == '') ? '' : (' ' + dropdowncontainerdiv.className));
    dropdowncontainerdiv.style.display = 'inline';
    
    var triggers = document.getElementsByClassName(dropdowndiv.id + '-trigger');

    for (t = 0; t < triggers.length; t ++) {
    	var n = changeElementTagName(triggers[t], 'a');
    	n.setAttribute('href', ' ');
    	n.setAttribute('onclick', 'dropDown("' + dropdowndiv.id + '"); return false');
    }
}

window.dropDown = function(dropDownDiv, animationInfo) {
    var dropdowndiv = (typeof dropDownDiv == 'string') ? document.getElementById('dropdown-' + dropDownDiv) : dropDownDiv;
    var containerdiv = dropdowndiv.dropDownContainer;
        
    if (!animationInfo) {
        if (dropdowndiv.dropDownAnimation) {
            animationInfo = dropdowndiv.dropDownAnimation;
        } else {
            animationInfo = { fade: true, fadeIE: false, slide: true, increment: .05, delay: 10 };
        }
    }

    var absPosition = absolutePosition(containerdiv);

    dropdowndiv.style.width = containerdiv.style.width;
    dropdowndiv.style.left = absPosition.left + 'px';
    dropdowndiv.style.top = (absPosition.top + containerdiv.offsetHeight) + 'px';

    if (!dropdowndiv.droppedDown) {
        dropdowndiv.droppedDown = true;
        openDropDown = dropDownDiv;

        if (animationInfo.fade) {
            dropdowndiv.style.opacity = '0';
            fadeIn(dropdowndiv, animationInfo.increment, animationInfo.delay, null, animationInfo.fadeIE, !animationInfo.slide);
        }

        dropdowndiv.style.display = 'block';

        if (animationInfo.slide) {
            var defaultHeightCSS = dropdowndiv.style.height;
            var defaultHeight = dropdowndiv.offsetHeight;
            dropdowndiv.style.height = '0';
            changeHeightTo(dropdowndiv, defaultHeight, animationInfo.increment, animationInfo.delay, function () { dropdowndiv.style.height = defaultHeightCSS; });
        }
    } else {
        dropdowndiv.droppedDown = false;
        openDropDown = null;

        if (animationInfo.fade) {
            fadeOut(dropdowndiv, animationInfo.increment, animationInfo.delay, function() {dropdowndiv.style.display = 'none'; dropdowndiv.style.opacity = ''; dropdowndiv.style.filter = ''}, animationInfo.fadeIE, !animationInfo.slide);
        }

        if (animationInfo.slide) {
            changeHeightTo(dropdowndiv, 0, animationInfo.increment, animationInfo.delay, function() {dropdowndiv.style.display = 'none'; dropdowndiv.style.height = '';});
        }

        if (!animationInfo.fade && !animationInfo.slide) {dropdowndiv.style.display = 'none';}
    }
}
