/*
 Top of Javascript */
/* tooltips and access keys */ 
// ==========================================================================
// BEGIN: MediaWiki interface button definition for Duesentrieb's image tools:
// "Check usage", "User image gallery", "Orphaned images of user" and 
// "Untagged images of user".
// If you are located at an "Image:" you get the "check usage" tab, if you
// are at a "User:" page you get the "gallery", "orphans" and "untagged" tab.
// If you are on other pages these tabs do not get shown. These functions are
// enabled in Wikimedia Commons by default via [[Mediawiki:Monobook.js]] and
// its local language subpages.
// This script has been tested with Konqueror 3.5 and Firefox 1.5 and other
// browsers including Opera and Internet Explorer have been reported to work
// as well. If you enhance or change this template please test it previous to
// editing here in your local monobook.js with as many as possible browsers
// (in order not to clash with the function names here change in your local
// monobook.js all "global_" to "local_").
// Be aware that the global JavaScripts do not get updated by the Wikimedia
// servers that fast after saving them.

// written by Dbenbenn, Avatar, Duesentrieb and Arnomane

// Switch for people that don't want the extra tabs.
if (load_extratabs == true)
{

// Translations (variables for internationalisation and localisation):

// project
// It is tempting to replace this with wgServer.replace("http://", "")
var tab_project = 'commons.wikimedia.org';

// namespace names
var tab_ns_image = 'Image:';
//var tab_ns_imagetalk = 'Image_talk:';
var tab_ns_user = 'User:';
//var tab_ns_usertalk = 'User_talk:';
//var tab_ns_category = 'Category:';
var tab_ns_special = 'Special:';

// Interface strings, get defined in the localised Mediawiki:Monobook.js/
// var tab_check_usage = 'check usage';
// var tab_categorize = 'find categories';
// var tab_log = 'log';
// var tab_gallery = 'gallery';
// var tab_orphans = 'orphans';
// var tab_untagged = 'untagged';
// var tab_tree = 'Tree';
// var tab_catscan = 'CatScan';
// var tab_blocklog = 'Blocklog';
// var tab_wikipedia = "en";  //this tabname is not likely to change depending on namespace

// Appends a new tab.

function global_append_tab(url, name, id)
{
  var na = document.createElement('a');
  na.setAttribute('href', url);  
 
  var txt = document.createTextNode(name);
  na.appendChild(txt);
 
  var li = document.createElement('li');
  if(id) li.id = id;
  li.appendChild(na);

  // Grab the element we want to append the tab and append the tab to it.
  var c1 = document.getElementById('column-one');
  var tabs = c1.getElementsByTagName('div')[0].getElementsByTagName('ul')[0];
  tabs.appendChild(li);
}

function global_add_image_tabs(imagetitle)
{
  imagetitle = imagetitle.substring(imagetitle.indexOf(':') + 1); // strip off "Image:" namespace
  
  global_append_tab('http://tools.wikimedia.de/~daniel/WikiSense/CheckUsage.php?i=' + imagetitle + '&w=_100000', tab_check_usage, 'ca-checkusage');
  
  global_append_tab('http://tools.wikimedia.de/~daniel/WikiSense/CommonSense.php?i=' + imagetitle + '&go-clean=yes' , tab_categorize, 'ca-commonsense');
  
  global_append_tab('/w/index.php?title=' + tab_ns_special + 'Log&page=' + tab_ns_image + imagetitle, tab_log, 'ca-log');
  
  // Add an "unthumb" link.  For Image:123px-Foo, links to Image:Foo.
  if (imagetitle.match(/^\d+px-/))
  {
    // imagetitle = imagetitle.substring(imagetitle.indexOf("px-") + 3);
    imagetitle = imagetitle.replace(/^\d+px-/, '');
    global_append_tab('/wiki/Image:' + imagetitle, 'unthumb', 'ca-unthumb');
  }
  
  // Add a tab going to enwiki
  global_append_tab('http://'+tab_wikipedia+'.wikipedia.org/wiki/Image:' + imagetitle, tab_wikipedia, 'ca-' + tab_wikipedia + 'wiki');

}

function global_add_user_tabs(username)
{
  // strip off namespaces like "User:"
  // username.split(":")[1] will fail if a colon is not present
  username = username.substring(username.indexOf(':') + 1);
  
  // removing subpages from the link
  // username.split("/")[0] works equally well
  // username = username.replace(/\/.*$/, '');
  username = username.split("/")[0];

  global_append_tab('http://tools.wikimedia.de/~daniel/WikiSense/Gallery.php?wikifam=' + tab_project + '&img_user_text=' + username , tab_gallery, 'ca-gallery');
  global_append_tab('http://tools.wikimedia.de/~daniel/WikiSense/OrphanImages.php?wiki=' + tab_project + '&img_user_text=' + username , tab_orphans, 'ca-orphans');
  global_append_tab('http://tools.wikimedia.de/~daniel/WikiSense/UntaggedImages.php?wiki=' + tab_project + '&img_user_text=' + username , tab_untagged, 'ca-untagged');
}

function global_add_category_tabs(category)
{
  category = category.substring(category.indexOf(':') + 1); // strip off "Category:" namespace
  // global_append_tab('/wiki/Special:CategoryTree?target=' + category + '&mode=categories&dotree=Laden', tab_tree);
  // global_append_tab('http://tools.wikimedia.de/~daniel/WikiSense/CategoryTree.php?wiki=' + tab_project + '&cat=' + category , tab_tree);
  global_append_tab('http://tools.wikimedia.de/~daniel/WikiSense/CategoryIntersect.php?wiki=' + tab_project + '&basecat=' + category , tab_catscan, 'ca-catscan');
}

// On Special:Contributions, we add the user tabs just like for a user page.
// On Special:Blockip, we add a blocklog tab.
function global_add_special_tabs(title)
{
  // Replace with wgPageName
  //  if (title.indexOf(tab_ns_special + 'Blockip') == 0)
  if (wgPageName == "Special:Blockip")
  {
    // so we don't break after blocking
    var username = document.getElementsByName('wpBlockAddress')[0] ? document.getElementsByName('wpBlockAddress')[0].defaultValue : '';
    
    if (username != '')
    {
      global_append_tab('/w/index.php?title=' + tab_ns_special + 'Log&type=block&page=' + tab_ns_user + username, tab_blocklog, 'ca-blocklog');
    }
  }
  // Replace with wgPageName
  // else if (title.indexOf(tab_ns_special + 'Contributions') == 0)
  else if (wgPageName == "Special:Contributions")
  {
    var username = document.getElementById('contentSub').getElementsByTagName('a')[0].firstChild.nodeValue;
    global_add_user_tabs(tab_ns_user + username);
  }
}
 
function global_do_onload()
{
    // extract the namespace
    // var title = global_get_tidy_title();
    var title = wgPageName;
    
    // Namespace numbers are (see [[m:Help:Variables]]):
    // -1: Special
    // 2:  User
    // 3:  User_talk
    // 6:  Image
    // 7:  Image_talk
    // 14: Category
    switch(wgNamespaceNumber)
    {
        case -1:
            global_add_special_tabs(title);
            break;
        case 2:
        case 3:
            global_add_user_tabs(title);
            break;
        case 6:
        case 7:
            global_add_image_tabs(title);
            break;
        case 14:
            global_add_category_tabs(title);
            break;
        default:
            // nothing
    }
}

addOnloadHook( global_do_onload );

}
// END: MediaWiki interface button definition for Duesentrieb's image tools.
// ========================================================================
/* Bottom of Javascript 
*/