// initial event setup
if( window.XMLHttpRequest || window.ActiveXObject )
{
	addEvent( window, "load", categoryBrowser_initialise, false )
	addEvent( window, "unload", categoryBrowser_dispose, false )
}

// dropdownlist prefix for category browser select lists
dropdown_prefix = "categoryBrowser_";

// variables
var http_request = false;
var selectedCategories = null;

/*
	setupInitialCategories
	...
*/
function populateCategories( categoryId, index, delegate )
{
	if( isNaN( categoryId ) )
	{
		//alert( "populateCategories: categoryId was not a number." );
		return;
	}
	else if( isNaN( index ) )
	{
		alert( "populateCategories: index was not a number." );
		return;
	}
	
	if( typeof http_request == "object" )
	{
		//return;
		http_request.abort();
	}
	else
	{
		http_request = false;

		if( window.XMLHttpRequest )
		{
			http_request = new XMLHttpRequest();
			
			if( http_request.overrideMimeType )
				http_request.overrideMimeType( "text/xml" );
		}
		else if( window.ActiveXObject )
		{
			try
			{
				http_request = new ActiveXObject( "Msxml2.XMLHTTP" );
			}
			catch( e )
			{
				try{http_request = new ActiveXObject( "Microsoft.XMLHTTP" );}
				catch( e ){}
			}
		}
	}
	
	if( !http_request )
	{
		alert( "populateCategories: cannot create XMLHttpRequest" );
		return false;
	}
	
	resetList( index );
	
	http_request.onreadystatechange = delegate || function(){ populateList( index ); };
	http_request.open( "GET", resolveUrl( "Utilities/Xml/Category/GetChildren.aspx?categoryId=" + categoryId ), true );
	http_request.send( null );
}

/*
	categoryBrowser_initialise
	...
*/
function categoryBrowser_initialise( e )
{
	var index = -1;
	var list;
	while( null != ( list = document.getElementById( getDropdownId( ++index ) ) ) )
	{
		// properties
		list.listIndex = index;
		list.nextListIndex = index + 1;
		
		// methods
		list.getCategoryId = function( e )
		{
			if( !e ) var e = window.event;
			var target = e.srcElement || e.target;
			return target.options[ target.selectedIndex ].value;
		};
		
		// event handlers
		list.categoryChanged = function( e )
		{
			if( !e ) var e = window.event;
			var target = e.srcElement || e.target;
			populateCategories( target.getCategoryId( e ), target.nextListIndex );
			
			// remove default item i.e. Step n from list
			if( isNaN( target.options[ 0 ].value ) )
			{
				//var selectedIndex = target.selectedIndex;
				target.removeChild( target.childNodes[ 0 ] );
				//target.selectedIndex = selectedIndex - 1;
			}
		};
		
		// events
		addEvent( list, "change", list.categoryChanged, false );
	}

	var params = document.location.search.substring( 1 ).split( '&' );
	for( var i = 0; i < params.length; i++ )
	{
		var pair = params[ i ].split( '=' );
		if( /^Path$/i.test( pair[ 0 ] ) )
		{
			selectedCategories = pair[ 1 ].split( ',' );
		}
	}
	
	if( null == selectedCategories && /CategoryId=/i.test( document.location.search ) )
	{
		var path = getCookie( "categoryBrowser_Path" );
		if( null != path )
		{
			selectedCategories = path.split( ',' );
		}
	}
	
	populateCategories( 0, 0 );
}

/*
	categoryBrowser_dispose
	...
*/
function categoryBrowser_dispose( e )
{
	//setCookie( "categoryBrowser_state", getSelectedCategories() );
	http_request = null;
}

/*
	changeCategory
	...
*/
function changeCategory( url )
{
	var path = getSelectedCategories();
	if( path.length > 0 )
	{
		window.location = url + getSelectedCategories();
	}
}

/*
	getSelectedCategories
	...
*/
function getSelectedCategories()
{
	var categories = [];
	var index = -1;
	var list;
	while( null != ( list = document.getElementById( getDropdownId( ++index ) ) ) )
	{
		var categoryId = list.options[ list.selectedIndex ].value;
		if( !isNaN( categoryId ) ) 
			categories.push( categoryId );
	}
	return categories;
}

/*
	getDropdownId
	...
*/
function getDropdownId( index )
{
	return dropdown_prefix + index;
}

/*
	resetList
	...
*/
function resetList( targetIndex )
{
	if( isNaN( targetIndex ) )
	{
		alert( "resetList: index was not a number." );
		return;
	}
	
	var categories = [];
	var index = targetIndex - 1;
	var list;
	while( null != ( list = document.getElementById( getDropdownId( ++index ) ) ) )
	{
		while( list.childNodes.length > 0 )
			list.removeChild( list.childNodes[ 0 ] );
		
		var name = index == targetIndex 
			? "Loading..." 
			: "Step " + ( index + 1 );
		appendOption( list, name, "-" );
	}
}

/*
	populateList
	...
*/
function populateList( index )
{
	if( isNaN( index ) )
	{
		alert( "populateList: index was not a number." );
		return;
	}
	
	if( http_request.readyState == 4 )
	{
		if( http_request.status == 200 )
		{
			var list = document.getElementById( getDropdownId( index ) );
			if( list )
			{
				if( list.childNodes.length != 0 )
					list.removeChild( list.childNodes[ 0 ] );
				
				var xmldoc = http_request.responseXML;
				var children = xmldoc.getElementsByTagName( "child" );
				
				var t = typeof document._categoryNoOption == "string" ? document._categoryNoOption : "Press Go"
				var defaultOptionName = children.length > 0 
					? "Step " + ( index + 1 ) 
					: "Step " + ( index + 1 ) + " - " + t;
				appendOption( list, defaultOptionName, "-" );
				
				list.disabled = children.length == 0;
				
				var selectedCategoryId = 0;
				if( null != selectedCategories && selectedCategories.length )
				{
					selectedCategoryId = selectedCategories[ index ];
				}
				
				for( var i = 0; i < children.length; i++ )
				{
					var category = children[ i ];
					var name = category.getAttribute( "name" );
					var categoryId = category.getAttribute( "categoryId" );
					var isSelected = categoryId == selectedCategoryId;
					appendOption( list, name, categoryId, isSelected );
					
					if( isSelected ) // call next population of categories
					{
						selectedCategories[ index ] = null;
						populateCategories( selectedCategoryId, index + 1 );
						if( isNaN( list.options[ 0 ].value ) )
						{
							list.removeChild( list.childNodes[ 0 ] );
						}
					}
				}
			}
		}
	}
}

/*
	appendOption
	...
*/
function appendOption( list, name, value, isSelected )
{
	var option = document.createElement( "option" );
	option.innerHTML = name;
	option.value = value;
	option.selected = isSelected || false;
	list.appendChild( option );
}
