
/*==============================================================================
  ------------------------------- TOOLTIPS -------------------------------------
 ============================================================================= */

//Muestra un tooltip en aquellos elementos que tienen definida la clase "tooltip"

function addTooltips(){
   $('.tooltip').each(function(i, element){
       var tooltipButton = $(this);
       var position = tooltipButton.position();
       var singleLine = tooltipButton.hasClass('single-line');

       var tooltip      = $('<div>').addClass('tooltip-container');
       var tooltipLeft  = $('<div>').addClass('tooltip-left tooltip-text').appendTo(tooltip).html(tooltipButton.html());
       var tooltipRight = $('<div>').addClass('tooltip-right').appendTo(tooltip);

       if(tooltipLeft.text().length < 22) tooltipLeft.css({paddingTop: 12})

       tooltip.insertAfter(tooltipButton).css({position: 'absolute', left: position.left, top: position.top}).fadeOut(0);
       tooltipButton.mouseenter(function(){
           tooltip.fadeIn(0, function(){
               tooltip.attr('ready', true);
           });
       });
       tooltipButton.mouseout(function(){
           if(tooltip.attr('ready')) tooltip.fadeOut(0,function(){tooltip.attr('ready', false)});
       });
   });
}
function removeTooltips(){
   $('.tooltip-container').remove();
}
function updateTooltips(){
   removeTooltips();
   addTooltips();
}

//Bind events to document ready and window resize
$(function(){
    addTooltips();
});
$(window).resize(function(){
    updateTooltips();
});


/*==============================================================================
  ----------------------------- PLACEHOLDERS -----------------------------------
 ============================================================================= */

/*
    Simula el comportamiento del atributo "placeholder" de HTML5 de los elementos input (text, password, textarea)
    Si el elemento tenia un valor inicial, cuando el usuario hace click en él este se guarda y se borra.
    Cuando el elemento pierde el foco, si está vacío, vuelve a tomar el valor original
*/
function addPlaceholders(){
   $('.placeholder:input').each(function(i, element){
       var element = $(this);
       var type = element.attr('type');
       var value = element.val();
       var title = element.attr('title');
       var placeholder = title || value;

       if(title && !value){
           element.attr('value', title);
       }

       element.attr('placeholder', placeholder)
       element.attr('disabled', false);

       if(element.val() != placeholder){
          element.removeClass('placeholder');
       }
       
       if(type == 'password'){
           var replace_element = $('<input type="text">').attr({className: element.attr('className'), style: element.attr('style')});
           replace_element.val(placeholder);
           replace_element.focus(function(){
               replace_element.hide();
               element.show();
               element.focus();
           });
           element.hide();
           element.before(replace_element);
       }
       element.focus(function(){
           if(element.val() == placeholder){
               element.val('').removeClass('placeholder');
           }
       });
       element.blur(function(){
          if($.trim(element.val()) == ''){
              if(type == 'password'){
                  element.hide();
                  replace_element.show();
              } else {
                  element.val(placeholder);
              }
              element.addClass('placeholder');
          }
       });
   });
   
   $('form').submit(function(){
      var placeholder = $('.placeholder:input', this);
      placeholder.each(function(i, element){
          var esto = $(this);
          if(esto.val() == esto.attr('placeholder')){
              esto.val('');
          }
      });
      return true;
   });
};

$(function(){
    addPlaceholders();
});



function actual_date(){
	fecha = new Date()
	mes = fecha.getMonth()
	diaMes = fecha.getDate()
	diaSemana = fecha.getDay()
	anio = fecha.getFullYear()
	dias = new Array('Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sábado')
	meses = new Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre')
	return dias[diaSemana] + ", " + diaMes + " de " + meses[mes] + " de " + anio;
}

$(function(){
    $('#header .today .date').text(actual_date());
});


jQuery.fn.equalCols = function(){
	//Array Sorter
	var sortNumber = function(a,b){return b - a;};
	var heights = [];
	//Push each height into an array
	$(this).each(function(){
		heights.push($(this).height());
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function(){
		//Set each column to the max height
		$(this).attr('style', 'height: '+maxHeight+'px !important');
	});
};


/*==============================================================================
  ----------------------------- GRID ACTIONS -----------------------------------
 ============================================================================= */

$(function(){
    $('.grid-actions :checkbox').click(function(){
        $(':checkbox.grid-action-checkbox').attr('checked', this.checked);
    });
});

/*==============================================================================
  ---------------------------- IGUALAR COLUMNAS --------------------------------
 ============================================================================= */

$(function(){
    var equal_columns = $('.equal-columns');
    var height = 0;
    equal_columns.each(function(){
        var current_column = $(this);
        if(current_column.outerHeight() > height){
            height = current_column.outerHeight();
        }
    });
    equal_columns.height(height);
});
