/* generated javascript */
var skin = 'wikiaoc';
var stylepath = '/skins';

/* MediaWiki:Common.js */
 /* Based on wikipedia's collapsible boxes */

 var hasClass = (function () {
     var reCache = {};
     return function (element, className) {
         return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
     };
 })();

 var autoCollapse = 2;
 var collapseCaption = "hide";
 var expandCaption = "show";
 
 function collapseTable( tableIndex )
 {
     var Button = document.getElementById( "collapseButton" + tableIndex );
     var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
     if ( !Table || !Button ) {
         return false;
     }
 
     var Rows = Table.rows;
 
     if ( Button.firstChild.data == collapseCaption ) {
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = "none";
         }
         Button.firstChild.data = expandCaption;
     } else {
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = Rows[0].style.display;
         }
         Button.firstChild.data = collapseCaption;
     }
 }
 
 function createCollapseButtons()
 {
     var tableIndex = 0;
     var NavigationBoxes = new Object();
     var Tables = document.getElementsByTagName( "table" );
 
     for ( var i = 0; i < Tables.length; i++ ) {
         if ( hasClass( Tables[i], "collapsible" ) ) {
 
             /* only add button and increment count if there is a header row to work with */
             var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
             if (!HeaderRow) continue;
             var Header = HeaderRow.getElementsByTagName( "th" )[0];
             if (!Header) continue;
 
             NavigationBoxes[ tableIndex ] = Tables[i];
             Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
             var Button     = document.createElement( "span" );
             var ButtonLink = document.createElement( "a" );
             var ButtonText = document.createTextNode( collapseCaption );
 
             Button.style.styleFloat = "right";
             Button.style.cssFloat = "right";
             Button.style.fontWeight = "normal";
             Button.style.textAlign = "right";
             Button.style.width = "6em";
 
             ButtonLink.style.color = Header.style.color;
             ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
             ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
             ButtonLink.appendChild( ButtonText );
 
             Button.appendChild( document.createTextNode( "[" ) );
             Button.appendChild( ButtonLink );
             Button.appendChild( document.createTextNode( "]" ) );
 
             Header.insertBefore( Button, Header.childNodes[0] );
             tableIndex++;
         }
     }
 
     for ( var i = 0;  i < tableIndex; i++ ) {
         if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
             collapseTable( i );
         }
     }
 }
 
 addOnloadHook( createCollapseButtons );

var aocttip = {
	from: null,
	element: null,
	ttid: 0,
	reqid: (new Date()).getTime(),
	x: 0,
	y: 0,
	cache: { },

	init: function() {
		addHandler(document, "mousemove", aocttip.mouseMove);

		var list = getElementsByClassName(document, "*", "aocttip");
		for (var i = 0; i < list.length; i++) {
			var item = list[i];
			item.ttitle = item.title;
			aocttip.stripTitles(item);
			item.onmouseover = function() { aocttip.display(this); };
			item.onmouseout = function() { aocttip.hide(); };
		}
	},

	stripTitles: function(ele) {
		if (ele == null) return;

		ele.title = "";

		try {
		var nodes = ele.childNodes;
		for (var i = 0; i < nodes.length; i++)
			aocttip.stripTitles(nodes[i]);
		} catch(e) { }
			
	},

	ensureElement : function() {
		if (this.element == null || document.getElementById("aocttipdiv") == null) {
			this.element = document.createElement("div");
			this.element.id = "aocttipdiv";
			this.element.style.display = "none";
			document.body.appendChild(this.element);
		}
	},

	display: function(from) {
		if (from == null)
			return;

		this.ensureElement();

		this.element.innerHTML = "";

		this.from = from;
		
		this.validatePosition();
		
		var childs = from.getElementsByTagName("div");
		for (var i = 0; i < childs.length; i++) {
			if (hasClass(childs[i], "aocttipcontent"))
			{
				this.fillFrom(childs[i]);
				return;
			}
		}

		if (from.ttitle != null && from.ttitle != "")
			this.fillAsync(from.ttitle);
	},

	fillFrom: function(element) {
		this.ttid++;

		if (element != null)
		{
			this.element.innerHTML = element.innerHTML;
			this.element.style.display = "";
		}
	},

	fillAsync: function(article) {
		var id = ++this.reqid;

		if (aocttip.element != null && aocttip.cache[article] != null)
		{
			aocttip.element.innerHTML = aocttip.cache[article];
			aocttip.element.style.display = "";
			return;
		}

		var req = sajax_init_object();
		var url = wgScriptPath + "/api.php?x=" + id + "&format=json&action=parse&prop=text&text={" + "{:" + article + "}" + "}";
		req.open("GET", url, true);

		req.onreadystatechange = function() {
			if (req.readyState != 4 || req.status != 200 || aocttip.reqid != id) return;

			var res = req.responseText;
	
			if (res.match("action=edit"))
				return;

			var json = aocttip.parseJSON(res);

			if (json)
			{
				if (json.parse == null || json.parse.text == null || json.parse.text["*"] == null) return;

				aocttip.cache[article] = json.parse.text["*"];

				if (aocttip.element != null)
				{
					aocttip.element.innerHTML = json.parse.text["*"];
					aocttip.element.style.display = "";
				}
			}
		}

		req.send(null);
	},

	parseJSON: function (text) {
		try {
			return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
				text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
				eval('(' + text + ')');
		} catch (e) {
			return false;
		}
	},

	validatePosition: function() {
		if (this.from == null || this.element == null)
			return;

		var top = (this.y + 15);
		var lft = (this.x + 15);

		if (top + this.element.offsetHeight > document.body.offsetHeight - 15)
			top = document.body.offsetHeight - this.element.offsetHeight - 15;

		if (lft + this.element.offsetWidth > document.body.offsetWidth - 15)
			lft = document.body.offsetWidth - this.element.offsetWidth - 15;

		if (top < 15)
			top = 15;

		if (lft < 15)
			lft = 15;
		

		this.element.style.top = top + "px";
		this.element.style.left = lft + "px";
	},

	hide: function() {
		this.from = null;
		if (this.element != null)
			this.element.style.display = "none";
		this.reqid++;
	},

	mouseMove: function(e) {
		e = e || window.event;

		var ele = e.srcElement || e.target;

		aocttip.x = e.pageX || e.clientX + document.documentElement.scrollLeft;
		aocttip.y = e.pageY || e.clientY + document.documentElement.scrollTop;

		aocttip.validatePosition();
	}
};

addOnloadHook( aocttip.init );

/* MediaWiki:Wikiaoc.js (deprecated; migrate to Common.js!) */
