﻿/**
* ------------------------------------------------------------------------------------------------
* @author 	Ivo Rafael ivo.rafael@gmail.com
* ------------------------------------------------------------------------------------------------
*/
var trace = function(param){
	try{ console.log(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.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(params){
	var source = this;
	$.each(source, function(i, item){
		var data = params[i] == null ? '' : params[i];
		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}{3}/{4}'.format([ this.getDate() < 10 ? '0' : '', this.getDate(), this.getMonth() < 9 ? '0' : '',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('=')
        var val;
        if (el[1] == 'true') {
            val = true;
        } else if (el[1] == 'false') {
            val = false;
        } else {
            val = el[1];
        }
        obj[el[0]] = val;
    }
    return obj;
}

$.getScriptSafe = function(url, callback) {
    $.getScript(url, function(e) {
        setTimeout(function() { callback(e); }, 300);
    })
};

function delegate(target, func) {
    return function () {
        return func.apply(target, arguments);
    }
}



