$(document).ready(function(){
	/* Handle switching tabs */
	$('a.animation-loading').click(function(){
		var target = $(this);
		var url = target.attr('href');
		var channel = url.substring(27).replace(/\+/, '_');
		
		$("#loading-popup").show();
		trackAjaxCall( url );
		$('#featured-tabs-content').load(url, function(){
			$("#loading-popup").hide();
			$('.animation-loading').click(animationloader);
			$('span[id^="link_"]').hide();
			$('span[id^="tab_"]').show();
			$('#tab_'+channel).hide();
			$('#link_'+channel).show();
			$('li.video-tab').removeClass('tabs-selected');
			target.parents('li.video-tab').addClass('tabs-selected');
		});
		return false;
	});
	
	/* Handle scrolling through the channels in the Video By Channels module */
	$('a.browse-button').click(function(e){
		var target = $(this);
		var channel = target.attr('href').substring(21).replace(/\+/, '_');
		var url = target.attr('href');
		var left = target.hasClass('left');
		
		if(left){
			if(!pltv_channelScrollDisabled[channel].left && !pltv_updateInProgress){
				animationloader(e);
				$("#loading-popup").show();
				pltv_updateInProgress = true;
				pltv_channelOffsets[channel] -= 3;
				pltv_channelScrollDisabled[channel].right = false;
				$.getJSON(url+'&o='+pltv_channelOffsets[channel], function(data){
					target.next().html(data.output);
					$("#loading-popup").hide();
					pltv_updateInProgress = false;
				});
				pltv_channelScrollDisabled[channel].left = (pltv_channelOffsets[channel] == 0)?true:false;
			}
		} else {
			if(!pltv_channelScrollDisabled[channel].right && !pltv_updateInProgress){
				animationloader(e);
				$("#loading-popup").show();
				pltv_updateInProgress = true;
				pltv_channelOffsets[channel] += 3;
				$.getJSON(url+'&o='+pltv_channelOffsets[channel], function(data){
					if(data.count > 0 && data.count < 3){
						pltv_channelScrollDisabled[channel].right = true;
					}else if(data.count == 0){
						pltv_channelScrollDisabled[channel].right = true;
						pltv_channelOffsets[channel] -= 3;
					}
					if(data.count > 0){
						target.prev().html(data.output);
						pltv_channelScrollDisabled[channel].left = false;
					}
					$("#loading-popup").hide();
					pltv_updateInProgress = false;
				});
			}
		}
		return false;
	});
	
	/* Handle redirects in the Channel/Tags browser */
	$('#tags-select').change(function(){
		if ($(this).val() != '_blank') {
			window.location.href = $(this).val();
		}
	});
	
	/* Handle highlighting the embed code and url on the video player page */
	$('input.pltv-code-container').bind('click focus', function(){
		$(this).select();
	});
});

function pltv_fbsClick()
{
	var u = location.href;
	var t = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&amp;t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}