var STORE_NAME = 'ninja250-FAQ-store';
var LocalServer;
var Store;
var HttpRequest;

var filesToCapture = [
  '/favicon.ico',
  '/skins/common/metadata.js',
  '/skins/common/common.css',
  '/skins/common/commonPrint.css',
  '/skins/common/wikibits.js',
  '/skins/common/images/poweredby_mediawiki_88x31.png',
  '/skins/monobook/main.css',
  '/skins/monobook/main.css?7',
  '/skins/monobook/IE60Fixes.css',
  '/skins/common/IEFixes.js',
  '/skins/monobook/background3.jpg',
  '/skins/monobook/user.gif',
  '/skins/monobook/title.gif',
  '/skins/monobook/spacer.gif',
  '/skins/monobook/wiki.png',
  '/skins/monobook/wiki-indexed.png',
  '/skins/monobook/link_icon.gif',
  '/skins/monobook/lock_icon.gif',
  '/skins/monobook/external.png',
  '/skins/monobook/bullet.gif',
  '/gears_init.js',
  '/ggears_save.js',
  '/index.php?title=-&amp;action=raw&amp;gen=js',
  '/images/9/92/Icon_mini_members.gif',
location.href
];

function ggears_menu_init(title)
{
    //alert ("Test");
  if (window.google && google.gears) 
  {
    //alert ("Google Gears Installed");
    //alert (title);

    if ( ! ggears_init()) return;

    if (! Store) Store = LocalServer.openStore(STORE_NAME);
    
    if (Store && Store.isCaptured(location.href))
    {
      document.getElementById("ggears_offline").innerHTML = " &nbsp;&nbsp;<sup><b><i>(Off-line)</b></i></sup>";
    }
    if (title == 'All articles')
    {
      document.getElementById("ggears").innerHTML = 
        '<A href="javascript:ggears_save()" target="_top">Go Offline</A>&nbsp;&nbsp;&nbsp' +
        '<A href="javascript:ggears_remove()" target="_top">Go ONLINE</A>&nbsp;&nbsp;';
    }
  }
}

function ggears_init()
{
  if (google.gears) 
  {
    if (LocalServer) return true;

    try 
    {
      LocalServer = google.gears.factory.create('beta.localserver', '1.0');
    } 
    catch (ex) 
    {
      alert ("Google Gears google.gears.factory.create failed.");
      return false;
    }
    return true;
  }
  return false;
}

function ggears_save()
{
  if ( ! ggears_init()) return;

  var links = document.getElementsByTagName('A');
  var hrefs = [];

  for (var i = 0, el; el = links[i]; i++) 
  {
    if (el.href.search("faq") != -1)
    {
        filesToCapture.push (el.href);
    }
  }

  //loadHttp ("/wiki/Special:Categories");

  // Store = LocalServer.openStore(STORE_NAME);
  Store = LocalServer.createStore(STORE_NAME);

  if (!Store) 
  {
    alert ('GGears: failed to create offline store.');
    return;
  }

  // Capture this page and the js library we need to run offline.
  Store.capture (filesToCapture, captureCallback);

  //alert ("Following URLs are saved:\n" + location + "\n" + filesToCapture);
  showStored (filesToCapture);
}

function captureCallback(url, success, captureId) 
{
  //addStatus(url + ' captured ' + (success ? 'succeeded' : 'failed'));
  if (OutputWindow && !OutputWindow.closed)
  {
    OutputWindow.document.write('<a href="' + url + '">' + url + '</a>&nbsp; -' + ' capture ' + (success ? 'succeeded' : 'failed') + '<br>');
  }
}

function ggears_remove()
{
  if ( ! ggears_init()) return;

  // We call openStore() to test for it's existence prior to removing it
  if (LocalServer.openStore(STORE_NAME)) 
  {
    LocalServer.removeStore(STORE_NAME);
    Store = null;
    alert ('Offline store removed');
  } 
  else 
  {
    alert ('The offline store does not exist');
  }
}

