function ScreenManager(){
     this.count = 0;
     this.topScreen = "";
     this.bottomScreen = "";
     this.transitionType = "none";
     this.transitionDir = "";
     this.transitionScreen = "";
     this.funct = new Function();
     this.transpBg = 0;
     this.addScreen = addScreen;
     this.hideScreen = hideScreen;
     this.showScreenBelow = showScreenBelow;
     this.showScreenAbove = showScreenAbove;
     this.showScreen = showScreen;
     this.runTransition = runTransition;     
}     

function addScreen( nameList ){
     for ( var a = 0; a < nameList.length; a++ ){
         this.hideScreen( nameList[a] );
     }
}     

function hideScreen( name ){
     var e = document.getElementById( name + "Screen" );
     e.style.opacity = "1";
     e.style.display = "none";
     e.style.zIndex = "";
     e.style.left = 0;
     e.style.top = 0;
}

function showScreenBelow( name ){
     var e = document.getElementById( name + "Screen" );
     e.style.display = "block";
     e.style.opacity = "1";
     e.style.zIndex = "1";
     e.style.left = 0;
     e.style.top = 0;
}

function showScreenAbove( name, opac ){
     var e = document.getElementById( name + "Screen" );
     e.style.display = "block";
     e.style.opacity = opac;
     e.style.zIndex = "2";
     e.style.left = 0;
     e.style.top = 0;
}

function runTransition(){
     this.count++;
     if ( this.count == 16 ){
            this.count = 0;
            if ( this.transitionDir == "out" ){
                  this.hideScreen( this.transitionScreen );
                  this.showScreenAbove( this.topScreen );
            }else{
                  if ( this.bottomScreen && this.transpBg == 0 ) this.hideScreen( this.bottomScreen );
                  this.showScreenAbove( this.topScreen );
            } 
            removeBlocker();
            this.funct();
			this.funct = blank;//new Function();
            return;             
     } 
     var c = this.count;  
     if ( this.transitionDir == "out" ){
           c = 16 - c;
     }
     var e = document.getElementById( this.transitionScreen + "Screen" );
     switch (this.transitionType ){
          case "fade":
               if ( c <= 8 ){
                   e.getElementsByTagName( "div" )[0].style.opacity = 0;
                   e.style.opacity = (c / 8 );
                }else{
                   e.getElementsByTagName( "div" )[0].style.opacity = ( (c - 8) / 8 );;
                   e.style.opacity = 1;
                }
                break;
          case "horiz":
                e.style.opacity = 1;
                e.style.left = Math.floor( (15 - c )  / 15 * 320 ) + "px";
                break;
          case "vert":
                e.style.opacity = 1;
                e.style.top = Math.floor(  -(15-c)  / 15 * 356 ) + "px";
                break;
     }
     setTimeout( "screenManager.runTransition();", 100 );     
}

function showScreen( name, transition, inOut, funct, transp ){
    if ( transp ) this.transpBg = transp;
    else this.transpBg = 0;
    if ( funct ) this.funct = funct;
    else this.funct = blank;//new Function();
    switch ( transition ){
         case "instant":
              if ( this.topScreen )this.hideScreen( this.topScreen );
              this.topScreen = name;
              this.showScreenAbove( name, 1 );
              this.funct();
			  this.funct = blank;//new Function();
              break;
         default:
             createBlocker();
             this.transitionType = transition;
             if ( transition == "fade" ) inOut = "in";
             this.transitionDir = inOut;
             if ( inOut == "out" ){
                     this.showScreenAbove( this.topScreen, 1 );
                     this.transitionScreen = this.topScreen;
                     this.bottomScreen = "";
                     this.showScreenBelow( name );                    
              }else{
                     this.showScreenBelow( this.topScreen ); 
                     this.bottomScreen = this.topScreen;
                     this.transitionScreen = name;
                     this.showScreenAbove( name, 0 );
               }
              this.topScreen = name; 
              setTimeout( "screenManager.runTransition();", 100 );
              break;
      }    
}

function createBlocker(){
	document.getElementById( "blocker" ).style.display = "block";
}
function removeBlocker(){
	document.getElementById( "blocker" ).style.display = "none";
}
