User:Tim Pierce/flickrbutton.js

// "Fail gracefully" if skin not supported switch (skin) { case 'modern': case 'monobook': case 'vector':

// Global variables if (typeof(cactions) == 'undefined') var cactions; eval(function {        var globals = cactions ? cactions : null;        cactions = {            admin: wgUserGroups.indexOf('sysop') > -1 ? true : false,            areqs: [],            hovms: (skin == 'vector') ? 50 : 400,            itabs: true,            menus: [],            mouse: null,            mwsvr: wgServer.indexOf('secure.wikimedia.org') == -1 ? wgServer.split('://')[1] : null,            pname: encodeURIComponent(mw.config.get('wgPageName')),            svars: [],            tbusr: false,            timer: [],            xmlhr: true        };        if (globals) for (i in globals) cactions[i] = globals[i];    });

// Propose new languages at http://en.wikipedia.org/wiki/User_talk:Haza-w mw.loader.load('http://en.wikipedia.org/w/index.php?title=User:Twp/cactions-languages.js&action=raw&ctype=text/javascript');

function xhr(request,url,orsc) { with (request) { open('GET',url,true); onreadystatechange = orsc; send(null); }   }

// Find absolute position of element function findPos(eid,offset) { var obj = document.getElementById(eid), pos = [0,0]; do with (obj) { pos[0] += offsetLeft; pos[1] += offsetTop; } while (obj = obj.offsetParent); pos[0] += offset[0]; pos[1] += offset[1]; return pos; }

// Create menu div element function createMenu(mid,vectorise,html) { var menu = document.createElement('div'); with (menu) { id = 'opt-' + mid; className = 'ca-menu'; style.display = 'none'; }       menu.onmouseover = function  {showMenu('opt-'+mid)}; menu.onmouseout = function {hideMenu('opt-'+mid)};

var elements = { ul: document.createElement('ul'), li: null, a: null, txt: null };       with (elements) { for (var i = 0; i < html.length; i++) if (html[i].length) { li = document.createElement('li'); li.id = html[i][0]; a = document.createElement('a'); a.href = html[i][1]; txt = document.createTextNode(cactions._lang[html[i][0].replace(/-/g,'_')]); a.appendChild(txt); li.appendChild(a); ul.appendChild(li); }           menu.appendChild(ul); }

document.body.appendChild(menu); if (vectorise) createTab(mid);

return 'opt-' + mid; }

// Create cactions LI tab function createTab(mid) { var mtitle = mid.charAt(0).toUpperCase + mid.substr(1);

if (skin == 'vector') { var cid = 'p-' + mid; var elements = { div: document.createElement('div'), h5: document.createElement('h5'), span: document.createElement('span'), a: document.createElement('a'), txt: null };           with (elements) { div.id = cid; div.className = 'vectorMenu extraMenu';

txt = document.createTextNode(mtitle); span.appendChild(txt); h5.appendChild(span);

a.href = '#'; a.onmouseover = function {showMenu('opt-'+mid,findPos(cid,[0,40]))}; a.onmouseout = function {hideMenu('opt-'+mid)};

span = document.createElement('span'); txt = document.createTextNode(mtitle); span.appendChild(txt); a.appendChild(span); h5.appendChild(a);

div.appendChild(h5); document.getElementById('right-navigation').insertBefore(div,document.getElementById('p-search')); }       }        else { var cid = 'ca-' + mid; var elements = { li: document.createElement('li'), a: document.createElement('a'), txt: document.createTextNode(mtitle) };           with (elements) { li.id = cid; a.href = '#'; a.onmouseover = function {showMenu('opt-'+mid,findPos(cid,[-10,20]))}; a.onmouseout = function {hideMenu('opt-'+mid)}; a.appendChild(txt); li.appendChild(a);

document.getElementById('p-cactions').getElementsByTagName('div')[0].getElementsByTagName('ul')[0].appendChild(li); }       }    }

