// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/*
Default timeout is 10s
*/

function showMessage(messages, timeout)
{
  jQuery.growl.settings.displayTimeout = timeout || 10000;
  jQuery.growl.settings.noticeTemplate = ''
  + '<div class="notification" style="border: 1px solid #c3c3c3; padding: 10px; background-color: #ffffff;">'
  //+     '<div class="%type%" rel="close">'
  +               '<div class="content" style="font-size: 14px; font-weight: bold; font-family: Helvetica Neue, Arial;">%message%</div>'
  //+     '</div>'
  +   '</div>';
  jQuery.growl.settings.noticeCss = {
    position: 'relative'
  };
  
  for (type in messages) {
    jQuery.growl(messages[type], type);
  }
};

function size_format (filesize)
{
  if (filesize >= 1073741824) {
       filesize = Math.floor(filesize / 1073741824) + ' Gb';
  } else { 
    if (filesize >= 1048576) {
         filesize = Math.floor(filesize / 1048576) + ' Mb';
     } else { 
      if (filesize >= 1024) {
        filesize = Math.floor(filesize / 1024) + ' Kb';
      } else {
        filesize = filesize + ' bytes';
      };
     };
  };
  return filesize;
};

function showLoader(text) 
{
  text = text || 'Loading';
  showMessage({info: text + '<img src="/images/loading.gif" alt="' + text + '" />'}, 3600000); // 1h timeout
}

function hideLoader()
{
  jQuery('.notification').remove();
}

$(document).ready(function(){
  $(':button, :submit').focus(function(){$(this).blur()});
  $(':button, :submit').mouseout(function(){
        $(this).parents('.form_button_left, .form_button_right').removeClass('active');
      }).mouseover(function(){
        $(this).parents('.form_button_left, .form_button_right').addClass('active');
      });


});

Date.prototype.distance_of_time_in_words = function(to) {
  distance_in_milliseconds = to - this;
  distance_in_minutes = Math.round(Math.abs(distance_in_milliseconds / 60000));
  if (distance_in_minutes == 0) {
    words = "less than a minute";
  } else if (distance_in_minutes == 1) {
    words = "1 minute";
  } else if (distance_in_minutes < 45) {
    words = distance_in_minutes + " minutes";
  } else if (distance_in_minutes < 90) {
    words = "about 1 hour";
  } else if (distance_in_minutes < 1440) {
    words = "about " + Math.round((distance_in_minutes / 60)) + " hours";
  } else if (distance_in_minutes < 2160) {
    words = "about 1 day";
  } else if (distance_in_minutes < 43200) {
    words = Math.round((distance_in_minutes / 1440)) + " days";
  } else if (distance_in_minutes < 86400) {
    words = "about 1 month";
  } else if (distance_in_minutes < 525600) {
    words = Math.round((distance_in_minutes / 43200)) + " months";
  } else if (distance_in_minutes < 1051200) {
    words = "about 1 year";
  } else {
    words = "over " + Math.round((distance_in_minutes / 525600)) + " years";
  }
  return words + " ago";
};
Date.prototype.time_ago_in_words = function() {
  return this.distance_of_time_in_words(Date.parse((new Date()).toUTCString()));
};

function updateDate() {
  jQuery("[data-time]").each(function (index){
    var date = new Date(parseInt(jQuery(this).attr('data-time')));
    jQuery(this).text(date.time_ago_in_words());
  });
};

jQuery.fn.exists = function() {
  return jQuery(this).length > 0;
};

function makeValid(what,mess, valid)
{
  if(msg = $('#'+what+'_box_message'))
  {
    alt = $('#'+what+'_box_alert');
    msg.html(mess);
    alt.addClass(valid ? 't_success' : 't_error');
    alt.removeClass(!valid ? 't_success' : 't_error');
    
    $('#user_'+what+'_welcome').addClass(valid ? 'success' : 'error')
    $('#user_'+what+'_welcome').removeClass(!valid ? 'success' : 'error')
  }
}
