var GBSelectedSection = '';
var GBRight = false;

function TitleOver(SectionName) {
	if (SectionName != '') {
		if (SectionName != GBSelectedSection) {
			var MenuImage = $(SectionName + 'Menu');	
			if (SectionName != 'Galerie') {var NewImageName = '/imgs/' + SectionName + 'ButtonOver.jpg';}
			else
			{var NewImageName = '/imgs/' + SectionName + 'ButtonOver.gif';}
			MenuImage.src = NewImageName;
		}
	}
}

function TitleOut(SectionName) {
	if (SectionName != '') {
		if (SectionName != GBSelectedSection) {
			var MenuImage = $(SectionName + 'Menu');	
			if (SectionName != 'Galerie') {var NewImageName = '/imgs/' + SectionName + 'Button.jpg';}		
			else {var NewImageName = '/imgs/' + SectionName + 'Button.gif';}
			MenuImage.src = NewImageName;
		}
	}
}

function ChangeSelectedMenu(SectionName) {
	var MenuImage = $(SectionName + 'Menu');
	if (SectionName != 'Galerie') {var NewImageName = '/imgs/' + SectionName + 'ButtonSelected.jpg';}
	else {var NewImageName = '/imgs/' + SectionName + 'ButtonSelected.gif';}
	OldSelection = GBSelectedSection;
	GBSelectedSection = SectionName;
	TitleOut(OldSelection);
	MenuImage.src = NewImageName;
}


function ChangeAndShowMainContent() {
	var DivTemplate = $(GBSelectedSection + 'ContentTemplate');
	var MainContent = $('MainContent');
	
	MainContent.innerHTML = DivTemplate.innerHTML;
	new Effect.Opacity(MainContent, {queue:'front', duration:0.5, from:0.0, to:1.0});
}

function UpdateMainDiv() {	
	var MainContent = $('MainContent');
	new Effect.Opacity(MainContent, {queue:'front', afterFinish: ChangeAndShowMainContent, duration:0.5, from:1.0, to:0.0});
}


function DoShowRight() {
		new Effect.Opacity('HomeSilhouette', {queue:'end', duration:0.3, from:1.0, to:0.0});			
		new Effect.Move('RightContentBackground', {queue:'end', x:298 , y:0, mode:'relative'});	
		new Effect.Opacity('ContactImage', {queue:'end', duration:0.1, from:0.0, to:1.0});			
		GBRight = true;
}

function UpdateRightContent() {
	var NewRightContent = $(GBSelectedSection + 'RightTemplate');
	if (NewRightContent != null) {
		$('RightContent').innerHTML = NewRightContent.innerHTML;
	}	
}

function UpdateAndShowRightContent() {
	UpdateRightContent();
	new Effect.Opacity('RightContent', {queue:'end', duration:0.5, from:0.0, to:1.0});
}

function UpdateMainDivAndShowRight() {
	var MainContent = $('MainContent');
	
	if(!GBRight) {
	
		UpdateRightContent();	

		new Effect.Opacity(MainContent, {queue:'front', afterFinish: ChangeAndShowMainContent, duration:0.5, from:1.0, to:0.0});
		new Effect.Opacity('HomeSilhouette', {queue:'end', duration:0.3, from:1.0, to:0.0});			
		new Effect.Move('RightContentBackground', {queue:'end', x:298 , y:0, mode:'relative'});	
		new Effect.Opacity('ContactImage', {queue:'end', duration:0.1, from:0.0, to:1.0});			
		GBRight = true;
	} else {
		new Effect.Opacity(MainContent, {queue:'front', afterFinish: ChangeAndShowMainContent, duration:0.5, from:1.0, to:0.0});
		new Effect.Opacity('RightContent', {queue:'end', afterFinish: UpdateAndShowRightContent, duration:0.5, from:1.0, to:0.0});
	}
}

function HideRightAndUpdateDivs() {
	if (GBRight) {		
		new Effect.Opacity('ContactImage', {queue:'front', duration:0.1, from:1.0, to:0.0});
		new Effect.Move('RightContentBackground', { queue:'end', afterFinish:UpdateMainDiv, x:-298 , y:0, mode:'relative'});
		new Effect.Opacity('HomeSilhouette', {queue:'end', duration:0.3, from:0.0, to:1.0});
		GBRight = false;
	} else {
		UpdateMainDiv();
	}
}

GBBioName = '';
function DoUpdateBio() {
	$('RightContent').innerHTML  = $(GBBioName + 'Bio').innerHTML;
	new Effect.Opacity('RightContent', {queue:'front', duration:0.5, from:0.0, to:1.0});
}

function ShowBio(Name) {
	if (Name != GBBioName) {
		if(!GBRight) {			
			$('RightContent').innerHTML  = $(Name + 'Bio').innerHTML;
			DoShowRight();
		} else {
			GBBioName = Name;
			new Effect.Opacity('RightContent', {queue:'front', afterFinish: DoUpdateBio, duration:0.5, from:1.0, to:0.0});
		}
	}
}

function ClearStatusBar() {
	$('StatusBar').innerHTML = "";
}

function Report(s) {
	$('StatusBar').innerHTML = s;
}

function SelectSection(SectionName, ShowRight) {
	ClearStatusBar();
	if (GBSelectedSection != SectionName) {				
		ChangeSelectedMenu(SectionName);
		if (ShowRight) {
			UpdateMainDivAndShowRight();			
		} else {
			HideRightAndUpdateDivs();
		}
	}
}

function FirstDisplayHome() {
	$('MainContent').innerHTML = $('HomeContentTemplate').innerHTML;
	GBSelectedSection = 'Home';
}

function ShowHome() {
	SelectSection('Home', false);
}

function ShowConcept() {
	SelectSection('Concept', false);
}

function ShowPlanning() {
	SelectSection('Planning', false);
}

function ShowEquipe() {
	SelectSection('Equipe', false);
}

function ShowContacts() {
	SelectSection('Contacts', true);
}

function ShowCredits() {
	SelectSection('Credits', false);
}

function ShowGalerie() {
	SelectSection('Galerie', false);
}

function RestoreColor(e) {
	e.style.backgroundColor = 'white';
	e.value = '';
	ClearStatusBar();
}

function CheckEmail(Email) {
	if (Email != '') {
		var MailRegexp =  /^[a-zA-Z0-9_-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}$/;
		return (MailRegexp.exec(Email) != null);
	}
	return false;
}

function CheckField(AttributeName, DefaultText) {	
	var e = $(AttributeName);
	var GoodToGo = true;
	if (e.value == '' || e.value == DefaultText) {		
		e.style.backgroundColor = 'rgb(166,222,50)';
		GoodToGo = false;
	} else {
		e.style.backgroundColor = 'white';
	}
	return GoodToGo;
}

function CheckAndSubmitContactForm() {
	var GoodToGo = true;
	
	GoodToGo = CheckField('Email', 'Votre email*');
	GoodToGo = CheckField('Name', 'Votre nom*') && GoodToGo;
	
	if (GoodToGo) {		
		if (CheckEmail($F('Email'))) {
			new Ajax.Updater('StatusBar', 'SendMail.php', {parameters:{Email:$F('Email'), Name:$F('Name'), Phone:$F('Phone'), Comment:$F('Comment')}} );
		} else {
			Email.style.backgroundColor = 'rgb(166,222,50)';
			Report("Veuillez vérifier l'adresse email");
		}
	} else {
		Report('Veuillez compléter les champs obligatoires colorés en vert');
	}
}
