$(document).ready(function(){
	$('#channel_show .video_list a').click(function(){
		var video_id = $(this).attr('id');
		load_video(video_id);
		return false;
	});
	$('#channel_show .commercial_list a').click(function(){
		var commercial_id = $(this).attr('id');
		load_commercial(commercial_id);
		return false;
	});
	custom_tabs($('#channel_show .custom_tabs'));
});

function load_video(video_id)
{
	var is_live = $('#channel_show #is_live').val();
	if(is_live == 1)
	{
		load_video_dialog(video_id);
	}
	else
	{
		load_video_player(video_id);
	}
}

function load_video_player(video_id)
{
	var result = $('#channel_show #video_wrapper');
	var video_url = '/video/show';
	var video_data = { id:video_id };
	
	$.ajaxSetup({
		beforeSend:function(){
			
		},
		success:function(){
			
		},
		error:function(){
			alert('This video cannot currently be loaded. We apologize for the inconvenience');
		}
	});
	
	result.load(video_url, video_data);
}

function load_video_dialog(video_id)
{
	var result = $('#video_dialog');
	var video = $('#channel_show #video_wrapper');
	var video_url = '/video/show';
	var video_data = { id:video_id };
	result.dialog({ 
		modal:true, 
		autoOpen:false, 
		width:610,
		beforeClose:function(){
			$(this).find('#tv_code').html(' ');
		},
		close:function(){
			window.location = window.location;
		}
	});
	
	$.ajaxSetup({
		beforeSend:function(){
			video.html(' ');
		},
		success:function(){
			
		},
		error:function(){
			alert('This video cannot currently be loaded. We apologize for the inconvenience');
		}
	});
	result.ajaxStop(function(){
		var title = result.parent().find('h2').html();
		result.parent().find('h2').hide();
		result.parent().find('.ui-dialog-titlebar span').html(title);
		$('#video_dialog').dialog('open');
	})
	
	result.load(video_url, video_data);
}

function load_commercial(commercial_id)
{
	var is_live = $('#channel_show #is_live').val();
	if(is_live == 1)
	{
		load_commercial_dialog(commercial_id);
	}
	else
	{
		load_commercial_player(commercial_id);
	}
}

function load_commercial_player(commercial_id)
{
	var result = $('#channel_show #video_wrapper');
	var commercial_url = '/commercial/show';
	var commercial_data = { id:commercial_id };
	
	$.ajaxSetup({
		beforeSend:function(){
			
		},
		success:function(){
			
		},
		error:function(){
			alert('This video cannot currently be loaded. We apologize for the inconvenience');
		}
	});
	
	result.load(commercial_url, commercial_data);
}

function load_commercial_dialog(commercial_id)
{
	var result = $('#commercial_dialog');
	var video = $('#channel_show #video_wrapper');
	var commercial_url = '/commercial/show';
	var commercial_data = { id:commercial_id };
	result.dialog({ 
		modal:true, 
		autoOpen:false, 
		width:610,
		beforeClose:function(){
			$(this).find('#tv_code').html(' ');
		},
		close:function(){
			window.location = window.location;
		}
	});
	
	$.ajaxSetup({
		beforeSend:function(){
			video.html(' ');
		},
		success:function(){
			
		},
		error:function(){
			alert('This video cannot currently be loaded. We apologize for the inconvenience');
		}
	});
	result.ajaxStop(function(){
		var title = result.parent().find('h2').html();
		result.parent().find('h2').hide();
		result.parent().find('.ui-dialog-titlebar span').html(title);
		$('#commercial_dialog').dialog('open');
	})
	
	result.load(commercial_url, commercial_data);
}
