var bt_wimpstateremap = new Array();
var bt_objinstances = 0;

// 0 The playback state is undefined. 
// 1 Playback is stopped. 
// 2 Playback is paused. 
// 3 The player is playing a stream. 
// 4 The player is scanning a stream forward. 
// 5 The player is scanning a stream in reverse. 
// 6 The player is buffering media. 
// 7 The player is waiting for streaming data. 
// 8 The player has reached the end of the media. 
// 9 The player is preparing new media. 
// 10 The player is ready to begin playback. 


bt_wimpstateremap[0]  = 1;  //Stopped
bt_wimpstateremap[1]  = 2;  //Paused
bt_wimpstateremap[2]  = 3;  //Playing
bt_wimpstateremap[3]  = 7;  //Waiting
bt_wimpstateremap[4]  = 4;  //Fast Forward
bt_wimpstateremap[5]  = 5;  //Rewinding
bt_wimpstateremap[6]  = 0;
bt_wimpstateremap[7]  = 0;
bt_wimpstateremap[8]  = 0;
bt_wimpstateremap[9]  = 0;
bt_wimpstateremap[10] = 0;

if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  dim aXControl,versionNo');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    set aXControl = CreateObject(activeXControlName)');
    document.writeln('     If IsObject(aXControl) Then');
    document.writeln('       versionNo = split(axControl.versionInfo,".")');
    document.writeln('       detectActiveXControl = versionNo(0)');
    document.writeln('     End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}

function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
        var pluginsArrayLength = navigator.plugins.length;
        // for each plugin...
        for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
            // loop through all desired names and check each against the current plugin name
            var numFound = 0;
            for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
                // if desired plugin name is found in either plugin name or description
                if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
                    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
                    // this name was found
                    numFound++;
                }
            }
            // now that we have checked all the required names against this one plugin,
            // if the number we found matches the total number provided then we were successful
            if(numFound == daPlugins.length) {
                pluginFound = true;
                // if we've found the plugin, we can stop looking through at the rest of the plugins
                break;
            }
        }
    }
    return pluginFound;
} // detectPlugin


function detectWindowsMedia() {
    pluginFound = detectPlugin('Windows Media');
    //alert(pluginFound);
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
        pluginFound = detectActiveXControl('WMPlayer.OCX');
    }
    return pluginFound;
}

var bt_playerversion = detectWindowsMedia();
//redirectCheck((detectWindowsMedia()>=9),thisurl+"&wm9",true);

