/**
 * cbf-js-lib
 * claudiusbähr+friends JavaScript library
 *
 * eine Sammlung von nützlichen JS Funktionen
 * a collection of useful JS functions
 *
 */
 
/**
 * getWindowSize()
 * returns an array with the current window size (X and Y dimensions)
 *
 */
 
function getWindowSize() {
  if (typeof(window.innerWidth) == 'number') { // Netscape
    //Non-IE
    windowSize = new Array(window.innerWidth, window.innerHeight);
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { // IE 6+
  	windowSize = new Array(document.documentElement.clientWidth, document.documentElement.clientHeight);
  }
  // return an array with [0] = width and [1] = height
  return windowSize;
}

/**
 * getElemOffset()
 * returns an array with the current offset of an element to the window (X and Y offset)
 *
 */

function getElemOffset(obj) {
	var elemOffsetX = 0;
	var elemOffsetY = 0;
    if (obj.offsetParent) {
        while(1) {
			elemOffsetX += obj.offsetLeft;
			elemOffsetY += obj.offsetTop;
			if (!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
    } else if (obj.x) {
		elemOffsetX += obj.x;
		elemOffsetY += obj.y;
	}
	// return an array with [0] = x offset and [1] = y offset
	elemOffset = new Array(elemOffsetX, elemOffsetY);
    return elemOffset;
}

/**
 * document.getElementsByClassName
 * returns an array with all elements with a specified className 
 *
 */
 
function getElementsByClassName(myClass) {
	// create an array results[] which will later include all found matches
	var results = new Array();
	// create a regular expressions of var myclass to enable the test() function on the string
	var myClass = new RegExp('\\b' + myClass + '\\b');
	// get all elements from the document
	var allElems = document.getElementsByTagName('*');
	// loop through all elements and check if the className returns a match
	for (var i = 0; i < allElems.length; i++) {
		// get the className from each element
		var curClass = allElems[i].className;
		// if it matches var myClass, insert it into the results[] array via push()
		if (myClass.test(curClass)) { results.push(allElems[i]) }
	}
	return results;
}

/**
 * detectBrowser()
 * returns a string based on the currently used browser
 *
 */

function detectBrowser() {
	var browser;
	switch (navigator.appName) {
		case 'Microsoft Internet Explorer':
			if (navigator.appVersion.match(/Windows Phone/i)) { browser = 'windowsphone7'; break; }
			if (navigator.appVersion.match(/MSIE 6\.0/i)) { browser = 'msie6'; break; }
			if (navigator.appVersion.match(/MSIE 7\.0/i)) { browser = 'msie7'; break; }
			if (navigator.appVersion.match(/MSIE 8\.0/i)) { browser = 'msie8'; break; }
			if (navigator.appVersion.match(/MSIE 9\.0/i)) { browser = 'msie9'; break; }
		case 'Netscape':
			if (navigator.appVersion.match(/iPad/i)) { browser = 'ipad'; break; }
			if (navigator.appVersion.match(/iPhone/i)) { browser = 'iphone'; break; }
			if (navigator.appVersion.match(/iPod/i)) { browser = 'ipod'; break; }
		default:
			browser = 'netscape';
	}
	return browser;
}
