
function backpack_add( in_uri, in_which ) {
	var img;
	if( img = getObj( in_which ) ) {
		if( !img._added ) {
			img._added = true;
			XMLReq.pullURI( in_uri, function( xml ) { } );
			b = img.src.split( '.' );
			s = b.pop();
			f = b.pop();
			
			var hovers = false;
			if( img.className.indexOf( 'uvHoverable' ) != -1 ) {
				hovers = true;
				f = f.replace( /_h$/, '' );
			}
			
			b.push( f + '_i' );
			b.push( s );
			
			img.src = b.join( '.' );
			
			img.style.cursor = 'default';
			img.alt = img.title = 'Added to your Trip Planner';
			
			if( hovers ) {
				img.uvhoverable_off = img.src;
				img.uvhoverable_on	= img.src;
				}
		
			if( backpackChangeCount ) {
				backpackChangeCount( +1 );
				}
			}
		}
		
	}
	
function view_day( ) {
	window.location = auri(
		  'backpack/print/?e='
		+ atpItineraryID
		+ '&'
		+ 'd='
		+ atpMultiday._selDay
		+ '&'
		+ '_print'
		);
	}

function add_note( ) {
	window.location
		= auri( 'tripnote/create/'+atpItineraryID+'/'+atpMultiday._selDay+'/' )
		;
	}

function add_map( ) {
	// FIXME: these should be services
//	window.location =
//		auri( 'tripmap/create/'+atpItineraryID+'/'+atpMultiday._selDay+'/' );
	window.location =
		auri( 'backpack/map/?f_itinerary='
			+ atpItineraryID
			+ '&'
			+ 'f_day='
			+ atpMultiday._selDay
			);
	}

function add_directions( ) {
	window.location = auri(
		  'backpack/directions/?f_itinerary='
		+ atpItineraryID
		+ '&'
		+ 'f_day='
		+ atpMultiday._selDay
		);
	}


function fbox( in_uri ) {
	
	var _fbox   = getObj( 'uvFBox' );
	
	if( !_fbox ) {
		document.body.appendChild(
			_fbox = document.createElement( 'div' )
			).id = 'uvFBox';
		
		_fbox.appendChild(
			_fbox._core = document.createElement( 'div' )
			).id = 'uvFBoxCore';
						
		_fbox._core.appendChild(
			_fbox._wait = document.createElement( 'img' )
			).id = 'uvFBoxWait';
		
		_fbox._wait.src = auri( 'resource/image/engine/status_wait.gif' );
		
		_fbox._core.appendChild(
			_fbox._main = document.createElement( 'div' )
			).id = 'uvFBoxMain';
		
		_fbox.assemble = function( xml, uri ) {
			if( xml.block ) {
			    
				this._cache[ uri ] = xml;
				setHTML( this._main, xml.block );
				this._main.style.display = 'block';
				this._wait.style.display = 'none';
				LiteboxController.update( );
				}
			}
			
		_fbox._cache    = [];
		_fbox._litebox  = new Litebox( );
		}
	
	document.onselectstart = function( ) { return true; }
	
	fbox_unfocus_bg( );
	fbox_prep_refocus( _fbox._litebox );
	
	if( _fbox._cache[ in_uri ] ) {
		_fbox.assemble( _fbox._cache[ in_uri ], in_uri );
		}
	else {
		_fbox._main.style.display = 'none';
		_fbox._wait.style.display = 'block';
		XMLReq.pullURI(
			in_uri,
			function( xml ) { _fbox.assemble( xml, in_uri ); }
			);
		}
	
	 _fbox
	._litebox
	.setCanvas( _fbox, true )
	.setIgnoreCanvasClick( true )
	.setScrollLock( false )
	.setDelayUncloaking( true );
	
	_fbox._litebox.show( );
	_fbox._litebox.updateCanvasPosition( );
	_fbox._litebox.uncloakCanvas( );
	
	}

function fbox_image( in_uri ) {
    fbox_unfocus_bg( );
	
    var litebox = new LiteboxImage( auri( in_uri ) );
    litebox.setIgnoreCanvasClick( true );
    
    fbox_prep_refocus( litebox );
}

