Function.prototype.bind = function(scope) {
	var _function = this;
	
	return function() {
		return _function.apply(scope, arguments);
	};
};

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
};

String.prototype.startsWith = function(str) {
    return (this.match("^"+str)==str);
};

String.prototype.endsWith = function(str) {
    return (this.match(str+"$")==str);
};

function fragment(href) {
	return href.split("#")[1];
}

function parameters(fragment) {
	return fragment.split(":");
}

function escapeId(id) {
	return id.replace(/:/g,"\\:").replace(/\./g,"\\.");
}

function getFormData(form) {
	if (window.authenticityToken) {
		return form.serialize() + "&_a=" + window.authenticityToken;
	}
	return form.serialize();
}

function taskPointer() {
	$("body").css("cursor", "progress");
}

function defaultPointer() {
	$("body").css("cursor", "default");
}

function crop(str, len) {
    if(str.length > len && len > 3) return str.substr(0,len-3) + '...';
    return str;
}


function getPos(owner) {
	if(owner == undefined) {
		return {top : 0, left:0 , width : 0, height : 0};
	}
 
	var e = owner;
	var oTop = e.offsetTop;
	var oLeft = e.offsetLeft;
	var oWidth = e.offsetWidth;
	var oHeight = e.offsetHeight;
	while(e = e.offsetParent) {
		oTop += e.offsetTop;
		oLeft += e.offsetLeft;
	}
 
	return {
		top : oTop,
		left : oLeft,
		width : oWidth,
		height : oHeight
	}
}

function findPos(obj) {
    var curleft = curtop = 0;
    curleft = getX(obj);
    curtop = getY(obj);
    return [curleft,curtop];
}

function getX( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getY( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function reportStats(id, type) {
	if(window.WIM) {
		WIM.reportAction(id, type);
	}
}

var getParameter = (function() {
	var urlParams = {};
	var e,
    d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); },
    q = window.location.search.substring(1),
    r = /([^&=]+)=?([^&]*)/g;

    while (e = r.exec(q))
    	urlParams[d(e[1])] = d(e[2]);
    return function(name) {
    	return urlParams[name];
    }
})();

