diff --git a/opds_catalog/FB2_22_xhtml.xsl b/opds_catalog/FB2_22_xhtml.xsl
index b1f49c7c7fdb445955998556a0578c48339acc97..6c8aa5123c1b046b1fa9e0a8077c3aa809643454 100644
--- a/opds_catalog/FB2_22_xhtml.xsl
+++ b/opds_catalog/FB2_22_xhtml.xsl
@@ -28,18 +28,15 @@
 				</style>
 			</head>
 			<body>
+				<h4 align="center">
+					<xsl:value-of select="fb:description/fb:title-info/fb:book-title"/>
+				</h4>
 
-
-						<h4 align="center">
-        					<xsl:value-of select="fb:description/fb:title-info/fb:book-title"/>
-						</h4>
-
-<xsl:for-each select="fb:description/fb:title-info/fb:coverpage/fb:image">
-		<xsl:call-template name="image"/>
-		</xsl:for-each>
+				<xsl:for-each select="fb:description/fb:title-info/fb:coverpage/fb:image">
+					<xsl:call-template name="image"/>
+				</xsl:for-each>
 
 				<xsl:for-each select="fb:description/fb:title-info/fb:annotation">
-
 					<div>
 						<xsl:call-template name="annotation"/>
 					</div>
@@ -100,7 +97,7 @@
 			</xsl:when>
 			<xsl:otherwise>
 				<li>
-					<a href="#TOC_{generate-id()}"><xsl:value-of select="normalize-space(fb:title/fb:p[1] | @name)"/></a>
+					<a href="#TOC_{position()}"><xsl:value-of select="normalize-space(fb:title/fb:p[1] | @name)"/></a>
 					<xsl:if test="fb:section">
 						<ul><xsl:apply-templates select="fb:section" mode="toc"/></ul>
 					</xsl:if>
@@ -118,7 +115,7 @@
 	</xsl:template>
 
 	<xsl:template match="fb:section">
-		<a name="TOC_{generate-id()}"></a>
+		<a name="TOC_{position()}"></a>
 		<xsl:if test="@id">
 			<xsl:element name="a">
 				<xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
@@ -133,7 +130,7 @@
 		<xsl:choose>
 			<xsl:when test="count(ancestor::node()) &lt; 9">
 				<xsl:element name="{concat('h',count(ancestor::node())-3)}">
-					<a name="TOC_{generate-id()}"></a>
+					<a name="TOC_{position()}"></a>
 					<xsl:if test="@id">
 						<xsl:element name="a">
 							<xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
@@ -175,7 +172,7 @@
 	</xsl:template>
 	<!-- p -->
 	<xsl:template match="fb:p">
-		<div align="justify"><xsl:if test="@id">
+		<div id="{position()}" align="justify"><xsl:if test="@id">
 				<xsl:element name="a">
 					<xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
 				</xsl:element>
diff --git a/opds_catalog/migrations/0006_bookshelf_position.py b/opds_catalog/migrations/0006_bookshelf_position.py
new file mode 100644
index 0000000000000000000000000000000000000000..ec7655d24950bd77a89d3f5976e50c349fb12918
--- /dev/null
+++ b/opds_catalog/migrations/0006_bookshelf_position.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10 on 2017-03-27 16:10
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('opds_catalog', '0005_auto_20161204_2138'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='bookshelf',
+            name='position',
+            field=models.FloatField(default=None, null=True),
+        ),
+    ]
diff --git a/opds_catalog/models.py b/opds_catalog/models.py
index 81c9a6e04686d7171157ea48ce61cb476510120f..4ed7f9d6dfb4eeb52e7b6cad53483d5861caf3ad 100644
--- a/opds_catalog/models.py
+++ b/opds_catalog/models.py
@@ -103,6 +103,7 @@ class bookshelf(models.Model):
     user = models.ForeignKey(User, db_index=True)
     book = models.ForeignKey(Book, db_index=True)
     readtime = models.DateTimeField(null=False, default=timezone.now, db_index=True)
