var player = null;
var speed = 800;
var vidego_video_uuid = get_url_parse('vidego_video_uuid');

$(function(){
	resizeBackgroundImage();

	$(".list").link();
	
	if(window.location.hash == '') {
		$('#choose_video').show_content();
		
		$('#video a').click(function(){
			$('#choose_video a:first').trigger('click');
			window.location.hash = $('#choose_video a:first').attr('href');
			$(this).remove();
			$('#choose_video').hide_content();
			return false;
		})
	} else {
		$('#video a').remove();
			play_video(window.location.hash);		
	}
	
	$('.nav a').click(function(){
		$this = $(this);
		target = $this.attr('href');
		
		if($(target).is(':hidden'))
		{
			$('.nav a').not($this).removeClass('active');
			$this.addClass('active');
			
			$others = $('.content:visible').not($(target));
			if($others.is(':visible')) {
				$others.hide_content(function(){
					$(target).show_content();
				});
			} else {
				$(target).show_content();
			}
		}
		else
		{
			$this.removeClass('active');
			$(target).hide_content();
		}
		
		return false;
	})
	
	$('#choose_video a').click(function(){
		play_video($(this).attr('href'));
		
		$('.choose_video a').removeClass('active');
		$('#choose_video').hide_content();
	})
	
	$('#send_to_friend form').validate({
		submitHandler: function(form){
			$(form).ajaxSubmit({
				url:		'process.php',
				success: 	send_to_friend_success,
				type: 'POST',
				dataType: 'json',
				data: {
					loc: window.location + ""
					}
			})

			$('button[type="submit"]', $(form))
				.find('span').html('Sending...').end()
				.removeClass('active')
				.removeClass('hover')
				.addClass('disabled')
				.attr('disabled', 'disabled');
		}
	})
	
	$('.button')
		.hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		)
		.mousedown(function(){
			$(this)
				.removeClass('hover')
				.addClass('active');
		})
		.mouseup(function(){
			$(this)
				.removeClass('hover')
				.removeClass('active');
		});
})

play_video = function(hash_tag)
{
	$('#choose_video tr').removeClass('active');
	$('#choose_video a[href="'+hash_tag+'"]').closest('tr').addClass('active');

	target = hash_tag.replace('#', 'v');
	createPlayer(target);
	
	pageTracker._trackEvent('Video', 'Play', hash_tag);
}

send_to_friend_success = function(data, $form, options)
{
	$form = $('#send_to_friend form');
	
	if(data.status == 'success')
	{
		$('#send_to_friend .inner').css({height: $form.height() + 12});	
		$form.fadeOut(1000, function(){
			$('.to_addresses input', $form).clearFields();
			$msg = $('<p class="success"></p>').html(data.message).hide();
			$form.parent().append($msg.fadeIn(1000, function(){
				setTimeout(function(){
						$msg.fadeOut(1000, function(){
							$(this).remove();
						});
						
						$('.send_to_friend a').removeClass('active');
						
						$form.closest('.content').hide_content(function(){
							$form.show();
						})
					}, 1000);
			}));

			value = $('button[type="submit"]', $form).attr('value');
			$('button[type="submit"]', $form)
				.find('span').html(value).end()
				.removeClass('disabled')
				.removeAttr('disabled');
		})
		
	}
	else
	{
		$('.error', $form).remove();
		$form.prepend('<p class="error">'+data.message+'</p>');

		$('button[type="submit"]', $form)
			.removeClass('disabled')
			.removeAttr('disabled');
	}

}

$.fn.show_content = function()
{
	$('.inner', $(this)).css({height: 'auto'});	
	return $(this).slideDown(speed);
}

$.fn.hide_content = function(callback)
{
	return $(this).slideUp(speed * .75, callback);
}

function get_url_parse(name)
{
	name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]"); 
	var regex = new RegExp("[\?&]"+name+"=([^&#]*)"); 
	var results = regex.exec(window.location.href); 
	
	return results == null ? "" : results[1];
}