// CSS hide elements function hideElements(elements,conditionals) { if (typeof(conditionals) == 'undefined') { for (var i = 0; i < elements.length; i++) if (document.getElementById(elements[i])) document.getElementById(elements[i]).style.display = 'none'; }       else for (var i = 0; i < elements.length; i++) if (document.getElementById(elements[i])) { document.getElementById(elements[i]).style.display = 'none'; if (conditionals[i]) document.getElementById(conditionals[i]).style.display = 'none'; }   }

// Show/hide menu functions function showMenu(mid,pos) { with (cactions) { mouse = mid; if (pos) for (var i = 0; i < menus.length; i++) { if (timer[menus[i]]) { clearTimeout(timer[menus[i]]); timer[menus[i]] = null; }               if (mid.replace(/-[^-]+$/,'') == menus[i]) continue; document.getElementById(menus[i]).style.display = 'none'; }           if (!timer[mid]) with (document.getElementById(mid).style) { display = ''; if (pos) { left = pos[0]+'px'; top = pos[1]+'px'; }           }            else { clearTimeout(timer[mid]); timer[mid] = null; }       }    }    function hideMenu(mid) { with (cactions) { if (mid == mouse.replace(/-[^-]+$/,'')) timer[mid] = null;

if (timer[mid]) { timer[mid] = null; document.getElementById(mid).style.display = 'none'; if (mid == mouse && mid.search(/opt-.*-/) != -1) document.getElementById(mid.replace(/-[^-]+$/,'')).style.display = 'none'; }           else timer[mid] = setTimeout('hideMenu(\''+mid+'\');',hovms); }   }

// Delink element function removeLink(eid) { var element = document.getElementById(eid); if (!element.getElementsByTagName('a').length) return false;

var a = element.getElementsByTagName('a')[0]; element.appendChild(a.removeChild(a.firstChild)); element.removeChild(a);

element.className = 'ca-disabled'; }

// CSS styles importStylesheetURI('http://en.wikipedia.org/w/index.php?title=User:Haza-w/cactions.css&ctype=text/css&action=raw');

// User options hook addOnloadHook(function {        switch (wgNamespaceNumber) {            case 2: case 3: cactions['uname'] = encodeURIComponent(wgTitle.split('/')[0].replace(/ /g,'_'));        }        if (wgCanonicalSpecialPageName == 'Contributions') for (var i = 0, hl; hl = document.getElementById('contentSub').getElementsByTagName('a')[i]; i++) {            if (hl.href.indexOf('user=') > -1) {                cactions['uname'] = hl.href.split('user=')[1].split('&amp;')[0];                break;            }        }

if (cactions.uname) { with (cactions) { menus[menus.length] = createMenu('images',true,Array( ['c-u-flickr',     'http://www.flickr.com/search/?q='+document.title+'&l=comm&ss=2&ct=0&mt=all&w=all&adv=1'] ));           }        }    } );

// shortcuts portlet hook addOnloadHook(function {        with (cactions) if (svars.length) {            var portlet = document.createElement('div');            with (portlet) {                id = 'p-sc';                className = (skin == 'vector') ? 'portal collapsed' : 'portlet';            }

var elements = { h5: document.createElement('h5'), div: document.createElement('div'), ul: document.createElement('ul'), li: null, a: null, txt: null };           with (elements) { h5.appendChild(document.createTextNode('Shortcuts')); portlet.appendChild(h5);

div.className = (skin == 'vector') ? 'body' : 'pBody';

for (var i = 0; i < svars.length; i++) if (!svars[i][1].indexOf('/') || !svars[i][1].search(/http[s]?:\/\//)) { li = document.createElement('li'); li.id = 's-'+svars[i][0].toLowerCase.replace(/\W/g,''); a = document.createElement('a'); a.href = svars[i][1]; txt = document.createTextNode(svars[i][0]); a.appendChild(txt); li.appendChild(a); ul.appendChild(li); }               div.appendChild(ul); portlet.appendChild(div); }

switch (skin) { case 'modern': var sidebarID = 'mw_portlets'; break; case 'monobook': var sidebarID = 'column-one'; break; case 'vector': var sidebarID = 'panel'; break; }           document.getElementById(sidebarID).insertBefore(portlet,document.getElementById('p-lang')); }   } ); }