(function($) {
  $.translate = function(word, path) {
    if(!path && !word) {
      return false;
    }
    if(!path) {
      path = '';
    }
    var translation = word;
    if ($.translations && $.translations[path] && $.translations[path][word]) {
      translation = $.translations[path][word];
    } else {
      $.post('json.php?type=translate', {
        path : path,
        word : word
      });
    }
    return translation;
  };
})(jQuery);
$.unique_id = function() {
  return ((new Date()).getTime() + "" + Math.floor(Math.random() * 1000000)).substr(0, 18);
};

// Nicer margins for images in def blocks
(function($){
  $(".def img").each(function() {
    if ( $(this).css("float") == "left" ) {
      $(this).css("margin-left", "0px");
    } else if ( $(this).css("float") == "right" ) {
      $(this).css("margin-right", 0);
    }
  });
})(jQuery);

(function($) {
  var userAgent = navigator.userAgent.toString().toLowerCase();
  if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1) && (navigator.platform=="Win32" || navigator.platform=="Win64")) {
    $("body").addClass("safari");
  }
})(jQuery);

/*Font size*/
(function($){
  //$.cookie('site_font_size', null);
  var currentFontSize = 100; // should be taken from cookie
  //console.debug($.cookie('site_font_size'));
  if($.cookie('site_font_size')) {
    currentFontSize = parseInt($.cookie('site_font_size'));
  }
  
  var body = $("body").addClass("siteFontSize"+currentFontSize);
  var fontchanger = $("#fontchanger");
  var minFontSize = 70;
  var maxFontSize = 120;

  function attachEvents() {
    $(".increase", fontchanger).click(function() {
      
      if (currentFontSize+10<=maxFontSize) {
        currentFontSize += 10;
        body.attr("class", "siteFontSize"+currentFontSize);
        $.cookie('site_font_size', currentFontSize, {
          expires: 7,
          path: '/'
        });
      }

    });
    $(".decrease", fontchanger).click(function() {
      if (currentFontSize-10>=minFontSize) {
        currentFontSize -= 10;
        body.attr("class", "siteFontSize"+currentFontSize);
        $.cookie('site_font_size', currentFontSize, {
          expires: 7,
          path: '/'
        });
      }
    });
  }

  attachEvents();

})(jQuery);

(function($){
  $.showFileInfo = function () {
    $(".def a").each(function() {
      if($(this).attr('rel').length) {
        var rel = $(this).attr('rel').split('|');
        var size = rel[0];
        var type = $.translate(rel[1].replace('/', '_'), 'editor.filetypes');
        var data = ' (' + type + ', ' + size + ')';
        $(this).after(data);
      }
    });
  };
})(jQuery);

