var BLOG_ROOT = '/blog';

var wpo        = null;
var wpoSyncing = null;
var wpoStatus  = null;
var wpoLink    = null;
var wpoText    = null;
var isSyncing  = false;
var isCanceled = false;
var progressBar = null;

if($.browser.mozilla) {
$(document).ready( function() {
  wpo = document.getElementById('wp-off');

  // Check if gears is installed
  if(!window.google || !google.gears) {
    wpo.className = 'wp-off-disabled';
    return;
  }

  try {
    var localServer = google.gears.factory.create('beta.localserver', '1.0');
  } catch (ex) {
    setError('Could not create local server: ' + ex.message);
    return;
  }

  // Google Gears is installed
  wpoLink = document.createElement('a');
  wpoLink.href = "#";
  wpoStatus = document.createElement('span');
  wpoStatus.id = 'wp-off-status';

  if(localServer && localServer.canServeLocally(window.location.href)) {
    wpoText = document.createTextNode('Read online');
    wpoLink.onclick = wpo_remove_store;
    wpoStatus.className = 'wp-off-disconnected';
  } else {
    wpoText = document.createTextNode('Read offline');
    wpoLink.onclick = wpo_init;
    wpoStatus.className = 'wp-off-connected-synced';
  }
  wpoLink.appendChild(wpoText);
  wpoLink.appendChild(wpoStatus);
  wpo.appendChild(wpoLink);

  wpo.className = 'wp-off-enabled';
});
}

function wpo_init(e) {
  e.preventDefault();

  if(isSyncing) {
    return false;
  }
  isSyncing = true;

  wpoSyncing = document.createElement('div');
  wpoSyncing.id = 'wp-off-syncing'; 
  wpo.appendChild(wpoSyncing);

  wpoStatus.className = 'wp-off-connected-syncing';

  wpo.className = 'wp-off-syncing';

  var progress = document.createElement('div');
  progress.id = 'wp-off-progress';
  wpoSyncing.appendChild(progress);

  createManagedStore();

  progressBar = new wpo_ProgressBar(progress, 180, 11, null, wpo_cancel);
  var timerId = window.setInterval(function() {
    if(isCanceled) {
      window.clearInterval(timerId);
    }
    if(progressBar.step >= 0.75) {
      window.clearInterval(timerId);
    }
    
    progressBar.setPercent(progressBar.step);
    progressBar.step += 0.01;
  }, 500);

  return false;
}

function wpo_cancel(e) {
  isCanceled = true;
  e.preventDefault();
  
  wpoStatus.className = 'wp-off-connected-synced';
  wpo.className = 'wp-off-enabled';
  wpo.removeChild(wpoSyncing);
  wpoLink.onclick = wpo_init;
  wpoText.data = 'Read offline';
  isSyncing = false;
  return false;
}

function wpo_complete() {
  var timerId = window.setInterval(function() {
    if(isCanceled) {
      window.clearInterval(timerId);
    }
    if(progressBar.step >= 1.0) {
      window.clearInterval(timerId);
      wpo.removeChild(wpoSyncing);
      wpoStatus.className = 'wp-off-disconnected';
      wpo.className = 'wp-off-enabled';
      wpoLink.onclick = wpo_remove_store;
      wpoText.data = 'Read online';
      isSyncing = false;
    }
    
    progressBar.setPercent(progressBar.step);
    progressBar.step += 0.01;
  }, 25);
}

function wpo_remove_store(e) {
  e.preventDefault();
  
  try {
    var localServer = google.gears.factory.create('beta.localserver', '1.0');
  } catch (ex) {
    setError('Could not create local server: ' + ex.message);
    return;
  }
  localServer.removeManagedStore(STORE_NAME);

  wpoStatus.className = 'wp-off-connected-synced';
  wpoLink.onclick = wpo_init;
  wpoText.data = 'Read offline';

  return false;
}

function createManagedStore() {
  try {
    var localServer = google.gears.factory.create('beta.localserver', '1.0');
  } catch (ex) {
    setError('Could not create local server: ' + ex.message);
    return;
  }
  var store = localServer.createManagedStore(STORE_NAME);
  store.manifestUrl = BLOG_ROOT + '/wp-content/plugins/wp-offline/manifest.json';
  store.checkForUpdate();

  var timerId = window.setInterval(function() {
    // When the currentVersion property has a value, all of the resources
    // listed in the manifest for that version are captured. There is an
    // open bug to surface this state change as an event.
    if (store.currentVersion && store.updateStatus == 0) {  // OK
      if(isCanceled) {
        try {
          var localServer = google.gears.factory.create('beta.localserver', '1.0');
        } catch (ex) {
          setError('Could not create local server: ' + ex.message);
          return;
        }
        localServer.removeManagedStore(STORE_NAME);
        isCanceled = false;
      }
      window.clearInterval(timerId);
      wpo_complete();
    } else if( store.updateStatus == 1) { // CHECKING
      // Don't do anything yet
    } else if( store.updateStatus == 2) { // DOWNLOADING
      // Don't do anything yet
    } else if( store.updateStatus == 3 ) { // FAILED
      window.clearInterval(timerId);
      setError('An error occurred: ' + store.lastErrorMessage);
      wpo_remove_store();
    }
  }, 500);
}
