var expanded = new Array( );
var roll_out = new Array( );
var roll_over = new Array( );
var images = new Array( );

// preload parent images
i = 0;
while ( i < parents.length ) {
	images[ i ] = new Image( image_width, image_height );
	images[ i ].src = parents[ i ];
	i++;

	images[ i ] = new Image( image_width, image_height );
	images[ i ].src = parents_over[ i ];
	i++;

	images[ i ] = new Image( image_width, image_height );
	images[ i ].src = parents_down[ i ];
	i++;
}

// preload children images
for ( var j = 0; j < children.length; j++ ) {
	for ( var k = 0; k < children[ j ].length; k++ ) {
		images[ i ] = new Image( image_width, image_height );
		images[ i ].src = children[ j ][ k ];
		i++;

		images[ i ] = new Image( image_width, image_height );
		images[ i ].src = children_over[ j ][ k ];
		i++;
	}
}

function ChangeSlotimg( id, over ) {
	if( document.images ) {

		if ( over ) {
			if ( roll_over[ id ] != "nochange" )
				document[ "slot" + id ].src = ( roll_over[ id ] ? roll_over[ id ] : image_shim );
		}
		else {
			if ( roll_out[ id ] != "nochange" )
				document[ "slot" + id ].src = ( roll_out[ id ] ? roll_out[ id ] : image_shim );
		}

	}
}

function pullout( needle, haystack ) {
	var newarray = new Array( );
	var newarrayindex = 0;
	for ( var i = 0; i < haystack.length; i++ )
		if ( haystack[ i ] != needle )
			newarray[ newarrayindex++ ] = haystack[ i ];
	return newarray;
}

function inArray( needle, haystack ) {
	for ( var i = 0; i < haystack.length; i++ )
		if ( haystack[ i ] == needle )
			return true;
	return false;
}

function selMe( id ) {
	if( document.images ) {

		var slotcount = 0;
		var parentslot = 0;
		var selected = false;
		
		for ( i = 0; slotcount < totalslots; i++ ) {

			if ( parents[ i ] ) {

				selected = ( children[ i ] && inArray( i, expanded ) );
				nowselected = ( parentslot == id );

				document[ "slot" + slotcount ].src = ( ( ( nowselected && !selected ) || ( !nowselected && selected ) ) ? parents_down[ i ] : parents[ i ] );
				roll_out[ slotcount ] = ( ( ( nowselected && !selected ) || ( !nowselected && selected ) ) ? "nochange" : parents[ i ] );
				roll_over[ slotcount ] = ( ( ( nowselected && !selected ) || ( !nowselected && selected ) ) ? "nochange" : parents_over[ i ] );

				if ( ( nowselected ) && ( parents_urls[ i ] ) ) {

					if ( parents_urls[ i ] == "popup:contact" )
						window.open( "http://www.tribalddb.ca/wwcontact/", "contact", "width=520,height=430,toolbar=no,resizable=yes,scrollbars=yes" );
					else
						top.location = parents_urls[ i ];

				}
				else if ( ( nowselected ) && ( selected ) ) {
					
					expanded = pullout( i, expanded );
					
				}
				else if ( ( children[ i ] ) && ( nowselected || selected ) ) {

					if ( !selected )
						expanded[ expanded.length ] = i;

					for ( var childcount = 0; ( childcount < children[ i ].length ) && ( (slotcount+1) < totalslots ); childcount++ ) {
						
						slotcount++;

						if ( ( slotcount != parentslot ) && ( slotcount == id ) && ( children_urls[ i ][ childcount ] ) ) {
						
							top.location = children_urls[ i ][ childcount ];
							
						}

						document[ "slot" + slotcount ].src = children[ i ][ childcount ];
						roll_out[ slotcount ] = children[ i ][ childcount ];
						roll_over[ slotcount ] = children_over[ i ][ childcount ];

					}

				}

				parentslot++;
				parentslot += ( selected ? children[ i ].length : 0 );
			}

			else {
				document[ "slot" + slotcount ].src = image_shim;
				roll_out[ slotcount ] = "";
				roll_over[ slotcount ] = "";
			}

			slotcount++;
		}

	}
}

function printTable() {

	var is_open = false;

	document.writeln('<table cellspacing="0" cellpadding="0" border="0">');
	for ( var i = 0, iP = 0; i < totalslots; i++, iP++ ) {
		// does this parent start opened?
		is_open = ( (initexpand-1) == iP );

		// print parent slot & set rollovers
		document.writeln('<tr><td><a href="javascript:selMe(' + i + ');" onMouseOver="ChangeSlotimg(' + i + ',true);" onMouseOut="ChangeSlotimg(' + i + ',false);"><img src="' + ( parents[ iP ] ? ( is_open ? parents_down[ iP ] : parents[ iP ] ) : image_shim ) + '" name="slot' + i + '" width=' + image_width + ' height=' + image_height + ' alt="" border="0"></a></td></tr>');
		roll_out[ i ] = ( is_open ? parents_down[ iP ] : parents[ iP ] );
		roll_over[ i ] = ( is_open ? parents_down[ iP ] : parents_over[ iP ] );

		if ( ( is_open ) && ( children[ iP ] ) ) {
			for ( var iC = 0; ( (i+1) < totalslots ) && ( iC < children[ iP ].length ); iC++ ) {
				i++;
				// print child slot & set rollovers
				document.writeln('<tr><td><a href="javascript:selMe(' + i + ');" onMouseOver="ChangeSlotimg(' + i + ',true);" onMouseOut="ChangeSlotimg(' + i + ',false);"><img src="' + children[ iP ][ iC ] + '" name="slot' + i + '" width=' + image_width + ' height=' + image_height + ' alt="" border="0"></a></td></tr>');
				roll_out[ i ] = children[ iP ][ iC ];
				roll_over[ i ] = children_over[ iP ][ iC ];
			}

			// remember that this parent is open
			expanded[ 0 ] = iP;
		}

	}
	document.writeln('</table>');

}