(function() {
	// Set Klockren namespace
	if(!window.KLOCKREN) {window['KLOCKREN'] = {} }
	
	// Get browser size
	function getBrowserWindowSize() {
		var de = document.documentElement;
		return {
			'width': (window.innerWidth || de && de.clientWidth || document.body.clientWidth),
			'height': (window.innerHeight || de && de.clientHeight || document.body.clientHeight)
		}
	};
	window['KLOCKREN']['getBrowserWindowSize'] = getBrowserWindowSize;
	
	// Check if user agent is iPod/iPhone/iPad/Android
	function isMobile() {
		var vendors = [
			'iPad',
			'iPhone',
			'iPod',
			'Android'
		];
		
		for(var i in vendors) {
			var vendor = navigator.userAgent.match(new RegExp(vendors[i], 'i'));
			if(vendor != null) {
				window['KLOCKREN']['MOBILE_VENDOR'] = vendor;
				return true;
			}
		}
		return false;
	};
	window['KLOCKREN']['isMobile'] = isMobile;
	
	// Get orientation of iPod/iPhone/iPad/etc device
	function getOrientation(degrees) {
		var orientation = (degrees === undefined) ? window.orientation : degrees;
		switch(orientation) {
		    case 0:
		        //document.body.setAttribute("class", "portrait");
		        return "portrait";
		        break;  
		    case 90:
		        //document.body.setAttribute("class","landscape-left");
		        return "landscape";
		        break;
		    case -90:  
		        //document.body.setAttribute("class","landscape-right");
		        return "landscape";
		        break;
		}
		return false;
	};
	window['KLOCKREN']['getOrientation'] = getOrientation;
	
	// Check if user has flash installed
	function hasFlash() {
		var hasFlash = false;
		try {
			var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
			if(ax)
				hasFlash = true;
		} catch(e) {
		  if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined)
		  	hasFlash = true;
		}
		return hasFlash;
	};
	window['KLOCKREN']['hasFlash'] = hasFlash;
	
	// Check if browser is IE
	function isIE() {
		return (navigator.appName=="Microsoft Internet Explorer");
	};
	window['KLOCKREN']['isIE'] = isIE;
	
	// Change class
	function changeClass(element, sclass) {
		if(element) {
			element.setAttribute("class", sclass);
			return true;
		}
		return false;
	};
	window['KLOCKREN']['changeClass'] = changeClass;
	
	// Check browser elements compability
	function isCompatible(other) {
		// Use capability detection to check requirements
		if(other === false || !Array.prototype.push || !Object.hasOwnProperty || !document.createElement || !document.getElementsByTagName) {
			return false;
		}
		return true;
	};
	
	//Check browser CSS3 compability
	//Argument must be written like Transform, TransformOrigin not as transform, transform-origin
	// Sets the global variable KLOCKREN.CSS3_VENDOR to current (if) vendor specifik prefix for CSS3 properties
	function CSS3Compatible(property) {
		var props = [
		    property,
		    'Webkit' + property,
		    'Moz' + property,
		    'O' + property,
		    'ms' + property,
		    'Khtml' + property
		],
		doc = document,
		element = doc.createElement('div'),
		e_style = element.style;
		
		for(var i in props) {
			if(e_style[props[i]] !== undefined) {
				var vendor_short = props[i].slice(0, (props[i].length - property.length));
				if(vendor_short != "") {
					window['KLOCKREN']['CSS3_VENDOR'] = "-" +  vendor_short.toLowerCase() + "-";
				}
				return true;
			}
		}
		return false;
	};
	window['KLOCKREN']['CSS3Compatible'] = CSS3Compatible;
	
	// Get elements by ID
	function $() {
		var elements = new Array();
		
		// Find all the elements supplied as arguments
		for(var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			
			// If the argument is a string assume it's an id
			if(typeof element == 'string') {
				element = document.getElementById(element);
			}
			
			// If only one argument is supplied, return the element immediatly 
			if(arguments.length == 1) {
				return element;
			}
			
			// Otherwise add it to the array
			elements.push(element);
		}
		
		// Return the array of mulitple request elements
		return elements;
	};
	window['KLOCKREN']['$'] = $;
	
	// Set CSS properties
	function setCSS(element, name, value) {
		if(element) {
			element.setAttribute("style", name + ": " + value);
			return true;
		}
		else {
			return false;
		};
	};
	window['KLOCKREN']['setCSS'] = setCSS;
	
	// Set META tags
	function setMeta(metaName, name, value) {
	     var meta_elements = document.getElementsByTagName("meta");
	     for(var i in meta_elements) {
	     	var current_meta = meta_elements[i],
	     	meta_name = current_meta.getAttribute("name");
	     	if(meta_name == metaName) {
	     		current_meta.setAttribute(name, value);
	     		alert(current_meta.getAttribute("content"));
	     	}
	     }
	 }
	window['KLOCKREN']['setMeta'] = setMeta;
	
	function addEvent(node, type, listener) {
		//Check compability using the earlier method to ensure graceful degradation
		if(!isCompatible()) { return false; }
		if(!(node = $(node))) { return false; }
		
		if(node.addEventListener) {
			// W3C method
			node.addEventListener(type, listener, false);
			return true;
		} else if(node.AttachEvent) {
			//MSIE method
			node['e'+type+listener] = listener;
			node[type+listener] = function () {
				node['e'+type+listener](window.event);
			}
			node.attachEvent('on'+type, node[type+listener]);
			return true;
		}
		
		// Didn't have either so return false
		return false;
	};
	window['KLOCKREN']['addEvent'] = addEvent;
	
	function removeChildren(parent) {
		if(!(parent = $(parent))) { return false; }
		
		// While there is a child remove it
		while(parent.firstChild) {
			parent.firstChild.parentNode.removeChild(parent.firstChild);
		}
		
		// Return the parent again so you can chain the methods
		return parent;
	};
	window['KLOCKREN']['removeChildren'] = removeChildren;

	
	// Check if DOM is ready, faster than window.onload
	/*******************************************************************************
	 DF1.1 :: domFunction 
	 ------------------------------------------------------------------------------
	 Copyright (c) 2005 James Edwards (brothercake)          <cake@brothercake.com>
	 BSD License                          See license.txt for licensing information
	 Info/Docs          http://www.brothercake.com/site/resources/scripts/domready/
	 ------------------------------------------------------------------------------
	*******************************************************************************/
	//DOM-ready watcher
	function domFunction(f, a)
	{
		//initialise the counter
		var n = 0;
		
		//start the timer
		var t = setInterval(function()
		{
			//continue flag indicates whether to continue to the next iteration
			//assume that we are going unless specified otherwise
			var c = true;
	
			//increase the counter
			n++;
		
			//if DOM methods are supported, and the body element exists
			//(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
			//in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
			if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
			{
				//set the continue flag to false
				//because other things being equal, we're not going to continue
				c = false;
	
				//but ... if the arguments object is there
				if(typeof a == 'object')
				{
					//iterate through the object
					for(var i in a)
					{
						//if its value is "id" and the element with the given ID doesn't exist 
						//or its value is "tag" and the specified collection has no members
						if
						(
							(a[i] == 'id' && document.getElementById(i) == null)
							||
							(a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
						) 
						{ 
							//set the continue flag back to true
							//because a specific element or collection doesn't exist
							c = true; 
	
							//no need to finish this loop
							break; 
						}
					}
				}
	
				//if we're not continuing
				//we can call the argument function and clear the timer
				if(!c) { f(); clearInterval(t); }
			}
			
			//if the timer has reached 60 (so timeout after 15 seconds)
			//in practise, I've never seen this take longer than 7 iterations [in kde 3 
			//in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time]
			if(n >= 60)
			{
				//clear the timer
				clearInterval(t);
			}
			
		}, 250);
	};
	window['KLOCKREN']['domFunction'] = domFunction;
})();
