var timeout = null;
var Delay = 10;
var sCurrentPage
var temp

function TreeNavigator(clientId, plus, minus)
{
	this.Container = document.getElementById(clientId);
    this.plus = plus;
    this.minus = minus;
    this.expanded = sDomainPath +"nav_arrow_down.jpg";
    this.expanded_hide = sDomainPath +"nav_arrow_over.jpg";
    var instance = this;
    var uls = this.Container.getElementsByTagName("ul");
    
    for(var i=0; i < uls.length; i++)
    {
		/* skip the product list (if present) */
		if (uls[i].id && uls[i].id == "categoryList")
			continue;
        if (i>0)
        {
            var img = document.createElement("img");
            uls[i].parentNode.insertBefore(img, uls[i].parentNode.firstChild);
            img.id = "img_"+ uls[i];
            
            if (uls[i].parentNode.className == "expanded")
            {
                img.src = this.minus;
            }
            else
            {
                if (sCurrentPage == uls[i].id) {
                    img.src = this.expanded;
                } else {
                    img.src = this.plus;
                }
            }
        }
    }
    
    var lis = this.Container.getElementsByTagName("li");
    var links = this.Container.getElementsByTagName("a");
    var lisUl = this.Container.getElementsByTagName("ul")[0].getElementsByTagName("ul");
    
    for (var i =0; i < links.length; i++) {
        if (links[i].nextSibling && links[i].nextSibling.tagName != "IMG")
        {
            links[i].onclick = function (e)
            {
                return instance.Toggle(e);
            }
        } else {
        
        }
    }
    
    for (var i =0; i < links.length; i++)
    {
        if (links[i].nextSibling && links[i].nextSibling.tagName != "IMG")
        {
            links[i].onclick = function (e)
            {
                return instance.Toggle(e);
            }
        } else {
        
            if (links[i].innerHTML == "Home") {
                links[i].style.display = "none";
            }
            
            if (lis[i].id == "houseMenuCurrentItem") {
                if (document.getElementById(lis[i].title)) {
                    document.getElementById(lis[i].title).style.display = "block";
                    sCurrentPage = lis[i].title;
                    document.getElementById(lis[i].title).parentNode.firstChild.src = this.expanded;
                }
                
                links[i].innerHTML = "<img src='"+ sDomainPath +"nav_bullet_down.jpg' border='0' align='absmiddle'> " + links[i].innerHTML
            } else {
                links[i].innerHTML = "<img src='"+ sDomainPath +"nav_bullet.jpg' border='0' align='absmiddle'> " + links[i].innerHTML
            }
        }
    }
    
    for (x=0;x<lisUl.length;x++) {
        for (y=0;y<lisUl[x].getElementsByTagName("ul").length;y++) {
            var lisXX = lisUl[x].getElementsByTagName("ul")[y].getElementsByTagName("li");
            
            for (z=0;z<lisXX.length;z++) {
                var ctr = eval(lisXX[z].className)+1;
                
                if (lisXX[z].id == "houseMenuCurrentItem") {
                    if(document.getElementById(lisUl[x].id)) {
                        document.getElementById(lisUl[x].id).style.display = "block"
                        document.getElementById(lisXX[z].className).style.display = "block"
                    }
                }
            }
        }
    }
}



TreeNavigator.prototype.Toggle = function(e)
{
	if (!e)
	{
		e = window.event;
	}

	var src = e.srcElement ? e.srcElement : e.target;
    
    if (!src.tagName)
    {
        src = src.parentNode;
    }
    
    if (src.tagName != "A")
	{
        return false;
	}
    
	var category = src.nextSibling;
	if (!category || category.tagName == "IMG")
	{
		return true;
	}
	if (category.style.display == "block")
	{
		this.Hide(category);
	}
	else
	{
		if (this.SingleExpand)
		{
			var parent = category.parentNode.parentNode;

			for (var i = 0; i < parent.childNodes.length; i++)
			{
                if (parent.childNodes[i].childNodes.length > 2)
				{
                    if (parent.childNodes[i].childNodes[2] != category)
                    {
                        parent.childNodes[i].childNodes[2].style.display = "none";
                        parent.childNodes[i].className = "collapsed";
                        this.HideImage(parent.childNodes[i].childNodes[2]);
                    }
                }
			}
		}
		
		//document.getElementById("txtMenuSelected").value = category.id;
		this.Show(category);
	}
	
	return false;
}

