window.addEvent('domready',function(){
	formtips = new Tips('.formtipCaller',{
		className : 'tooltip',
		offsets:{'x':0,'y':0},
		showDelay:0,
		hideDelay:0,
		fixed:true,
		onShow: function(el,caller){
			var el_ = el.getCoordinates();
			var cal_ = caller.getCoordinates();

			this.options.offset.y = -(el_.height +8);
			this.options.offset.x = ((el_.width - cal_.width)/-2).toInt();
			//new Element('p',{'html':'dif: '+dif }).inject('testing');new Element('p',{'html':'el: '+el_.width+' : '+el_.height }).inject('testing');new Element('p',{'html':'onShow: '+this.options.offset.x+' : '+this.options.offset.y }).inject('testing');
			el.set('morph',{ duration:600, link:'cancel', transition:'back:out' }).morph({ 'margin-top':[-20,0], 'opacity':[0,.9] });
		},
		onHide: function(el){
			el.morph({ 'margin-top':[0,-20], 'opacity':[.9,0] });
		}

	});
	myTips = new Tips('.tipCaller',{
		className : 'tooltip',
		offsets:{'x':16,'y':8},
		showDelay:0,
		hideDelay:0,
		fixed:false,
		onShow: function(el){ el.set('morph',{ duration:600, link:'cancel', transition:'back:out' }).morph({ 'margin-top':[-20,0], 'opacity':[0,1] });},
		onHide: function(el){ el.morph({ 'margin-top':[0,-20], 'opacity':[1,0] }); }

	});

});

function mooPop(msg){
	var pop = new Element('div',{ 'class':'mooPop', styles:{ opacity:0 }}).inject(document.body,'top');
	pop.makeDraggable();
	new Element('a',{ href:'javascript:;','class':'mooPopClose',html:'<img src="/img/popClose.gif" alt="Cerrar" />', events:{ click:function(){ this.getParent('.mooPop').nix(); }}}).inject(pop);
	new Element('div',{'class':'mooPopMsg', html:msg.replace('. ','<br/>')}).inject(pop);
	var pop_size = pop.getSize();
	pop.setStyles({top:((window.getScrollTop()+window.getHeight()-pop_size.y)/2).toInt(),left:((window.getScrollLeft()+window.getWidth()-pop_size.x)/2).toInt()}).reveal();
}

function facebook_fade(mode,el,destroyable){
	destroyable = destroyable || true;
	if(mode){
		el.setStyle('opacity',0);
		new Fx.Slide(el,{
			link:'chain',
			duration: 400,
			transition:Fx.Transitions.Quint.easeInOut
		}).slideIn().chain(function(){
			el.tween('opacity',1);
		});
	} else{
		el.fade('out').get('tween').chain(function(){
			new Fx.Slide(el,{
				link:'chain',
				duration: 400,
				transition:Fx.Transitions.Quint.easeInOut
			}).slideOut().chain(function(){
				if(destroyable) el.getParent().destroy(); //Destruye el div creado para el slide
			});
		});
	}
}

function tbAddRow(tabla,data,attbts){
	attbts = attbts || {};
	fila = new Element('tr',attbts);
	
	for(var i=0;i<data.length;i++){
		switch(_getType(data[i])){
		case 'string':
			new Element('td',{ html:data[i] }).inject(fila);
			break;
			
		case 'array':	
			celda = new Element('td');
			for(var j=0;j<data[i].length;j++){
				if(_getType(data[i])=='string')
					celda.set('html',data[i][j]);
				else
					celda.inject(data[i][j]);
			}
			break;
			
		case 'object':
			data[i].inject(new Element('td').inject(fila));
			break;
		}
	}
	fila.inject(tabla.getElement('tbody'));
	return fila;
}

//== CORE EXTENSION ====================================

var _getType = function(inp) {
	var type = typeof inp, match; var key;
	
	if (type == 'object' && !inp) return 'null';
	if (type == "object") {
		if (!inp.constructor) return 'object';

		var cons = inp.constructor.toString();
		if (match = cons.match(/(\w+)\(/)) cons = match[1].toLowerCase();

		var types = ["boolean", "number", "string", "array"];

		for(key in types) {
			if (cons == types[key]) { type = types[key]; break; }
		}
	}
	return type;
};
