﻿
// http://www.thefutureoftheweb.com/blog/adddomloadevent
function addDOMLoadEvent(func) {
	if (!window.__load_events) {
		var init = function () {
			// quit if this function has already been called
			if (arguments.callee.done)
				return;
			// flag this function so we don't do the same thing twice
			arguments.callee.done = true;
			// kill the timer
			if (window.__load_timer) {
				clearInterval(window.__load_timer);
				window.__load_timer = null;
			}
			// execute each function in the stack in the order they were added
			for (var i=0;i < window.__load_events.length;i++) {
				window.__load_events[i]();
			}
			window.__load_events = null;
      	};
		// for Mozilla/Opera9
		if (document.addEventListener) {
			document.addEventListener("DOMContentLoaded", init, false);
		}
		// for Internet Explorer
		/*@cc_on @*/
		/*@if (@_win32)
			document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");
			var script = document.getElementById("__ie_onload");
			script.onreadystatechange = function() {
			if (this.readyState == "complete") {
				init(); // call the onload handler
			}
		};
		/*@end @*/
		// for Safari
		if (/WebKit/i.test(navigator.userAgent)) { // sniff
			window.__load_timer = setInterval(function() {
				if (/loaded|complete/.test(document.readyState)) {
					init(); // call the onload handler
				}
			}, 10);
		}
		// for other browsers
		window.onload = init;
		// create event function stack
		window.__load_events = [];
	}
	// add function to event stack
	window.__load_events.push(func);
}

// Basic common functions involving js constructs and DOM.

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

//
function addLoadEventBlink(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	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 (var i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// http://www.quirksmode.org/dom/getElementsByTagNames.html
function getElementsByTagNames(list,obj) {
	if (!obj)
		var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i = 0; i < tagNames.length; i++) {
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j = 0; j < tags.length; j++) {
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (!testNode)
		return [];
	if (testNode.sourceIndex) {
		resultArray.sort(function (a,b) {
			return a.sourceIndex - b.sourceIndex;
		});
	} else if (testNode.compareDocumentPosition) {
		resultArray.sort(function (a,b) {
			return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}

function toggleBlink(obj) {
	var el;
	if (typeof obj == 'string')
		el = document.getElementById(obj);
	else
		el = obj;
	if (!el)
		return;
	if (el.style.display != 'none') {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}

function dualToggle(o1, o2) {
	toggleBlink(o1);
	toggleBlink(o2);
}

function hideBlink(obj) {
	var el;
	if (typeof obj == 'string')
		el = document.getElementById(obj);
	else
		el = obj;
	if (!el)
		return;
	if (el.style.display == '')
		el.style.display = 'none';
}

function show(obj) {
	var el;
	if (typeof obj == 'string')
		el = document.getElementById(obj);
	else
		el = obj;
	if (!el)
		return;
	if (el.style.display == 'none')
		el.style.display = '';
}

function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}
function $Blink() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

// Advanced common functions involving styling or positioning.

function hasClass(ele, cls) {
	return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(ele, cls) {
	if (!this.hasClass(ele, cls))
		ele.className += " " + cls;
}

function removeClass(ele, cls) {
	if (hasClass(ele, cls)) {
		var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
		ele.className = ele.className.replace(reg, ' ');
	}
}

function toggleClass(id, srcCls, dstCls) {
	var ele = $Blink(id);
	if (!ele)
		return;
	if (hasClass(ele, srcCls))
		removeClass(ele, srcCls);
	if (!hasClass(ele, dstCls))
		addClass(ele, dstCls);
}

function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (document.defaultView && document.defaultView.getComputedStyle) {
        var compStyle = document.defaultView.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function getElementTop(container) {
	var offsetY = 0;
	if (container.getBoundingClientRect) {
		var rect = container.getBoundingClientRect();
		offsetY = rect.top;
		var z = getScrollXY();
		var scrolly = z[1];
		offsetY += scrolly;
	} else {
		var parentElem = container;
		while (parentElem &&
		       parentElem != document.body &&
		       parentElem != document.documentElement) {
			offsetY += parentElem.offsetTop;
			parentElem = parentElem.offsetParent;
		}
	}
	return offsetY;
}

// this works for IE6 and 7, unlike findPos() from quirksmode
function getElementPos(container) {
	var offsetY = 0;
	var offsetX = 0;
	if (container.getBoundingClientRect) {
		var rect = container.getBoundingClientRect();
		offsetY = rect.top - 2;
		offsetX = rect.left - 2;
		var z = getScrollXY();
		offsetY += z[1];
		offsetX += z[0];
	} else {
		var parentElem = container;
		while (parentElem &&
		       parentElem != document.body &&
		       parentElem != document.documentElement) {
			offsetY += parentElem.offsetTop;
			offsetX += parentElem.offsetLeft;
			parentElem = parentElem.offsetParent;
		}
	}
	return [offsetX, offsetY];
}

function getWindowWidth() {
	if (window.innerWidth && window.innerWidth != 0)
		return window.innerWidth;
	if (document.documentElement.clientWidth && document.documentElement.clientWidth != 0)
		return document.documentElement.clientWidth;
	if (document.body.clientWidth && document.body.clientWidth != 0)
		return document.body.clientWidth;
	return 0;
}

function getWindowHeight() {
	if (window.innerHeight && window.innerHeight != 0)
		return window.innerHeight;
	if (document.documentElement.clientHeight && document.documentElement.clientHeight != 0)
		return document.documentElement.clientHeight;
	if (document.body.clientHeight && document.body.clientHeight != 0)
		return document.body.clientHeight;
	return 0;
}

function getElementHeight(target) {
	if (target.clientHeight) return target.clientHeight;
	else if (target.innerHeight) return target.innerHeight;
	else if (target.offsetHeight) return target.offsetHeight;
	else return 0;
}

function getElementWidth(target) {
	if (target.clientWidth) return target.clientWidth;
	else if (target.innerWidth) return target.innerWidth;
	else if (target.offsetWidth) return target.offsetWidth;
	else return 0;
}

function disappear(id) {
	var e = $Blink(id);
	if (e) {
		setTimeout(function() { e.style.opacity = 0.9; }, 100);
		setTimeout(function() { e.style.opacity = 0.8; }, 200);
		setTimeout(function() { e.style.opacity = 0.7; }, 300);
		setTimeout(function() { e.style.opacity = 0.6; }, 400);
		setTimeout(function() { e.style.opacity = 0.5; }, 500);
		setTimeout(function() { e.style.opacity = 0.4; }, 600);
		setTimeout(function() { e.style.opacity = 0.3; }, 700);
		setTimeout(function() { e.style.opacity = 0.2; }, 800);
		setTimeout(function() { e.style.opacity = 0.1; }, 900);
		setTimeout(function() { e.style.opacity = 1.0; e.innerHTML = ''; }, 1000);
	}
}

