function VideoPlayer(data) {
	/* PRIVATE */
	var _videoData = data;
	var _nbcuPlayer = null;
	var _videoId = null;				// currently playing video id
	var _pageViewTimeout = null;		// number of seconds user must watch before the page view is recorded
    var isUpdatePageLinks = false;
	
	/* PUBLIC */
	return {
		init: function(nbcuPlayer) {
			if (typeof nbcuPlayer == 'undefined') {
				return false;
			}
			
			var THIS = this;
			_nbcuPlayer = nbcuPlayer;
			
			_nbcuPlayer.addEventListener("Player.start", function(e) {
				var metadata = _nbcuPlayer.getMetaData();
				jqN('#video_player .video_title').html(metadata.properties.title);
				jqN('#video_player .video_description').html(metadata.properties.description);
				if (typeof updatePageLinks == 'function') {
					updatePageLinks(metadata.properties);
                    isUpdatePageLinks = true;
				} 				
			});
			
			_nbcuPlayer.addEventListener("Player.end", function(e) {
			});
			
			// test code start
			adEngineExtension.addEventListener("AD_BREAK_EVENT_ON_COMPLETE", function(e) {
				if(e.data.state == "AD_STATE_POSTROLL") {
					var nextVideoId = THIS.getNextVideo();
					if (nextVideoId !== false && nextVideoId != '') {
						THIS.play(nextVideoId);
					}
				}
			});

			// test code end
			_nbcuPlayer.addEventListener("Player.seekStart", function(e) {
			});
            if (!updatePageLinks) {
                if (typeof updatePageLinks == 'function') {
                    if (_nbcuPlayer.properties) {
					    updatePageLinks(_nbcuPlayer.properties);
                    }
                }

            }

			return this;
		},
		
		play: function(videoId, remember) {
			_videoId = videoId;
			
			remember = typeof remember == 'boolean' && remember == false ? false : true;
			if (remember === true) {
				// keep track of last full eps video watched
				var cookiePrefix = nbcu.config.getParam("nbcuEnvironment") == 'production' ? '' : nbcu.config.getParam("nbcuEnvironment") + '_';
				jqN.cookie(cookiePrefix + 'lpv', videoId, {expires: 30});
			}
			
			_nbcuPlayer.playVideo(videoId);
			
			// Record viewcount after 30 seconds
			clearTimeout(_pageViewTimeout);
			_pageViewTimeout = setTimeout("player.viewCount("+videoId+")", 30000);
			
			// pixel tracking
			var bannerId = this.getVideoBannerId(videoId);
			if (bannerId !== false) {
				jqN('body').append('<img src="http://core.insightexpressai.com/adServer/adServerESI.aspx?bannerID=' + bannerId + '&script=false&redir=http://core.insightexpressai.com/adserver/1pixel.gif">');
			}

            if (!isUpdatePageLinks) {
                updatePageLinks(_nbcuPlayer.getMetaData());
                isUpdatePageLinks = true;
            }
			
			return this;
		},
		
		getVideoCurrentTime: function() {
			var sec = _nbcuPlayer.getCurrentTimeStart();
			return Math.round(sec);
		},
		
		viewCount: function(videoId) {
			nbcu.util.common.loadClass("Nbcu.Sn.ViewCount");
			nbcu.sn.viewCount.setViewCount("void(0);", null, null, "VIDEO-" + videoId, "SS_VIDEO");
		},
		
		getNextVideo: function(videoId) {
			videoId = videoId || _videoId;
			
			var videoData = jqN.extend(true, [], _videoData);
			var vidId = false;
			var found = false;
			while (true) {
				if (videoData.length < 1) {
					break;
				}
				if (found) {
					vidId = videoData[0].videoId;
					break;
				}
				if (videoId == videoData[0].videoId) {
					found = true;
				}
				videoData.shift();
			}
			
			if (vidId === false || vidId == '') {
				vidId = _videoData[0].videoId;
			}
			
			return vidId;
		},
		
		getVideoBannerId: function(videoId) {
			videoId = videoId || _videoId;
			
			var videoData = jqN.extend(true, [], _videoData);
			var bannerId = false;
			while (true) {
				if (videoData.length < 1) {
					break;
				}
				if (videoId == videoData[0].videoId) {
					bannerId = videoData[0].bannerId;
					break;
				}
				videoData.shift();
			}
			
			return bannerId;			
		}
	}
}

function VideoBlade(data) {
	/* PRIVATE */
	var _videoData = data;
	
	/* PUBLIC */
	return {
		init: function(cssSelector) {
			// populate carousel
			var text = '';
                     var pholder = 0;
			try {
				for (var i = 0; i < _videoData.length; i++) {
					text += '<li id="video-' + _videoData[i].videoId + '">'
                                  +'<a href="javascript:void(0);" style="cursor:pointer"><img src="' + _videoData[i].thumbnailUri + '" border="0"  style="cursor:pointer"/></a>'
					
					
					    +'<div class="content_container">'
						 + '<h5>' + _videoData[i].title + '</h5>';
					if (_videoData[i].videoId != '') {
						text += '<a href="javascript:void(0);">Watch now &raquo;</a>'
					}
					text += '</div>'
						 + '</li>';

				}
			} catch(err) {
			}
	
			// init carousel
			jqN('ul' + cssSelector).html(text).jcarousel({
				scroll: 4,
				initCallback: function(c) {
						jqN('#horizontal_carousel_container #next a').bind('click', function() {
						c.next();
						return false;
					});
				 
					jqN('#horizontal_carousel_container #prev a').bind('click', function() {
						c.prev();
						return false;
					});					
				},
				buttonNextCallback: function(c, e, fl) {
					if (fl) {
						jqN('#horizontal_carousel_container #next a').removeClass('disabled');
					} else {
						jqN('#horizontal_carousel_container #next a').addClass('disabled');
					}
				},
				buttonPrevCallback: function(c, e, fl) {
					if (fl) {
						jqN('#horizontal_carousel_container #prev a').removeClass('disabled');
					} else {
						jqN('#horizontal_carousel_container #prev a').addClass('disabled');
					}					
				}
			});
			
			// bind click event to each video
			jqN('ul' + cssSelector + ' li a').click(function() {
				var videoId = jqN(this).parents('li').attr('id').replace('video-', '');
				if (videoId != '') {
					player.play(videoId);
				}
			});

			return this;		
		}
	}
}
function igwtPromoteNextVidAdvance()
{
	player.play(player.getNextVideo());
}

