ihf.HistoryManagerClass = Class.create({
   NAMESPACE: 'HistoryManager',
  
   myModuleStateChangeHandler: function( searchControl, historyObject )
   {
      //
      // If we have a modal open - then close it
      if ( $('ihf_modal_frame') != null )
      {
         //
         // Check to see if the dialog has a close box
         if ( $('ihf_modal_frame').down('a.closeModals') != null )
         {
            var modalNav = new ihf.ModalNavigationControl($('ihf_modal_frame').down('a.closeModals'));
            modalNav.closeModals();
         }
      }
   
      var ihfState = $('ihf');
      ihfState.calledFromHistory = true;
   
      var currentObj = this.currentFrame(searchControl.frame);
      
      if ( currentObj != null || searchControl.url )
      {
         switch( searchControl.frame )
         {
            case "ihf_search_frame":
            	if ( $('ihf_search_frame') != null )
            	{
            		var nextObj = new ihf.SearchFrame('ihf_search_frame');
            		if ( nextObj != null )
            		{
         		      currentObj.deselect();
            		   nextObj.select();
                     ihfState.calledFromHistory = false;
            		}
         		}
         		break;
         		
            case "ihf_map_frame":
               if ( $('ihf_map_frame') != null )
               {
                  var nextObj = new ihf.MapFrame('ihf_map_frame');
                  if ( nextObj != null )
                  {
                     currentObj.deselect();
                     nextObj.select();
                     ihfState.calledFromHistory = false;
                  }
               }
               break;

            case "ihf_results_frame":
            	if ( $('ihf_results_frame') != null )
            	{
            		var nextObj = new ihf.ListingsFrame('ihf_results_frame');
            		if ( nextObj != null )
            		{
            		   if ( searchControl.url != null )
            		   {
            		      nextObj.request( searchControl.url, ((searchControl.options != null) ? searchControl.options : 'searchMapForm') );
            		   }
            		   else
            		   {
                        currentObj.deselect();
               		   nextObj.select();
                        ihfState.calledFromHistory = false;
            		   }
            		}
         		}
         		break;
         		
            case "ihf_details_frame":
            	if ( $('ihf_details_frame') != null )
            	{
            		var nextObj = new ihf.ListingDetailFrame('ihf_details_frame');
            		if ( nextObj != null )
            		{
            		   if ( searchControl.url != null )
            		   {
            		      nextObj.request( searchControl.url );
            		   }
            		   else
            		   {
                        currentObj.deselect();
               		   nextObj.select();
                        ihfState.calledFromHistory = false;
            		   }
            		}
         		}
               break;
               
            default:
               alert( "Unknown state change: [" + searchControl.frame + "]" );
               break;
         }
      }
      else
      {
         ihfState.calledFromHistory = false;
      }
   },
   
   sameFrame: function(newFrame, possibleOtherFrame)
   {
      if ( dsPageStyle <= 1 )
      {
         return true;
      }
      
      switch( newFrame )
      {
         case 'ihf_search_frame':
            if ( possibleOtherFrame == 'ihf_results_frame' )
            {
               return true;
            }
            break;
            
         case 'ihf_results_frame':
            if ( possibleOtherFrame == 'ihf_search_frame' )
            {
               return true;
            }
            break;
            
         case 'ihf_map_frame':
            if ( possibleOtherFrame == 'ihf_details_frame' )
            {
               return true;
            }
            break;
            
         case 'ihf_detail_frame':
            if ( possibleOtherFrame == 'ihf_map_frame' )
            {
               return true;
            }
            break;
      }
      
      return false;
   },

   currentFrame: function(newFrame)
   {
      if ( $('ihf_search_frame') != null && this.sameFrame( newFrame, 'ihf_search_frame' ) )
      {
         var searchObj = new ihf.SearchFrame( 'ihf_search_frame' );
         if ( searchObj != null && searchObj.isCurrent() )
         {
            return searchObj; 
         }
      }
      
      if ( $('ihf_results_frame') != null && this.sameFrame( newFrame, 'ihf_results_frame' ) )
      {
         var listingsObj = new ihf.ListingsFrame( 'ihf_results_frame' );
         if ( listingsObj != null && listingsObj.isCurrent() )
         {
            return listingsObj; 
         }
      }
      
      if ( $('ihf_details_frame') != null && this.sameFrame( newFrame, 'ihf_details_frame' ) )
      {
         var detailObj = new ihf.ListingDetailFrame( 'ihf_details_frame' );
         if ( detailObj != null && detailObj.isCurrent() )
         {
            return detailObj; 
         }
      }
      
      if ( $('ihf_map_frame') != null && this.sameFrame( newFrame, 'ihf_map_frame' ) )
      {
         var mapObj = new ihf.MapFrame( 'ihf_map_frame' );
         if ( mapObj != null && mapObj.isCurrent() )
         {
            return mapObj; 
         }
      }

      return null;
   }
});

var HistoryManager =
{
   searchFrameHistoryManager: new ihf.HistoryManagerClass(),
   resultsFrameHistoryManager: new ihf.HistoryManagerClass(),
   detailsFrameHistoryManager: new ihf.HistoryManagerClass(),
   tabHistoryManager: new ihf.HistoryManagerClass()
}; 

dsHistory.addFunction(HistoryManager.searchFrameHistoryManager.myModuleStateChangeHandler, HistoryManager.searchFrameHistoryManager, {frame: 'ihf_search_frame'});

