/**
 * 	
 *  Copyright(c) 2009: Mantecao / Grupo Alion C.A.
 *  url: www.mantecao.com.ve/licensing
 *  email: licensing@mantecao.com.ve   
 *  author: Alexander Cabezas / Carlos Mantilla 
 * 
 */

if (typeof(core) == "undefined") { core = {}; }

core.constant = {};
core.globals = {};
core.globals.fn = {};
core.globals.defaults = {};
core.globals.defaults.html = {};
core.globals.current = {};
core.viewstate = {};
core.utils = {};
core.container = {};
core.config = {};
core.fn = {};
core.utils.ajax = {};
core.notifiers = {};


core.constant = {
	kinship: {
		PARENT: "parent",
		SIBLING: "sibling",
		SIBLING_LEFT: "sibling_left",
		SIBLING_RIGHT: "sibling_right",
		CHILD: "child"
	}
};

/**
 *
 * 
 * @author: cmantilla@mantecao.com.ve
 * Funciones para realizar el jsonp
 * 
 * 
 */


core.utils.ajax.get = function(url){
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = url;
    head.appendChild(script);
};

core.jsonp = core.utils.ajax.get;

// helper fn for console logging
core.log = function() {
    if (window.console && window.console.log)
        window.console.log('[core]' + Array.prototype.join.call(arguments,''));
};

Date.patterns = {
    ISO8601Long:"Y-m-d H:i:s",
    ISO8601Short:"Y-m-d",
    ShortDate: "n/j/Y",
    LongDate: "l, F d, Y",
    FullDateTime: "l, F d, Y g:i:s A",
    MonthDay: "F d",
    ShortTime: "g:i A",
    LongTime: "g:i:s A",
    SortableDateTime: "Y-m-d\\TH:i:sP",
    UniversalSortableDateTime: "Y-m-d H:i:sO",
    YearMonth: "F, Y"
};


/**
 * 	
 *  author: Alexander Cabezas
 *  description: Función que permite obtener el valor de un parametro, del los parametros que se pasan en el URL 
 *	
 *  usage: core.utils.gup(nombre_de_la_variable);
 *
 */
core.utils.gup = function( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null ){
    return "";
  }else{
    return results[1];
  }
};

core.utils.setfocus = function(id){
	$("#" + id).focus();
}
/**
 * descripcion: Función que trunca un texto, dejándolo en un número de caractéres menor o igual al parámetro length y
 * concatenando al final el valor ellipsis
 * @param {Object} text
 * @param {Object} length
 * @param {Object} ellipsis
 * 
 * usage: core.utils.truncate(string, 200,'...')
 */
core.utils.truncate = function(text, length, ellipsis, ellipsis_always){
	// Set length and ellipsis to defaults if not defined
    if (typeof length == 'undefined') var length = 100;
    if (typeof ellipsis == 'undefined') var ellipsis = '';

    // if the text is already lower than the cutoff
    if (text.length > length) {
		// Otherwise, check if the last character is a space.
		// If not, keep counting down from the last character
		// until we find a character that is a space
		if (text.indexOf(' ') != -1 && text.indexOf(' ') < length) {
			for (var i = length - 1; text.charAt(i) != ' '; i--) {
				length--;
			}
		}
		ellipsis = '... ' + ellipsis;
	}else{
		if(!ellipsis_always) ellipsis = '';
		else ellipsis = ' ' + ellipsis;
	}
    // The for() loop ends when it finds a space, and the length var
    // has been updated so it doesn't cut in the middle of a word.
    return text.substr(0, length) + ellipsis;
}

core.utils.capitalize = function(str){
    tmpChar = str.substring(0,1).toUpperCase();
    postString = str.substring(1,str.length);
    return tmpChar + postString;    
};

core.utils.strip_html = function(str){
	str=str.replace(/<br>/gi, "\n");
	str=str.replace(/<p.*>/gi, "\n");
	str=str.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, "$2");
	str=str.replace(/<(?:.|\s)*?>/g, "");
	return str;
} 

core.utils.send_notification = function(params, callback){
	
	var extra_params = {service: 'mailsdeliver',url: ".json",verb: 'POST'};
		
	$.post(config.api(), config.default_params(params, extra_params), callback, "json");
}

core.utils.now = function(){
	var now = new Date();
    var iyear = now.getFullYear();
    var imonth = now.getMonth() + 1;
    var iday = now.getDate();
    var ihours = now.getHours();
    var iminutes = now.getMinutes();
    var iseconds = now.getSeconds();
	var rr;
	
	// begin_: leading zeropad single-digit numbers
    imonth = (imonth < 10)? "0" + imonth : imonth;
    iday = (iday < 10)? "0" + iday : iday;
	ihours = (ihours < 10)? "0" + ihours : ihours;
	iminutes = (iminutes < 10)? "0" + iminutes : iminutes;
	iseconds = (iseconds < 10)? "0" + iseconds : iseconds;
	
	rr = iday + "/" + imonth + "/" + iyear +  " " + ihours + ":" + iminutes + ":" + iseconds;
	return rr;
}

