var Util = {
	addDivWithClass: function(parent, classname) {
		var div = document.createElement('div');
		div.className = classname;
		
		parent.appendChild(div);
		return div;
	},
	
	elementIsChildId: function(element, id) {
		if (element.tagName == 'body' || element.tagName == 'BODY') return false;
		
		if (element.id == id) {
			return true;
		} else {
			return Util.elementIsChildId(element.parentNode, id);
		}
	},
	
	createCookie: 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=/";
	},
	
	readCookie: 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;
	},

	eraseCookie: function(name) {
		createCookie(name,"",-1);
	}
};

var Observable = function() { }

Observable.prototype.addListener = function(eventName, callback) {
	if (this.listeners == null) this.listeners = {};
	if (this.listeners[eventName] == null) this.listeners[eventName] = [];
	
	this.listeners[eventName].push(callback);
}

Observable.prototype.fire = function(eventName, data) {
	if (!this.listeners || !this.listeners[eventName]) return;
	
	for (var i=0; i<this.listeners[eventName].length; i++) {
		var listener = this.listeners[eventName][i];
		listener(data);
	}
}

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

Function.method('inherits', function (parent) {
    var d = {}, p = (this.prototype = new parent());
    this.method('uber', function uber(name) {
        if (!(name in d)) {
            d[name] = 0;
        }        
        var f, r, t = d[name], v = parent.prototype;
        if (t) {
            while (t) {
                v = v.constructor.prototype;
                t -= 1;
            }
            f = v[name];
        } else {
            f = p[name];
            if (f == this[name]) {
                f = v[name];
            }
        }
        d[name] += 1;
        r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
        d[name] -= 1;
        return r;
    });
    return this;
});

// The .bind method from Prototype.js 
if (!Function.prototype.bind) { // check if native implementation available
  Function.prototype.bind = function(){ 
    var fn = this, args = Array.prototype.slice.call(arguments),
        object = args.shift(); 
    return function(){ 
      return fn.apply(object, 
        args.concat(Array.prototype.slice.call(arguments))); 
    }; 
  };
}


