BDAddPlane('contents', 'showNewContent');
BDAddPlane('newsletterSave', 'nlDidSave_');

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

$(window).load(function() {
	
	if ( showIntro ) {
		animIntro();
	} else {
		changeToPage_(selectedMenuItem, false);
	}
	
});

function animIntro()
{
	
	$('#logo').css({ 'opacity' : '0.0' });
	$('#accueil').css({ 'top' : '-415px' });
	$('#inscription').css({ 'top' : '-455px' });
	$('#programme').css({ 'top' : '-500px' });
	$('#agenda').css({ 'top' : '-500px' });
	$('#newsletter').css({ 'top' : '-555px' });
	$('#chroniques').css({ 'top' : '-597px' });
	$('#contact').css({ 'top' : '-597px' });
	$('#partenaires').css({ 'top' : '-597px' });
	$('#underPage').css({ 'left' : '314px', 'opacity' : '0.0' });
	
	$('#underPage #poles img').css({ 'opacity' : '0.0' });
		
	$('#underPage').animate({ 'opacity' : '1.0' }, 1500, function () {
		
		$('#underPage #poles #theatre').css({ 'top' : '219px', 'left' : '51px' }).animate({ 'top' : '207px', 'left' : '4px' }, 500);
		$('#underPage #poles #theatre img:first').css({ 'width' : '0px' }).animate({ 'opacity' : '1.0', 'width' : '95px' },500, function () {
			
			$('#underPage #poles #cinema').css({ 'top' : '224px', 'left' : '138px' }).animate({ 'top' : '213px', 'left' : '90px' }, 500);
			$('#underPage #poles #cinema img:first').css({ 'width' : '0px' }).animate({ 'opacity' : '1.0', 'width' : '97px' }, 500, function () {
				
				$('#underPage #poles #photo').css({ 'top' : '219px', 'left' : '237px' }).animate({ 'top' : '208px', 'left' : '180px' }, 500);
				$('#underPage #poles #photo img:first').css({ 'width' : '0px' }).animate({ 'opacity' : '1.0', 'width' : '114px' }, 500, function () {
					
					$('#underPage #poles #danse').css({ 'top' : '222px', 'left' : '329px' }).animate({ 'top' : '210px', 'left' : '286px' }, 500);
					$('#underPage #poles #danse img:first').css({ 'width' : '0px' }).animate({ 'opacity' : '1.0', 'width' : '80px' }, 500).delay(1500, function () {
						
						$('#underPage').animate({ 'left' : '470px' }, 500, function () {
							$(this).animate({ 'left' : '460px' }, 150);
						});
						
						$('#contact').animate({ 'top' : '299px' }, 300);
						$('#partenaires').animate({ 'top' : '341px' }, 330);
						$('#chroniques').animate({ 'top' : '238px' }, 350);
						$('#newsletter').animate({ 'top' : '196px' }, 400);
						$('#agenda').animate({ 'top' : '155px' }, 450);
						$('#programme').animate({ 'top' : '100px' }, 500);
						$('#inscription').animate({ 'top' : '55px' }, 550)
						$('#accueil').animate({ 'top' : '15px' }, 600).delay(1500, function() {
							changeToPage_(selectedMenuItem, false);
						});
						
					})
					
				});
				
			});
			
		});
			
	});
	
}

