var GENERAL = {};

GENERAL.time = 0;

GENERAL.menu = function(id) {
	if((id != null) && (id != "")) {
		var selector = "#que-navegacion #" + id;
		$(selector).addClass("activo");
	}
	$(selector).addClass("activo");
};

GENERAL.submenu = function(id) {
	if((id != null) && (id != "")) {
		var selector = "#que-navegacion-sub #" + id;
		$(selector).parent("ul").find(".navegasub0").after($(selector));
		$(selector).addClass("navegasub1");
	}
};

GENERAL.str_replace = function (search, replace, subject) {
	var f = search, r = replace, s = subject;
	var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
	while (j = 0, i--) {
		if (s[i]) {
			while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
		}
	};
	return sa ? s : s[0];
};

GENERAL.jq = function (myid) {
  return '#' + myid.replace(/(:|\.)/g,'\\$1');
};

GENERAL.checkEmail = function(email) {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return filter.test(email) ? true : false;
};

GENERAL.ajaxPost = function(frm,funRespuesta,respuesta) {
	funRespuesta = ($.isFunction(funRespuesta)) ? funRespuesta : function() {void(0)};
	var target = frm.target;
	$("#ifr_ajax_post").remove();
	var iframeObj = $('<iframe name="ifr_ajax_post" id="ifr_ajax_post" src="about:blank" width="0" height="0" frameborder="0" scrolling="yes"/>');
	$("body").append(iframeObj);
	$(iframeObj).load(
		function() {
			respuesta = (respuesta != null) ? respuesta : $(this).get(0).contentWindow.document.body.innerHTML;
			frm.target = target;
			funRespuesta(respuesta);
			$("#ifr_ajax_post").remove();
		}
	);
	frm.target = "ifr_ajax_post";
	frm.submit();
};

GENERAL.timeHace = function(timestamp) {
	var salida = "";
	timestamp = parseInt(timestamp);
	if(!isNaN(timestamp)) {
		if(GENERAL.time == 0) $.ajax({type: 'GET', url: '/backend/GENERAL.time.php', async: false,success: function(time) {GENERAL.time = time}});
		if(GENERAL.time > 0) {
			var segundos = GENERAL.time - timestamp;
			if (segundos > 60) {
				if(segundos < 3600) salida = "hace " + Math.floor(segundos/60) + " minutos";
				else if(segundos < 3600*2) salida = "hace una hora y " + Math.floor((segundos-3600)/60) + " minutos";
				else if(segundos < 3600*24) salida = "hace " + Math.floor(segundos/3600) + " horas";
				else if(segundos < 3600*48) salida = "hace un día y " + Math.floor((segundos-(3600*24))/3600) + " horas";
				else salida = "hace " + Math.floor(segundos/(3600*24)) + " días";
			}
			else {
				salida = 'hace menos de un minuto';
			}
		}
	}
	return salida;
};

GENERAL.console = function(texto) {
	if(typeof(console) != "undefined") {
		console.log(texto);
	}
};

GENERAL.in_array = function(needle, haystack, argStrict) {

    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
};

GENERAL.reload = function() {
	
	var pagina = document.location.href;
	var v = 1;
	
	if (pagina.indexOf('anker_') != -1) {
		
		var campos = document.location.href.split('?');
		var campos2 = campos[1].split('_');
		
		v = isNaN(parseInt(campos2[1])) ? 1 : (parseInt(campos2[1]) + 1);
		pagina = campos[0];
	}
	
	urlreload = pagina + "?anker_" + v;
	
	window.location = urlreload;
};

GENERAL.strip_tags = function(str, allowed_tags) {
	  
	    var key = '', allowed = false;
	    var matches = [];
	    var allowed_array = [];
	    var allowed_tag = '';
	    var i = 0;
	    var k = '';
	    var html = '';

	    var replacer = function (search, replace, str) {
	        return str.split(search).join(replace);
	    };

	    // Build allowes tags associative array
	    if (allowed_tags) {
	        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
	    }

	    str += '';

	    // Match tags
	    matches = str.match(/(<\/?[\S][^>]*>)/gi);

	    // Go through all HTML tags
	    for (key in matches) {
	        if (isNaN(key)) {
	            // IE7 Hack
	            continue;
	        }

	        // Save HTML tag
	        html = matches[key].toString();

	        // Is tag not in allowed list? Remove from str!
	        allowed = false;

	        // Go through all allowed tags
	        for (k in allowed_array) {
	            // Init
	            allowed_tag = allowed_array[k];
	            i = -1;

	            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
	            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
	            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

	            // Determine
	            if (i == 0) {
	                allowed = true;
	                break;
	            }
	        }

	        if (!allowed) {
	            str = replacer(html, "", str); // Custom replace. No regexing
	        }
	    }

	    return str;
};

GENERAL.trim = function (str, charlist) {

    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
};

GENERAL.strtr = function (str, from, to) {

    var fr = '', i = 0, j = 0, lenStr = 0, lenFrom = 0, tmpStrictForIn = false, fromTypeStr = '', toTypeStr = '', istr = '';
    var tmpFrom = [];
    var tmpTo = [];
    var ret = '';
    var match = false;

    // Received replace_pairs?
    // Convert to normal from->to chars
    if (typeof from === 'object') {
        tmpStrictForIn = this.ini_set('phpjs.strictForIn', false); // Not thread-safe; temporarily set to true
        from = this.krsort(from);
        this.ini_set('phpjs.strictForIn', tmpStrictForIn);

        for (fr in from) {
            if (from.hasOwnProperty(fr)) {
                tmpFrom.push(fr);
                tmpTo.push(from[fr]);
            }
        }

        from = tmpFrom;
        to = tmpTo;
    }
    
    lenStr  = str.length;
    lenFrom = from.length;
    fromTypeStr = typeof from === 'string';
    toTypeStr = typeof to === 'string';

    for (i = 0; i < lenStr; i++) {
        match = false;
        if (fromTypeStr) {
            istr = str.charAt(i);
            for (j = 0; j < lenFrom; j++) {
                if (istr == from.charAt(j)) {
                    match = true;
                    break;
                }
            }
        }
        else {
            for (j = 0; j < lenFrom; j++) {
                if (str.substr(i, from[j].length) == from[j]) {
                    match = true;
                    // Fast forward
                    i = (i + from[j].length)-1;
                    break;
                }
            }
        }
        if (match) {
            ret += toTypeStr ? to.charAt(j) : to[j];
        } else {
            ret += str.charAt(i);
        }
    }

    return ret;
};

function gaweb() {return "Embebido";}

GENERAL.reloadSemillas = function() {
	(typeof(trac) != 'undefined') && trac.record().post(); //recarga nielsen
	try { //recarga analytics
		eval("pageTracker." + "_" + "trackPageview();");
	} catch(err) {}
}

