// ==UserScript==
// @name           TGC (Triburile.ro Game Controller)
// @version        0.7.1
// @description    Enhanced gameplay. WARNING: using it might get you banned!
// @namespace      http://altblue.com/
// @author         Marius Feraru <altblue@n0i.net>
// @license        GPLv3
// @include        http://ro*.triburile.ro/staemme.php*
// @include        http://ro*.triburile.ro/game.php*
// ==/UserScript==

/* $Id: triburilero.user.js,v 1.11 2008/02/18 06:57:36 altblue Exp $
 *
 * WARNING:
 *   Using this "Tribal Wars" (RO version) add-on might get you banned
 *   as it fringes the game's 7th rule of implementing "premium account"
 *   features: http://www.triburile.ro/rules.php?rule=7
 *   Don't claim I didn't warn you ;-)
 *
 * Copyright 2006, 2007 Marius Feraru <altblue@n0i.net>
 * All rights reserved.
 *
 * This file is part of TGC (Triburile.ro Game Controller)
 *
 * TGC 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/>
 *
 */

var GM = {
  name:      'triburile.ro',
  namespace: 'http://people.n0i.net/altblue/greasemonkey/',

  initialize: function() {
    this.stopTimers();
    if (window.self === window.top) { // staemme.php
      // this.removeScripts();
      this.minimizeAdFrame();
    }

    this.removeStyleSheets();

    this.cssTop = document.getElementsByTagName('head')[0];
    this.cssREF = document.createElement('link');
    this.cssREF.type = 'text/css';
    this.cssREF.rel  = 'stylesheet';
    this.addStyleSheets([ 'prototip.css', 'window-themes/default.css',
      'window-themes/lighting.css'], true);

    this.jsTop = document.getElementsByTagName('body')[0]
                 || document.getElementsByTagName('head')[0];
    this.jsREF = document.createElement('script');
    this.jsREF.type = 'text/javascript';
    this.addScripts([ 'prototype.js', 'effects.js', 'dragdrop.js',
      'window.js', 'prototip.js', 'neodom.js'], true);

    if (window.self === window.top) {  // staemme.php
      this.addScripts(['controller.js']);
    } else {                           // game.php
      this.addStyleSheets(['game.css']);
      this.addScripts(['game.js']);
    }
  },

  addScripts: function(names, shared) {
    names.forEach(function(name){
      var s = this.jsREF.cloneNode(true);
      s.src = this.namespace + (shared ? 'lib' : this.name) + '/' + name;
      this.jsTop.appendChild(s);
    }, this);
  },

  addStyleSheets: function(names, shared) {
    names.forEach(function(name){
      var s = this.cssREF.cloneNode(true);
      s.href = this.namespace + (shared ? 'lib' : this.name) + '/' + name;
      this.cssTop.appendChild(s);
    }, this);
  },

  stopTimers: function() {
    var ti = window.setInterval(function(){},9999);
    while(ti--) window.clearInterval(ti);

    var tt = window.setTimeout(function(){},9999);
    while(tt--) window.clearTimeout(tt);
  },

  removeScripts: function() {
    Array.forEach(
      document.getElementsByTagName('script'),
      function(s) {
        s.parentNode.removeChild(s);
      }
    );
  },

  removeStyleSheets: function() {
    Array.forEach(
      document.getElementsByTagName('style'),
      function(s) {
        s.parentNode.removeChild(s);
      }
    );
    Array.forEach(
      document.getElementsByTagName('link'),
      function(s) {
        if (s.getAttribute('type') == 'text/css')
          s.parentNode.removeChild(s);
      }
    );
  },

  minimizeAdFrame: function() {
    var fs = document.getElementsByTagName('frameset')[0];
    if (fs) { // lumea 2 - 4
      fs.cols = fs.cols.replace(/\d+/, '0');
    } else { // lumea 1
      var iframes = document.getElementsByTagName('iframe');
      for (var i = 0; i < iframes.length; i++) {
        var frame = iframes[i], div = frame.parentNode;
        if (frame.src.indexOf('game.php') > -1) { // "main"
          div.style.cssText += 'right: 0px; margin: 0; padding: 0; border: none; overflow: hidden';
        } else { // "ad"
          frame.src = 'about:blank';
          div.style.cssText = 'display: none; overflow: hidden; width: 0px; height: 0px';
        }
      }
    }
  }

};

GM.initialize();
