var Presentation = Class.create();
Presentation.prototype = {
  modules: [], // will hold our module, buckets, playlists, and topics
  currentModule: null,
	
	initialize: function( config ){
		this.modules = config.modules;
	},
	
  start: function( ){
		// alert( this.modules );
		// alert( this.modules.length );
    if( this.modules && (this.modules.length > 0) ){
      // define default module, bucket, and playlist to play when we start
      var moduleId = this.modules[0].id;
      var bucketId = this.modules[0].buckets[0].id
      var playlistId = 'default';
			var customPlaylist = null;  
      var module = null;
      
      // see if we have a module and/or playlist on the querystring
      qs = document.location.search.substring( 1, document.location.search.length );
      if( qs.length > 0 ){
        o_qs = qs.parseQuery();
        if( o_qs.module ){
          moduleId = o_qs.module;
        }
        if( o_qs.bucket ){
          bucketId = o_qs.bucket;
        }
        if( o_qs.playlist ){
          playlistId = o_qs.playlist;
					customPlaylist = CustomPlaylist.find( playlistId );
        }
      }
    
      // start the presentation
      this.currentModule = this.getModule( moduleId );
      if( this.currentModule ){
				if( customPlaylist ){
					this.currentModule.addBucket(
					  new Bucket( 'custom_playlist', customPlaylist.name, 'Custom Presentation', [customPlaylist.playlist] )
					);
					bucketId = 'custom_playlist';
					// TODO: pull playlistId from everything...
					// TODO: get rid of any extra Object.extend() calls
					playlistId = 'default';
					this.currentModule.deleteBucket();					
				}
        this.currentModule.show( bucketId, playlistId );
      }
      else {
        alert( 'invalid module' );
      }
    }
    else {
      alert( 'Modules have not been defined' );
    }
          
  }, // end start()   

  getModule: function( id ){
    return this.modules.find( function( module ){
      return( module.id == id );
    });  
  },

  goNext: function( ){
    this.currentModule.goNext();
  },
  
  goPrevious: function( ){
    this.currentModule.goPrevious();
  },

  primaryNavDispatcher: function( e ){
    this.currentModule.handlePrimaryNavClick( e );
  },

  secondaryNavDispatcher: function( e ){
    this.currentModule.currentBucket.currentPlaylist.handleSecondaryNavClick( e );
  },

  controlNavDispatcher: function( e ){
    this.currentModule.handleControlNavClick( e );
  },

  loadStageWith: function( url ){
    var goTo = top.document.location.href;
    var idx = goTo.indexOf( '/presentation.' );
    goTo = goTo.substring( 0, idx + 1 );
    goTo = goTo + url;
    $('stage').contentWindow.document.location.href = goTo;
    /*
    // fun with object vs. iframe...
    // see: http://www.createblog.com/tutorials/tutorial.php?id=118
    var stage = $('stage');
    var stageClone = stage.cloneNode( true );
    stageClone.data = url;
    var container = $('c1').getElementsByClassName( 'content' )[0];
    container.removeChild( stage );
    container.appendChild( stageClone );
    */
  },

  preload: function( slide ){
    //  TODO: I'm thinking we could provide a hidden iframe/object
    //  and preload the next slide in it. We'll have to not allow
    //  that slide to play. We'll also need to see whether that
    //  loading causes trouble for the slide being played.
  },

  clearPlaylistNav: function( ){
    var ul = $('playlist');
    var children = ul.immediateDescendants();
    for( var i=0; i<children.length; i++ ){
      // the problem with using an effect here is that the new
      // elements get drawn before we finish fading the old. If we
      // want an effect, we probably need to break the append
      // step into another function and call it w/ setTimeout.
      /*
      id = children[i].id;
      if( id ){
        new Effect.Fade( id, {delay: (i*.25), duration: .5} );
        setTimeout( 'Element.remove(' + children[i].id + ' )', ((i*.25*1000) + (.5*1000)) );
      }
      else {
        Element.remove( children[i] );
      }
      */
      Element.remove( children[i] );
    }
  }
};