// initialization variables
window.flowplayer = new Object();
window.loaded = 0;
window.bufferingStopped = false;

flowplayer.fireEvent = function(objectID, event, obj1) {
	switch(event) {
		case "onFinish":
			if(loaded == 0) {
				loaded = 1;			
				} 
				else if (loaded == 2) {
					loaded = 1;
					setTimeout("parsePlaylist()", 500);
				}
			break;
		case "onBeforeBegin":
			if (obj1 != "0") {
				playlistNowPlaying(parseInt(obj1));
			}
			break;					
	}
}

jQuery(document).ready(function() {
	window.playlistId = $('object[name="embedded_inctv_playlist"]').attr("id");
	if (typeof playlistId != 'undefined') {
		if (playlistId == 'embedded_inctv_playlist') {
			window.width = parseInt($('#embedded_inctv_playlist').attr('width'));
			var height = parseInt($('#embedded_inctv_playlist').attr('height'));
			if (height == 228) {
				var playlistHeight = 167;
				playlistHeight = "height: " + playlistHeight + "px;";
				}
				else {
				var playlistHeight = " ";
				}
			$("#embedded_inctv_playlist").after('<div id="inctv-playlist-container" class="playlist-' + width + '" style="width:' + width + 'px;' + playlistHeight + '"><div id="inctv-playlist-wrapper"><div id="inctv-meta-container"><div class="inctv-meta-bg"><\/div><\/div><div class="playlist-scrollable-wrap"><a class="inctv-playlist-nav prev browse left"></a><div class="scrollable"><ul id="inctv-title-list" class="items"><\/ul><\/div><a class="inctv-playlist-nav next browse right"></a><\/div><\/div><\/div>');
			//$('#inctv-playlist-wrapper').hide();
		}
	}
	//check if load event has fired already, and if not, set the flag
	if (loaded == 1) {
		setTimeout("parsePlaylist()", 500);
		} 
		else if (loaded == 0) {
			loaded = 2;
	}
});

function playlistObj(vid,link,title,description) {
	this.vid=vid;
	this.link=link;
	this.title=title;
	this.description=description;
}

function parsePlaylist() {
	if (typeof playlistId != 'undefined') {
		window.playlist = new Array();
		playlist[0] = '';
		if (document.getElementById) {
			var fp_playlist = document.getElementById(playlistId).fp_getPlaylist();
		}
		$.each(fp_playlist, function(i,item){
			if (i != 0) {
				var title = item.title;
				var guid = item.guid.text;
				var description = item.description;
				if (item["custom-field:description-short"]) {
					var description = item["custom-field:description-short"];
					}
					else {
					var description = item.description;
					}
				if (item["custom-field:permalink"]) {
						var link = item["custom-field:permalink"];
					} else {
						var link = "http://www.inc.com/inctv";
				}
				playlist[i] = new playlistObj(guid, link, title, description);
			}
		});
		if (playlistId == 'embedded_inctv_playlist') {
			buildPlaylist();
		} else if (playlistId == 'inctv_lander_playlist') {
			buildPlayer();
		}
	}
}

// load the playlist into the player
function buildPlaylist() {
  if (width == 560) {
    descriptionLength = 215;
    titleLength = 60;
  }
  else {
    descriptionLength = 170;
    titleLength = 37;
  }
	$.each(playlist, function(i) {
	if (i != 0) {
		$('#inctv-title-list').append('<li playlistindex="' + i + '" id="inctv-button-' + i + '" class="inctv-title-button item"><div class="inctv-title-pointer"></div><div class="inctv-title-play"></div><img src="http://video.inc.com/videos/' + playlist[i].vid + '/screenshots/120w.jpg" class="inctv-title-thumb"></li>');
		$('.inctv-meta-bg').hide();
		$('.inctv-title-pointer').show().css({'background-position' : '-999999'});
		$('.inctv-title-play').css({'background-position' : '-999999'});
		$('#inctv-meta-container').append('<div style="display:none;" id="inctv-title-' + i + '" class="inctv-meta"><div class="inctv-meta-tools"></div><h4 class="inctv-meta-title"></h4><div class="inctv-meta-teaser"></div></div>');
		$('#inctv-title-' + i + ' > .inctv-meta-title').html(playlistTruncate(playlist[i].title, titleLength));
		$('#inctv-title-' + i + ' > .inctv-meta-teaser').html(playlistTruncate(playlist[i].description, descriptionLength) + ' <a class="inctv-meta-permalink" target="_new" href="' + playlist[i].link + '" >See more ></a>');
		$('#inctv-title-' + i + ' > div.inctv-meta-tools').append('<div addthis:url="' + playlist[i].link + '" addthis:title="' + playlist[i].title + '" id="inctv_addthis_' + i + '" class="addthis_toolbox" ><a class="addthis_button_compact">Share</a><span class="addthis_separator"></span><a class="addthis_button_email"></a><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a></span></div>');
//		var addthis_config = { services_compact : 'print,email,favorites' };
//		var addthis_share = { url : }
		addthis.toolbox("#inctv_addthis_" + i);
		}
	});
	$('.inctv-meta-teaser > img').hide();
	setTimeout("buildPlayer()", 500);
}
									
