﻿
var timeout = null;
var Delay = 20;

function Panelbar(clientId, plus, minus)
{
	this.Container = document.getElementById(clientId);
    this.plus = plus;
    this.minus = minus;
    var instance = this;

    var uls = this.Container.getElementsByTagName("ul");
    for(var i=0; i < uls.length; i++)
    {
		if (uls[i].id && uls[i].id == "categoryList"){
			continue;
		}
    }
    var links = this.Container.getElementsByTagName("a");
    for (var i =0; i < links.length; i++)
    {
        if (links[i].nextSibling )
        {            
            links[i].onclick = function (e)
            {
                return instance.Toggle(e);
            }
        }
    }
}

Panelbar.GetParentOfClicked = function(source){

  if(source.tagName == "A"){
    return  source;
  }
  else if(source.tagName == "SPAN"){
    return source.parentNode;
  }
  else if(source.tagName == "IMG"){
    return  source.parentNode.parentNode;
  }
}

Panelbar.GetFirstDivOfHref = function(source){
  return source.nextSibling;
}

Panelbar.prototype.ChangeDivDisplayOfLiNode = function(senderLi, style){    
    var divs = senderLi.getElementsByTagName("div");
    if(divs.length > 0){
	    var firstDiv = divs[0];
        firstDiv.style.display = style;        
	}
}

Panelbar.prototype.ChangeClassNameOfLiNode = function(senderLi){
   
    var parentUL = senderLi.parentNode;
    
    for (var i = 0; i < parentUL.childNodes.length; i++)
	{
	    var level1Li = parentUL.childNodes[i];	   	   
	           
	    if(senderLi == level1Li){ //
	        if(parentUL.className == "rootGroup"){
	            level1Li.firstChild.className = level1Li.childNodes.length > 1 ? "link PanelCategoryItem expanded" : "link PanelCategoryItem normal" ;
	        }
	        else{
	            level1Li.firstChild.className = level1Li.childNodes.length > 1 ? "link PanelCategoryItemSub expanded" : "link PanelCategoryItemSub normal" ;	            
	        }
	    }
	    else{	   	        
	        if(this.SingleExpand){
                this.ChangeDivDisplayOfLiNode(level1Li, "none");
                if(parentUL.className == "rootGroup"){
	                var level122Li = parentUL.childNodes[i];
	                level1Li.firstChild.className = level1Li.childNodes.length > 1 ? "link PanelCategoryItem collapsed" : "link PanelCategoryItem normal" ;
	            }
	            else{
	                var len = 1;
	                level1Li.firstChild.className = level1Li.childNodes.length > len ? "link PanelCategoryItemSub collapsed" : "link PanelCategoryItemSub normal" ;
	            }
            }
        }
    }//for
}


Panelbar.prototype.Toggle = function(e)
{
	if (!e)
	{
		e = window.event;
	}


	var src = e.srcElement ? e.srcElement : e.target;
    if (!src.tagName)
    {
        src = src.parentNode;
    }
    
  
    var hrefNode;
    hrefNode = Panelbar.GetParentOfClicked(src);
    

  
    var divOfHref = hrefNode.nextSibling;   
    //firefoxdan dolayi 
    while (divOfHref.tagName == undefined){
        divOfHref = divOfHref.nextSibling;
    }
    
   

    category = divOfHref;
    
	if (category.style.display == "block")
	{
        var parentUL = hrefNode.parentNode.parentNode;
        this.Hide(divOfHref);       
        hrefNode.className = parentUL.className == "rootGroup" ? "link PanelCategoryItem collapsed" : "link PanelCategoryItemSub collapsed";
	}
	else
	{
	     
	
        this.Show(divOfHref);
        this.ChangeClassNameOfLiNode(hrefNode.parentNode);
    }
	return false;
}

Panelbar.prototype.HideImage = function(node)
{
    if (document.all)
    {

        var parentLi = node.parentNode;
        if(parentLi){   
            var firstHref = parentLi.firstChild;
            var textSpan = firstHref.firstChild;
            var instance = this;
            if(textSpan.firstChild.tagName == undefined){
            
            }
            else{
                window.setTimeout(function() {textSpan.text= "";}, 1);}
            }
    }
    else
    {
    }
}

Panelbar.prototype.ShowImage = function(node)
{


    if (document.all)
    {

        var parentLi = node.parentNode;
        if(parentLi){
            var firstHref = parentLi.firstChild;
            var textSpan = firstHref.firstChild;
            var instance = this;            
            if (textSpan.firstChild.tagName == undefined){             
            }else{
              window.setTimeout(function() {textSpan.text = "dd"; }, 1);}
            }
    }
    else
    {

    }
}

Panelbar.prototype.Hide = function(node, img)
{
	if (timeout && this.Showing)
	{
		window.clearTimeout(timeout);
		this.Showing = false;
	}
	this.Hiding = true;

	node.style.overflow = "hidden";
  //  this.HideImage(node);
	window.HideStep = function (height)
	{
		Panelbar.HideStep(height, node);
	}
	timeout = window.setTimeout("HideStep(" + node.scrollHeight + ")",5);
}

Panelbar.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";
	}
}

Panelbar.prototype.Show = function(node, img)
{
  //  alert("show" + node.tagName + " " + node.innerHTML);
	if (timeout && this.Hiding)
	{
		window.clearTimeout(timeout);
		this.Hiding = false;
	}
	
	var ulNode;
	for( var x = 0; node.childNodes[x]; x++ ) 
	{
	    if(node.childNodes[x].tagName != undefined){
	        ulNode = node.childNodes[x];
	        break;
	    }   
	}

	
	this.Showing = true;    
    node.style.width = "100%";
	node.style.overflow = "hidden";
	node.style.display = "block";
	node.style.height = "1px";
	ulNode.style.display = "block";


	window.ShowStep = function(step)
	{
		Panelbar.ShowStep(step, node);
	};
	timeout = window.setTimeout("ShowStep(1)", 5);
}

Panelbar.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 = "";
	}
}



