/*----------------------------------------------------------------*/
/*	© 2009 Hal Lindsey Media Ministries
/*	JavaScript functions for The Hal Lindsey Report: Videos
/*----------------------------------------------------------------*/

function loadPlaylist(list, override){
	$('#videos').fadeOut('medium', function(){
		$('#videos').load('/includes/ajax/ajax_videos.php', {type:'playlist',list:list}, function(){
			$('#videos').fadeIn();
			resetSlider();
			if(!override) loadVideo();
		});
	});
}

function loadCategory(ctgID){
	$('#videos').fadeOut('medium', function(){
		$('#videos').load('/includes/ajax/ajax_videos.php', {type:'category',category:ctgID}, function(){
			$('#videos').fadeIn();
			resetSlider();
			loadVideo();
		});
	});
}

function loadVideo(i){
	if($('#videos .video').length > 0){
		var video = (i != null && i != "") ? $('#video'+i) : $('.video:first');
		var autoPlay = (i != null && i != "") ? true : false;
		var videoID = $(video).find('input.videoID').val();
		var title = $(video).find('.title').text();
		var hasAudio = $(video).find('input.hasAudio').val();
		hasAudio = (hasAudio == '1') ? true : false;
		playVideo(videoID, title, autoPlay, hasAudio);
	}else{
		$('#current_video .title').fadeOut();
		$('#the_video').html('');	
	}
}

function playVideo(id, title, autoPlay, hasAudio){
	updateViewCount(id);
	$('#current_video .title').fadeOut('medium', function(){
		$('#current_video .title').html(title);
		$('#current_video .title').fadeIn();
		var soVideo = new SWFObject("/swf/HL_video_player.swf", "HL_video_player", "640", "360", "8", "", true);
		soVideo.addParam("base", "/");
		soVideo.addParam("menu", "false");
		soVideo.addParam("wmode", "transparent");
		soVideo.addParam("allowFullScreen", "true");
		soVideo.addVariable("vidfile", ""+id);
		if(autoPlay) soVideo.addVariable("do_autoPlay","yes");
		soVideo.write("the_video");
		
		//if(typeof(console) != null) console.log(hasAudio);
		if(hasAudio){
			$('#audio_download').show();
			$('#audio_download a').attr('href','/download.php?video='+id);
		}else{
			$('#audio_download').hide();
			$('#audio_download a').attr('href','#');
		}
	});
}

function updateViewCount(videoID){
	$.ajax({
		type : "POST",
		url : "/includes/ajax/ajax_views.php",
		data : "type=video&id="+videoID,
		cache : false
	});
}

function resetSlider(){
	var visibleRange = $('#videos_container').innerHeight();
	var slideRange = $('#videos').innerHeight() - visibleRange;
	if(slideRange <= 0){
		/* Hide the Slider if there's not enough rows to warrant the use of scrolling */
		$('#video_slider_wrapper').hide();
	}else{
		/* Adjust Slider Handle height based on length of rows */
		var sliderHeightFactor = Math.floor($('#videos .video').length/4) - 2;
		//var currentHeight = parseInt($('#video_slider_handle').css('height').replace("px",""));
		var newHeight = 200 - (10 * sliderHeightFactor);
		if(newHeight < 40) newHeight = 40;
		$('#video_slider_handle').css('height', newHeight);
		
		/* Initialize the sliding functionality... */
		$('#video_slider').attr('slideRange',slideRange);
		$('#video_slider').slider({
			axis: 'vertical',
			handle: '#video_slider_handle',
			startValue: 0,
			slide: function(event, ui){
				var top = Math.ceil(parseInt($('#video_slider').attr('slideRange'))*(ui.value/100));
				$('#videos').css("top", (top * -1));
			},
			stop: function(event, ui){
				var top = Math.ceil(parseInt($('#video_slider').attr('slideRange'))*(ui.value/100));
				$('#videos').animate({'top' : top * -1}, 300);
			}
		});
		$('#video_slider_wrapper').show();
	}
}

function loadArchives(page){
	var month = $('#year').val() + "-" + $('#month').val();
	var category = $('#category').val();
	if(page == null) page = 1;
	$('#loader').fadeIn();
	$('#archives_videos').fadeOut('medium', function(){
		$('#archives_videos').load("/includes/ajax/ajax_videos_archive.php", {month:month, category:category, page:page}, function(){
			$('#loader').fadeOut();
			$('#archives_videos').fadeIn();
		});
	});
}
