/* Copyright (c) 2012-2015 The Tagspaces Authors. All rights reserved.
 * Use of this source code is governed by a AGPL3 license that
 * can be found in the LICENSE file. */
/* global define  */
define(function(require, exports, module) {
  'use strict';
  var TSCORE = require('tscore');
  var TSPOSTIO = require('tspostioapi');

  console.log('Loading utils.js ...');

  var TSCORE = require('tscore');

  //Conversion utility  
  function arrayBufferToDataURL(arrayBuffer, mime) {
    var blob = new Blob([arrayBuffer], {type: mime});
    var url = window.URL || window.webkitURL;
    return url.createObjectURL(blob);
  }

  function base64ToArrayBuffer(base64) {
    var bstr = window.atob(base64);
    var bytes = new Uint8Array(bstr.length);
    for (var i = 0; i < bstr.length; i++)        {
      bytes[i] = bstr.charCodeAt(i);
    }
    return bytes.buffer;
  }

  function dataURLtoBlob(dataURI) {
    var arr = dataURI.split(','), mime = arr[0].match(/:(.*?);/)[1];
    var arrBuff = base64ToArrayBuffer(arr[1]);
    return new window.Blob([arrBuff], {type:mime});
  }

  function getBase64Image(imgURL) {
    var canvas = document.createElement("canvas");
    var img = new Image();
    img.crossOrigin = 'anonymous';
    img.src = imgURL;
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    return canvas.toDataURL("image/png");
  }

  function arrayBufferToStr(buf) {
    var str = '',
    bytes = new Uint8Array(buf);
    for (var i = 0; i < bytes.length; i++) {
      str += String.fromCharCode(bytes[i]);
    }
    return decodeURIComponent(escape(str));
  }
  
  function arrayBufferToBuffer(ab) {
    var buffer = new Buffer(ab.byteLength);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buffer.length; ++i) {
      buffer[i] = view[i];
    }
    return buffer;
  }

  function baseName(dirPath) {
    var fileName = dirPath.substring(dirPath.lastIndexOf(TSCORE.dirSeparator) + 1, dirPath.length);
    return fileName ? fileName : dirPath;
  }

  function dirName(dirPath) {

    return dirPath.replace(/\\/g, '/').replace(/\/[^\/]*$/, '');
  }

  function getFileExt(fileURL) {
    var ext = fileURL.split('.').pop();
    return (ext === fileURL) ? "" : ext;
  }

  function getURLParameter(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    }
    return false;
  }

  /*function getParameterByName(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
      results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
  }*/

  exports.arrayBufferToDataURL = arrayBufferToDataURL;
  exports.base64ToArrayBuffer = base64ToArrayBuffer;
  exports.dataURLtoBlob = dataURLtoBlob;
  exports.getBase64Image = getBase64Image;
  exports.arrayBufferToStr = arrayBufferToStr;
  exports.baseName  = baseName;
  exports.dirName = dirName;
  exports.getFileExt = getFileExt;
  exports.arrayBufferToBuffer = arrayBufferToBuffer;
  exports.getURLParameter = getURLParameter;

});

@@ -22,9 +22,9 @@
 -201
-5
+6
  The
@@ -167,16 +167,18 @@
 ile. */%0A
+%0A%0A
 /* globa
@@ -2577,18 +2577,16 @@
 %0A  %7D%0A%0A  
-/*
 function
@@ -2590,294 +2590,377 @@
 ion 
-getParameterByName(name) %7B%0A    name = name.replace(/%5B%5C%5B%5D/, '%5C%5C%5B').replace(/%5B%5C%5D%5D/, '%5C%5C%5D');%0A    var regex = new RegExp('%5B%5C%5C?&%5D' + name + '=(%5B%5E&#%5D*)'),%0A      results = regex.exec(location.search);%0A
+isVisibleOnScreen(element) %7B%0A    var rectangle = element.getBoundingClientRect();%0A    var isVisible = (%0A      rectangle.top %3E= 0 &&%0A      rectangle.left %3E= 0 &&%0A      rectangle.bottom %3C= (window.innerHeight %7C%7C document.documentElement.clientHeight) &&%0A 
     
+ 
 re
-turn results === null ? '' : decodeURIComponent(results%5B1%5D.replace(/%5C+/g, ' '))
+ctangle.right %3C= (window.innerWidth %7C%7C document.documentElement.clientWidth)%0A    );%0A    return isVisible
 ;%0A  %7D
-*/
 %0A%0A  
@@ -3389,14 +3389,63 @@
 rameter;
+%0A  exports.isVisibleOnScreen = isVisibleOnScreen;
 %0A%0A%7D);%0A

adding new function isVisibleOnScreen