+    position = models.FloatField(null=True,default=None)
 
 
 class CounterManager(models.Manager):
diff --git a/sopds_web_backend/templates/BookReader.html b/sopds_web_backend/templates/BookReader.html
index 85ae0f1e081a9555d0e79239c17eedf1bdb2b2f7..bbfcbf9d8186e9de43d1b86b04cdfa325f42d319 100644
--- a/sopds_web_backend/templates/BookReader.html
+++ b/sopds_web_backend/templates/BookReader.html
@@ -1,11 +1,100 @@
 {% extends "sopds_main.html" %}
 {% load i18n %}
+{% load staticfiles %}
 {% block body %}
 <script>
 	var BookID = {{ book_id }};
-	document.addEventListener("DOMContentLoaded", function(event) { 
-		$('#ReaderBlock').load('{% url 'opds_catalog:read' book_id %}', function() {});
-	});
+	var delay = 1000;
+	var timeout = null;
+	
+	var waitForLoad = function(){
+		if (typeof jQuery != "undefined") {
+			LoadBook();
+			$(window).bind('scroll',function(){
+				clearTimeout(timeout);
+				timeout = setTimeout(function(){
+					SetPos();
+				},delay);
+			});
+		} else {
+			window.setTimeout(waitForLoad, 500);
+		}
+	};
+	window.setTimeout(waitForLoad, 500);  
+	
+	var ChangeBookDate = function(ID){
+		
+	}
+	
+	var SetPos = function(){
+		var CurrentPos = window.scrollY;
+		$.ajax( {
+			url: '{% url 'web:setpos' book_id %}?pos='+CurrentPos,
+			type: 'GET',
+			cache: false,
+			success: function() {
+				window.setTimeout(SetPos, 10000);
+			}
+		});		
+	}
+	
+	var GetPos = function(){
+		$.ajax( {
+			url: '{% url 'web:getpos' book_id %}',
+			type: 'GET',
+			cache: false,
+			success: function(data) {
+				window.scrollTo(0,data);
+			}
+		});		
+	}
+
+	var LoadBook = function(){
+		if (localStorage.getItem(BookID)){
+			$("#ReaderBlock").html(LZString.decompress(localStorage.getItem(BookID)));
+			GetPos();
+		} else {
+			$.ajax( {
+				url: '{% url 'opds_catalog:read' book_id %}',
+				type: 'GET',
+				cache: true,
+				success: function(html) {
+					$("#ReaderBlock").html(html);
+					
+					var StoreDates = {};
+					if (localStorage.getItem('StoreDates')) {
+						StoreDates = JSON.parse(localStorage.getItem('StoreDates'));
+					}
+					StoreDates[BookID] = Date();
+					
+					var CompressedBook = LZString.compress(html);
+					
+					var StoreBook = function(){
+						try {
+							localStorage.setItem(BookID,CompressedBook);
+							localStorage.setItem('StoreDates',JSON.stringify(StoreDates));
+							$('#DownloadBook').hide();
+						} catch (err) {
+							var Old = Date();
+							var OldBookID;
+							$.each(StoreDates,function(idx,el){
+								if (Old > el) {
+									Old = el;
+									OldBookID = idx;
+								}
+							})
+							localStorage.removeItem(OldBookID);
+							delete StoreDates[OldBookID];
+							StoreBook();
+						}
+					}
+					StoreBook();
+					
+					GetPos();
+				}
+			});	
+		}
+	}
 </script>
