function CommunityServer_CollapseBar_Init( appPath ) {
	if ( !CommunityServer_CollapseBar_UplevelBrowser() ) { return; }
	window.CommunityServer_CollapseBar_AppPath = appPath;
	for( var i=0; i < CommunityServer_CollapseBars.length; i++ ) {
		var barInfo = CommunityServer_CollapseBars[i];
		barInfo.ItemCount = parseInt( barInfo.ItemCount );
		barInfo.InitialSelectedIndex = parseInt( barInfo.InitialSelectedIndex );
		CommunityServer_CollapseBar_Load( barInfo );
	}
}
function CommunityServer_CollapseBar_UplevelBrowser() {
	if ( typeof( CommunityServer_CollapseBars ) == "undefined" ) return false;
	if ( !document.getElementById ) return false;
	if ( !document.getElementsByTagName ) return false;
	return true;
}
function CommunityServer_CollapseBar_Load( barInfo ) {
	var bar = document.getElementById( barInfo.ID );
	if ( bar == null ) { return; }
	bar.Items = new Array();
	bar.IndexOf = CommunityServer_CollapseBar_IndexOfItem;
	bar.CurrentItemTracker = CommunityServer_CollapseBar_GetHiddenElement( barInfo.HiddenID );
	bar.CurrentItemTracker.value = barInfo.InitialSelectedIndex;
	bar.CookieID = barInfo.ID;
	bar.SetCookie = CommunityServer_CollapseBar_SetCookie;
	bar.ClearSelection = CommunityServer_CollapseBar_ClearSelection;
	bar.AddItem = CommunityServer_CollapseBar_AddItem;
	
	var nestedDivs = bar.getElementsByTagName( "DIV" );
	var childDivs = new Array();
	for( var i = 0; i < nestedDivs.length; i++ ) {
		if ( nestedDivs[ i ].parentElement == bar ) {
			childDivs[ childDivs.length ] = nestedDivs[ i ];
		}
	}

	if ( childDivs.length == barInfo.ItemCount * 2 ) {
		for ( var i = 0; i < childDivs.length / 2; i++ ) {
			var itemButton = childDivs[ i ];
			var itemBody = childDivs[ i + barInfo.ItemCount ];
			bar.AddItem( itemBody, itemButton );
		}
	}

	if ( bar.Items.length > 1 ) {
		var selectedIndex = parseInt( bar.CurrentItemTracker.value );
		var selectedItem = bar.Items[ selectedIndex ];
		var nonSelectedItem;
		if ( selectedIndex == 0 ) {
			nonSelectedItem = bar.Items[ selectedIndex + 1 ];
		} else {
			nonSelectedItem = bar.Items[ 0 ];
		}
		if ( selectedItem.StylingElement.style.cssText ) {
			bar.CurrentItemStyle = selectedItem.StylingElement.style.cssText;
			bar.CurrentItemCssClass = selectedItem.StylingElement.className;
			bar.ItemStyle = nonSelectedItem.StylingElement.style.cssText;
			bar.ItemCssClass = nonSelectedItem.StylingElement.className;
		} else {
			bar.CurrentItemStyle = "";
			bar.CurrentItemCssClass = "";
			bar.ItemStyle = "";
			bar.ItemCssClass = "";
		}
	}
}
function CommunityServer_CollapseBar_AddItem( itemBody, itemButton ) {
	CommunityServer_CollapseBar_OverridePostbacks( itemButton );

	this.Items[ this.Items.length ] = itemButton;
	itemButton.Owner = this;
	itemButton.BodyPanel = itemBody;
	itemButton.StylingElement = itemButton;
	itemButton.StylingElement.ApplyStyle = CommunityServer_CollapseBar_ApplyStyle;
	itemButton.Select = CommunityServer_CollapseBar_Select;
	itemButton.onclick = itemButton.Select;
}
function CommunityServer_CollapseBar_ClearSelection() {
	for( var i=0; i < this.Items.length; i++ ) {
		var child = this.Items[i];
		child.StylingElement.ApplyStyle( this.ItemStyle );
		child.StylingElement.className = this.ItemCssClass;
		child.BodyPanel.style.display = "none";
	}
	this.CurrentItemTracker.value = -1;
	this.SetCookie( "-1" );
	
}
function CommunityServer_CollapseBar_Select() {
	this.Owner.ClearSelection();
	this.StylingElement.ApplyStyle( this.Owner.CurrentItemStyle );
	this.StylingElement.className = this.Owner.CurrentItemCssClass;
	this.BodyPanel.style.display = "";
	this.Owner.CurrentItemTracker.value = this.Owner.IndexOf( this );
	this.Owner.SetCookie( this.Owner.CurrentItemTracker.value );
}
function CommunityServer_CollapseBar_GetHiddenElement( name ) {
	for( var i = 0; i < document.forms.length; i++ ) {
		var theForm = document.forms[ i ];
		if ( theForm[ name ] != null ) {
			return theForm[ name ];
		}
	}
	return null;
}
function CommunityServer_CollapseBar_IndexOfItem( barItem ) {
	for( var i=0; i < this.Items.length; i++ ) {
		var item = this.Items[i];
		if ( item == barItem ) {
			return i;
		}
	}
	return -1;
}
function CommunityServer_CollapseBar_OverridePostbacks( container ) {
	container.onclick = function() { return false; };
	var postBackControls;
	postBackControls = container.getElementsByTagName( "A" );
	if ( postBackControls != null ) {
		for( var i = 0; i < postBackControls.length; i++ ) {
			var postBackControl = postBackControls[ i ];
			postBackControl.onclick = function() { return false; };
			postBackControl.href = "";
		}
	}
}
function CommunityServer_CollapseBar_ApplyStyle( newStyle ) {
	var isOpera = navigator.userAgent.toLowerCase().indexOf("opera") != -1;
	if ( isOpera ) {
		CommunityServer_CollapseBar_ClearStyle( this );

		if ( newStyle.length == 0 ) { return; }
		var nameValues = newStyle.split(";");
		for ( var i = 0; i < nameValues.length; i++ ) {
			var nameValue = nameValues[ i ].split( ":" );
			var name = CommunityServer_CollapseBar_Trim( nameValue[ 0 ] );
			var value = CommunityServer_CollapseBar_Trim( nameValue[ 1 ] );
			if ( name.indexOf( "-" ) != -1 ) {
				var namePieces = name.split( "-" );
				if ( namePieces.length > 1 ) {
					name = CommunityServer_CollapseBar_Trim( namePieces[ 0 ] ).toLowerCase();
					for( var j = 1; j < namePieces.length; j++ ) {
						var namePiece = CommunityServer_CollapseBar_Trim( namePieces[ j ] ).toLowerCase();
						namePiece = namePiece.substring( 0, 1 ).toUpperCase() + namePiece.substring( 1, namePiece.length );
						name += namePiece;
					}
				}
			}
			
			this.style[ name ] = value;
		}
	} else {
		this.style.cssText = newStyle;
	}
	var childElements;
	childElements = this.getElementsByTagName( "A" );
	if ( childElements != null ) {
		for( var i = 0; i < childElements.length; i++ ) {
			var childElement = childElements[ i ];
			childElement.style.color = this.style.color;
		}
	}
	childElements = this.getElementsByTagName( "INPUT" );
	if ( childElements != null ) {
		for( var i = 0; i < childElements.length; i++ ) {
			var childElement = childElements[ i ];
			childElement.style.color = this.style.color;
			childElement.style.backgroundColor = this.style.backgroundColor;
		}
	}
}
function CommunityServer_CollapseBar_ClearStyle( target ) {
	var nameValues = target.style.cssText.split(";");
	for ( var i = 0; i < nameValues.length; i++ ) {
		var nameValue = nameValues[ i ].split( ":" );
		var name = CommunityServer_CollapseBar_Trim( nameValue[ 0 ] );
		var value = CommunityServer_CollapseBar_Trim( nameValue[ 1 ] );
		if ( name.indexOf( "-" ) != -1 ) {
			var namePieces = name.split( "-" );
			if ( namePieces.length > 1 ) {
				name = CommunityServer_CollapseBar_Trim( namePieces[ 0 ] ).toLowerCase();
				for( var j = 1; j < namePieces.length; j++ ) {
					var namePiece = CommunityServer_CollapseBar_Trim( namePieces[ j ] ).toLowerCase();
					namePiece = namePiece.substring( 0, 1 ).toUpperCase() + namePiece.substring( 1, namePiece.length );
					name += namePiece;
				}
			}
		}
		target.style[ name ] = "";
	}
}
function CommunityServer_CollapseBar_Trim( s ) {
	if ( s == null ) {
		return "";
	}
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {
		s = s.substring(1,s.length);
	}
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
		s = s.substring(0,s.length-1);
	}
	return s;
}
function CommunityServer_CollapseBar_SetCookie( value ) {
	var name = "CollapseBar:" + this.CookieID;
	document.cookie = name + "=" + escape( value ) +
		( "; expires=" + ( new Date( ( new Date() ).getTime() + 30*24*60*60*1000) ).toGMTString() ) +
		( "; path=" + window.CommunityServer_CollapseBar_AppPath );
}