$(function() {

	$('.pd, a[id=upload-video]').click(function () {

      // register click and determine tab/pane
      var selected_tab = $(this);
      var pane_id = $(this).attr('id');
      var close_panel = $('div[id=' + pane_id +']').is(':visible');



      if(close_panel) {

          $('div[id=' + pane_id +']:visible').toggle();
          // remove any previously selected & shaded tabs
          $('li.shaded').removeClass('shaded');
          $('li.selected').removeClass('selected');

      } else {

          // remove any previously selected & shaded tabs
          $('li.shaded').removeClass('shaded');
          $('li.selected').removeClass('selected');

          // hide any visible panes
          $('.hpb-pulldown-pane:visible').toggle();

          // add selected
          $('li[id='+ pane_id +']').addClass('selected');

          // add shaded
          $('#pd-tabs > li:not(.selected)').addClass('shaded');

          $('div[id=' + pane_id +']:hidden').toggle();
      }

   });



   // Loading Comments - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   $('#expand-comment-box').click(function() {

		if($('.comments_box_holder').is(':visible')) {
			$('.comments_box_holder').html('');
			$('#hpb-vid-comment-box').hide();
		}
		else {

			var uploadid = $(this).attr('name');
			var page = 'home';
			var widget = 'comments_box';

			// Load fbml for clicked video...
			fbml_loader(uploadid, page, widget, function(data){

				$.each(data, function(i, item) {
					$('.'+item.tool+'_holder').html(item.fbml);
				});

				window.setTimeout(function() {
					FB.XFBML.parse();
				}, 100);

			});

			$('#hpb-vid-comment-box').show();


		}
		
	});

	$('#hpb-vid-info-box ul.tabs').tabs('#hpb-vid-info-box div.panes > div.pane');

	$('ul.hpb-playlist-tabs').tabs('.hpb-playlist-panes > div.hpb-playlist-single-pane');

	$('.hpb-playlist-items-scroller').scrollable({
		speed: 0
	}).navigator();


	// CLICKING A VIDEO LINK - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	$('.hpb-playlist-item').click(function() {

		if($('.comments_box_holder').is(':visible')) {
			$('.comments_box_holder').html('');
			$('#hpb-vid-comment-box').hide();
		}

		// Styling Changes for active video
		$('.hpb-playlist-item').removeClass('active-item');
		$(this).addClass('active-item');


		// grab upload Id of clicked video
		var vid_uploadid = $(this).attr('id');
		var page = 'home';
		var widget = 'like_button';

		// Load fbml for clicked video...
		fbml_loader(vid_uploadid, page, widget, function(data){

			$.each(data, function(i, item) {
				$('.'+ item.tool +'_holder').html(item.fbml);
			});

			window.setTimeout(function() {
				FB.XFBML.parse();
			}, 500);

		});

		// Now load the video
		get_vid_info(vid_uploadid);

		//Change the name attribute on the comments button to the new upload id
		$('#expand-comment-box').attr('name', vid_uploadid);


	});

	active_tab_class_add('ul.hpb-playlist-tabs');
	hover_tab_class_add('ul.hpb-playlist-tabs');

	active_tab_class_add('#hpb-vid-info-box ul.tabs');
	hover_tab_class_add('#hpb-vid-info-box ul.tabs');

});


/* MISCELLANEOUS FUNCTIONS */

function active_tab_class_add(list_class, class_name) {

	if( ! class_name)
		class_name = 'current';

	$(list_class+' a').click(function() {

		$(list_class+' li a').parent().removeClass(class_name);
		$(this).parent().addClass(class_name);

	});
}

function hover_tab_class_add(list_class, class_name) {

	if( ! class_name)
		class_name = 'hover';

	$(list_class+' li').hover(function() {
			$(this).addClass(class_name);
		},
		function() {
			$(this).removeClass(class_name);
		});
}

function get_vid_info(uploadid) {

	// Get javascript object for video
		$.getJSON(base_url() + "ajax/singleupload/"+ uploadid, function(data) {
			$.each(data, function(i, item) {

				//FB.Event.subscribe('xfbml.render', function(response) {
					$('#hpb-vid-info-box .vid-id').text(uploadid);
					$('#hpb-vid-info-box .vid-title').text(item.upload_title);
					$('#hpb-vid-info-box .comments-heading').text('Comments for '+item.upload_title);
					$('#hpb-vid-info-box .vid-desc').text(item.upload_caption);
					$f('hpb-first-player').play(base_url()+'user_files/'+item.user_id+'/video/'+item.user_upload_id+'.'+item.upload_file_type);
				//});

			});
		});

}