function btwimpplayer(width,height){
  this.mediaplayerObj = null;
  this.mediaplayerId = (++bt_objinstances);
  this.width = width;
  this.height = height;
  this.url = null;
  this.showcontrols = false;
  this.live = false;

  this.play = function() {
    if (this.mediaplayerObj) {
      if (bt_playerversion>8) {
        if (this.mediaplayerObj.URL=="") {
          return (this.url!="")?(this.mediaplayerObj.URL = this.url):false;
        } 
        return this.mediaplayerObj.controls.Play();
      } else {
        this.mediaplayerObj.play();
        return true;
      }
    }
    return false;
  }

  this.setstreamurl = function(streamurl) {
    this.url = streamurl;
  }
 
  this.getheight = function() {
    return (this.height+((this.showcontrols)?64:0));
  }

  this.getwidth = function() {
    return this.width;
  }

  this.stop = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        this.mediaplayerObj.controls.stop();
        //this.setCurrentPosition(0);
        return true;
      } else {
        this.mediaplayerObj.Stop();
        this.mediaplayerObj.CurrentPosition=0; 
        return true;
      }
    }
    return false;
  }

  this.pause = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        return this.mediaplayerObj.controls.Pause();
      } else {
        return this.mediaplayerObj.Pause();
      }
    }
  }
  
  this.fastforward = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        if (this.mediaplayerObj.controls.isAvailable('FastForward')) {
          return this.mediaplayerObj.controls.fastForward();
        }  else {
          return this.skip(10);
        }
      } else {
        return (this.mediaplayerObj.CanScan)?this.mediaplayerObj.FastForward():false;
      }
    }
  }
  
  this.fastreverse = function() { 
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) { 		  
        if (this.mediaplayerObj.controls.isAvailable('FastReverse')) {
          return this.mediaplayerObj.controls.fastReverse();
        } else {
          return this.skip(-10);
        }
      } else {
        return (this.mediaplayerObj.CanScan)?this.mediaplayerObj.FastReverse():false;
      }
    }
  }  

  this.setCurrentPosition = function(seconds) {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        this.mediaplayerObj.controls.currentPosition = seconds; 
      } else {
        this.mediaplayerObj.CurrentPosition = seconds;
      }
    }
  }

  this.getCurrentPosition = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        return this.mediaplayerObj.controls.currentPosition;
      } else {
        return this.mediaplayerObj.CurrentPosition;
      }
    }
    return 0;
  }
  
  

  this.getDuration = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.currentMedia) {
        return this.mediaplayerObj.currentMedia.duration;
      } else {
        return this.mediaplayerObj.Duration;
      }
    }
    return false;
  }

  this.getPlayState = function() {
    if (this.mediaplayerObj) { 
      if (bt_playerversion>8) {
        return this.mediaplayerObj.playState;
      } else {
        return bt_wimpstateremap[this.mediaplayerObj.PlayState];
      }
    }
  }

  this.setCurrentMarker = function(marker) {
    if (this.mediaplayerObj) { 
      if (marker>0 && marker<=this.getMarkerCount()) {
        if (this.mediaplayerObj.controls) {
          this.mediaplayerObj.controls.currentMarker = marker;
        } else {
          return this.mediaplayerObj.currentMarker = marker;
        }
        return true;
      }
    }
    return false;
  }

  this.getCurrentMarker = function(marker) {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        return this.mediaplayerObj.controls.currentMarker;
      } else {
        return this.mediaplayerObj.currentMarker;
      }
    }
    return false;
  }

  this.getMarkerCount = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.currentMedia) {
        return this.mediaplayerObj.currentMedia.markerCount;
      } else {
        return this.mediaplayerObj.MarkerCount;
      }
    }
  }

  this.getVolume = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.Settings) {
		return;
        return((this.mediaplayerObj.Settings.volume));
      } else {
        return(((this.mediaplayerObj.Volume)/1000)+100);
      }
    }
  }

  this.setVolume = function(level) {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.Settings) {
        this.mediaplayerObj.Settings.volume=level;
        return(this.mediaplayerObj.Settings.volume);
      } else {
        volume = level*1000;
        if (volume>0) volume=0;
        if (volume<-10000) volume=-10000;
        this.mediaplayerObj.Volume=volume;
        return(((this.mediaplayerObj.Volume)/1000)+100);
      }
    }
  }

  this.getMute = function() {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.Settings) {
        return this.mediaplayerObj.settings.mute;
      } else {
        return this.mediaplayerObj.Mute;
      }
    }
    return false;
  }

  this.setMute = function(muted) {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.Settings) {		  
        return(this.mediaplayerObj.Settings.mute = muted);
      } else {
        return(this.mediaplayerObj.Mute = muted);
      }
    }
    return false;
  }

  this.skip = function(seconds) {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        this.mediaplayerObj.controls.currentPosition+=seconds; 
      } else {
        this.mediaplayerObj.CurrentPosition = this.mediaplayerObj.CurrentPosition+seconds;
      }
    }
  }

  this.fullscreen = function(full) {
    if (this.mediaplayerObj) {
      if (this.mediaplayerObj.controls) {
        if (this.getPlayState()==3) {
          this.mediaplayerObj.fullScreen = (full)?'true':'false';
        }
      } else {
        this.mediaplayerObj.DisplaySize = (full)?3:4;
      }
    }
  }

  this.setshowcontrols = function(show) {
    alert(show);
    this.showcontrols = show; 
  }

  this.checkcapability = function(name) {
    switch(name) {
      case 'playpause':
        return (this.live)?false:true;
      break;
      case 'mute':
        return true;
      break;
      case 'volume':
        return true;
      break;
      case 'search':
        //return false;
        return (this.live)?false:true;
      break;
      case 'duration':
        return (this.live)?false:true;
      break;
      case 'stop':
        return (this.live)?false:true;
      break;
      default:
        return false;
      break;
    }
  }

  this.initialise = function() {
    this.mediaplayerObj = document.getElementById("btMediaPlayer"+this.mediaplayerId); 
    if (bt_playerversion>8) this.play();
  }

  this.draw = function() {
    visible = (this.width>0 && this.height>0);
    document.writeln('<span style="visibility:'+(visible?"visible":"hidden")+';">');
    document.writeln('<scr'+'ipt language="JavaScript" for="btMediaPlayer'+this.mediaplayerId+'" event="Warning(lType, lParam, sDescription)"></script>');
    document.writeln('<scr'+'ipt language="JavaScript" for="btMediaPlayer'+this.mediaplayerId+'" event="ScriptCommand(sType, sParam)">');
    document.writeln('playerscriptcommand(sType,sParam);');
    document.writeln('</scr'+'ipt>');

    document.writeln('<object id="btMediaPlayer'+this.mediaplayerId+'" width="'+this.getwidth()+'" height="'+this.getheight()+'"');
    if (bt_playerversion>8) {
      document.writeln('classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"');
    } else {
      document.writeln('classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"');
    }
    document.writeln('codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"');
    document.writeln('standby="Loading Microsoft Windows Media Player components..."');
    document.writeln('type="application/x-oleobject">');
    document.writeln('<param name="FileName" value="'+this.url+'">');
    document.writeln('<param name="AutoStart" value="1">');
    document.writeln('<param name="TransparentAtStart" value="true">');
    document.writeln('<param name="ShowControls" value="'+((this.showcontrols)?'1':'0')+'">');
    document.writeln('<param name="ShowDisplay" value="0">');
    document.writeln('<param name="ShowStatusBar" value="0">');
    document.writeln('<param name="AutoSize" value="0">');
    document.writeln('<param name="AnimationAtStart" value="0">');
    document.writeln('<param name="AllowChangeDisplaySize" value="1">');
    document.writeln('<param name="ShowTracker" value="1">');
    document.writeln('<param name="StretchTofit" value="1">')
    document.writeln('<param name="AllowScan" value="1">');
    document.writeln('<param name="EnableContextMenu" value="1">');
    document.writeln('<param name="InvokeURLs" value="0">');
    document.writeln('<param name="uIMode" value="'+(visible?"none":"invisible")+'">');
    document.writeln('<param name="Volume" value="100">');
    document.writeln('<embed type="application/x-mplayer2"');
    document.writeln('pluginspage = "http://www.microsoft.com/Windows/MediaPlayer/"');
    document.writeln('src="'+this.url+'"');
    document.writeln('name="btMediaPlayer'+this.mediaplayerId+'"');
    document.writeln('width="'+this.width+'"');
    document.writeln('height="'+this.height+'"');
    document.writeln('ShowControls="'+((this.showcontrols)?'1':'0')+'"');
    document.writeln('ShowTracker="'+((this.showcontrols)?'1':'0')+'"');
    document.writeln('ShowStatusBar="'+((this.showcontrols)?'1':'0')+'"');
    document.writeln('ShowDisplay="'+((this.showcontrols)?'1':'0')+'"');
    document.writeln('AnimationAtStart="1"');
    document.writeln('TransparentAtStart="0"');
    document.writeln('AutoStart="1"');
    document.writeln('uIMode="'+(visible?1:0)+'"');
    document.writeln('AllowChangeDisplaySize="1">');
    document.writeln('</embed>');
    document.writeln('</object>');
    document.writeln('</span>');
	document.writeln('<script language="JavaScript" for="btMediaPlayer'+this.mediaplayerId+'" event="Warning(lType, lParam, sDescription)"></script>');
    document.writeln('<script language="JavaScript" for="btMediaPlayer'+this.mediaplayerId+'" event="ScriptCommand(sType, sParam)">');
    document.writeln('handlescriptcommand(sType,sParam);');
    document.writeln('</script>');
    this.initialise();
  }
}
