$(document).ready(function()
{
	$('ul.newscategories li ul').hide();
	
	var active_news_category = $('ul.newscategories li.active');
	if(active_news_category.length)
	{
		active_news_category.parent().show();
	}

	$('ul.newscategories > li > a').click(function()
	{
		$('ul.newscategories li ul').hide();
		$(this.parentNode).find("ul").show();
		
		return false;
	});
	
	
	
	if($('.video_container').length)
	{
		var i = 0;
		var players = [];
		
		$('.video_container').each(function()
		{
        	players[i] = new VideoPlayer(
	        	$(this).attr('id'), 
	        	'start', 
	        	{
	        		basePath: base_path,
	        		debug: false,
	        		googleAnalytics: false,
					playlistPosition: "after",
					playlistElementWidth: 150,
					showInfoOnHover: true,
					noUrls: ($('.video_container').length > 1),
					hideScrubberWhilePlaying: true
	        	}
	        );

			if($('.video_container').length == 1)
			{
				var selector = '#header, #docu_content';

				var whilePlaying = function(player)
				{
					if(!player.isPaused())
					{
						$(selector).animate({
							'opacity': .4
						}, { queue: false, duration: 2000 });
					}
				}

				var notPlaying = function()
				{
					$(selector).animate({
						'opacity': 1
					}, { queue: false, duration: 1000 });
				}

				players[i].bindEvent('videoLoading', whilePlaying);
				players[i].bindEvent('videoPlay', whilePlaying);
				players[i].bindEvent('videoPause', notPlaying);
				players[i].bindEvent('playlistEnd', notPlaying);

				$(selector).mouseover(notPlaying);

				$(selector).mouseout(function()
				{
					whilePlaying(players[0]);
				});
			}
			
			
			i++;
		});
	}
	
	slideshow.init();
})


var slideshow = {
	
	'imageCount': 0,
	'currentImage': 0,
	'speed': 5000,
	'transition': 1000,
	'showNextTimeout': false,
	
	'init': function()
	{
		// check if we have a slideshow on the page
		if($('ul.slideshow').length)
		{
			// get image count
			$('ul.slideshow li').hide();
			$('#slideshow_sidebar').hide();
			
			$('ul.slideshow li:first img').one('load', function()
			{
				$('ul.slideshow').show();
				$(this.parentNode).fadeIn();
			}).each(function()
			{
				if(this.complete) $(this).load();
			});
			
			var img = document.createElement("img");
			img.src = "/modules/videos/media/transparent_black_50.png";
			
			$(img).one('load', function()
			{
				$('#slideshow_sidebar').fadeIn();
			}).each(function()
			{
				if(this.complete) $(this).load();
			});
			
			this.imageCount = $('ul.slideshow li').length;
			
			// only start if we have more than one image
			if(this.imageCount > 1)
			{
				// show next image on click
				$('ul.slideshow li').click(function()
				{
					//clearTimeout(slideshow.showNextTimeout);
					slideshow.showNextImage();
				})
				
				// start slideshow
				slideshow.showNextTimeout = setTimeout(slideshow.showNextImage, slideshow.speed);
			}
		}
	},
	
	
	'start': function()
	{
		
	},
	
	'showNextImage': function()
	{
		// clear timeout to prevent overlapping of click event and timer
		clearTimeout(slideshow.showNextTimeout);
		
		// calculate next image
		var next = (slideshow.currentImage + 1 < slideshow.imageCount) ? slideshow.currentImage + 1 : 0;
		
		// fade in/out images
		$('ul.slideshow li').eq(next).fadeIn(slideshow.transition);
		$('ul.slideshow li').eq(slideshow.currentImage).fadeOut(slideshow.transition);
		
		// set current image and fire next timeout
		slideshow.currentImage = next;
		slideshow.showNextTimeout = setTimeout(slideshow.showNextImage, slideshow.speed);
	}
}