TreeNavigator.prototype.HideImage = function(node)
{
    if (document.all)
    {
        var instance = this;
        
        window.setTimeout(function() {
            if (sCurrentPage == node.id) {
                node.parentNode.firstChild.src = instance.expanded_hide;
            } else {
                node.parentNode.firstChild.src = instance.plus
            }

        }, 1);
    }
    else
    {
        if (sCurrentPage == node.id) {
            node.parentNode.firstChild.src = this.expanded_hide;
        } else {
            node.parentNode.firstChild.src = this.plus;
        }
    }
}

TreeNavigator.prototype.ShowImage = function(node)
{
    if (document.all)
    {
        var instance = this;
        window.setTimeout(function() {
            if (sCurrentPage == node.id) {
                node.parentNode.firstChild.src = instance.expanded;
            } else {
                node.parentNode.firstChild.src = instance.minus; 
            }
        }, 1);
    }
    else
    {
        if (sCurrentPage == node.id) {
            node.parentNode.firstChild.src = this.expanded;
        } else {
            node.parentNode.firstChild.src = this.minus;
        }
    }
}

TreeNavigator.prototype.Hide = function(node, img)
{
	if (timeout && this.Showing)
	{
		window.clearTimeout(timeout);
		this.Showing = false;
	}
	this.Hiding = true;
	node.style.overflow = "hidden";
	node.parentNode.className = "collapsed";
    this.HideImage(node);
	window.HideStep = function (height)
	{
		TreeNavigator.HideStep(height, node);
	}
	timeout = window.setTimeout("HideStep(" + node.scrollHeight + ")",5);
}

TreeNavigator.HideStep = function (scroll, node)
{
	var height = node.scrollHeight;
    scroll = scroll - Delay;
	if (scroll > 10 )
	{
		node.style.height = scroll + "px";
		timeout = window.setTimeout("HideStep(" + scroll + ")",5);
	}else
	{
		node.style.oveflow = "";
		node.style.display = "none";
	}
}

TreeNavigator.prototype.Show = function(node, img)
{
	if (timeout && this.Hiding)
	{
		window.clearTimeout(timeout);
		this.Hiding = false;
	}
	this.Showing = true;

	node.style.width = "100%";
	node.style.overflow = "hidden";
	node.style.display = "block";
	node.style.height = "1px";
	node.parentNode.className = "expanded";
    
    this.ShowImage(node);
    
	window.ShowStep = function(step)
	{
		TreeNavigator.ShowStep(step, node);
	};
	timeout = window.setTimeout("ShowStep(1)", 5);
}

TreeNavigator.ShowStep = function(scroll, node)
{
	var height = node.scrollHeight;
	scroll = scroll + Delay;
	if (scroll <= height)
	{
		node.style.height = scroll + "px";
		timeout= window.setTimeout("ShowStep(" + scroll + ")", 5);
	}else
	{
		node.style.height = "";
		node.style.overflow = "";
	}
}

function LoadMenuState(objState) {
    if (document.getElementById(objState)) {
        document.getElementById(objState).className = "expanded";
        document.getElementById(objState).style.display = "block";
        document.getElementById("txtMenuSelected").value = objState;
        
        return objState;
    }
}

function retrieveCookie(cookieName) {
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
	}
	return null;
}

function setCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue)
                     + ";expires="+expire.toGMTString();

}

function del_cookie(cookieName) {
    document.cookie = cookieName +
                     '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}