// define the vimeo Channel ID or name in the page calling this script
/* e.g.
*	var channel_id = 74507;
*/
//////////////////////////////////////////
var channel_id;
if(channel_id == null)
	alert('Error: No Vimeo channel ID defined!');

// redefine jQuery to stop collision with other script
var $j = jQuery.noConflict();

var current_video = false;
var video_ids = [];
		
// add a video to the video list

var add_video = function (video)
{
	var new_vid = $j('#video_template').clone();
	new_vid.attr('id', 'video-'+video.id);
	new_vid.find('img').attr('src', video.thumbnail_medium);
	var title = video.title;
	var title_parts = title.split('|');
	new_vid.find('.title').text(title_parts[2]);
	new_vid.show();
	$j('#videos').append(new_vid);
	video_ids.push(video.id);
}
var refresh_navigation = function()
{
	// find the current id in the array
	var position = video_ids.indexOf(current_video);
	
	if(position == '0')
		$j('.controls .previous').hide();
	else
		$j('.controls .previous').show();
	
	if(position >= video_ids.length-1)
		$j('.controls .next').hide();
	else
		$j('.controls .next').show();
		
}

var play_video = function(video_id)
{
	$j.ajax({
		url: 'http://vimeo.com/api/oembed.json?autoplay=true&color=3ab64f&byline=false&width=600&title=false&portrait=false&url=http://vimeo.com/'+video_id,
		dataType: 'jsonp',
		success: function(oembed) {
			// add the play button back to the old video
			if(video_id != current_video)
			{
				$j('#video-'+current_video).prepend('<div class="play"></div><div class="hover_cover"></div>');
				$j('#video-'+current_video+' .title').css({'background-color' : '#e7e7de', 'color' : '#282425'});
			}
				
				
			$j('.video_container').hide();
			$j('#video_object').show();
			$j('#video_object').html(oembed.html);
			var title = oembed.title;
			title = title.split('|');
			title[0] = title[0].replace('The Founders', '').replace('"', '').replace('"', '');
			title[0] += ' - '+title[2];
			$j('.nav .title').text(title[0]);
			$j('#video-'+video_id+' .play').remove();
			$j('#video-'+video_id+' .hover_cover').remove();
			$j('#video-'+video_id+' .title').css({'background-color' : '#3ab64f', 'color' : '#fff'});
			current_video = video_id;
			window.location = '#logo';
			refresh_navigation();
		}
	});
}

$j(document).ready(function() {
	// get the most recent videos
	$j.ajax({
		url: 'http://vimeo.com/api/v2/channel/'+channel_id+'/videos.json?callback=?',
		dataType: 'jsonp',
		success: function(data) {
			var first = true;
			$j.each(data, function(i,video){
				add_video(video);
				// set the default video
				if(first)
				{
					$j('.video_container img').attr('src', video.thumbnail_large);
					var title = video.title;
					title = title.split('|');
					title[0] = title[0].replace('The Founders', '').replace('"', '').replace('"', '');
					title[0] += ' - '+title[2];
					$j('.nav .title').text(title[0]);
					current_video = video.id;
				}
				first = false;
			});
			refresh_navigation();
		}
	});
	
	$j('.hover_cover').live('mouseover mouseout', function (event) {
	  if (event.type == 'mouseover') {
	    $j(this).parent().find('.play').fadeIn('fast');
	  } else {
	    $j(this).parent().find('.play').hide();
	  }
	});
	
	$j('.hover_cover').live('click', function ()
	{
		var video_id = $j(this).parent().attr('id').replace('video-', '');
		play_video(video_id);
		refresh_navigation();
	});
	
	// big play button action
	$j('.big_play').click(function ()
	{
		play_video(current_video);
		refresh_navigation();
	});
	
	$j('.next, .previous').click(function ()
	{
		if($j(this).hasClass('next'))
		{
			play_video(video_ids[video_ids.indexOf(current_video)+1]);
			
		}
		else if($j(this).hasClass('previous'))
		{
			play_video(video_ids[video_ids.indexOf(current_video)-1]);
		}
	});
});