function createPlayer(video_id) {
	$('#player').remove();
	$('#video').empty();
	
	video_id = getVideo(video_id);

	if(jQuery.type(video_id) === 'string')
	{

		var so = new SWFObject("http://vidego.316networks.com/templates/V0-200.swf", "player", "608", "342", "9", "#000000");
		so.addVariable("ts", "1257112867");
		so.addVariable("projectid", "52334");
		so.addVariable("projectuuid", "q4242e67");
		so.addVariable("programid", video_id);
		so.addVariable("programuuid", vidego_video_uuid);
		so.addVariable("loadingdomain", "http://vidego.316networks.com");
		so.addVariable("serviceURL", "http://vidego.316networks.com/services/index.php");
		so.addVariable("doResize", "false");
		so.addParam("allowFullScreen", "true");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("typePlayer", "vod");
		so.write("video");
	}
	else
	{
		$('<div id="player" />').appendTo('#video');
		var flashvars = {};
		flashvars.AutoPlay = "true";
		flashvars.ScrubMode = "advanced";
		flashvars.BufferTime = "1.5";
		flashvars.VideoFit = "automatic";
		flashvars.DefaultRatio = "1.777778";
		flashvars.LogoPosition = "topleft";
		flashvars.ColorBase = "#000000";
		flashvars.ColorControl = "#666666";
		flashvars.ColorHighlight = "#0099cc";
		flashvars.ColorFeature = "#0099cc";
		flashvars.File = "http://bitcast-g.bitgravity.com/passioncon/passioncitychurch/messages/"+video_id.high;
		flashvars.FileQuality2 = "http://bitcast-g.bitgravity.com/passioncon/passioncitychurch/messages/"+video_id.med;
		flashvars.FileQuality3 = "http://bitcast-g.bitgravity.com/passioncon/passioncitychurch/messages/"+video_id.low;
		flashvars.FileLabel = "High";
		flashvars.FileQuality2Label = "Medium";
		flashvars.FileQuality3Label = "Low";
		flashvars.FileBitrate = "1112";
		flashvars.FileQuality2Bitrate = "782";
		flashvars.FileQuality3Bitrate = "475";
		flashvars.DefaultLevel = "1";
		flashvars.Mode = "stream";
		flashvars.Volume = "1";
		var params = {};
		params.allowFullScreen = "true";
		params.allowScriptAccess = "always";
		var attributes = {};
		attributes.id = "player";
		swfobject.embedSWF(stablerelease, "player", "608", "362", "9.0.115", "http://bitcast-b.bitgravity.com/player/expressInstall.swf", flashvars, params, attributes);
		$('#player').append('<video preload="none" src="'+flashvars.FileQuality3+'" width="608" height="342" />');
	}

}

$.fn.link = function() {
	$('tbody tr', this).each(function(){
		$link = $('td:first a', this);
		var id = $link.attr('href');
		if(id && id != '')
		{
			$(this)
				.mouseover(function(){
					$(this).css('cursor', 'pointer');
					window.status = id;
				})
				.click(function(){
					window.location = id;
					play_video(id);
					$('.choose_video a').removeClass('active');
					$('#choose_video').hide_content();
				})
				.mouseout(function(){
					window.status = '';
				})
		}
	})
}

resizeBackgroundImage = function() {
	var size;
	
	if(screen.availHeight <= 1200) 	size = 1200;
	if(screen.availHeight <= 1050) 	size = 1050;
	if(screen.availHeight < 950) 		size = 950;
	if(screen.availHeight < 850)		size = 850;
	if(screen.availHeight < 768) 		size = 768;
	if(screen.availHeight < 600) 		size = 600;
	
	$('body').css({
		'background-image' : 'url(images/bg'+size+'.jpg)'
	})
	$('.background').hide();
}
