/**
* ------------------------------------------------------------------------------------------------
* @author 	Ivo Rafael ivo.rafael@gmail.com
* ------------------------------------------------------------------------------------------------
*/
var trace = function(param, type) {

try {
        console[type == undefined ? 'log' : type](param);
    } catch (e) { };
};

var getUrl = function(_url) {

    if (_url.substr(0, 4) == 'http') return _url;
    var url = _url.substr(0, 1) == '/' ? _url.substr(1, _url.length) : _url;
    return root + url;
}

String.prototype._replace = function(a, b) {
    return this.replace(/a/g, b);


    //return this.split(a).join(b);
}

String.prototype._strip = function() {
    var a = this.toString();
    for (var i = 0; i < arguments.length; i++) a = a._replace(arguments[i].toString(), '');
    return a;
}

/*String.prototype._slice = function(max){
if(this.length > max) return this.substr(0, (max - 3)) + '...';
return this.toString();	
}*/

String.prototype._slice = function(max) {
    var arrWords = this.split(' ');
    var fnTxt = '';
    var sliced = false;

    for (var i in arrWords) {
        var word = arrWords[i];
        var n = fnTxt.length + word.length + 3;

        if (n > max) {
            return fnTxt + '...';
        }

        fnTxt += (' ' + word);
    }

    return fnTxt;
}

String.prototype.clearAcc = function() {
    var withAccent = 'áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';
    var noAccent = 'aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';
    var word = '';

    for (var i = 0; i < this.length; i++) {
        var item = this.substr(i, 1).toString();
        if (withAccent.search(item) >= 0) {
            word += noAccent.substr(withAccent.search(item), 1);
        } else {
            word += item;
        }
    }

    return word;
}

String.prototype.clearEncode = function() {
    var url = this;
    url = url.toLowerCase();
    url = url.trim();
    url = url.split(' ').join('-');
    url = url.split('+').join('-');
    url = url.clearAcc(url);

    return url;
}

String.prototype.encode = function() {
    return escape(this).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

String.prototype.decode = function() {
    return unescape(this.replace('+', ' '));
}

String.prototype.format = function() {
    var source = this;
    $.each(arguments, function(i, item) {
        var data = item == null ? '' : item;
        source = source.split('{' + i + '}').join(data);
    })

    return source;
}

String.prototype.toInt = function() {
    try {
        return Number(this);
    } catch (e) {
        return 0;
    }
}

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

Number.prototype.round = function() {
    return Math.round(this);
}

Number.prototype.floor = function() {
    return Math.floor(this);
}

Number.prototype.ceil = function() {
    return Math.ceil(this);
}

Date.prototype.getPtBrDate = function() {
    return '{0}/{1}/{2}'.format([this.getDate(), this.getMonth() + 1, this.getFullYear()]);
}

toQueryString = function(obj) {
    var o; var s = '';
    for (o in obj) {
        s += '{0}={1}&'.format([o, obj[o]]);
    }
    return s.substr(0, s.length - 1);
}

toObj = function(str) {
    var arr = str.split('&');
    var obj = {};
    for (var i in arr) {
        var el = arr[i].split('=')
        obj[el[0]] = el[1];
    }

    return obj;
}

$.getScriptSafe = function(url, callback) {
    $.getScript(url, function(e) {
        setTimeout(function() { callback(e); }, 100);
    })
};

function delegate(target, func) {
    return function() {
        return func.apply(target, arguments);
    }
}