var warmed = false;
function showPage_(pageId)
{
	
	var subMenuItem = null;
	if ( pageId.indexOf('-') > -1 ) {
		var t = pageId.split('-');
		subMenuItem = pageId;
		pageId = t[0];
	}
	
	var sameSubMenu = false;
	if ( warmed && subMenuItem ) {
		var sp = selectedMenuItem.split('-');
		if ( sp[0] == pageId ) {
			sameSubMenu = true;
		}
	}
	
	if ( !sameSubMenu ) {
		
		$('#menu .subMenu').each(function() {
		
			var sub = '#' + $(this).attr('id') + 'M';
			$(sub + ' a.current').removeClass('current');
			if ( $(sub).css('marginTop') == '0px' ) {
				$(sub).addClass('animating').animate({ 'marginTop' : ( '-' + $(this).height().toString() + 'px' ) }, 0, function() {
					$(this).removeClass('animating');
				});
			}
		
		});
	
		$('#accueil').animate({ 'top' : '15px' }, 200);
		$('#inscription').animate({ 'top' : '55px' }, 200);
		$('#programme').animate({ 'top' : '100px' }, 200);
		$('#agenda').animate({ 'top' : '155px' }, 200);
		$('#newsletter').animate({ 'top' : '196px' }, 200);
		$('#chroniques').animate({ 'top' : '238px' }, 200);
		$('#contact').animate({ 'top' : '299px' }, 200);
		$('#partenaires').animate({ 'top' : '341px' }, 200);
		
		var subAdd = 0;
		var sub = null;
		$('#' + pageId + ' div.subMenu:first').each(function() {
			sub = '#' + $(this).attr('id') + 'M';
			if ( $(sub).css('marginTop') == '0px' ) {
				subAdd = $(this).height();	
			} else {
				subAdd = Math.abs(parseInt($(sub).css('marginTop')));
			}
			if ( subMenuItem ) {
				$(sub + ' #' + subMenuItem).addClass('current');
			}
			$(sub).animate({ 'marginTop' : ( '0px' ) }, 400);
		});
	
		switch ( pageId ) {
		
			case 'accueil':
			$('#inscription').animate({ 'top' : (70 + subAdd).toString() + 'px' }, 200);
			$('#programme').animate({ 'top' : (115 + subAdd).toString() + 'px' }, 200);
			$('#agenda').animate({ 'top' : (170 + subAdd).toString() + 'px' }, 200);
			$('#newsletter').animate({ 'top' : (211 + subAdd).toString() + 'px' }, 200);
			$('#chroniques').animate({ 'top' : (253 + subAdd).toString() + 'px' }, 200);
			$('#contact').animate({ 'top' : (314 + subAdd).toString() + 'px' }, 200);
			$('#partenaires').animate({ 'top' : (356 + subAdd).toString() + 'px' }, 200);
			$('body').animate({ 'backgroundColor' : '#38468f' }, 500);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-05.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-05.png\')' });
			break;
		
			case 'inscription':
			$('#accueil').animate({ 'top' : '0px' }, 200);
			$('#programme').animate({ 'top' : (115 + subAdd).toString() + 'px' }, 200);
			$('#agenda').animate({ 'top' : (170 + subAdd).toString() + 'px' }, 200);
			$('#newsletter').animate({ 'top' : (211 + subAdd).toString() + 'px' }, 200);
			$('#chroniques').animate({ 'top' : (253 + subAdd).toString() + 'px' }, 200);
			$('#contact').animate({ 'top' : (314 + subAdd).toString() + 'px' }, 200);
			$('#partenaires').animate({ 'top' : (356 + subAdd).toString() + 'px' }, 200);
			$('body').animate({ 'backgroundColor' : '#f17c00' }, 500);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-01.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-01.png\')' });
			break;
		
			case 'programme':
			$('#accueil').animate({ 'top' : '0px' }, 200);
			$('#inscription').animate({ 'top' : '40px' }, 200);
			$('#agenda').animate({ 'top' : (170 + subAdd).toString() + 'px' }, 200);
			$('#newsletter').animate({ 'top' : (211 + subAdd).toString() + 'px' }, 200);
			$('#chroniques').animate({ 'top' : (253 + subAdd).toString() + 'px' }, 200);
			$('#contact').animate({ 'top' : (314 + subAdd).toString() + 'px' }, 200);
			$('#partenaires').animate({ 'top' : (356 + subAdd).toString() + 'px' }, 200);
			$('body').animate({ 'backgroundColor' : '#b5007d' }, 1000);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-02.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-02.png\')' });
			break;
		
			case 'agenda':
			$('#accueil').animate({ 'top' : '0px' }, 200);
			$('#inscription').animate({ 'top' : '40px' }, 200);
			$('#programme').animate({ 'top' : '85px' }, 200);
			$('#newsletter').animate({ 'top' : (211 + subAdd).toString() + 'px' }, 200);
			$('#chroniques').animate({ 'top' : (253 + subAdd).toString() + 'px' }, 200);
			$('#contact').animate({ 'top' : (314 + subAdd).toString() + 'px' }, 200);
			$('#partenaires').animate({ 'top' : (356 + subAdd).toString() + 'px' }, 200);
			$('body').animate({ 'backgroundColor' : '#ffcc00' }, 500);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-06.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-06.png\')' });
			break;
		
			case 'newsletter':
			$('#accueil').animate({ 'top' : '0px' }, 200);
			$('#inscription').animate({ 'top' : '40px' }, 200);
			$('#programme').animate({ 'top' : '85px' }, 200);
			$('#agenda').animate({ 'top' : '140px' }, 200);
			$('#chroniques').animate({ 'top' : (253 + subAdd).toString() + 'px' }, 200);
			$('#contact').animate({ 'top' : (314 + subAdd).toString() + 'px' }, 200);
			$('body').animate({ 'backgroundColor' : '#bbdf00' }, 500);
			$('#partenaires').animate({ 'top' : (356 + subAdd).toString() + 'px' }, 200);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-03.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-03.png\')' });
			break;
		
			case 'chroniques':
			$('#accueil').animate({ 'top' : '0px' }, 200);
			$('#inscription').animate({ 'top' : '40px' }, 200);
			$('#programme').animate({ 'top' : '85px' }, 200);
			$('#agenda').animate({ 'top' : '140px' }, 200);
			$('#newsletter').animate({ 'top' : '181px' }, 200);
			$('#contact').animate({ 'top' : (314 + subAdd).toString() + 'px' }, 200);
			$('body').animate({ 'backgroundColor' : '#4b9df2' }, 500);
			$('#partenaires').animate({ 'top' : (356 + subAdd).toString() + 'px' }, 200);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-04.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-01.png\')' });
			break;
			
			case 'contact':
			$('#accueil').animate({ 'top' : '0px' }, 200);
			$('#inscription').animate({ 'top' : '40px' }, 200);
			$('#programme').animate({ 'top' : '85px' }, 200);
			$('#agenda').animate({ 'top' : '140px' }, 200);
			$('#newsletter').animate({ 'top' : '181px' }, 200);
			$('#chroniques').animate({ 'top' : '223px' }, 200);
			$('#partenaires').animate({ 'top' : (356 + subAdd).toString() + 'px' }, 200);
			$('body').animate({ 'backgroundColor' : '#38468f' }, 500);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-04.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-04.png\')' });
			break;
			
			case 'partenaires':
			$('#accueil').animate({ 'top' : '0px' }, 200);
			$('#inscription').animate({ 'top' : '40px' }, 200);
			$('#programme').animate({ 'top' : '85px' }, 200);
			$('#agenda').animate({ 'top' : '140px' }, 200);
			$('#newsletter').animate({ 'top' : '181px' }, 200);
			$('#chroniques').animate({ 'top' : '223px' }, 200);
			$('#contact').animate({ 'top' : '284px'}, 200);
			$('body').animate({ 'backgroundColor' : '#ba0000' }, 500);
			$('#topLine').css({ 'backgroundImage' : 'url(\'/images/top-04.png\')' });
			$('#bottomLine').css({ 'backgroundImage' : 'url(\'/images/bottom-04.png\')' });
			break;
			
		}
		
		warmed = true;
		
	} else {
		
		$('#menu a.current').removeClass('current');
		$('#' + pageId + ' div.subMenu:first').each(function() {
			sub = '#' + $(this).attr('id') + 'M';
			$(sub + ' #' + subMenuItem).addClass('current');
		});
		
	}
	
	$('#page').animate({ 'opacity' : '1.0' }, 200, function () {
		$('#underPage #bigLogo img:first').attr('src', '/images/logo-big-blur-onWhite.png');
		$('#poles').css({ 'opacity' : '0.0' });
		$('#underPage').animate({ 'opacity' : '0.1' }, 150);
		$('#logo').animate({ 'opacity' : '1.0' }, 200);
	});
	
	if ( subMenuItem ) {
		selectedMenuItem = subMenuItem;
	} else {
		selectedMenuItem = pageId;
	}
	
}

