xbook.html 7.58 KB
Newer Older
Piotr Mitros committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
Copyright (c) 2012 Jeff Kent, https://jeffkent.net/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
<title>6.002x Textbook for Kindle</title>
<script type="text/javascript" src="https://mitxstatic.s3.amazonaws.com/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
<script type="text/javascript">
var exhibit = Object();
exhibit.width = 960;
exhibit.height = 1080;
exhibit.ratio = exhibit.width / exhibit.height;
exhibit.current = 1;
exhibit.first = 1;
exhibit.last = 1008;
exhibit.url = "https://mitxstatic.s3.amazonaws.com/book_images/p{0}.png";
exhibit.format = Array();
exhibit.format[0] = {width: 550,
                     height: 975,
                     left_odd: -117,
                     left_even: -295,
                     top: -55,
                     ratio: 550/975};
exhibit.format[1] = {width: 775,
                     height: 975,
                     left_odd: -117,
                     left_even: -70,
                     top: -55,
                     ratio: 775/975};
exhibit.format[2] = {width: 960,
                     height: 1080,
                     left_odd: 0,
                     left_even: 0,
                     top: 0,
                     ratio: 960/1080};

var format_mode = 0;

function zeropad(num, size){
  var s = num+"";
  while (s.length < size)
    s = "0" + s;
  return s;
}

function load(n){
  var pg = 0;
  if (exhibit.current <= 1)
    pg = exhibit.current + n;
  else if (exhibit.current < exhibit.last)
    pg = exhibit.current - 1 + n;
  else
    pg = exhibit.last - 2 + n;

  var url = exhibit.url.replace("{0}", zeropad(pg, 3));
  var div = $("#exhibit" + n);
  var img = $("#exhibit" + n + " img");
  div.css({width:exhibit.format[format_mode].width + "px",
           height:exhibit.format[format_mode].height + "px"});
  img.css({position:"relative",
           width:exhibit.width + "px",
           height:exhibit.height + "px",
           top:exhibit.format[format_mode].top + "px"});
  if (pg % 2)
    img.css({left:exhibit.format[format_mode].left_odd + "px"});
  else
    img.css({left:exhibit.format[format_mode].left_even + "px"});
  img.attr("src", url);
}

function load_all(){
  if (exhibit.current <= 1) {
    load(0);
    load(1);
    load(2);
  } else {
    load(1);
    load(2);
    load(0);
  }
}

function shift_up(){
  $("#exhibit0").html("");
  $("#exhibit1 img").appendTo("#exhibit0");
  $("#exhibit1").html("");
  $("#exhibit2 img").appendTo("#exhibit1");
  $(window).scrollTop($(window).scrollTop() - exhibit.format[format_mode].height - 15);
  $("#exhibit2").html("<img>");
  load(2);
}

function shift_down(){
  $("#exhibit2").html("");
  $("#exhibit1 img").appendTo("#exhibit2");
  $("#exhibit1").html("");
  $("#exhibit0 img").appendTo("#exhibit1");
  $(window).scrollTop($(window).scrollTop() + exhibit.format[format_mode].height + 15);
  $("#exhibit0").html("<img>");
  load(0);
}

function update_status(){
  $("#status").html("&nbsp; " + exhibit.current + "/" + exhibit.last);
}

function goto_page(pg, offset){
  offset = typeof offset !== 'undefined' ? offset : 0;

  exhibit.current = pg;
  load_all();
  if (exhibit.current <= 1)
    $(document).scrollTop(offset);
  else if (exhibit.current < exhibit.last)
    $(document).scrollTop((exhibit.format[format_mode].height + 15) + offset);
  else
    $(document).scrollTop((exhibit.format[format_mode].height + 15) * 2 + offset);

  $.cookie('exhibit.current', exhibit.current, {expires: 7});
  $.cookie('exhibit.offset', 0, {expires: 7});
}

function get_offset(){
  var offset;

  if (exhibit.current <= 1)
    offset = $(document).scrollTop();
  else if (exhibit.current < exhibit.last)
    offset = $(document).scrollTop() - (exhibit.format[format_mode].height + 15);
  else
    offset = $(document).scrollTop() - (exhibit.format[format_mode].height + 15) * 2;

  return offset;
}

$(function(){
  $("#container").width(exhibit.format[format_mode].width);
  setTimeout(function(){
    var current = parseInt($.cookie("exhibit.current"),10);
    var offset = parseInt($.cookie("exhibit.offset"),10);
    current = (isNaN(current) || current < 1 || current > exhibit.last) ? 3 : current;
    offset = isNaN(offset) ? 0 : offset;

    goto_page(current, offset);
    update_status();
  },1);
});

$(window).keydown(function(e){
  if (e.keyCode == 67) {        /* [c]ontents */
    goto_page(9);
  } else if (e.keyCode == 70) { /* [f]ormat */
    if (format_mode < 2)
      format_mode += 1;
    else
      format_mode = 0;
    $("#container").width(exhibit.format[format_mode].width);
    load_all();
  } else if (e.keyCode == 71) { /* [g]oto */
    var pg = prompt("Goto page: ");
    if (pg !== null)
      if (pg > 0 && pg <= exhibit.last)
        goto_page(parseInt(pg,10));
  } else if (e.keyCode == 73) { /* [i]ndex */
    goto_page(997);
  }
});

$(window).scroll(function(){
  setTimeout(function(){
    if (exhibit.current <= 1) {
      if ($(window).scrollTop() + $(window).height()/2 >= (exhibit.format[format_mode].height + 15))
        exhibit.current += 1;
    } else if (exhibit.current <= exhibit.last - 1) {
      if ($(window).scrollTop() + $(window).height()/2 < (exhibit.format[format_mode].height + 15)) {
        exhibit.current -= 1;
        if (exhibit.current != 1)
	  shift_down();
      } else if ($(window).scrollTop() + $(window).height()/2 >= (exhibit.format[format_mode].height + 15) * 2) {
        exhibit.current += 1;
        if (exhibit.current != exhibit.last)
	  shift_up();
      }
    } else if (exhibit.current >= exhibit.last) {
      if ($(window).scrollTop() + $(window).height()/2 < (exhibit.format[format_mode].height + 15) * 2)
        exhibit.current -= 1;
    }
    $.cookie('exhibit.current', exhibit.current, {expires: 7});
    $.cookie('exhibit.offset', get_offset(), {expires: 7});
    update_status();
  },1);
});
</script>
<style type="text/css">
body {
  background-color: #333;
  margin: 15px 0;
}
#status {
  position: fixed;
  bottom: 0;
  left: 0;
  background-color: black;
  color: white;
  width: 100%;
  z-index: 100;
}
#help {
  position: fixed;
  bottom: 0;
  right: 0;
  color: white;
  z-index:100;
}
#container {
  overflow: hidden;
  margin: 0 auto;
}
#exhibit0, #exhibit1, #exhibit2 {
  overflow:hidden;
}
.spacer {
  height: 15px;
}
</style>
</head>
<body>
<div id="container">
<div id="exhibit0"><img /></div>
<div class="spacer"></div>
<div id="exhibit1"><img /></div>
<div class="spacer"></div>
<div id="exhibit2"><img /></div>
</div>
<div id="status">&nbsp;</div>
<div id="help">[g]oto [f]ormat [c]ontents [i]ndex &nbsp;</div>
<div>&nbsp;<div>
</body>
</html>