var IS_IPHONE = ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) ? true : false;
function delegate(that, thatMethod) {
	return function() { return thatMethod.call(that); }
}
(function() {

	if (foo == undefined)
		var foo = {};

	function getDimensions(el, width) {
		if (el.style.display != 'none')
			return width ? el.offsetWidth : el.offsetHeight;
		// reset css properties to get accurate reading
		var old = el.resetCSS({display: '', visibility: 'hidden', position: 'absolute'});
		var dim = (width ? el.clientWidth : el.clientHeight) ;
		el.restoreCSS(old);
		return dim;
	}

	function extend() {
		function ext(dest, src) {
			for(var p in src) dest[p] = src[p];
		}
		if(arguments.length == 2)
			ext(arguments[0], arguments[1])
		else {
			var l = arguments.length, src = arguments[l-1], i = l-1;
			while(i--) { ext(arguments[i], src); }
		}
	}
	
	extend(String.prototype, {
		trim: function() {
			var re = new RegExp(/\s+?/);
		    return this.replace(re, '');
		}
	});

	extend(HTMLElement.prototype, {
	 	resetCSS: function(ext) {
			var old = {};
			for (var p in ext) {
				old[p] = this.style[p];
				this.style[p] = ext[p];
			}
			return old;
		},
		restoreCSS: function(ext) {
			for (var p in ext)
				this.style[p] = ext[p]
		},
		getFullHeight: function() { return getDimensions(this, false); },
		getFullWidth: function() { return getDimensions(this, true); },
		hasClass: function(className) {
	        var re = new RegExp('(?:^|\\s+)'+className+'(?:\\s+|$)');
	        return re.test(this.className);
		},
		removeClass: function(className) {
	        var re = new RegExp('(?:^|\\s+)'+className+'(?:\\s+|$)');
	        this.className = this.className.replace(re, ' ');
			this.className.trim();
		},
		addClass: function(className) {
	        if(!this.hasClass(className))
	            this.className += ' ' + className.trim();	
		},
		css: function() {
			if (arguments.length == 2 && typeof arguments[0] == 'string')
				this.style[arguments[0]] = arguments[1];
			else if (arguments.length == 1) {
				if (typeof arguments[0] == 'object')
					extend(this.style, arguments[0]);
				else if (typeof arguments[0] == 'string')
					return (this.style[arguments[0]] != undefined ? this.style[arguments[0]] : null);
			}
		}
	});
	
	extend((foo.Cookie = {}), {
		create: function(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		},
		read: function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		},
		erase: function(name) {
			this.create(name,"",-1);
		}
	});

	// utility functions
	extend((foo.Utils = {}), {
		extend: extend,
		hideURLBar: function(offset) {
			if (window.pageYOffset < 2)
				setTimeout('window.scrollTo(0, '+(offset!=undefined?offset:1)+')', 200);
		},
		domQuery: function (query, context) {
			if (!context) context = document;
			return context.querySelector(query)
		},
		domQueryAll: function(query, context) {
			if (!context) context = document;
			return context.querySelectorAll(query)
		}
	});
	
	window.Cookie = foo.Cookie;
	window.Utils = foo.Utils;
	window.$ = foo.Utils.domQuery; 		// watch out if you have a lib like jquery as these two lines
	window.$$ = foo.Utils.domQueryAll;	// will clobber the $, $$
	window.delegate = function(obj,origFunc) {
		var scope = obj;
		var func = origFunc;
		var f = function() {
			return func.apply(scope, arguments);
		};

		return f;
	}
})();
