/* (c) SISTRUM 2007 */

function CustomCategoriesNavigation()
{
    this.AxisIds = "";
    this.Delimiter = "#";
    this.StyleBlock = "block";
    this.StyleNone = "none";
    
    //this.SelectedColor = "#999";
    this.SelectedNodeCssClass = "sel";
    this.ProductListUrl = "Proizvodi.aspx?ctype=";
    
    this.ExpandSelectedCategory();
}

// Setup navigacije, otvara se odabrana kategorija.
CustomCategoriesNavigation.prototype.ExpandSelectedCategory = function()
{
    cat_id = RequestQueryString('ctype');
	if( !cat_id || ( cat_id == "" ) )
	    return;

	var obj = document.getElementById( "div_" + cat_id );	

	if( obj )
	{
		this.FormatSelected( obj );
		this.AxisIds = this.Delimiter + obj.id + this.Delimiter;
		this.GetParentIds( obj );
		
		var s = this.AxisIds.substring( 1, this.AxisIds.length-1 );
		var arr = s.split( "#" );
		for( var i = 0; i < arr.length; i++ )
		{
			var el = document.getElementById( arr[i] );
			if( el )
			{
				if( this.IsDiv( el ) )
				{
					el.style.display = this.StyleBlock;
			    }
			}				
		}	
		
		//otvori podstavke
	    for( var i = 0; i < obj.childNodes.length; i++ )
	    {			    
		    var el = obj.childNodes.item(i);				
		    if( this.IsDiv( el ) )
		    {
			    el.style.display = this.StyleBlock;
		    }
	    }							
	}		
}

// Otvara kliknuti DIV promjenom visibility style property-a.
// Zatvara sve ostale koji nisu na axis-u.
CustomCategoriesNavigation.prototype.Toggle = function(e)
{
    if(!e) {e = window.event;} 
    var obj = this.GetEventObject(e);
    obj = obj.parentNode;

	for( var i = 0; i < obj.childNodes.length; i++ )
	{			    
		var el = obj.childNodes.item(i);				
		if( this.IsDiv( el ) )
		{
			if( el.style.display == this.StyleBlock )
				el.style.display = this.StyleNone;
			else
			{
				el.style.display = this.StyleBlock;
				this.CloseOtherDivs( obj );
			}
		}
	}	
	this.StopEventBubbling(e);	
}

//Otvara stranicu sa odabranom kategorijom.
CustomCategoriesNavigation.prototype.Open = function(e)
{
    if(!e) {e = window.event;} 
    var obj = this.GetEventObject(e);
	var url = this.ProductListUrl + obj.id.replace( "div_", "" );
	this.StopEventBubbling(e);
	document.location.href = url;	
}

/* --------------- Pomoæne funkcije --------------- */

CustomCategoriesNavigation.prototype.StopEventBubbling = function( e )
{
	if(typeof(e.stopPropagation) == "function")
    {
	    e.stopPropagation();
	    e.preventDefault();
	}
	else
	{
	    e.cancelBubble = true;
	    e.returnValue = false;	    
    }    
}

CustomCategoriesNavigation.prototype.GetEventObject = function( e )
{
    var obj = null;    
    if(typeof(window.event) != "undefined" )
        obj = e.srcElement;
    else
    {      
        obj = e.target;
        while(obj.nodeType != 1)
	        obj = obj.parentNode;
    }
    
    return obj;
}

// Formatira odabranu kategoriju.
CustomCategoriesNavigation.prototype.FormatSelected = function( obj )
{
	obj.className = this.SelectedNodeCssClass;
}

// Poziva se iz toggle nakon expanda.
CustomCategoriesNavigation.prototype.CloseOtherDivs = function( obj )
{
	this.AxisIds = this.Delimiter + obj.id + this.Delimiter;
	this.GetKidIds( obj );
	this.GetParentIds( obj );	
	this.CloseAxisIds();
}

// Zatvara sve DIV ciji je ID u delimitiranom AxisIds stringu
CustomCategoriesNavigation.prototype.CloseAxisIds = function ()
{
	var categories_navigation = document.getElementById( "categories_navigation" );
	if( categories_navigation )
	{
		var divs = categories_navigation.getElementsByTagName( "div" );
		for( var i = 0; i < divs.length; i++ )
		{
			if( this.AxisIds.indexOf( this.Delimiter + divs[i].id + this.Delimiter ) == -1 )
			{
				divs[i].style.display = this.StyleNone;
			}
		}
	}
} 

// Dohvat ID-a ispod poslanog DIV-a.
CustomCategoriesNavigation.prototype.GetKidIds = function ( obj )
{
	for( var i = 0; i < obj.childNodes.length; i++ )
	{
		var el = obj.childNodes.item( i );
		if( this.IsDiv( el ) )
			this.AxisIds += el.id + this.Delimiter;
	}
}

// Dohvat ID-a od parent DIV-ova.
CustomCategoriesNavigation.prototype.GetParentIds = function( obj )
{
	if( obj.id != "categories_navigation" )
	{
		if( obj.parentNode )
		{
			this.GetSiblings( obj.parentNode );
			if( this.IsDiv( obj.parentNode ) && ( obj.parentNode.id != "categories_navigation" ) )
			{
				this.AxisIds += obj.parentNode.id + this.Delimiter;
			}			
			this.GetParentIds( obj.parentNode ); //rekurzija
		}
	}
}

// Dohvat siblinga od poslanog DIV-a.
CustomCategoriesNavigation.prototype.GetSiblings = function ( obj )
{
	if( obj )
	{
		for( var i = 0; i < obj.childNodes.length; i++ )
		{
			var el = obj.childNodes.item(i);
			if( this.IsDiv( el ) )
				this.AxisIds += el.id + this.Delimiter;
		}
	}
}


// Testira da li je DOM obj div/DIV.
CustomCategoriesNavigation.prototype.IsDiv = function ( el )
{
	return ( el.nodeName.toLowerCase() == "div" )
}