function showStored (files)
{
  var winwidth  = screen.width / 2;
  var winheight = screen.height / 2;
  var X = (screen.width) / 4;
  var Y = (screen.height) / 4;

  properties="toolbar=yes,location=yes,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+winwidth+",height="+winheight+",";

  checkie = navigator.appName.indexOf("icrosoft");
  if (checkie == -1)
  {
    properties = properties+"screenX="+X+",screenY="+Y;
  }
  else
  {
    properties = properties+"left="+X+",top="+Y;
  }

  OutputWindow = window.open('','GGearsOutputWin',properties);
  OutputWindow.document.open();
  OutputWindow.document.write('<html><head>');
  OutputWindow.document.write('</head>');
  OutputWindow.document.write('<body>');

  /*for (var i = 0, file; file = files[i]; i++)
  {
    OutputWindow.document.write('<a href="' + file + '">' + file + '</a><br>');
    //el.disabled = true;
    //alert (el);
  }*/
  //OutputWindow.document.close ();

  OutputWindow.focus();
}

function loadHttp (url)
{
  HttpRequest=null;

  //  alert (url + "2");

  // code for Mozilla, etc.
  if (window.XMLHttpRequest)
  {
    HttpRequest=new XMLHttpRequest();
    if (HttpRequest.overrideMimeType)
    {
      // set type accordingly to anticipated content type
      //http_request.overrideMimeType('text/xml');
      HttpRequest.overrideMimeType('text/html');
    }
  }
  else if (window.ActiveXObject) // code for IE
  {
    HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  }

  if (HttpRequest!=null)
  {
    HttpRequest.onreadystatechange = httpRequestStateChange;
    HttpRequest.open('GET', url, true);
    HttpRequest.send(null);
  }
  //else
  //{
  //  alert("Your browser does not support HttpRequest.")
  //}
}

function httpRequestStateChange()
{
  //alert ("gg = " + HttpRequest.readyState);
  // if HttpRequest shows "loaded"
  if (HttpRequest.readyState==4)
  {
    // if "OK"
    if (HttpRequest.status==200)
    {
      //alert(HttpRequest.responseText);
      //document.getElementById('A1').innerHTML=HttpRequest.status
      //document.getElementById('A2').innerHTML=HttpRequest.statusText
      //document.getElementById('A3').innerHTML=HttpRequest.responseText
      var body   = document.getElementsByTagName("body")[0];
      /*var iframe = document.createElement("iframe");
      iframe.setAttribute ("id", "iframeRPC");
      body.appendChild(iframe);

      if (typeof iframe.document != "undefined" &&
          typeof iframe.contentWindow == "undefined" &&
          typeof iframe.contentDocument == "undefined")
      {
          alert("JJ");
      }
      
      if (typeof iframe.contentDocument != "undefined")
          alert("aa");
      if (typeof iframe.contentWindow != "undefined")
          alert("bb");

      body.removeChild (iframe);

      var iframeHTML='<iframe id="iframeRPC"></iframe>';

      body.innerHTML += iframeHTML;
      */
      //iframe = document.getElementById("iframeRPC");

      //iframe.contentWindow = new Object();
          alert("cc");
      //iframe.contentWindow.document.location.replace("/wiki/Special:Categories");
      window.frames['iframeRPC'].location = "/wiki/Special:Categories";
          alert("dd");

      //var links = iframe.contentWindow.document.getElementsByTagName('A');
      var links = window.frames['iframeRPC'].document.getElementsByTagName('A');
          alert("dd1");
      
      for (var i = 0, el; el = links[i]; i++) 
      {
        alert (el.href);
        if (i >= 4) break;
        if (el.href.search("faq") != -1)
        {
            filesToCapture.push (el.href);
        } 
      }
          alert("dd2");
    }
    //else
    //{
    //  alert("Problem retrieving XML data:" + HttpRequest.statusText)
    //}
  }
}
