var currentVideoPlayerState = null;
var currentMusicPlayerState = null;
var playerBandeDemo = null;
var playerMusic = null;
var previousSectionId = '';

function zoomPicture(currentPicture)
{
	$("#photos").prepend('<div class="zoom"><div class="zoombackground"></div><div class="zoomimage" onclick="unZoom()"><img src="' + currentPicture.src + '" /><br />' + '<span class="red">' + currentPicture.title.substr(0,1) + '</span><div id="photoclose">Fermer X</div>' + currentPicture.title.substr(1) + '</div></div>');
	$('.zoom').animate({'opacity': 1}, 300);
}

function unZoom()
{
	if ($('.zoom').length)
	{
		$('.zoom').animate({'opacity': 0}, 300);
		setTimeout("$('.zoom').remove();", 300);
	}
}

function switchPhoto()
{
	$('.photoSwitcher').each(function () {
		if ($('img:visible', this).next().length)
		{
			$('img:visible', this).fadeOut(500, function() {$(this).next().fadeIn(510)});
		}
		else
		{
			$('img:visible', this).fadeOut(500, function() {$(this).prevAll(':last').fadeIn(510)});
		}
	});
	setTimeout('switchPhoto()', 3000);
}

//Functions needed by the video FLV plugin
//////////////////////////////////////////
	
function bandeDemoStateListener(obj) //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
{
	currentVideoPlayerState = obj.newstate;
}

function musicStateListener(obj) //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
{
	currentMusicPlayerState = obj.newstate;
}

//setting up the listener for getting the player object
var videolistenercreated = 0;

function playerReady(obj)
{
	if (obj.id == 'musicplayer')
	{
		playerMusic = document.getElementById(obj['id']);
		playerMusic.addModelListener("STATE", "musicStateListener");
	}
	else
	{
		playerBandeDemo = document.getElementById(obj['id']);
		playerBandeDemo.addModelListener("STATE", "bandeDemoStateListener");
		videolistenercreated = 1;
	}
}

//////////////////////////////////////////
function updateLeftTitle(title)
{
	$("#currentsection").html(title);
}

function updateNavImages(currentElement)
{
	setTimeout('updateLeftTitle("' + currentElement.attr('title') + '")', 300);
	$('#currentsection').animate({'opacity': 0}, 300).animate({'opacity': 1}, 300);

	if ((currentElement.attr('id') == 'films' && currentVideoPlayerState != 'PLAYING') || (currentElement.attr('id') != 'films' && currentVideoPlayerState == 'PLAYING'))
		playerBandeDemo.sendEvent('PLAY');

	if (((currentElement.attr('id') == 'marques' || currentElement.attr('id') == 'photos' || currentElement.attr('id') == 'news') && currentMusicPlayerState != 'PLAYING') ||
		(((previousSectionId == 'marques' || previousSectionId == 'photos' || previousSectionId == 'news') && currentElement.attr('id') != 'marques' && currentElement.attr('id') != 'photos' && currentElement.attr('id') != 'news') && currentMusicPlayerState == 'PLAYING'))
		playerMusic.sendEvent('PLAY');

	previousSectionId = currentElement.attr('id');
}

//onload
$(function() {
	setTimeout('$(".temp").hide("slow")', 3000);
	
	if ($('.photoSwitcher > img').length > 1)
		setTimeout('switchPhoto()', 3000);
	
	var anchor = location.hash;
	if (anchor == '')
		anchor = savedStartSection;

	switch(anchor)
	{
		case '#films':
			var startSection = 0;
		break;
		case '#news':
			var startSection = 1;
		break;
		case '#marques':
			var startSection = 2;
		break;
		case '#boutique':
			var startSection = 3;
		break;
		case '#contact':
			var startSection = 4;
		break;
		case '#photos':
			var startSection = 5;
		break;
	}
	
	if (anchor == '#films' || anchor == '')
	{
		var autoStartBandeDemo = true;
		var autostartMusic = false;
		previousSectionId = "films";
	}
	else if (anchor == '#photos' || anchor == '#marques' || anchor == '#news')
	{
		var autoStartBandeDemo = false;
		var autostartMusic = true;
		previousSectionId = "photos";
		updateLeftTitle($('#' + anchor.substr(1)).attr('title'));
	}
	else
	{
		var autoStartBandeDemo = false;
		var autostartMusic = false;
		previousSectionId = "contact";
		updateLeftTitle($('#' + anchor.substr(1)).attr('title'));
	}
	
	if (admin)
	{
		autoStartBandeDemo = false;
		autostartMusic = false;
		startSection = 6;
	}	
	
	//creating the video									
	var s1 = new SWFObject('video/player.swf','videoplayer','734','432','9','#000');
	s1.addParam('allowfullscreen','true');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('wmode','transparent');
	s1.addParam('autostart',autoStartBandeDemo);
	s1.addParam('flashvars','file=' + videofile + '&autostart=' + autoStartBandeDemo);
	s1.write('videocontainer');

	//creating the music									
	var s2 = new SWFObject('video/player.swf','musicplayer','10','10','9','#000');
	s2.addParam('allowfullscreen','true');
	s2.addParam('allowscriptaccess','always');
	s2.addParam('wmode','transparent');
	s2.addParam('autostart', autostartMusic);
	s2.addParam('flashvars','file=music.flv&repeat=always&autostart=' + autostartMusic);
	s2.write('musiccontainer');

	$("#carousel").jCarouselLite({
		circular: false,
		speed: 800,
		visible: 1,
		scrolling: 1,
		start: startSection,
		btnGo: ["#tofilms", "#tonews", "#tomarques", "#toboutique", "#tocontact", "#tophotos", "#toadmin"],
		beforeStart: function(a) {
			unZoom();
		},
		afterEnd: function(a) {
			updateNavImages(a);
		}
	});
});

function nextNews()
{
	$('.newstrio:first').animate({marginTop: parseInt($('.newstrio:first').css("margin-top")) - 411 + "px"}, 300);
	setTimeout("updateNewsNav()", 310);
}

function previousNews()
{
	$('.newstrio:first').animate({marginTop: parseInt($('.newstrio:first').css("margin-top")) + 411 + "px"}, 300);
	setTimeout("updateNewsNav()", 310);
}

function updateNewsNav()
{
	//alert(parseInt($('.newstrio:first').css("margin-top")));
	if (parseInt($('.newstrio:first').css("margin-top")) >= 0)
	{
		$('#previousnews').attr('onclick', '');
		$('#previousnews img').attr('src', 'images/nopreviousnews.jpg');
	}
	else
	{
		$('#previousnews').bind('click', function() {previousNews();});
		$('#previousnews img').attr('src', 'images/previousnews.jpg');
	}

	if (-parseInt($('.newstrio:first').css("margin-top")) >= parseInt($('.newstrio').length * 411 - 420))
	{
		$('#nextnews').attr('onclick', '');
		$('#nextnews img').attr('src', 'images/nonextnews.jpg');
	}
	else
	{
		$('#nextnews').bind('click', function() {nextNews()});
		$('#nextnews img').attr('src', 'images/nextnews.jpg');
	}
}

function telechargementPopup(newsId, newsmailtext, img)
{
	$('#newsmailtext').html(newsmailtext);
	$('#newsimage').attr('src', img);
	$('#telechargement').animate({'opacity': 0}, 0).show().animate({'opacity': 1}, 300);
	$('#newsid').attr('value', newsId);
}

function telechargementPopdown()
{
	$('#telechargement').animate({'opacity': 0}, 300);
	setTimeout("$('#telechargement').hide()", 301);
}
