/*
Script by RoBorg
RoBorg@geniusbug.com
http://javascript.geniusbug.com | http://www.roborg.co.uk
Please do not remove or edit this message
Please link to this website if you use this script!
*/

slideSpeed = 100;

function initMenus(openGenres)
{
	if(!document.getElementsByTagName) return;
	
	slider = new Array();
	//var divs = document.getElementsByTagName('div');
	var divs = getElementsUnderMenu(document.getElementById('menuContainer'));

	/*for(var x=0; x<divs.length; x++)
	{	if(divs[x].className == 'button1')
			alert(divs[x].innerHTML);
	}*/

	for(var x=0; x<divs.length; x++)
	{
		divs[x].originalHeight = divs[x].offsetHeight;
		if(divs[x].className == 'menu') divs[x].speed = -1;
		if(divs[x].className == 'button1' || divs[x].className == 'button2') divs[x].onclick = function() { toggle(this); }
	}

	firstLevel = false;
	secondLevel = false;

	for(var x=0; x<divs.length; x++)
	{
		if(openGenres){
			if(divs[x].className == 'button1'){
				divs[x].className = 'pressedButton1';
				firstLevel = true;
				secondLevel = false;
				continue;
			}

			if(divs[x].className == 'button2'){
				secondLevel = true;
				continue;
			}

			if(divs[x].className != 'menu') continue;

			if(firstLevel && (!secondLevel)){ 
				divs[x].speed = 1;
				continue;
			}
		}
		else {
			if(divs[x].className != 'menu') continue;
		}

		divs[x].style.height = '1px';
		divs[x].style.display = 'none';
	}
}

function getElementsUnderMenu(startNode) {
	var arr = new Array();
	rec(startNode, arr);
	return arr;
}

function rec(startNode, arr) {
	if(startNode == null || !startNode.hasChildNodes()) return;
	var children = startNode.childNodes;
	//alert('children length '+children.length);
	for(var i=0; i < children.length; i++){
		//alert(children[i].tagName);
		
		if(children[i].tagName == 'div' || children[i].tagName == 'DIV'){
			arr.push(children[i]);
		}
		rec(children[i], arr);
	}
}

function toggle(obj)
{	
	if(obj.className == 'button1' || obj.className == 'pressedButton1'){
		obj.className = (obj.className == 'button1')?'pressedButton1':'button1';
	}
	if(obj.className == 'button2' || obj.className == 'pressedButton2'){
		obj.className = (obj.className == 'button2')?'pressedButton2':'button2';
	}
	while(obj.nextSibling && (obj.className != 'menu')) obj = obj.nextSibling;
	
	obj.speed = -1 * obj.speed;
	if(obj.slideTimer) return;	//Already moving

	var x = slider.length;
	slider[x] = obj;
	slide(x);
}

function slide(x)
{
	var obj = slider[x];
	if(obj.style.display != 'block') obj.style.display = 'block';
	var height = obj.offsetHeight + obj.speed * slideSpeed;
	var targetHeight = getChildrensHeights(obj);

	if(height > targetHeight)
	{
		obj.style.height = targetHeight + 'px';
		obj.slideTimer = false;
		resizeParents(obj, 0);
		return;
	}
	
	if(height <= 1)
	{
		obj.style.height = '1px';
		obj.style.display = 'none';
		obj.slideTimer = false;
		resizeParents(obj, 0);
		return;
	}
	
	obj.style.height = height + 'px';
	obj.slideTimer = setTimeout('slide(' + x + ');', 50);

	resizeParents(obj, targetHeight - height);
}

function getChildrensHeights(obj)
{
	if(!obj.firstChild) return 0;
	if(!obj.tagName.match(/div/i)) return 0;

	var height = 0;
	tmp = obj;
	obj = obj.firstChild;
	do height += getChildrensHeights(obj);
	while(obj = obj.nextSibling);
	
	if(height == 0) height = tmp.offsetHeight;
	
	return height;
}

function resizeParents(obj, diff)
{
	if(obj.className == 'menuContainer') return;
	obj = obj.parentNode;
	height = getChildrensHeights(obj) - diff;
	obj.style.height = height + 'px';
	resizeParents(obj, diff);
}