-<div id="ReaderBlock"></div>
+<div id="ReaderBlock"><div id="DownloadBook" style="text-align: center;"><img src="{% static "images/download.gif" %}" style="width: 200px;height: 200px;"></div></div>
 {% endblock %}{# body #}
\ No newline at end of file
diff --git a/sopds_web_backend/templates/sopds_main.html b/sopds_web_backend/templates/sopds_main.html
index 1b3e24993f1f511c1103175fb73e54cf44498f43..65ea1e0b6ba534fa10dd26a31209fcacec999a46 100644
--- a/sopds_web_backend/templates/sopds_main.html
+++ b/sopds_web_backend/templates/sopds_main.html
@@ -55,6 +55,7 @@
 
   <script src="{% static "js/vendor/jquery.js" %}"></script>
   <script src="{% static "js/vendor/foundation.min.js" %}"></script>
+  <script src="{% static "js/vendor/lz-string.js" %}"></script>
 
   <script>
     $(document).foundation();   
diff --git a/sopds_web_backend/urls.py b/sopds_web_backend/urls.py
index 7ff1ef3f238e9e42c30956efcc2dd5ce51489946..edc4da5c8c3a097027e0dd06e02ccfadeef970b5 100644
--- a/sopds_web_backend/urls.py
+++ b/sopds_web_backend/urls.py
@@ -1,6 +1,3 @@
-import logging
-logging.basicConfig(filename='/var/www/sopds2/example.log',level=logging.DEBUG)
-
 from django.conf.urls import url
 from sopds_web_backend import views
 
@@ -18,5 +15,7 @@ urlpatterns = [
     url(r'^logout/$',views.LogoutView, name='logout'),
     url(r'^bs/delete/$',views.BSDelView, name='bsdel'),
     url(r'^bs/clear/$', views.BSClearView, name='bsclear'),
+    url(r'^bs/setpos/(?P<book_id>[0-9]+)/$', views.BSSetPos, name='setpos'),
+    url(r'^bs/getpos/(?P<book_id>[0-9]+)/$', views.BSGetPos, name='getpos'),
     url(r'^$',views.hello, name='main'),
 ]
diff --git a/sopds_web_backend/views.py b/sopds_web_backend/views.py
index 58149822b6897e287253e885538dc79c5329072b..2267dccef16de7ef118de5e7c077889627dd04c7 100644
--- a/sopds_web_backend/views.py
+++ b/sopds_web_backend/views.py
@@ -16,6 +16,7 @@ from constance import config
 from opds_catalog.opds_paginator import Paginator as OPDS_Paginator
 
 from sopds_web_backend.settings import HALF_PAGES_LINKS
+from django.http import HttpResponse
 
 def sopds_login(function=None, redirect_field_name=REDIRECT_FIELD_NAME, url=None):
     actual_decorator = user_passes_test(
@@ -506,6 +507,29 @@ def BSDelView(request):
     
     return redirect("%s?searchtype=u"%reverse("web:searchbooks"))
 
+@sopds_login(url='web:login')
+def BSSetPos(request,book_id):
+    if request.GET:
+        pos = request.GET.get('pos', None)
+    else:
+        pos = None
+       
+    pos = float(pos)
+
+    bookshelf.objects.filter(user=request.user, book=book_id).update(position=pos)
+
+    response = HttpResponse()
+    response.write('OK')
+
+    return response
+
+@sopds_login(url='web:login')
+def BSGetPos(request,book_id):
+    pos = bookshelf.objects.get(user=request.user, book=book_id).position
+    response = HttpResponse()
+    response.write(pos)
+    return response
+
 @sopds_login(url='web:login')
 def BSClearView(request):
     bookshelf.objects.filter(user=request.user).delete()
diff --git a/static/images/download.gif b/static/images/download.gif
new file mode 100644
index 0000000000000000000000000000000000000000..0630e15099ce7a44609a569f7edaf402d6c8a0f1
Binary files /dev/null and b/static/images/download.gif differ
diff --git a/static/js/vendor/lz-string.js b/static/js/vendor/lz-string.js
new file mode 100644
index 0000000000000000000000000000000000000000..1194e25d7e9935a06891cd127288e799accc61a7
--- /dev/null
+++ b/static/js/vendor/lz-string.js
@@ -0,0 +1,506 @@
+// Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
+// This work is free. You can redistribute it and/or modify it
+// under the terms of the WTFPL, Version 2
+// For more information see LICENSE.txt or http://www.wtfpl.net/
+//
+// For more information, the home page:
+// http://pieroxy.net/blog/pages/lz-string/testing.html
+//
+// LZ-based compression algorithm, version 1.4.4
+var LZString = (function() {
+
+// private property
+var f = String.fromCharCode;
+var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
+var baseReverseDic = {};
+
+function getBaseValue(alphabet, character) {
+  if (!baseReverseDic[alphabet]) {
+    baseReverseDic[alphabet] = {};
+    for (var i=0 ; i<alphabet.length ; i++) {
+      baseReverseDic[alphabet][alphabet.charAt(i)] = i;
+    }
+  }
+  return baseReverseDic[alphabet][character];
+}
+
+var LZString = {
+  compressToBase64 : function (input) {
+    if (input == null) return "";
+    var res = LZString._compress(input, 6, function(a){return keyStrBase64.charAt(a);});
+    switch (res.length % 4) { // To produce valid Base64
+    default: // When could this happen ?
+    case 0 : return res;
+    case 1 : return res+"===";
+    case 2 : return res+"==";
+    case 3 : return res+"=";
+    }
+  },
+
+  decompressFromBase64 : function (input) {
+    if (input == null) return "";
+    if (input == "") return null;
+    return LZString._decompress(input.length, 32, function(index) { return getBaseValue(keyStrBase64, input.charAt(index)); });
+  },
+
+  compressToUTF16 : function (input) {
+    if (input == null) return "";
+    return LZString._compress(input, 15, function(a){return f(a+32);}) + " ";
+  },
+
+  decompressFromUTF16: function (compressed) {
+    if (compressed == null) return "";
+    if (compressed == "") return null;
+    return LZString._decompress(compressed.length, 16384, function(index) { return compressed.charCodeAt(index) - 32; });
+  },
+
+  //compress into uint8array (UCS-2 big endian format)
+  compressToUint8Array: function (uncompressed) {
+    var compressed = LZString.compress(uncompressed);
+    var buf=new Uint8Array(compressed.length*2); // 2 bytes per character
+
+    for (var i=0, TotalLen=compressed.length; i<TotalLen; i++) {
+      var current_value = compressed.charCodeAt(i);
+      buf[i*2] = current_value >>> 8;
+      buf[i*2+1] = current_value % 256;
+    }
+    return buf;
+  },
+
+  //decompress from uint8array (UCS-2 big endian format)
+  decompressFromUint8Array:function (compressed) {
+    if (compressed===null || compressed===undefined){
+        return LZString.decompress(compressed);
+    } else {
+        var buf=new Array(compressed.length/2); // 2 bytes per character
+        for (var i=0, TotalLen=buf.length; i<TotalLen; i++) {
+          buf[i]=compressed[i*2]*256+compressed[i*2+1];
+        }
+
+        var result = [];
+        buf.forEach(function (c) {
+          result.push(f(c));
+        });
+        return LZString.decompress(result.join(''));
+
+    }
+
+  },
+
+
+  //compress into a string that is already URI encoded
+  compressToEncodedURIComponent: function (input) {
+    if (input == null) return "";
+    return LZString._compress(input, 6, function(a){return keyStrUriSafe.charAt(a);});
+  },
+
+  //decompress from an output of compressToEncodedURIComponent
+  decompressFromEncodedURIComponent:function (input) {
+    if (input == null) return "";
+    if (input == "") return null;
+    input = input.replace(/ /g, "+");
+    return LZString._decompress(input.length, 32, function(index) { return getBaseValue(keyStrUriSafe, input.charAt(index)); });
+  },
+
+  compress: function (uncompressed) {
+    return LZString._compress(uncompressed, 16, function(a){return f(a);});
+  },
+  _compress: function (uncompressed, bitsPerChar, getCharFromInt) {
+    if (uncompressed == null) return "";
+    var i, value,
+        context_dictionary= {},
+        context_dictionaryToCreate= {},
+        context_c="",
+        context_wc="",
+        context_w="",
+        context_enlargeIn= 2, // Compensate for the first entry which should not count
+        context_dictSize= 3,
+        context_numBits= 2,
+        context_data=[],
+        context_data_val=0,
+        context_data_position=0,
+        ii;
+
+    for (ii = 0; ii < uncompressed.length; ii += 1) {
+      context_c = uncompressed.charAt(ii);
+      if (!Object.prototype.hasOwnProperty.call(context_dictionary,context_c)) {
+        context_dictionary[context_c] = context_dictSize++;
+        context_dictionaryToCreate[context_c] = true;
+      }
+
+      context_wc = context_w + context_c;
+      if (Object.prototype.hasOwnProperty.call(context_dictionary,context_wc)) {
+        context_w = context_wc;
+      } else {
+        if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
+          if (context_w.charCodeAt(0)<256) {
+            for (i=0 ; i<context_numBits ; i++) {
+              context_data_val = (context_data_val << 1);
+              if (context_data_position == bitsPerChar-1) {
+                context_data_position = 0;
+                context_data.push(getCharFromInt(context_data_val));
+                context_data_val = 0;
+              } else {
+                context_data_position++;
+              }
+            }
+            value = context_w.charCodeAt(0);
+            for (i=0 ; i<8 ; i++) {
+              context_data_val = (context_data_val << 1) | (value&1);
+              if (context_data_position == bitsPerChar-1) {
+                context_data_position = 0;
+                context_data.push(getCharFromInt(context_data_val));
+                context_data_val = 0;
+              } else {
+                context_data_position++;
+              }
+              value = value >> 1;
+            }
+          } else {
+            value = 1;
+            for (i=0 ; i<context_numBits ; i++) {
+              context_data_val = (context_data_val << 1) | value;
+              if (context_data_position ==bitsPerChar-1) {
+                context_data_position = 0;
+                context_data.push(getCharFromInt(context_data_val));
+                context_data_val = 0;
+              } else {
+                context_data_position++;
+              }
+              value = 0;
+            }
+            value = context_w.charCodeAt(0);
+            for (i=0 ; i<16 ; i++) {
+              context_data_val = (context_data_val << 1) | (value&1);
+              if (context_data_position == bitsPerChar-1) {
+                context_data_position = 0;
+                context_data.push(getCharFromInt(context_data_val));
+                context_data_val = 0;
+              } else {
+                context_data_position++;
+              }
+              value = value >> 1;
+            }
+          }
+          context_enlargeIn--;
+          if (context_enlargeIn == 0) {
+            context_enlargeIn = Math.pow(2, context_numBits);
+            context_numBits++;
+          }
+          delete context_dictionaryToCreate[context_w];
+        } else {
+          value = context_dictionary[context_w];
+          for (i=0 ; i<context_numBits ; i++) {
+            context_data_val = (context_data_val << 1) | (value&1);
+            if (context_data_position == bitsPerChar-1) {
+              context_data_position = 0;
+              context_data.push(getCharFromInt(context_data_val));
+              context_data_val = 0;
+            } else {
+              context_data_position++;
+            }
+            value = value >> 1;
+          }
+
+
+        }
+        context_enlargeIn--;
+        if (context_enlargeIn == 0) {
+          context_enlargeIn = Math.pow(2, context_numBits);
+          context_numBits++;
+        }
+        // Add wc to the dictionary.
+        context_dictionary[context_wc] = context_dictSize++;
+        context_w = String(context_c);
+      }
+    }
+
+    // Output the code for w.
+    if (context_w !== "") {
+      if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
+        if (context_w.charCodeAt(0)<256) {
+          for (i=0 ; i<context_numBits ; i++) {
+            context_data_val = (context_data_val << 1);
+            if (context_data_position == bitsPerChar-1) {
+              context_data_position = 0;
+              context_data.push(getCharFromInt(context_data_val));
+              context_data_val = 0;
+            } else {
+              context_data_position++;
+            }
+          }
+          value = context_w.charCodeAt(0);
+          for (i=0 ; i<8 ; i++) {
+            context_data_val = (context_data_val << 1) | (value&1);
+            if (context_data_position == bitsPerChar-1) {
+              context_data_position = 0;
+              context_data.push(getCharFromInt(context_data_val));
+              context_data_val = 0;
+            } else {
+              context_data_position++;
+            }
+            value = value >> 1;
+          }
+        } else {
+          value = 1;
+          for (i=0 ; i<context_numBits ; i++) {
+            context_data_val = (context_data_val << 1) | value;
+            if (context_data_position == bitsPerChar-1) {
+              context_data_position = 0;
+              context_data.push(getCharFromInt(context_data_val));
+              context_data_val = 0;
+            } else {
+              context_data_position++;
+            }
+            value = 0;
+          }
+          value = context_w.charCodeAt(0);
+          for (i=0 ; i<16 ; i++) {
+            context_data_val = (context_data_val << 1) | (value&1);
+            if (context_data_position == bitsPerChar-1) {
+              context_data_position = 0;
+              context_data.push(getCharFromInt(context_data_val));
+              context_data_val = 0;
+            } else {
+              context_data_position++;
+            }
+            value = value >> 1;
+          }
+        }
+        context_enlargeIn--;
+        if (context_enlargeIn == 0) {
+          context_enlargeIn = Math.pow(2, context_numBits);
+          context_numBits++;
+        }
+        delete context_dictionaryToCreate[context_w];
+      } else {
+        value = context_dictionary[context_w];
+        for (i=0 ; i<context_numBits ; i++) {
+          context_data_val = (context_data_val << 1) | (value&1);
+          if (context_data_position == bitsPerChar-1) {
+            context_data_position = 0;
+            context_data.push(getCharFromInt(context_data_val));
+            context_data_val = 0;
+          } else {
+            context_data_position++;
+          }
+          value = value >> 1;
+        }
+
+
+      }
+      context_enlargeIn--;
+      if (context_enlargeIn == 0) {
+        context_enlargeIn = Math.pow(2, context_numBits);
+        context_numBits++;
+      }
+    }
+
+    // Mark the end of the stream
+    value = 2;
+    for (i=0 ; i<context_numBits ; i++) {
+      context_data_val = (context_data_val << 1) | (value&1);
+      if (context_data_position == bitsPerChar-1) {
+        context_data_position = 0;
+        context_data.push(getCharFromInt(context_data_val));
+        context_data_val = 0;
+      } else {
+        context_data_position++;
+      }
+      value = value >> 1;
+    }
+
+    // Flush the last char
+    while (true) {
+      context_data_val = (context_data_val << 1);
+      if (context_data_position == bitsPerChar-1) {
+        context_data.push(getCharFromInt(context_data_val));
+        break;
+      }
+      else context_data_position++;
+    }
+    return context_data.join('');
+  },
+
+  decompress: function (compressed) {
+    if (compressed == null) return "";
+    if (compressed == "") return null;
+    return LZString._decompress(compressed.length, 32768, function(index) { return compressed.charCodeAt(index); });
+  },
+
+  _decompress: function (length, resetValue, getNextValue) {
+    var dictionary = [],
+        next,
+        enlargeIn = 4,
+        dictSize = 4,
+        numBits = 3,
+        entry = "",
+        result = [],
+        i,
+        w,
+        bits, resb, maxpower, power,
+        c,
+        data = {val:getNextValue(0), position:resetValue, index:1};
+
+    for (i = 0; i < 3; i += 1) {
+      dictionary[i] = i;
+    }
+
+    bits = 0;
+    maxpower = Math.pow(2,2);
+    power=1;
+    while (power!=maxpower) {
+      resb = data.val & data.position;
+      data.position >>= 1;
+      if (data.position == 0) {
+        data.position = resetValue;
+        data.val = getNextValue(data.index++);
+      }
+      bits |= (resb>0 ? 1 : 0) * power;
+      power <<= 1;
+    }
+
+    switch (next = bits) {
+      case 0:
+          bits = 0;
+          maxpower = Math.pow(2,8);
+          power=1;
+          while (power!=maxpower) {
+            resb = data.val & data.position;
+            data.position >>= 1;
+            if (data.position == 0) {
+              data.position = resetValue;
+              data.val = getNextValue(data.index++);
+            }
+            bits |= (resb>0 ? 1 : 0) * power;
+            power <<= 1;
+          }
+        c = f(bits);
+        break;
+      case 1:
+          bits = 0;
+          maxpower = Math.pow(2,16);
+          power=1;
+          while (power!=maxpower) {
+            resb = data.val & data.position;
+            data.position >>= 1;
+            if (data.position == 0) {
+              data.position = resetValue;
+              data.val = getNextValue(data.index++);
+            }
+            bits |= (resb>0 ? 1 : 0) * power;
+            power <<= 1;
+          }
+        c = f(bits);
+        break;
+      case 2:
+        return "";
+    }
+    dictionary[3] = c;
+    w = c;
+    result.push(c);
+    while (true) {
+      if (data.index > length) {
+        return "";
+      }
+
+      bits = 0;
+      maxpower = Math.pow(2,numBits);
+      power=1;
+      while (power!=maxpower) {
+        resb = data.val & data.position;
+        data.position >>= 1;
+        if (data.position == 0) {
+          data.position = resetValue;
+          data.val = getNextValue(data.index++);
+        }
+        bits |= (resb>0 ? 1 : 0) * power;
+        power <<= 1;
+      }
+
+      switch (c = bits) {
+        case 0:
+          bits = 0;
+          maxpower = Math.pow(2,8);
+          power=1;
+          while (power!=maxpower) {
+            resb = data.val & data.position;
+            data.position >>= 1;
+            if (data.position == 0) {
+              data.position = resetValue;
+              data.val = getNextValue(data.index++);
+            }
+            bits |= (resb>0 ? 1 : 0) * power;
+            power <<= 1;
+          }
+
+          dictionary[dictSize++] = f(bits);
+          c = dictSize-1;
+          enlargeIn--;
+          break;
+        case 1:
+          bits = 0;
+          maxpower = Math.pow(2,16);
+          power=1;
+          while (power!=maxpower) {
+            resb = data.val & data.position;
+            data.position >>= 1;
+            if (data.position == 0) {
+              data.position = resetValue;
+              data.val = getNextValue(data.index++);
+            }
+            bits |= (resb>0 ? 1 : 0) * power;
+            power <<= 1;
+          }
+          dictionary[dictSize++] = f(bits);
+          c = dictSize-1;
+          enlargeIn--;
+          break;
+        case 2:
+          return result.join('');
+      }
+
+      if (enlargeIn == 0) {
+        enlargeIn = Math.pow(2, numBits);
+        numBits++;
+      }
+
+      if (dictionary[c]) {
+        entry = dictionary[c];
+      } else {
+        if (c === dictSize) {
+          entry = w + w.charAt(0);
+        } else {
+          return null;
+        }
+      }
+      result.push(entry);
+
+      // Add w+entry[0] to the dictionary.
+      dictionary[dictSize++] = w + entry.charAt(0);
+      enlargeIn--;
+
+      w = entry;
+
+      if (enlargeIn == 0) {
+        enlargeIn = Math.pow(2, numBits);
+        numBits++;
+      }
+
+    }
+  }
+};
+  return LZString;
+})();
+
+if (typeof define === 'function' && define.amd) {
+  define(function () { return LZString; });
+} else if( typeof module !== 'undefined' && module != null ) {
+  module.exports = LZString
+} else if( typeof angular !== 'undefined' && angular != null ) {
+  angular.module('LZString', [])
+  .factory('LZString', function () {
+    return LZString;
+  });
+}