text: // Copyright (c) 2012,2013 Peter Coles - http://mrcoles.com/ - All rights reserved.
  // Use of this source code is governed by the MIT License found in LICENSE
  
  
  //
  // State fields
  //
  
  var currentTab, // result of chrome.tabs.query of current active tab
      resultWindowId; // window id for putting resulting images
  
  
  //
  // Utility methods
  //
  
  function $(id) { return document.getElementById(id); }
  function show(id) { $(id).style.display = 'block'; }
  function hide(id) { $(id).style.display = 'none'; }
  
  
  function getFilename(contentURL) {
      var name = contentURL.split('?')[0].split('#')[0];
      if (name) {
          name = name
              .replace(/^https?:\/\//, '')
              .replace(/[^A-z0-9]+/g, '-')
              .replace(/-+/g, '-')
              .replace(/^[_\-]+/, '')
              .replace(/[_\-]+$/, '');
          name = '-' + name;
      } else {
          name = '';
      }
      //return 'screencapture' + name + '-' + Date.now() + '.png';
      return name + '-' + Date.now() + '.png';
  }
  
  
  //
  // Capture Handlers
  //
  
  
  function displayCaptures(filenames) {
      if (!filenames || !filenames.length) {
          show('uh-oh');
          return;
      }
  
      _displayCapture(filenames);
  }
  
  
  function _displayCapture(filenames, index) {
      index = index || 0;
  
      var filename = filenames[index];
      var last = index === filenames.length - 1;
  
      if (currentTab.incognito && index === 0) {
          // cannot access file system in incognito, so open in non-incognito
          // window and add any additional tabs to that window.
          //
          // we have to be careful with focused too, because that will close
          // the popup.
          chrome.windows.create({
              url: filename,
              incognito: false,
              focused: last
          }, function(win) {
              resultWindowId = win.id;
          });
      } else {
          chrome.tabs.create({
              url: filename,
              active: last,
              windowId: resultWindowId,
              openerTabId: currentTab.id,
              index: (currentTab.incognito ? 0 : currentTab.index) + 1 + index
          });
      }
  
      chrome.downloads.download({
          url: filename
          //filename: "Downloads/Medata"
      });
  
      if (!last) {
          _displayCapture(filenames, index + 1);
      }
  }
  
  
  function errorHandler(reason) {
      show('uh-oh'); // TODO - extra uh-oh info?
  }
  
  
  function progress(complete) {
      if (complete === 0) {
          // Page capture has just been initiated.
          show('loading');
      }
      else {
          $('bar').style.width = parseInt(complete * 100, 10) + '%';
      }
  }
  
  
  function splitnotifier() {
      show('split-image');
  }
  
  
  //
  // start doing stuff immediately! - including error cases
  //
  
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      var tab = tabs[0];
      currentTab = tab; // used in later calls to get tab info
  
      var filename = getFilename(tab.url);
  
      CaptureAPI.captureToFiles(tab, filename, displayCaptures,
                                errorHandler, progress, splitnotifier);
  });
  