// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var MIT = {
  showPlaylist : function (container,single,homepage){
	single = single || false; // flag to show only a single item
	homepage = homepage || false; // flag if this is the homepage
    var baseUrl = 'http://techtv.mit.edu/'; 
    var userID = 405 // get this from your playlist rss feed url
	if(!homepage) {
		$.getJSON(baseUrl + 'users/' + userID + '/playlist?format=js&jsoncallback=?', function(data) {
			var videos = data.video_list.video; 
	        var params = { autoplay: "f", key: videos[0].id };
	        // Creates a list thumbnails in the video sidebar
	        var video_list = $('<ul id="video_playlist"/>').appendTo(container);
			video_list.wrap('<div id="video_playlist_wrapper"/>').before('<div id="video_playlist_left">&laquo;</div><div id="video_playlist_right">&raquo;</div>');
		    for (var i = 0; i < videos.length; i ++) {
				var video = videos[i],
					link = $('<a href="http://www.viddler.com/simple/' + video.id + '/" title="' + video.title + '"><img src="' + baseUrl + video.thumbnail_url + '" alt="' + video.title + '" width="58" height="43"/></a>').data('video',video); // generate the link and attach the video data
				$('<li/>').append(link).appendTo(video_list); // add the new link, in a LI, to the video list
	        }
			// Video scrolling
			$('#video_playlist_left').click(function() {
				if(video_list.is(':animated')) return;
				if(video_list.position().left>=0) {
					video_list.animate({left:'5px'},100,'easeOutSine',function() {
						video_list.animate({left:0},100,'easeInCubic');
					});
					return;	
				}				
				$('#video_playlist').animate({left:'+=78px'});
			});
			$('#video_playlist_right').click(function() {
				if(video_list.is(':animated')) return;
				if(video_list.position().left<=330-(video_list.children().length*78)) {
					video_list.animate({left:'-=5px'},100,'easeOutSine',function() {
						video_list.animate({left:'+=5px'},100,'easeInCubic');
					});
					return;	
				}
				$('#video_playlist').animate({left:'-=78px'});
			});
			// Attach the player
			var player = $('<div id="video_player"><div id="mit_player">Watching this video requires Flash Player 8 or higher.</div></div>').prependTo('#news_video'),
				swf = $('#video_playlist a').eq(0).attr('href');
			swfobject.embedSWF(swf, // embed the linked SWF
				'mit_player', // replacing the item with this ID
				'305', '223', // width&height
				'8', null, null,
				{allowScriptAccess:'always',wmode:'transparent'}, // needed for JS api
				{id:'mit_player'}
			);        
	        // Makes the video sidebar thumbnail links change the video in the player
			$('#video_playlist a').click(function() {
				var video = $(this).data('video'),
					height = ((video.height/video.width)*305)+20;
				$('#video_playlist a').removeClass('playing');
				$(this).addClass('playing');
				$('#mit_player').attr('height',height); // adjust the height
				if(document.getElementById('mit_player').setMovie) document.getElementById('mit_player').setMovie(video.id,0); // set the movie with the JS API
				return false; // cancel the original click
	        }).eq(0).click(); // and click the first one now
		});
	} else { // otherwise (homepage only)
		$.getJSON(baseUrl + 'users/' + userID + '/playlist?format=js&jsoncallback=?', function(data) {
			var videos = data.video_list.video; 
	        var params = { autoplay: "f", key: videos[0].id };
	        // Creates a list thumbnails in the video sidebar
	        var video_list = $('<ul id="video_playlist"/>').appendTo(container);
	        for (var i = 0; i < Math.min(videos.length,7); i ++) {
	          var video = videos[i];
	          video_list.append('<li><a href="http://www.viddler.com/simple/' + video.id + '/" rel="'+video.id+'" title="' + video.title + '"><img src="' + baseUrl + video.thumbnail_url + '" alt="' + video.title + '" width="58" height="43"/></a></li>');
	        }
	        // Makes the video sidebar thumbnail links change the video in the player
	        $('#video_playlist a').click(function() {
				$('#video_playlist a').removeClass('playing');
		          $(this).addClass('playing');
			  if($('#mit_player').length) {
				document.getElementById('mit_player').setMovie($(this).attr('rel'), 0);
			  } else {
				var blackout = $('<div class="blackout"/>').fadeTo(0,0.5).prependTo('#news'),
					overlay = $('<div class="video_overlay"><div class="close_overlay">close</div><div id="mit_player">Watching this video requires Flash Player 8 or higher.</div></div>').prependTo('#news .content'),
					swf = $(this).attr('href');
					swfobject.embedSWF(swf, // embed the linked SWF
						'mit_player', // replacing the item with this ID
						'505', '230', // width&height
						'8', null, null,
						{allowScriptAccess:'always',wmode:'transparent'}, // needed for JS api
						{id:'mit_player'}
					);
					blackout.add('.close_overlay').click(function() { // clicking the blackout or close button
						$('#video_playlist a').removeClass('playing');
						blackout.add(overlay).remove(); // closes the overlay
					});
			  }
			  return false; // cancel the original click
	        });
		});
	}

  }
}
