/* Functions neccesary for the proper operation of the navigation. 
 * These functions require certain CSS classes in the stylesheet. 
 */

var closeSpeed = 8;
var openSpeed = 8;
var changeSpeed = 1; 

function rmc_pushNav( handle )
{
    // get target box
    var showBox = true;
    var box = handle + "_list";
    var arrow = handle + "_arrow";
    var navBox = document.getElementById('nav_container');
    var targetBox = document.getElementById(box);

    // Detemine level
    var level = handle.substring(0,7);
            
    // Check and see if box is showing or hidden
    var boxClass = targetBox.className;
    if ( boxClass == 'nav_hide' )
        showBox = true;
    else
        showBox = false; 
	// Hide all open elements
    rmc_hideAllElements(level);
    // if we have to show something, show it.
    if ( showBox == true ) rmc_showSingleElement(targetBox.id);


}

function rmc_hideAllElements(level)
{
    var id = '';
    var shownElements = [];
    var checkLevel = '';
    var navBox = document.getElementById('nav_container');
    shownElements = getElementsByClassName('nav_show', 'ul', navBox);
    var countShowing = shownElements.length;

    if ( level == 'level_1' )
    {
        // Handle Level 1 hide
        // If you're hiding level 1, you have to hide level 1 and level 2.
        for ( y = 0; y < countShowing; y++ )
        {
            rmc_hideSingleElement(shownElements[y].id);
        }
        
    }
    else if ( level == 'level_2' )
    {
        // Handle level 2 hide
        for ( y = 0; y < countShowing; y++ )
        {
            id = shownElements[y].id;
            checkLevel = id.substring(0,7);
            if ( level == checkLevel )
            {
                rmc_hideSingleElement(shownElements[y].id);
            }
        }   
    }
    else
    {
        // Shouldn't hit this. Something's broken.
        alert('Javascript Error: Navigation Levels out of order');
    }
}

function rmc_hideSingleElement(id)
{
    // Hide the one element
    var target = document.getElementById(id);
    if ( target.className == 'nav_show' )
    {
        var fullHeight = 0;
        var moveBy = closeSpeed;
        var intId = setInterval(function() {
            var curHeight = target.offsetHeight;
            var newHeight = curHeight - moveBy;
            if (newHeight > fullHeight)
                target.style.height = newHeight + "px";
            else {
                clearInterval(intId);
                target.className = "nav_hide";
                target.style.height = "auto";
            }
        }, changeSpeed);
    }
	rmc_changeArrow(id);
}

function rmc_showSingleElement(id)
{
    // Show the one element
    var count = [];
    var target = document.getElementById(id);
    if ( target.className == 'nav_hide' )
    {
        var fullHeight = 0;
        var level = id.substring(0,7);
        // alert(level);
        if ( level == 'level_1' )
        {
            count = target.getElementsByTagName('ul');
			count2 = target.getElementsByTagName('div');
            count = count.length;
			count2 = count2.length;
			count = count + count2;
            // alert(count);
        }
        else
        {
            count = target.getElementsByTagName('li');
            count = count.length;
        }       
        
        // alert(count);
        fullHeight = count * 15;
        // alert(fullHeight);
        target.style.height = '0px';
        target.className = 'nav_show';
        var moveBy = openSpeed;
        var intId = setInterval(function() {
            var curHeight = target.offsetHeight;
            var newHeight = curHeight + moveBy;
            if (newHeight < fullHeight)
                target.style.height = newHeight + "px";
            else {
                clearInterval(intId);
                target.className = "nav_show";
                target.style.height = "auto";
            }
        }, changeSpeed);
    }
	rmc_changeArrow(id);
}

function rmc_changeArrow(id){
	// id should be the id of the list, not the arrow
	var count = id.length;
	count = count - 4
	var handle = id.substring(0,count);
	var targetArrow = handle + "arrow";
	var arrow = document.getElementById(targetArrow); 
	var arrowClass = arrow.className;
	// alert(arrowClass);
	switch ( arrowClass )
	{
    	case 'arrow_one':
		{
			arrow.className = 'arrow_one_down';
			break;
		}
    	case 'arrow_one_down':
		{
			arrow.className = 'arrow_one';
			break;
		}
    	case 'arrow_two':
		{
	   		arrow.className = 'arrow_two_down';
			break;
		}
	   	
    	case 'arrow_two_down':
		{
			arrow.className = 'arrow_two';
			break;
		}
		default:
		{
			// do nothing
			break;
		}
    }
}

function getElementsByClassName(className, tag, elm){
    var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
    var tag = tag || "*";
    var elm = elm || document;
    var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for(var i=0; i<length; i++){
        current = elements[i];
        if(testClass.test(current.className)){
            returnElements.push(current);
        }
    }
    return returnElements;
}
