/**
 * HKIFF Society Video Slideshow
 * Copyright (c) 2009 Vizztech
 */
function hkiff_indexvp_on_complete()
{	
	$('#slideshow-video-container').playNextMovie();
}

var hkiff_indexvp_init = false;
function hkiff_indexvp_on_ready()
{	
	if (!hkiff_indexvp_init)
	{
		$('#slideshow-video-container').onFlashReady();
		hkiff_indexvp_init = true;
	}
}

;(function($)
{
	var
		flashVideo,
		currentIndex = -1,
		initCountdown = 2,
		activateCount = 2,
		navButtons = [],
		paused = false,
		ctnrNavIndex = false,
		ctnrDescription = false;
		
	$.fn.extend({

		playNextMovie: function()
		{
			moveToNextMovie();
		},
		
		videoSlideShowReady: function()
		{
			return (initCountdown <= 0);
		},

		activateVideoSlideShow: function()
		{
			if (currentIndex != -1)
			{
				setIndex(currentIndex);
			}
			else
			{
				moveToNextMovie();
			}
		},

		deactivateVideoSlideShow: function()
		{
			flashVideo.stopVideo();
		},

		onDocumentReady: function()
		{
			initCountdown--;
			if (initCountdown == 0)
			{
				$(this).initIndexVideoSlideShow();
			}
		},

		onFlashReady: function()
		{
			initCountdown--;
			if (initCountdown == 0)
			{
				$(this).initIndexVideoSlideShow();
			}
		},

		initIndexVideoSlideShow: function() 
		{
			return this.each(function() {
	
				// buttons
				ctnrNavIndex = $('#slideshow-video-navindex');
				flashVideo = getFlashMovie('videoplayer');

				$('a', ctnrNavIndex).each(function(idx){
					var t = $(this);
					t.data('url', t.attr('href'));
					t.data('idx', idx);
					t.attr('href', '#');
					navButtons[idx] = $(this);
				}).click(navBtnClick);

				// activate description cycle
				ctnrDescription = $('#slideshow-video-info', this);

				ctnrDescription.cycle({
					speed: 'fast',
					after:  onTextAfter,
					timeout: 0
				});
				
				$('#slideshow-video-nav-prev').click(moveToPrevMovie);
				$('#slideshow-video-nav-next').click(moveToNextMovie);
				$('#slideshow-video-nav-pause').click(pauseClick);

				$('#slideshow-video-nav-mute').click(muteClick);
			});
		}
	});

	function getFlashMovie(movieName)
	{
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
     }

	function assignVideo(idx)
	{
		return flashVideo.loadVideo(navButtons[idx].data('url'));
	}

	function navBtnClick()
	{
		setIndex($(this).data('idx'));
		return false;
	}

	function setIndex(idx)
	{
		if (assignVideo(idx))
		{
			currentIndex = idx;
			highlightSelected();
			if (paused)
			{
				paused = false;
				updatePauseButton();
			}
		}
	}

	function moveToNextMovie()
	{
		var nextIdx = (currentIndex+1 >= navButtons.length)?0:currentIndex+1;
		setIndex(nextIdx);
		return false;
	}

	function moveToPrevMovie()
	{
		var nextIdx = (currentIndex-1 < 0)?navButtons.length-1:currentIndex-1;
		setIndex(nextIdx);
		return false;
	}

	function highlightSelected()
	{
		$('a', ctnrNavIndex).removeClass('active'); // clear highlight
		navButtons[currentIndex].addClass('active');

		if (ctnrDescription)
		{
			ctnrDescription.cycle(currentIndex);
		}
	}

	function onTextAfter(i, o, opt)
	{
		if (jQuery.browser.msie)
		{
			$('div', ctnrDescription).each(function()
			{
				this.style.removeAttribute('filter'); 
			});
		}
	}

	function updatePauseButton()
	{
		var o = $('img', $('#slideshow-video-nav-pause'));
		if (paused)
		{
			$('#slideshow-video-nav-pause').addClass('active');
		}
		else
		{
			$('#slideshow-video-nav-pause').removeClass('active');
		}
	}

	function pauseClick()
	{
		if (paused)
		{
			flashVideo.resumeVideo();
		}
		else
		{
			flashVideo.pauseVideo();
		}
		paused = !paused;
		updatePauseButton();
		return false;
	}

	function muteClick()
	{
		var img = $(this);
		var result = flashVideo.toggleVideoMute();

		if (result == 1) // not mute
		{
			img.removeClass('active');
		}
		else
		if (result == 2) // mute
		{
			img.addClass('active');
		}
		return false;
	}

})(jQuery);