function changeToPage_(pageId, requestContents)
{
	
	if ( requestContents !== undefined && requestContents ) {
		xmlreqGET('/AJAX/' + pageId + '/');
	} else {
		showPage_(pageId);
	}
	
	return false;
	
}

function showNewContent(someXML)
{
	
	var contentsId = someXML.getElementsByTagName('contentsId');
	if ( contentsId.length < 1 ) { return; };
	contentsId = contentsId[0].firstChild.nodeValue;
	
	var newContents = someXML.getElementsByTagName('contents');
	if ( newContents.length < 1 ) { return; };
	newContents = newContents[0].firstChild.nodeValue;
	
	showPage_(contentsId);
	$('#page').html(newContents);
	
}

var saved = false;
function nlSave()
{
	
	if ( !saved ) {
	
		email = document.getElementById('nlEmail').value;
		xmlreqGET('/AJAX/nlSave/' + URLencode(email) + '/');
	
		$('#nlEmail').animate({ 'backgroundColor' : '#fff' }, 150);
		
	}
	
}

function nlDidSave_(someXML)
{
	
	var result = someXML.getElementsByTagName('contents');
	if ( result.length < 1 ) { return; };
	result = result[0].firstChild.nodeValue;
	
	switch ( result ) {
		
		case '0':
		$('#nlEmail').animate({ 'backgroundColor' : '#ff5555' }, 150);
		break;
		
		case '1':
		case '2':
		saved = true;
		email = document.getElementById('nlEmail').value;
		$('#nlEmail').replaceWith(email);
		$('#sendButton').css({ 'background' : 'no-repeat url(\'/images/merci.png\')' });
		break;
		
		case '3':
		$('#nlEmail').animate({ 'backgroundColor' : '#ff5555' }, 150);
		break;
		
		default:
		break;
		
	}
	
}