// Strip out controls and scroll boxes that don't respect the overlay
function fbox_unfocus_bg( ) {
    if( document.body ) {
		document.body.origClass = document.body.className;
		document.body.className = document.body.className + ' uvNoFocus';
	}
}

// Restore controls/scroll box visibility on return
function fbox_prep_refocus( in_litebox ) {
    in_litebox.getFrame( ).onclick = chainHandlers(
          in_litebox.getFrame( ).onclick
        , function( e ) {
			document.onselectstart = abortEvent;
			if( document.body ) {
				document.body.className = document.body.origClass;
			}
          }
    );
}

new Image().src = auri( 'resource/image/backpack/half_mask.gif' );

function remove_from_tabs( in_id, in_tabs, in_uri ) {
	if( in_tabs[ in_id ] ) {
		for( var k in in_tabs[ in_id ] ) {
			var nTab = in_tabs[ in_id ][ k ];

			var nTarg = getObj(
				'uvTab_magnitude_' + nTab
				);

			if( nTarg ) {
				var n =  parseInt( getText( nTarg ) ) - 1;
				if( n == 0 ) {
					if( nTab != 0 ) {
						removeNode( getObj( 'uvTab_widget_' + nTab ) );
						removeNode( getObj( 'uvTab_pane_' + nTab ) );
						}
					else {
						setText( nTarg, n );
						var pane = getObj( 'uvTab_pane_' + nTab );
						setText( pane, 'Your backpack has no items.' );
						}
					}
				else {
					setText( nTarg, n );
					}
				}

			var rTarg = getObj(
				'rtarg_' + in_id + '_' + in_tabs[ in_id ][ k ]
				);
			removeNode( getObj( 'rtarg_' + in_id + '_' + nTab ) );
			}
		}
	}

function inventory_add( id ) {
	
	var ok;
	var img = getObj( 'uvInventory_Add_' + id );
	if( img ) {
		if( img._state ) {
			// Nuh-uh.
			}
		else {
			inventory_disable( id );
			atpMultiday.addItem( id );
			}
		}
	
	return false;
	}


function inventory_disable( id ) {
	var img = getObj( 'uvInventory_Add_' + id );
	if( img && !img._state ) {
		img._state	= 1;
		img.src		= auri( 'resource/image/backpack/tool_add_i.jpg' );
		
		}
	}

function inventory_enable( id ) {
	var img = getObj( 'uvInventory_Add_' + id );
	if( img && img._state ) {
		img._state	= 0;
		img.src		= auri( 'resource/image/backpack/tool_add.jpg' );
		}
	}


function connect_inventory_context( ) {
	for( var k in atpInventory ) {
		var id	= atpInventory[ k ];
		var m	= getObj( 'uvInventory_Menu_' + id );
		if( m ) {
			var removeAction = new Object( );
			removeAction.id	= id;
			removeAction.act	= function( ) {
				XMLReq.pullURI(
					  auri(
					 'engine/backpack/item.xml.php?'
					+'do=rem&which=' + this.id + '&wait'
					)

					,
					
					function( xml ) {
						window.location.reload( true );
						}
					);
				}
			
			new ContextMenuControl( 'uvInventory_Menu_' + id, 235 )
//				.addItem( 'Add Directions To&hellip;', function(){} )
//				.addItem( 'Add Directions From&hellip;', function(){} )
				.addItem( 'Remove From Backpack', removeAction )
				;
			}
		}
	}


function browserSupportsCalendar( ) {
	
	// Feature detection, not browser detection.
	
	if(
			document.getElementById 	// Require DOM
		&&	XMLReq.test()				// Require XMLHTTP

	) { return true; }
	
	return false;
	}

document.onload = chainHandlers(
	 function( ) {
	    // CALENDAR
		if( browserSupportsCalendar( ) ) {
			var s;

			if( s = getObj( 'uvCalendar_BrowserShow' ) )
				{ s.style.display = 'inline'; }

			if( s = getObj( 'uvCalendar_Core' ) )
				{ s.style.display = 'block'; }
				
			if( s = getObj( 'uvCalendar_Alt' ) )
				{ s.style.display = 'none'; }

			}
		}
	,document.onload
	);
