var currentVideoPlayerState = '';
var playerBandeDemo = null;
var previousSectionId = '';
var scrollingPhotoCarousel = false;
var totalPhotosWidth = 0;
var slideshowPlaying = 0;

function updateNavImages(currentElement)
{
	switch(currentElement.attr('id'))
	{
		case 'references':
			setTimeout("$('#imgnext').attr('src', 'images/versfilmsd.png')", 300);
			
			$('#imgprevious').animate({'opacity': 0}, 300);
			$('#imgnext').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			
			if (previousSectionId == 'bandedemo')
			{
				setTimeout("$('#imgcontact').attr('src', 'images/contactgris.png')", 300);
				$('#imgcontact').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			}
			break;
		
		case 'bandedemo':
			setTimeout("$('#imgprevious').attr('src', 'images/versreferencesg.png')", 300);
			setTimeout("$('#imgnext').attr('src', 'images/versphotosd.png')", 300);
			setTimeout("$('#imgcontact').attr('src', 'images/contactrouge.png')", 300);
			
			$('#imgprevious').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			$('#imgnext').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			$('#imgcontact').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			
			try { playerBandeDemo.sendEvent('PLAY') } catch(e) {}

			break;
		
		case 'photos1':
			setTimeout("$('#imgprevious').attr('src', 'images/versfilmsg.png')", 300);
			setTimeout("$('#imgnext').attr('src', 'images/versphotosplusd.png')", 300);
			
			$('#imgprevious').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			$('#imgnext').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);

			if (previousSectionId == 'bandedemo')
			{
				setTimeout("$('#imgcontact').attr('src', 'images/contactgris.png')", 300);
				$('#imgcontact').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			}
			break;
		
		case 'photos2':
			setTimeout("$('#imgprevious').attr('src', 'images/versphotosg.png')", 300);
			
			$('#imgprevious').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);
			$('#imgnext').animate({'opacity': 0}, 300);
			break;
	}
	
	previousSectionId = currentElement.attr('id');
}

function unZoom()
{
	if ($('#zoom').length)
	{
		$('#zoom').animate({'opacity': 0}, 300, function () { $(this).remove() });
		$('#carousel, #contact').animate({'opacity': 1}, 300);
	}
}

//Functions needed by the video FLV plugin
//////////////////////////////////////////
	
	function bandeDemoStateListener(obj) //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	{
		currentVideoPlayerState = obj.newstate;
	}
	
	//setting up the listener for getting the player object
	function playerReady(obj)
	{
		playerBandeDemo = document.getElementById(obj['id']);
		playerBandeDemo.addModelListener("STATE", "bandeDemoStateListener");
	}
	
//////////////////////////////////////////


