// ==UserScript==
// @name           warbook
// @version        0.4.9
// @description    enhanced gameplay ;-)
// @namespace      http://altblue.com/
// @author         Marius Feraru
// @include        http://optimus.freewebz.com/*
// @include        http://warbook.freewebz.com/*
// @include        http://warbook.freewebs.com/*
// ==/UserScript==

/* $Id: warbook.user.js,v 1.6 2008/01/22 15:39:13 altblue Exp $
 *
 * Copyright 2006, 2007 Marius Feraru <altblue@n0i.net>
 * All rights reserved.
 *
 * This file is part of WBx (Warbook Enhancer)
 *
 * WBx is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details:
 * <http://www.gnu.org/licenses/>
 *
 */

// You should use "about:config" to modify these settings ;-)
var WarBookSettings = {
  // if no useful results found, how many times to retry with the same size?
  SearchRetry: 3,
  // delay (in seconds) between requests
  SearchDelay: 5,
  // size increment
  SearchInc:   200,
  // how far from minimum attack size to start searching?
  SearchDelta: 1000,
  // hero's current level
  HeroLevel:   0,
  // hero's type (Visionary, Necromancer, etc)
  HeroType:    ''
};


// Update current hero "level" and "type" if possible
if (window.location.pathname == '/kingdom') {
  var hinfo = document.evaluate(
                './/div[contains(@class,"info")]/a[@href="/hero/"]/..',
                document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
  );
  if (hinfo) {
    hinfo = hinfo.singleNodeValue;
    var m = hinfo.textContent.match(/Your\s+Hero\s+is\s+a\s+Level\s+(\d+)\s+(\S+)/);
    if (m) {
      GM_setValue('HeroLevel', parseInt(m[1]));
      GM_setValue('HeroType', m[2]);
    }
  }
}


// Add settings to document
var js  = document.createElement('script'),
    src = document.createTextNode('');
js.type = 'text/javascript';
for (var opt in WarBookSettings) {
  var qv = GM_getValue(opt, WarBookSettings[opt]);
  if (typeof qv != 'number') qv = "'" + qv + "'";
  src.data += 'WBx' + opt + ' = ' + qv + ';\n';
}
js.appendChild(src);
document.getElementsByTagName('head')[0].appendChild(js);


// Add script's dependencies
var GM_NS   = 'http://people.n0i.net/altblue/greasemonkey/',
    GM_NAME = 'facebook-warbook';
var libs = ['prototype', 'neodom'];

function addScript(name, shared) {
  var p = document.getElementsByTagName('body')[0];
  if (!p) p = document.getElementsByTagName('head')[0];
  var s = document.createElement('script');
  s.type = 'text/javascript';
  s.src  = GM_NS + (shared ? 'lib' : GM_NAME) + '/' + name;
  p.appendChild(s);
}

function addStyleSheet(name, shared) {
  var s  = document.createElement('link');
      s.type = 'text/css';
      s.rel  = 'stylesheet';
      s.href = GM_NS + (shared ? 'lib' : GM_NAME) + '/' + name;
  document.getElementsByTagName('head')[0].appendChild(s);
}

libs.forEach(function(name){addScript(name + '.js', true)});
addStyleSheet('warbook.css');
addScript('warbook.js');
