
// fullscreen playback
VIEWS.Play = {
    
    playerIndex: null,
    currentPlaylist: null,
    currentVideo: null,
	videos: null,
	amosPlayTime: null,
    
    init: function(args) {
        AMOSTV.startLoading();
        this.currentVideo = 0;
        this.playerIndex = args.playerIndex;
        this.positions = AMOSTV.players[args.playerIndex].positions;
		var self = this;
		
		//brightcove api calls to retrieve the playlist and videos of the playlist
		Brightcove.getPlaylistsByPlayer(AMOSTV.players[this.playerIndex].id, function(player){
			AMOSTV.players[self.playerIndex].playlists = player.items;
			
			Brightcove.getVideoById(AMOSTV.players[self.playerIndex].playlists[self.positions.curr].videoIds[0], function(playlist){
				AMOSTV.players[self.playerIndex].playlists[self.positions.curr].videos = [playlist];  
				self.loadPlayList();
			});
		});
		
		var amosCookie = document.cookie.split(';');
		var playercookie = 'amosgtv'+this.playerIndex+""+this.positions.curr;
		
		this.amosPlayTime = CookieHandler.getCookieValue(playercookie);
		
		
		
		if(this.amosPlayTime == null || isNaN(this.amosPlayTime) || this.amosPlayTime == "")
		{
			CookieHandler.setPlayerCookie(this.playerIndex,this.positions.curr);
		}
		this.amosPlayTime = CookieHandler.getCookieValue(playercookie);
		
		//Somehow the user got to this page with a invalid cookie
		if(this.amosPlayTime == null){
			//for now we just redirect to menu page
			AMOSTV.loadView('Menu');
			
		} else {
			AMOSTV.showView('Play');
			
		}

		
    },
    
	// function to update the current playlist timelimit
	updateAmosPlayTimeLeft: function(time){
		this.amosPlayTime -= time;
		var cookieName = "amosgtv" + this.playerIndex+""+this.positions.curr;
		
		if(this.amosPlayTime > 0){
			CookieHandler.setCookie(cookieName,'?playTimeLeft--' + this.amosPlayTime);
		} else {			
			//CookieHandler.removeCookie(cookieName);
			//modVP.stop();
			//brightcove.removeExperience("player");
			//AMOSTV.networkError('timelimit'); // show err view with timelimit message
			//AMOSTV.players[this.playerIndex].player = null; // fix to make sure that we recieve data correctly if we want to view the list of playlist from the same category as we jsut viewed
		}
	},
	
	//function to load playlist and create a player loaded with the first video
	loadPlayList: function(){
		_gaq.push(['_trackPageview', '/amosgtv/playlistLoaded/'+this.playerIndex + '' +AMOSTV.players[this.playerIndex].playlists[AMOSTV.players[this.playerIndex].positions.curr].name]);
        this.currentPlaylist = AMOSTV.players[this.playerIndex].playlists[this.positions.curr];
        this.videos = AMOSTV.players[this.playerIndex].playlists[this.positions.curr].videos;
		createFlashPlayer("818965027001", this.videos[this.currentVideo].id,"Play", "player");
	},
	
    exit: function() {
    },
    
    keyHandler: function(key) {
        if ( key == KEYS.PAUSE ) {
            //AMOSTV.Video.pause();
			if(!modVP.isPlaying()){
				modVP.play();
			} else {
				modVP.pause(modVP.isPlaying());
			}
        }
        
        if ( key == KEYS.PLAY ) {
			if(!modVP.isPlaying()){
				modVP.play();
			}
        }
        
        if ( key == KEYS.BACK || key == KEYS.STOP || key == KEYS.GOOGLESTOP || key == KEYS.ESC ) {
			modVP.stop();
			brightcove.removeExperience("player");
            AMOSTV.loadView("List",  { player: this.playerIndex, fromPlay: true});
        }
		
		if( key == KEYS.GOOGLEPLAY ){
			if(!modVP.isPlaying()){
				modVP.play();
			} else {
				modVP.pause(modVP.isPlaying());
			}
		}
    },
    	
	playNextVideo: function(){
		this.currentVideo++;
		if ( typeof AMOSTV.players[this.playerIndex].playlists[this.positions.curr].videos[this.currentVideo] != 'undefined' ) {
			this.playVideo();
        } else { // playlist has reached it's end
			//brightcove.removeExperience("player");
			AMOSTV.networkError('playlist');
			AMOSTV.players[this.playerIndex].player = null;
		}
	},
    
    playVideo: function() { // function to call video load function with a new video
		this.videos = AMOSTV.players[this.playerIndex].playlists[this.positions.curr].videos;
		this.bcLoadVideo();
    },
    
	// function to load a video
	bcLoadVideo: function(){
		modVP.loadVideo(this.videos[this.currentVideo].id);
		this.loadNextVideo();
		AMOSTV.stopLoading();
	},
	
    // preload data for next video in playlist
    loadNextVideo: function() {
        
        // if there exists a next video
        if ( typeof this.currentPlaylist.videoIds[this.currentVideo+1] != 'undefined' ) {
         // this.currentVideo++;
		  //this.bcLoadVideo();
		   
            // if next video has not been loaded
            if ( typeof this.currentPlaylist.videos[this.currentVideo+1] == 'undefined' ) {
                
                // get next video
                var self = this;
                Brightcove.getVideoById(this.currentPlaylist.videoIds[this.currentVideo+1]
                , function(video){
                    
                    // store next video in global data storage
                    AMOSTV.players[self.playerIndex].playlists[self.positions.curr].videos[self.currentVideo+1] = video;
                });
            }
        }
    },
	
};

