//Playlist Order
var playlistOrder = {
	//reset to intro
	'reset': 'khtmSwNP',
	//intro to ctaSong
	'khtmSwNP': 'a523pEKH',
	//ctaSong to cta
	'a523pEKH': 'uQ99eRIe',
	//cta to song1
	'uQ99eRIe': 'mKNCONx2', //uQ99eRIe should not switch on complete
	//songtrigger to song2
	'song2': 'r9FFmKjs',
	
	
	/* SONG PLAYLISTS START */
	//song1 to response
	'mKNCONx2': '8fxgBoFR',
	//song2 to response
	'r9FFmKjs': '8fxgBoFR',
	
	'song-m9t6wem8': 'm9t6wem8', //The Twelve Days of Christmas
	'song-MjbJ7Np0': 'MjbJ7Np0', //Rudolph the Red Nosed Reindeer
	'song-6g06gQcV': '6g06gQcV', //Joy to the World
	'song-RMTN9pp1': 'RMTN9pp1', //Hark! The Herald Angels Sing
	'song-16vTT9GE': '16vTT9GE', //The Christmas Song
	'song-se9YMv7F': 'se9YMv7F', //Christmas Time
	'song-sCDjMWaI': 'sCDjMWaI', //White Christmas
	'song-OfRROQAg': 'OfRROQAg', //Mele Kalikimaka
	'song-7wOJgnIW': '7wOJgnIW', //Winter Wonderland
	'song-0WSBsVpQ': '0WSBsVpQ', //It's the Most Wonderful Time of the Year
	'song-JcAgbkON': 'JcAgbkON', //Jingle Bell Rock
	'song-hlrtt2c1': 'hlrtt2c1', //Baby, It's Cold Outside
	'song-nkGcQuhm': 'nkGcQuhm', //Fairytale of New York
	'song-4PKHosw4': '4PKHosw4', //Little Drummer Boy
	'song-GH8sO2Xp': 'GH8sO2Xp', //Up on the Rooftop
	'song-3M37T9lY': '3M37T9lY', //Santa Claus is Coming to Town
	'song-HpTLtmJr': 'HpTLtmJr', //Silent Night
	'song-SRQf8uX1': 'SRQf8uX1', //Deck the Halls
	'song-qxiivoAq': 'qxiivoAq', //Greensleeves
	'song-aX7kkYfW': 'aX7kkYfW', //Frosty the Snowman
	'song-4J9OAZAq': '4J9OAZAq', //Let It Snow! Let It Snow! Let It Snow!
	'song-jM9fbhbN': 'jM9fbhbN', //Jingle Bells 
	'song-015v8ySw': '015v8ySw', //Feliz Navidad
	'song-LmntJ1qj': 'LmntJ1qj', //Silver Bells
	
	'm9t6wem8': '8fxgBoFR', //The Twelve Days of Christmas
	'MjbJ7Np0': '8fxgBoFR', //Rudolph the Red Nosed Reindeer
	'6g06gQcV': '8fxgBoFR', //Joy to the World
	'RMTN9pp1': '8fxgBoFR', //Hark! The Herald Angels Sing
	'16vTT9GE': '8fxgBoFR', //The Christmas Song
	'se9YMv7F': '8fxgBoFR', //Christmas Time
	'sCDjMWaI': '8fxgBoFR', //White Christmas
	'OfRROQAg': '8fxgBoFR', //Mele Kalikimaka
	'7wOJgnIW': '8fxgBoFR', //Winter Wonderland
	'0WSBsVpQ': '8fxgBoFR', //It's the Most Wonderful Time of the Year
	'JcAgbkON': '8fxgBoFR', //Jingle Bell Rock
	'hlrtt2c1': '8fxgBoFR', //Baby, It's Cold Outside
	'nkGcQuhm': '8fxgBoFR', //Fairytale of New York
	'4PKHosw4': '8fxgBoFR', //Little Drummer Boy
	'GH8sO2Xp': '8fxgBoFR', //Up on the Rooftop
	'3M37T9lY': '8fxgBoFR', //Santa Claus is Coming to Town
	'HpTLtmJr': '8fxgBoFR', //Silent Night
	'SRQf8uX1': '8fxgBoFR', //Deck the Halls
	'qxiivoAq': '8fxgBoFR', //Greensleeves
	'aX7kkYfW': '8fxgBoFR', //Frosty the Snowman
	'4J9OAZAq': '8fxgBoFR', //Let It Snow! Let It Snow! Let It Snow!
	'jM9fbhbN': '8fxgBoFR', //Jingle Bells 
	'015v8ySw': '8fxgBoFR', //Feliz Navidad
	'LmntJ1qj': '8fxgBoFR', //Silver Bells
	/* SONG PLAYLISTS END */
	
	
	//404 and 404 profanity
	'profanity': 'jywxrA8T', //profanity
	'404': '6igp4zWd',
	'jywxrA8T': 'uQ99eRIe', //profanity
	'6igp4zWd': 'uQ99eRIe',
	'easteregg': 'EWGBQnIo', //lolz
	'EWGBQnIo': 'uQ99eRIe', 
	
	
	//response to cta
	'8fxgBoFR': 'uQ99eRIe'
};

