/**
 * @author wolf
 */
Core = {
	apply: function(oWhatApply, oDefault){
		oWhatApply = oWhatApply || {};
		oDefault = oDefault || {};

		for (var sProp in oDefault) {
			if(undefined === oWhatApply[sProp]){
				oWhatApply[sProp] = oDefault[sProp];
			} else if ("object" === typeof oDefault[sProp]) {
				this.apply(oWhatApply[sProp], oDefault[sProp]);
			}
		}

		return oWhatApply;
	},
    getBody: function(){
        return $(document.body);
    }
};

Array.prototype.shuffle = function(){
	this.sort(function(){
		return	0.5 - Math.random();
	});
};

Function.prototype.delegate = function(){
	var args = $A(arguments);
	var fn = this;

	return function(){
		fn.apply(args.shift(), args);
	};
};

var PrototypeExtensions = {
    on: function(element, strEventName, fnCallback, scope){
        element = $(element);
		var args = $A(arguments).slice(4);
        var fnCallbackSaved = fnCallback.bindAsEventListener.apply(fnCallback, [scope || window].concat(args));

		element.observe(strEventName, fnCallbackSaved);
        return fnCallbackSaved;
    },
    un: function(element, strEventName, fnCallback){
        element.stopObserving(strEventName, fnCallback);
    },
	getSize: function(element){
		return {
			width: element.getWidth(),
			height: element.getHeight()
		};
	},
	setSize: function(element, oSize, i){
		i = i || 0;
		return element.setStyle({
			width: (oSize.width + i) + "px",
			height: (oSize.height + i) + "px"
		});
	},
    setWidth: function(element, iWidth, strUnit){
        return element.setStyle({
            width: iWidth + (strUnit || "px")
        });
    },
    setHeight: function(element, iHeight, strUnit){
        return element.setStyle({
            height: iHeight + (strUnit || "px")
        });
    },
    addClassOnOver: function(element, strClass){
        element.on("mouseover", function(){
            this.addClassName(strClass);
        }, element);
        element.on("mouseout", function(){
            this.removeClassName(strClass);
        }, element);
    },
    getLeft: function(element){
        return parseInt(element.offsetLeft, 10) || 0;
    },
    getRight: function(element){
        return element.getLeft() + element.getWidth();
    }
};

Element.addMethods(PrototypeExtensions);
