/***********************************************************************

   Ok, this is the JavaScript code for Collapsible Categories extension.
   
   And it's made by me, the DtTvB. http://dt.in.th/
   
   It does not use any framework. And you can modify it to your like!

***********************************************************************/

setTimeout (function() {

	// Make array from something not array.
	// Well, like, arguments or HTMLCollection.
	// You just need the length operator.
	function makeArray(wtf) {
		var o = [];
		if (wtf.length === undefined)
			return o;
		for (var i = 0; i < wtf.length; i ++)
			o.push (wtf[i]);
		return o;
	}
	
	// When getElementsByClassName is not available we use this instead.
	// This should work well.
	function lookForDiv(wtf) {
		var o = [];
		var l;
		if (document.getElementsByTagName)
			l = makeArray(document.getElementsByTagName('div'));
		else
			return o;
		for (var i = 0; i < l.length; i ++) {
			if (l[i] && l[i].className !== undefined && (' ' + l[i].className.replace(/\s+/g, ' ') + ' ').indexOf(' ' + wtf + ' ') != -1) {
				o.push (l[i]);
			}
		}
		return o;
	}

	// This uses above 2 functions...
	// Just to get a list of categories elements.
	function getCategoryList() {
		var m;
		if (document.getElementsByClassName) {
			if ((m = document.getElementsByClassName('main-category')) && m.length > 0) // SVN
				return makeArray(m);
			if ((m = document.getElementsByClassName('category')) && m.length > 0) // 1.2b
				return makeArray(m);
			return [];
		}
		m = lookForDiv('main-category');
		if (m.length > 0) return m;
		m = lookForDiv('category');
		if (m.length > 0) return m;
	}
	
	// And this finds the header element matching it.
	// Should work with 1.3 beta 2 and in SVN.
	function getCategoryHeader(el) {
		var c = el.previousSibling;
		var processedClassName;
		while (c) {
			if (c.className != undefined) {
				processedClassName = (' ' + c.className.replace(/\s+/g, ' ') + ' ');
				if (processedClassName.indexOf(' main-subhead ') != -1 || processedClassName.indexOf(' main-head ') != -1) {
					return c;
				}
			}
			c = c.previousSibling;
		}
		return false;
	}

	// Get the expiry date.
	function getExpiryDate() {
		var d = new Date();
		d.setTime (d.getTime() + (86400000 * 30));
		return d.toGMTString();
	}
	
	// I found the categories.
	// What the heck we gonna do??
	function doSomethingWithThisCategoryWhoKnows(wtf) {
	
		// Yes find the categories. Not found? Loser.
		var header = getCategoryHeader(wtf);
		if (!header)
			return;

		// What's your ID?
		if (wtf.id === undefined || wtf.id == '')
			return;

		// It must be clickable!
		header.style.cursor = 'pointer';

		// Where does the text go?
		var h2 = header.getElementsByTagName('h2');
		if (!h2)
			return;
		
		// I found it.
		h2 = h2[0];

		// Make elements..
		var a = document.createElement('span');
		var i = document.createElement('img');
		var c = document.createElement('div');
		var n = document.createElement('div');
		i.style.verticalAlign = 'baseline';
		i.style.border = '0';
		i.width  = 11;
		i.height = 10;
		i.alt = 'Collapse / Expand';
		a.style.marginLeft = '5px';
		
		// co[N]tainer and [C]ontent.
		n.style.overflow = 'hidden';
		n.style.height = 'auto';
		n.style.position = 'relative';
		c.style.position = 'relative';
		
		// Add to container.
		while (wtf.firstChild)
			c.appendChild (wtf.firstChild);
		
		// In another container.
		n.appendChild (c);
		wtf.appendChild (n);
		
		// Functions for collapsible!
		var position = 1;
		var increment = 0;
		var timeout = 0;
		var firstTry = true;
		function updatePosition() {
			position += increment;
			position = Math.min(1, Math.max(0, position));
			var xa = position * position;
			var xb = 1 - position;
			var xc = 1 - (xb * xb);
			var xd = xa + ((xc - xa) * position);
			var maxHeight = c.offsetHeight;
			var height = xd * maxHeight;
			if (position != 1) {
				n.style.position = 'relative';
				c.style.position = 'relative';
			}
			n.style.height = Math.round(height) + 'px';
			c.style.top = Math.round(height - maxHeight) + 'px';
			if (position == 1) {
				n.style.height = 'auto';
				n.style.position = 'static';
				c.style.position = 'static';
			}
			if (position != 1 && position != 0) {
				timeout = setTimeout(updatePosition, 1);
			}
		}
		function collapse() {
			if (firstTry) {
				position = 0;
				firstTry = false;
			}
			i.src = DtTvB_Collapsible_Path + '/down.gif';
			header.onclick = expand;
			clearTimeout (timeout);
			increment = -0.06;
			timeout = setTimeout(updatePosition, 1);
			document.cookie = 'sdtc' + wtf.id + '=onfin;expires=' + getExpiryDate();
			return false;
		}
		function expand() {
			i.src = DtTvB_Collapsible_Path + '/up.gif';
			header.onclick = collapse;
			if (firstTry) {
				firstTry = false;
				return false;
			}
			clearTimeout (timeout);
			increment = 0.06;
			document.cookie = 'sdtc' + wtf.id + '=off;expires=' + getExpiryDate();
			timeout = setTimeout(updatePosition, 1);
			return false;
		}
		
		if (document.cookie.indexOf('sdtc' + wtf.id + '=onfin') != -1) {
			collapse ();
		} else {
			expand ();
		}
		
		h2.appendChild (a);
		a.appendChild (i);

	}
	
	// Get category list and loop thru.
	var list = getCategoryList();
	for (var i = 0; i < list.length; i ++) {
		doSomethingWithThisCategoryWhoKnows (list[i]);
	}

}, 0);