var videoEvents = {
	'Intro-rev': [{ 
		position: 46, 
		timesCalled: 0, 
		callback: function() {
				$('#song_search').slideDown(600); 
			} 
		}],
	'idle_loop1': [{ 
		position: 10, 
		timesCalled: 0, 
		callback: function() { 
				$('#song_search').addClass('cta'); //Incase the form failed to slide down
				$('#song').addClass('cta', 'fast', function() {
					$(this).removeClass('cta', 3000);
				});
			} 
		}],
	'idle_loop2': [{ 
		position: 4, 
		timesCalled: 0, 
		callback: function() {
				$('#song_search').addClass('cta'); //Incase the form failed to slide down
				$('#song').addClass('cta', 'fast', function() {
					$(this).removeClass('cta', 3000);
				});
			} 
		}],
	'idle_loop3': [{ 
		position: 3, 
		timesCalled: 0, 
		callback: function() {
				$('#song_search').addClass('cta'); //Incase the form failed to slide down 
				$('#song').addClass('cta', 'fast', function() {
					$(this).removeClass('cta', 3000);
				});
			 } 
		}],
	'idle_loop4': [{ 
		position: 3, 
		timesCalled: 0, 
		callback: function() {
				$('#song_search').addClass('cta'); //Incase the form failed to slide down 
				$('#song').addClass('cta', 'fast', function() {
					$(this).removeClass('cta', 3000);
				});
			} 
		}]
};

//Var for playlist specific events
var activeEvents;
//Currently playing video
var activeVideo;
//Current playhead position in seconds
var playheadPosition = 0;

function playerReady(obj) {
	player = document.getElementById(obj.id);
	//extract the playlist id from the player object and set it to a global variable for the Current Playlist id
	currentPlaylist = obj.id.substr(5, 8);
	// player.addModelListener('STATE','stateHandler');
	//player.sendEvent("SEEK",20);
	player.sendEvent("PLAY", "false");
	player.addModelListener('STATE', 'stateHandler');
	player.addControllerListener('ITEM', 'itemHandler');
	//Set the video events
	activeEvents = videoEvents[currentPlaylist];
}

function stateHandler(obj) {
    //var elm = document.getElementById('message');
    if(obj.newstate == 'COMPLETED') { 
       // elm.innerHTML = "Completed! Thank you for watching...";
       // elm.style.display = 'block';
        //switch playlist on completed
        if(currentPlaylist != 'uQ99eRIe') {
            switchPlaylist(currentPlaylist);
        }
    } else if (obj.newstate == 'PAUSED') {
        //elm.innerHTML = "Please continue the video!";
       // elm.style.display = 'block';
        //$('#playback').trigger('click');
		$('#playback').removeClass('pause').addClass('play');
		$(window).trigger('PAUSED'); //Trigger global paused event
    } else if (obj.newstate == 'PLAYING') {
        //$('#playback').trigger('click');
		$('#playback').removeClass('play').addClass('pause');
		$(window).trigger('PLAYING'); //Trigger global playing event
    } else {
        //elm.style.display = 'none';
    }
};

function itemHandler(obj) {
	var playlist = player.getPlaylist();
	activeVideo = playlist[obj.index].title;
	//Reset the playhead position
	playheadPosition = 0;
	//Find the events for the specific video
	activeEvents = videoEvents[playlist[obj.index].title];
	//If the video has events bind the listener
	if (activeEvents) {
		player.addModelListener('TIME', 'timeHandler');
	//Else unbind the listener
	} else {
		player.removeModelListener('TIME', 'timeHandler');
	}
}

//Switch the playist based on Playlist Order
function switchPlaylist(key) {
	//grab the new playlist id from the playlist order based off of the current playlist id
	var value = (playlistOrder[key]);
	//load the new playlist
	$('#message').html(player.id);
	player.sendEvent("LOAD","http://content.bitsontherun.com/jwp/" + value + ".xml");
	//hide next trigger by default
	$('#next').hide();
	//show next trigger if type of video is song
	if(playlistOrder[value] === '8fxgBoFR' || value === '6igp4zWd') {
		$('#next').show();
	}
	//reset the Current Playlist id
	currentPlaylist = value;
};
	    
function timeHandler(obj) {
	var newPosition = obj.position;
	//If the playhead has moved by a second
	if (Math.floor(newPosition) > playheadPosition) {
		//Reset the playhead position
		playheadPosition = Math.floor(newPosition);
		//Loop through the active events and check for a matching position
		$.each(activeEvents, function(key, event) {
			//If matched, do callback and increment the timesCalled
			if (playheadPosition == event.position) {
				event.callback();
				event.timesCalled = event.timesCalled + 1;
			}
		});
	}
	//$('#message').text(newPosition);
}

