var num = 0; function JSVideoPlayer(playListIn, descriptionContainerIDIn, titleContainerIDIn, navCallBackFunctionIn,playlistContainerIDIn,usePlayListIn,overLayCallBackFunctionIn){ this.internalPlaylist = playListIn; this.descriptionContainerID = descriptionContainerIDIn; this.titleContainerID = titleContainerIDIn; this.navCallBackFunction= navCallBackFunctionIn; this.currentPosition = 0; this.lastPlayerStatus = -10; this.playerStatus = -10; this.playStack = new Array(); this.ytplayerID = 'ytplayer' + Math.ceil(Math.random() * 1000); this.ytDevKey = 'AI39si5ZYda4KHUD8hIxgJZVKx-t5SP0QHnN_GysjQno86PT67hpyRIHFUMflLHz_AnwkB2PUspNy7_lZyBr44Rf9a4Q7rmHrA'; this.allowAutoSkip = true; this.playlistContainerID = playlistContainerIDIn; this.currentLoadedHash = new Array(); this.stateStack = new Array(); this.adVids = new Array(); this.adMode = false; this.ignoreNextState = false; this.stateStack[this.stateStack.length] = this.playerStatus; this.overLayCallBackFunction = overLayCallBackFunctionIn; this.showedAd = false; if(typeof usePlayListIn == 'undefined' || usePlayListIn) this.usePlayList = true; else this.usePlayList = false; //lets find a uniqueid that isn't in use while(document.getElementById(this.ytplayerID)){ this.ytplayerID = 'ytplayer' + Math.ceil(Math.random() * 1000); } this.setAdMode = function(adModeIn, vid){ this.adMode = adModeIn; try{ //this.overLayCallBackFunction(adModeIn, this.internalPlaylist.videos[this.currentPosition +1].ID); this.overLayCallBackFunction(adModeIn, vid); }catch(e){ //alert(e.message); } } this.onBufferFull = function(clip){// - Called when a video has completely buffered. this.notifyOfStateChange(1); } this.onResume = function(clip){//(clip) - Called when the video resumes playback after being stopped or paused. this.notifyOfStateChange(4); } this.onPlay = function(clip){//(clip) - Called when the play button is clicked. var newid = ''; try{ newid = clip['info']['id']; }catch(e){ newid = ''; } this.notifyOfStateChange(3, newid); } this.onPause = function(clip){//(clip) - Called when the pause button is clicked. this.notifyOfStateChange(2); } this.onStop = function(clip){//(clip) - Called when the stop button is clicked. this.notifyOfStateChange(7); } this.onClipDone = function(clip){//(clip) - Called when a video completes playback. //logText('onclipdone called'); this.notifyOfStateChange(0); } //replaces the internal play list with what is passed this.loadPlayList = function (playListIn){ if(this.adMode) return; this.internalPlaylist = playListIn; //queUpVideo(0); this.startPlaying(0, true); this.updatePlaylistDisplay(); } this.removeVideoByVID = function(videoIDIn){ logText('removing: ' + videoIDIn + ' from playlist of length ' + this.internalPlaylist.videos.length); var counter; for (counter = 0; counter < this.internalPlaylist.videos.length; counter++){ if (videoIDIn.toUpperCase() == this.internalPlaylist.videos[counter].ID.toUpperCase()){ logText('removeVideoByVID: ' + videoIDIn.toUpperCase() + ' == ' + this.internalPlaylist.videos[counter].ID.toUpperCase()); break; }else{ logText('removeVideoByVID: ' + videoIDIn.toUpperCase() + ' != ' + this.internalPlaylist.videos[counter].ID.toUpperCase()); } } if (counter < this.internalPlaylist.videos.length){ for(var i = counter; counter < this.internalPlaylist.videos.length -1; counter++){ this.internalPlaylist.videos[counter] = this.internalPlaylist.videos[counter +1]; logText('removeVideoByVID: copying to ' + counter + ' from ' + (counter + 1)); } if (this.currentPosition >= counter) this.currentPosition--; this.internalPlaylist.videos.pop(); this.updatePlaylistDisplay(); return true; } return false; } //returns the underlying youtube object this.getPlayerObj = function(){ //TODO change to internal reference return document.getElementById(this.ytplayerID); } //returns the current video this.getCurrentVideo = function(){ return this.internalPlaylist.videos[this.currentPosition]; } //appends the passed play list to the current list this.queToList = function(playListIn){ for(var i =0 ; i < playListIn.videos.length; i++) this.internalPlayList.addVideo(playListIn[i]); } this.playVideo = function(playListIn){ if(this.adMode) return; this.loadPlayList(playListIn); this.startPlaying(0); } this.present = function(targetDiv, autoplay, widthIn, heightIn){ try{ var useCurrent = -1; if(autoplay){ for(var i = 0; i < this.currentLoadedHash.length; i++){ if(this.internalPlaylist.videos[this.currentPosition].ID == this.currentLoadedHash[i]){ useCurrent = i; //logText("present: video found in current list :" + useCurrent); } } } if (useCurrent > -1 && autoplay){ this.playStack[this.playStack.length] = this.currentPosition; var isAd = false; for(var k = 0; k < this.adVids.length && !isAd; k++) if( this.adVids[k] == this.currentLoadedHash[useCurrent]) isAd = true; this.setAdMode(isAd, this.currentLoadedHash[useCurrent]); logText("present: ToClip: '" + useCurrent + "': " + this.currentLoadedHash[useCurrent]); this.ignoreNextState = true; try{ setTimeout('player.getPlayerObj().ToClip(' + useCurrent + ')' , 10); if(document.all){ setTimeout('player.getPlayerObj().DoPlay()', 100); setTimeout('player.getPlayerObj().DoPlay()', 120); }else{ setTimeout('player.getPlayerObj().DoPlay()', 500); setTimeout('player.getPlayerObj().DoPlay()', 1000); // setTimeout('player.getPlayerObj().DoPlay()', 1500); } }catch(e){alert(e.message);} this.logList(); //if(this.playerStatus == 2 || this.playerStatus == 4 || this.playerStatus == -1){ // logText('present: plstatus: ' + this.playerStatus + ', setting doplay, 100'); // setTimeout('player.getPlayerObj().DoPlay()', 100); //} }else{ try{ if(typeof autoplay == 'undefined' || !autoplay) autoplay = false; if (typeof widthIn == 'undefined' || !widthIn || isNaN(widthIn)) var width = document.getElementById(targetDiv).offsetWidth; else var width = widthIn; if (typeof heightIn == 'undefined' || !heightIn || isNaN(heightIn)) var height = document.getElementById(targetDiv).offsetHeight; else var height = heightIn; if(!width || width == 0){ width = document.getElementById(targetDiv).style.width.substring(0,document.getElementById(targetDiv).style.width.length -2); } if(!height || height == 0){ height = document.getElementById(targetDiv).style.height.substring(0,document.getElementById(targetDiv).style.height.length -2); } var embedCode = "http://service.twistage.com/plugins/player.swf?p=pbrtv&autoplay=" + autoplay; var ytFlash = new SWFObject(embedCode, this.ytplayerID, width, height, "9.0"); ytFlash.addParam('allowscriptaccess', "always"); ytFlash.addParam('wmode', "transparent"); ytFlash.addParam('base', 'http://service.twistage.com'); ytFlash.addParam('autoplay', autoplay); ytFlash.addParam('allowFullScreen', 'true'); var listHash = "["; this.currentLoadedHash = new Array(); if(! this.showedAd){ this.showedAd = true; //this is where we can use any logic to choose to diplay an ad or not } listHash += "{video_id:'" + this.internalPlaylist.videos[this.currentPosition].ID + "'},"; this.currentLoadedHash[this.currentLoadedHash.length] = this.internalPlaylist.videos[this.currentPosition].ID; for( var i = 0 ; i < this.internalPlaylist.videos.length; i++){ if(this.internalPlaylist.videos[this.currentPosition].ID != this.internalPlaylist.videos[i].ID){ listHash += "{video_id:'" + this.internalPlaylist.videos[i].ID + "'},"; this.currentLoadedHash[this.currentLoadedHash.length] = this.internalPlaylist.videos[i].ID; } } listHash = listHash.substring(0, listHash.length -1);//trim the final comma listHash += "]"; logText("present: " + listHash); ytFlash.addParam('flashvars', 'v=0&l=' + listHash + '&config={config:{autoplay:true}}'); // ytFlash.addParam('flashvars', 'v=' + this.internalPlaylist.videos[this.currentPosition].ID + '&config={config:{autoplay:'+autoplay+'}}'); ytFlash.addParam('base', 'http://service.twistage.com'); document.getElementById(targetDiv).innerHTML = ""; ytFlash.write(targetDiv); //We need to record that we have played a video (or attempted to). this.playStack[this.playStack.length] = this.currentPosition; }catch(e){alert(e.message);} } if(this.internalPlaylist.videos[this.playStack[this.playStack.length - 2]]) this.navCallBackFunction(this.playerStatus, this.lastPlayerStatus, this.internalPlaylist.videos[this.currentPosition].ID, this.internalPlaylist.videos[this.playStack[this.playStack.length - 2]].ID); else this.navCallBackFunction(this.playerStatus, this.lastPlayerStatus, this.internalPlaylist.videos[this.currentPosition].ID, 'asdasdfadf'); this.updateTitleInfo(); }catch(e){ logText("present : " + e.message); } } this.onError = function(error){ logText('general error: '); } this.startPlaying = function (position, autoplay){ if(typeof autoplay == 'undefined' || ! autoplay) autoplay = false; try{ if(!position) position = 0; if(isNaN(position)) position = 0; }catch(e){ position = 0; } logText("startPlaying: " + position + " with autoplay: " + autoplay); if(this.playerStatus == 2){ document.getElementById(this.ytplayerID).DoPlay(); logText('startPlaying unpausing:' + this.currentPosition); }else{ logText('startPlaying new position:' + position); this.currentPosition = position; this.present("videoPlayer", autoplay); } } this.notifyOfStateChange = function (newState, newidIn){ if(newState == 2) this.setAdMode(true); if(typeof newidIn == 'undefined' || !newidIn){ var newid = ''; }else{ var newid = newidIn; } if(this.ignoreNextState){ this.ignoreNextState = false; logText('notifyOfStateChange : (IGNORED) ' + newState + ": " + newidIn); return; }else logText('notifyOfStateChange : ' + newState + ": " + newidIn); //this handles user nav from the player itself if( newState == 3 && newid != '' && newid != this.getCurrentVideo().ID && !this.adMode){ logText('notifyOfStateChange: curpos: ' + this.currentPosition + ':' + newid + ' != ' + this.getCurrentVideo().ID ); //when we get here its becaue the user interacted with the player controls to swap videos //first lets try to find the video in the current play list var pos = -1; for(var l = 0; l < this.internalPlaylist.videos.length; l++){ if (newid == this.internalPlaylist.videos[l].ID) pos = l; } if(pos != -1 && !this.adMode){ logText('notifyOfStateChange: navigating to playlist index '+pos+' to keep up with user nav'); this.startPlaying(pos, true); }else if (!this.adMode){ //if we get here the user interacted with the player controls and selected a video not in our play list // this could be an ad or a video we don't want to see for(var k = 0; k < this.adVids.length; k++){ if( this.adVids[k] == newid){ logText('notifyOfStateChange: ad playing -- setting ad mode '); this.setAdMode(true, newid); //no more action required return; }else{ logText('notifyOfStateChange: navigated to video not in play list'); } } }else{ // we are just recovering from displaying an ad //not going to adjust anything } } this.stateStack[this.stateStack.length] = newState; this.lastPlayerStatus = this.playerStatus; this.playerStatus = newState; if(this.playerStatus == 0){ this.stateStack[this.stateStack.length] = -2; this.lastPlayerStatus = this.playerStatus; this.playerStatus = -2; logText('notifyOfStateChange : ' + '-2'); try{ this.getPlayerObj().DoStop(); }catch(e){ logText('notifyOfStateChange: ' + e.message); } if(this.currentPosition < this.internalPlaylist.videos.length){ logText('notifyOfStateChange: end of video, playing next'); var useSpot = this.currentPosition; if(this.adMode){ this.setAdMode(false); logText('notifyOfStateChange: rolling into current video from ad. starting position ' + ( useSpot)); }else{ logText('notifyOfStateChange: roll over to next video, starting position ' + (++useSpot)); } this.stateStack[this.stateStack.length] = -1; this.lastPlayerStatus = this.playerStatus; this.playerStatus = -1; logText('notifyOfStateChange : ' + '-1'); this.startPlaying(useSpot, true); }else{ logText('notifyOfStateChange: end of playlist'); } } } this.navigateVideoBy = function (changeAmt) { if(this.adMode) return; this.currentPosition += changeAmt; if(this.currentPosition < 0) this.currentPosition = 0; else if(this.currentPosition >= this.internalPlaylist.videos.length) this.currentPosition = this.internalPlaylist.videos.length - 1; this.allowAutoSkip = false; this.startPlaying(this.currentPosition); } this.updateTitleInfo = function () { var dContainer = document.getElementById(this.descriptionContainerID); var tContainer = document.getElementById(this.titleContainerID); if(tContainer) tContainer.innerHTML = this.internalPlaylist.videos[this.currentPosition].Title; if(dContainer) dContainer.innerHTML = this.internalPlaylist.videos[this.currentPosition].Description; } this.updatePlaylistDisplay = function() { if(!this.usePlayList) return; //alert(this.internalPlaylist.videos.length); var idList = ""; var resultHTML = ''; try{ for(var i = 0; i < this.internalPlaylist.videos.length; i++){ resultHTML += ''; idList += this.internalPlaylist.videos[i].ID + ","; } }catch(e){ logText("updatePlaylistDisplay : " + e.message); } resultHTML += '
'+this.internalPlaylist.videos[i].Title+'
'; //here we either request the list from the server or statically populate via client side HTML generation above //document.getElementById(this.playlistContainerID).innerHTML = resultHTML; var playListSender = new myXMLObj("ignore", "/video/videoSearch.cfm", "playList=true&vidList=" + idList + "&playListID=" + this.playlistContainerID + "¤tVid=" + this.internalPlaylist.videos[this.currentPosition].ID); playListSender.callback = this.managePlayListUpdate; playListSender.send(); } this.managePlayListUpdate = function(respJSON){ try{ var responseObj = eval( "(" + respJSON + ")" ); }catch(e){ logText("JSVideoPlayer: managePlayListUpdate: 1: Unable to parse server return data. Your upload may have started, but may not have."); } try{ document.getElementById(responseObj.PLID).innerHTML = responseObj.SEARCHTEXT; }catch(e){ logText("JSVideoPlayer:managePlayListUpdate 2: " + e.message); } try{ loadMyFuncModule(responseObj.RESPONSEJS); }catch(e){ logText("JSVideoPlayer:managePlayListUpdate 3: "+ e.message); } toggleLoad(false); } this.addVideo = function(videoIn){ for(var m = 0; m < this.internalPlaylist.videos.length; m++){ if(videoIn.ID == this.internalPlaylist.videos[m].ID){ return m; } } var videoIndex = this.internalPlaylist.videos.length; this.internalPlaylist.addVideo(videoIn); this.updatePlaylistDisplay(); return videoIndex; } this.logList = function(){ for (var l =0; l < this.internalPlaylist.videos.length; l++){ logText("playlistDump" + l + ": " + this.internalPlaylist.videos[l].ID); } } this.updatePlaylistDisplay(); return this; } function PlayList(){ this.videos = new Array(); this.addVideo = function(videoIn) { var videoIndex = this.videos.length; this.videos[this.videos.length] = videoIn; return videoIndex; } return this; } function Video(IDIn, TitleIn, DescriptionIn, EmbedURLIn, WatchURLIn) { this.ID = IDIn; this.Title = TitleIn; this.Description = DescriptionIn; this.EmbedUrl = EmbedURLIn; this.WatchURL = WatchURLIn; return this; }