//onload
$(function() {
	
	var anchor = location.hash;
	if (anchor == "#photos")
	{
		var autoStartBandeDemo = false;
		var startSection = 2;
		$('#imgcontact').attr('src', 'images/contactgris.png');
		$('#imgprevious').attr('src', 'images/versfilmsg.png');
		$('#imgnext').attr('src', 'images/versphotosplusd.png');
		previousSectionId = "photos";
	}	
	else
	{
		var autoStartBandeDemo = true;
		var startSection = 1;
		previousSectionId = "bandedemo";
	}	
	
	//creating the video									
	var s1 = new SWFObject('video/player.swf','videocultureplayer','600','468','9','#000');
	s1.addParam('allowfullscreen','true');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('wmode','transparent');
	s1.addParam('autostart',autoStartBandeDemo);
	s1.addParam('flashvars','file=bandedemo.flv&autostart=' + autoStartBandeDemo);
	s1.write('videocontainer');

	$("#carousel").jCarouselLite({
		btnPrev: ".previous",
		btnNext: ".next",
		circular: false,
		visible: 1,
		scrolling: 1,
		start: startSection,
		btnGo: [".toreferences", ".tofilms"],
		beforeStart: function(a) {
			if (currentVideoPlayerState == 'PLAYING')
				playerBandeDemo.sendEvent('PLAY');

			slideshowPlaying = 0;
			unZoom();
		},
		afterEnd: function(a) {
			updateNavImages(a);
		}
	});
	
	$('.leftnav, .rightnav').animate({'opacity' : 0}, 0);
	
	$('.seriecontainer').hover(function () {
		$(this).find('.leftnav, .rightnav').stop().animate({'opacity' : 1}, 200);
	}, function () {
		$(this).find('.leftnav, .rightnav').stop().animate({'opacity' : 0}, 200);
	});
	
	$('.leftnav').click(function () {
		scrollingPhotoCarousel = $(this).next().find('div');
		scrollRight();
	});
	
	$('.rightnav').click(function () {
		scrollingPhotoCarousel = $(this).prev().find('div');
		scrollLeft();
	});
	
	$('.seriescroller img').clone()
		.appendTo('#imagePreloader').each(function () { $(this).attr('src', $(this).attr('src').replace('small/', 'megasmall/'))})
		.clone()
		.appendTo('#imagePreloader').each(function () { $(this).attr('src', $(this).attr('src').replace('megasmall/', ''))});

	$('.seriescroller img').click(function () {
		$("#container").append('<div id="zoom"><div id="photoscanvas"></div><div id="thumbnailscontainer"><a id="playslideshow" href="#"></a><div id="thumbnails"></div><a id="closeslideshow" href="#"></a></div><img id="titrefullscreen" /></div>');
		$(this).parent().children().clone().appendTo('#photoscanvas').clone().appendTo('#thumbnails');
		$('#photoscanvas img').each(function () {
			$(this).attr('src', $(this).attr('src').replace('small/', ''));
		});
		$('#thumbnails img').each(function () {
			$(this).attr('src', $(this).attr('src').replace('small', 'megasmall'));
		});
		$('#zoom').animate({'opacity': 1}, 300);
		$('#carousel, #contact').animate({'opacity': 0}, 700);
		$('#closeslideshow').click(function() {
			unZoom();
		});
		$('#playslideshow').click(function() {
			slideshow();
		});
		$('#thumbnails img').click(function() {
			slideshowPlaying = 0;
			slidePhoto($(this).attr('src'));
		});
		$('#photoscanvas img[src|="' + $(this).attr('src').replace('small/', '') + '"]').animate({'opacity' : 0}, 0).show().animate({'opacity' : 1}, 300);
		$('#thumbnailscontainer').animate({'margin-left': 0}, 600);
		$('#titrefullscreen').attr('src', $(this).parent().prev().attr('src'));
	});
});

function scrollRight()
{
	if (parseInt(scrollingPhotoCarousel.css('margin-left')) < 0)
	{
		scrollingPhotoCarousel.parent().next().css('background-image', 'url(images/rightnav.png)');

		scrollingPhotoCarousel.stop(true, true);
		if (parseInt(scrollingPhotoCarousel.css('margin-left')) == 0)
			scrollingPhotoCarousel.parent().prev().css({'background' : 'url(images/leftnavs.png)'});
		else
			scrollingPhotoCarousel.animate({'margin-left' : '+=175px'}, 200, function() {
				if (parseInt(scrollingPhotoCarousel.css('margin-left')) == 0)
					scrollingPhotoCarousel.parent().prev().css({'background' : 'url(images/leftnavs.png)'});
			});
	}
}

function scrollLeft()
{
	totalPhotosWidth = ($(scrollingPhotoCarousel).find('img').size() - 1) * 16;
	$(scrollingPhotoCarousel).find('img').each(function () { totalPhotosWidth += $(this).width() });

	if (parseInt(scrollingPhotoCarousel.css('margin-left')) > -totalPhotosWidth + 510)
	{
		scrollingPhotoCarousel.parent().prev().css('background-image', 'url(images/leftnav.png)');
		
		scrollingPhotoCarousel.stop(true, true);
		if (parseInt(scrollingPhotoCarousel.css('margin-left')) <= -totalPhotosWidth + 520)
			scrollingPhotoCarousel.parent().next().css('background-image', 'url(images/rightnavs.png)');
		else
		scrollingPhotoCarousel.animate({'margin-left' : '-=175px'}, 200, function() {
			if (parseInt(scrollingPhotoCarousel.css('margin-left')) <= -totalPhotosWidth + 520)
				scrollingPhotoCarousel.parent().next().css('background-image', 'url(images/rightnavs.png)');
		});
	}
}

function slidePhoto(src)
{
	$('#photoscanvas img:visible').stop().animate({'left' : '-500px', 'opacity' : 0}, function() { $(this).hide() });
	$('#photoscanvas img[src|="' + src.replace('megasmall/', '') + '"]').stop().animate({'left' : '15px', 'opacity' : 0}, 0).show().animate({'opacity' : 1}, 1000);
}

function slideshow()
{
	slidePhoto($('#photoscanvas img').first().attr('src'));
	slideshowPlaying = 1;
	setTimeout('next()', 4000);
}

function next()
{
	if ($('#photoscanvas img:visible').next().size() && slideshowPlaying)
	{
		slidePhoto($('#photoscanvas img:visible').next().attr('src'));
		setTimeout('next()', 4000)
	}
}
