$(document).ready( function(){
	$('.video_tab').hover(
			function(){
				if( ! $(this).hasClass('video_tab_active') ){
					$(this).css('background-color','#666' );
				}
			},
			function(){
				if( ! $(this).hasClass('video_tab_active') ){
					$(this).css('background-color','#000' );
				}
			}
		);
	$(".video_tab").click( function(){
			$(".video_tab_active").css('background-color','#000' );
			$(".video_tab_active").removeClass("video_tab_active");
			$(this).addClass("video_tab_active");
			$(this).css('background-color','#093' );
			e = new Object();
			e.data = new Object();
			e.data.video_type = $(this).attr("id");
			e.data.offset = 0;
			fetchVideoThumbs( e );
		});

	$("#thumbs_left").hover(
		function(){
			if( !$(this).hasClass('inactive') ){
				$(this).css('background-color','#333' );
				$(this).css('border', '1px solid #fff' );
				$(this).css('border-right-color', '#666' );
				$(this).css('border-bottom-color', '#666' );
			}
		},
		function(){
			$(this).css('background-color','#000' );
			$(this).css('border', '1px solid #333' );
		}
	);
	$("#thumbs_right").hover(
		function(){
			if( !$(this).hasClass('inactive') ){
				$(this).css('background-color','#333' );
				$(this).css('border', '1px solid #fff' );
				$(this).css('border-right-color', '#666' );
				$(this).css('border-bottom-color', '#666' );
			}
		},
		function(){
			$(this).css('background-color','#000' );
			$(this).css('border', '1px solid #333' );
		}
	);


});

function fetchVideoThumbs( incoming ){
	$.ajax(
		{
			type: "POST",
			url: "/video/fetchThumbs",
			cache: false,
			data: {
				offset: incoming.data.offset,
				type: incoming.data.video_type
			},
			dataType: "json",
			success: function( data, status ){
					$('#thumb_window').html( data.html );
					offset = parseInt( data.offset );
					limit = parseInt( data.limit );
					total = parseInt( data.total );
					if( offset > 0 ){
						$('#thumbs_left').removeClass('inactive');
						$('#thumbs_left').bind( 'click', {video_type: data.type, offset: (offset-limit)}, fetchVideoThumbs);
					}
					else{
						$('#thumbs_left').addClass('inactive');
						$('#thumbs_left').unbind('click',fetchVideoThumbs);
					}
					if( (offset+limit) <= total ){
						$('#thumbs_right').removeClass('inactive');
						$('#thumbs_right').bind( 'click', {video_type: data.type, offset: (offset+limit)}, fetchVideoThumbs);
					}
					else{
						$("#thumbs_right").addClass('inactive');
						$("#thumbs_right").unbind('click', fetchVideoThumbs );
					}

					$('.thumb_container').click( function(){
							title = $(this).find('.thumb_title').text();
							path = "/video_files/"+$(this).attr("video_id")+"/"+$(this).attr('video_id')+"_320.flv";
							img = "/video_files/"+$(this).attr("video_id")+"/thumb.jpg";
							player = document.getElementById("video_player");
							player.setImagePath(img);
							player.setVideoPath(path);
							pageTracker._trackPageview("/home/video/"+$(this).attr("video_id")+"/"+title);
						});
				}
		}
	);
}