// load first playlist item in player
function buildPlayer() {
	if (playlistId == 'embedded_inctv_playlist') {
    scrollableConfig = new Object();
	  scrollableConfig.size = 1;
	  scrollableConfig.hoverClass = 'hover';
	  scrollableConfig.api = 'true';	  
  	window.scrollableApi = $('div.playlist-scrollable-wrap').scrollable(scrollableConfig);
	}
	playlistToggleActive(1);
	playlistRegisterEvents();	
}

// call the playlist item on the player. Many oddities to this process.
function playlistPlay(id) {
	document.getElementById(playlistId).fp_play(id);
	playlistToggleActive(id);
}

// Toggle classes, to show the proper info for the video
function playlistToggleActive(id) {
	if (playlistId == 'inctv_lander_playlist') {
		$('.channelf2').removeClass('active');
		var embedValue = '<object width="512" height="313" id="embedded_player_' + playlist[id].vid + '" type="application/x-shockwave-flash" data="http://video.inc.com/plugins/player.swf?v=' + playlist[id].vid + '"><param name="movie" value="http://video.inc.com/plugins/player.swf?v=' + playlist[id].vid + '"/><param name="allowfullscreen" value="TRUE"/><param name="allowscriptaccess" value="always"/><param name="base" value="http://video.inc.com"/><\/object>';
		//window.playlistShareValue = "addthis_open(this, '', '" + playlist[id].link + "', '" + playlist[id].title + "')";
		window.playlistCurrentLink = playlist[id].link;
		window.playlistCurrentTitle = playlist[id].title;
		var downloadValue = 'http://video.inc.com/videos/' + playlist[id].vid + '/formats/podcast_default/file.mp4';
		var commentValue = playlist[id].link + '#comment';
		$('.inctv-player-list-item-embed > input').attr("value", embedValue);
		$('.inctv-player-list-item-comment > a').attr("href", commentValue);
		$('.inctv-player-list-item-download > a').attr("href", downloadValue);
		//id = id + 1;
		$('#feature' + id).addClass('active');
		} 
		else if (playlistId == 'embedded_inctv_playlist') {
			$('.inctv-title-button').removeClass("active");
			$('#inctv-button-' + id).addClass("active");
			$('.inctv-meta').removeClass("active").hide();
			$('.inctv-meta-bg').fadeOut('medium');
			$('.inctv-title-pointer').fadeOut('medium');
			$('.inctv-title-play').fadeOut('medium');
			$('#inctv-title-' + id).addClass("active").show();
	}
}

// playlist interaction events, this could be cleaned up
function playlistRegisterEvents() {
	if (playlistId == 'inctv_lander_playlist') {
		//$(".channelf2 a[playlistindex]").attr("href", '#');
		$(".channelf2 a[playlistindex]").click(
			function() {
				id = parseInt($(this).attr("playlistindex"));
				playlistPlay(id);
				return false;
			}
		);
	} 
	else if (playlistId == 'embedded_inctv_playlist') {
		$('.inctv-title-button').hover(
			function() {
				$(this).addClass("hover");
				$(this).children('.inctv-title-pointer').show().css({'background-position' : '0'});
				$(this).children('.inctv-title-play').css({'background-position' : '0'});
				var id = parseInt($(this).attr("playlistindex"));
				var meta = '#inctv-title-' + id;
				$(".inctv-meta").hide();
				$(meta).addClass("hover").show();
				$('.inctv-meta-bg').show();
			},
			function() {
				$(this).removeClass("hover");
				$(this).children('.inctv-title-pointer').hide().css({'background-position' : '-99999'});
				$(this).children('.inctv-title-play').css({'background-position' : '-999999'});
				var id = parseInt($(this).attr("playlistindex"));
				var meta = '#inctv-title-' + id;
				$(".inctv-meta").removeClass("hover").hide();
				$(".inctv-meta-bg").hide();
				$(".active").removeClass("hover").show();
			}
		);
		$(".inctv-title-button").click(
			function() {
				id = parseInt($(this).attr("playlistindex"));
				playlistPlay(id);
				}
		);
	}
}

function playlistNowPlaying(id) {
	playlistToggleActive(id);
	if (scrollableApi) {
		scrollableApi.seekTo(id - 1);	
	}	
}

//Simple truncate function
function playlistTruncate(str,len) {
	if (str.length > len) {
		str = str.substring(0, len);
		str = str.replace(/\w+$/, '');
		str += '...';
		}
	return str;
} 
