BDAddPlane('contents', 'didReceiveContents_');

var gmapsInited = false;
cache = null;
$(window).load(function() {
	
	cache = new SimpleDictionary();
	
	initMenu();
	preloadImages();
	lameTrickToCenterThePage();
	AnimIntro();
	
	lb();
	
	if ( currentContentsPath == 'infospratiques-acces' && !gmapsInited ) {
		initGmaps();
	}
	
});

function lb()
{
	
	$('.jq-lb a').lightBox();
	
}

function didReceiveContents_(contents)
{
	
	var contentsPath = contents.getElementsByTagName('contentsPath');
	if ( contentsPath.length == 0 ) {
		return;
	} else {
		contentsPath = contentsPath[0].firstChild.nodeValue;
	}
	
	cache.setValue_forKey_(contents, contentsPath);
	
	var pageTitle = contents.getElementsByTagName('pageTitle');
	pageTitle = ( pageTitle.length == 0 ) ? '' : pageTitle[0].firstChild.nodeValue;
	
	document.title = pageTitle;
	
	var html = contents.getElementsByTagName('contents');
	html = ( html.length == 0 ) ? '' : html[0].firstChild.nodeValue;
	
	currentContentsPath = contentsPath;
	
	$('#contents').each(function() {
		
		var h = $(this).height();
		
		$(this).animate({ top: (h + 50).toString() + 'px' }, 600, function() {
			
			$(this).html(html);
			
			var nh = $(this).height() + 25;
			$('#scene').animate({ height: nh + 'px' }, 500, function() {
				var t = setTimeout('fitContents()', 1000);
			});
			
			$(this).animate({ top: '10px' }, 500, function() {
				
				lb();
				
				if ( currentContentsPath == 'infospratiques-acces' && !gmapsInited ) {
					initGmaps();
				}
				
			});
			
		});
		
	});
	
	$(window).scrollTop('0');
	
}

function fitContents()
{
	
	$('#contents').each(function() {
		var nh = $(this).height() + 25;
		$('#scene').animate({ height: nh + 'px' }, 500);
	});
	
}

function preloadImages()
{
	
	var cache = [];
	var i = 0;
	
	$('#menu img').each(function () {
		
		var currentSource = this.src;
		var toLoad = '';
		
		if ( currentSource.indexOf('_h') > -1 ) {
			toLoad = currentSource.replace('_h', '');
		} else {
			toLoad = currentSource.replace('.png', '_h.png');
		}
		
		cache[i] = new Image();
		cache[i].src = toLoad;
		
		i++;
		
	});
	
	for ( var x = 1; x < 20; x++ ) {
			
		var s = x.toString();
		s = ( s.length < 2 ) ? '0' + s : s;
		
		cache[i] = new Image();
		cache[i].src = '/headers/2011a/' + s + '.jpg';
		
		i++;
			
	}
	
}

function initMenu()
{
	
	$('#headerLogo a').click(function () {
		selectMenuItemAtPath_animated_('index', true);
		return false;
	});
	
	$('div.menuItem').each(function () {
		var id = $(this).attr('id');
		var path = id.substr(id.indexOf('_') + 1, id.length);
		id = id.substr(0, id.indexOf('_'));
		$(this).children('a').click(function () {
			selectMenuItemAtPath_animated_(path, true);
			return false;
		});
		$(this).attr('id', id);
	});
	
	$('div.menuItem a img').each(function () {
		
		var normal = '';
		var hover = '';
		
		if ( this.src.indexOf('_h') > -1 ) {
			normal = this.src.replace('_h', '');
			hover = this.src;
		} else {
			normal = this.src;
			hover = this.src.replace('.png', '_h.png');
		}
		
		$(this).mouseenter(function () {
			if ( !$(this).parent().parent().hasClass('current') ) {
				this.src = hover;
			}
		});
		
		$(this).mouseleave(function () {
			if ( !$(this).parent().parent().hasClass('current') ) {
				this.src = normal;
			}
		});
		
	});
	
}

function commonRootComponentsBetweenPath_andPath_(pathA, pathB)
{
	
	var aComponents = pathA.split('-');
	var bComponents = pathB.split('-');
	var c = Math.min(aComponents.length, bComponents.length);
	var commonComponents = [];
	
	for ( var i = 0; i < c; i++ ) {
		if ( aComponents[i] == bComponents[i] ) {
			commonComponents[commonComponents.length] = aComponents[i];
		} else {
			break;
		}
	}
	
	return commonComponents.join('-');
	
}

function loadContentsAtPath_(path)
{
		
	if ( cache.valueForKey_(path) === null && currentContentsPath != path ) {
		xmlreqGET(path);
	} else if ( currentContentsPath != path ) {	
		didReceiveContents_(cache.valueForKey_(path));
	}
	
	return false;
	
}

function selectMenuItemAtPath_animated_(menuItemPath, animated)
{
	
	var oldPath = currentContentsPath;
	
	if ( cache.valueForKey_(menuItemPath) === null && currentContentsPath != menuItemPath ) {
		xmlreqGET('/AJAX/path/' + menuItemPath + '/');
	} else if ( currentContentsPath != menuItemPath ) {	
		didReceiveContents_(cache.valueForKey_(menuItemPath));
	}
	
	var commonItemsIds = elementsIdsFromPath_(commonRootComponentsBetweenPath_andPath_(oldPath, menuItemPath));
	
	// Slide up sub menus
	if ( animated ) {
	
		$('#menu>div.menu>div.menuItem>div.subMenu').each(function () {
			
			$(this).children('div.menuItem').children('div.subMenu').each(function () {
				
				if ( commonItemsIds.indexOf_('#' + $(this).parent().attr('id')) == null ) {
					$(this).slideUp('slow');
				}
				
			});
			
			if ( commonItemsIds.indexOf_('#' + $(this).parent().attr('id')) == null ) {
				$(this).slideUp('slow');
			}
			
		});
	
	} else {
		
		$('#menu>div.menu>div.menuItem>div.subMenu').each(function () {
			
			$(this).children('div.menuItem').children('div.subMenu').each(function () {
				
				if ( commonItemsIds.indexOf_('#' + $(this).parent().attr('id')) == null ) {
					$(this).hide();
				}
				
			});
			
			if ( commonItemsIds.indexOf_('#' + $(this).parent().attr('id')) == null ) {
				$(this).hide();
			}
			
		});
		
	}
	
	// Deselect
	$('#menu div.menuItem.current, #rightFootMenu div.menuItem.current').each(function () {
		
		if ( commonItemsIds.indexOf_('#' + $(this).attr('id')) != null ) {
			return;
		}
		
		var sub = ( $(this).parent().hasClass('subMenu') );
		
		$(this).children('a').children('img').each(function () {
			
			if ( this.src.indexOf('_h') > -1 ) {
				this.src = this.src.replace('_h.png', '.png');
			}
			
		});
		
	});
	
	// Remove the .current class
	$('#menu div.current, #rightFootMenu div.current').removeClass('current');
	
	// Get the ids of each menu items in the path
	var menuItemsIds = elementsIdsFromPath_(menuItemPath).join(', ');
	
	// Select
	$(menuItemsIds).addClass('current').each(function () {
		
		var sub = ( $(this).parent().hasClass('subMenu') );
		
		$(this).children('a').children('img').each(function () {
			
			if ( this.src.indexOf('_h') == -1 ) {
				this.src = this.src.replace('.png', '_h.png');
			}
			
		});
		
	});
	
	// Slide down sub menus (no effect on common items)
	if ( animated ) {
	
		$('#menu>div.menu>div.menuItem.current>div.subMenu').slideDown('slow').each(function () {
			$(this).children('div.menuItem.current').children('div.subMenu').each(function () {
				$(this).slideDown('slow');
			});
		});
	
	} else {
		
		$('#menu>div.menu>div.menuItem.current>div.subMenu').show().each(function () {
			$(this).children('div.menuItem.current').children('div.subMenu').each(function () {
				$(this).show();
			});
		});
		
	}
	
}

function elementsIdsFromPath_(path)
{
	
	var components = path.split('-');
	var c = components.length;
	var ids = ['#' + components[0]];
	for ( var i = 1; i < c; i++ ) {
		ids[i] = '#' + components.slice(0, i + 1).join('-');
	}
	
	return ids;
	
}

function lameTrickToCenterThePage()
{
	
	var nw = Math.round(($(window).width() - 900 ) / 2);
	$('#bg').css({ left: nw + 'px' });
	$(window).bind('resize', function() {
		var nw = Math.round(($(window).width() - 900 ) / 2);
		$('#bg').css({ left: nw + 'px' });
	});
	
}

function initGmaps()
{
	
	if ( currentContentsPath != 'infospratiques-acces' ) {
		return true;
	}
	
	var center = new google.maps.LatLng(48.858563, 2.347196);
	var myOptions = {
		zoom: 14,
		center: center,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
			mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		zoomControl: true,
		zoomControlOptions: {
			style: google.maps.ZoomControlStyle.SMALL
		},
	}
	var map = new google.maps.Map(document.getElementById("gmapsMap"), myOptions);
    
	// B2
	addMarkerToMap_withLatitude_longitude_title_info_(
		map, 
		48.903467, 2.211272, 
		'Cinéma B2', 
		'<div class="mapsInfoWindow"><p><strong>Cinéma B2</strong></p>'
		+ '<p>Université Paris Ouest Nanterre La Défense<br />200, avenue de la république<br />92001 Nanterre CEDEX</p>'
		+ '<p><a href="http://www.u-paris10.fr/25996272/0/fiche___pagelibre/&amp;RH=FR&amp;RF=1245926935704">www.u-paris10.fr</a></p></div>');
	
	// Studio Galande
	addMarkerToMap_withLatitude_longitude_title_info_(
		map, 
		48.851621, 2.347054, 
		'Studio Galande', 
		'<div class="mapsInfoWindow"><p><strong>Studio Galande</strong></p>'
		+ '<p>42, rue Galande<br />75005 Paris</p>'
		+ '<p><a href="http://www.studiogalande.fr/">www.studiogalande.fr</a></p></div>');
	
	// Forum des Images
	addMarkerToMap_withLatitude_longitude_title_info_(
		map, 
		48.862774, 2.344385, 
		'Le Forum des Images', 
		'<div class="mapsInfoWindow"><p><strong>Le Forum des Images</strong></p>'
		+ '<p>Forum des Halles<br />2, rue du Cinéma<br />75045 Paris CEDEX 01</p>'
		+ '<p><a href="http://www.forumdesimages.fr/">www.forumdesimages.fr</a></p></div>');
	
}

function addMarkerToMap_withLatitude_longitude_title_info_(map, latitude, longitude, title, info)
{
	
	var marker = new google.maps.Marker({
		position: new google.maps.LatLng(latitude, longitude),
		map: map,
		title: title
	});
	
	var infoWindow = new google.maps.InfoWindow({
		content: info
	});
	
	google.maps.event.addListener(marker, 'click', function() {
		infoWindow.open(map, marker);
	});
	
}

var logoTop = 0;
var logoLeft = 0;
function AnimIntro()
{
	
	if ( true || currentContentsPath != 'index' || !detectQuickTime() ) {
		$('#bg').css({ background: 'repeat-y url(\'/images/bg2.png\')' }).show();
		$('#main').css({ background: 'repeat-y url(\'/images/bg2.png\')' }).show();
		$('#headerLogo').show();
		$('#headerHC').show();
		$('#headerMask').show();
		$('#container').show();
		selectMenuItemAtPath_animated_(currentContentsPath, false);
		$('#footer').show();
		$('#copyright').show();
		$('#headerDeco').show();
		setTimeout('ChangeHeader()', 6000);
		return;
	};
	
	// Get width of #main and width / height of window
	var mainWidth = $('#main').width();
	var wWidth = $(window).width();
	var wHeight = $(window).height();
	
	$('#headerLogo').each(function() {
		
		// Increase the size of the logo and move it to center
		$('img:first', this).css({ width: '397px', height: '198px' });
		var iTop = ( wHeight - $(this).height() ) / 2 - 50;
		var iLeft = ( mainWidth - $(this).width() ) / 2;
		$(this).css({ top: iTop.toString() + 'px', 
					  left: iLeft.toString() + 'px' });
		
		// Center the movie verticaly on the page, move it out of bound and show it
		$('#teaser_movie').css({ top: ( ( wHeight - 384 ) / 2 ).toString() + 'px', left: '-' + ( wWidth / 2 + 500 ) + 'px' }).show();
		
		// Logo blink, shrink and move
		$(this).fadeIn('slow').fadeOut('fast').fadeIn('fast', function() {
			$('img:first', this).animate({ width: '318px', height: '154px' }, 1000);
			$('#teaser_movie').animate({ left: ( ( mainWidth - 480 ) / 2 ).toString() + 'px' }, 1000);
			$(this).animate({
				top: (iTop - 175).toString() + 'px',
				left: (iLeft - 57).toString() + 'px'
			}, 1000, function() {
				md = setTimeout('MovieDone();', 89000);
			});
		});
		
	});
	
}

var md = null;
function MovieDone()
{
	
	if ( md !== null ) { clearTimeout(md) };
	document.m.Stop();
	
	var bgLeft = parseInt($('#bg').css('left'));
	var wWidth = $(window).width();
	
	$('#headerLogo').each(function() {
		
		// Hide movie
		$('#teaser_movie').animate({ left: ( wWidth - bgLeft - 10 ).toString() + 'px', width: '1px' }, 500, function() {
				$(this).hide().css({ left: '-' + ( wWidth / 2 + 500 ) + 'px', width: '480px' });
		});
		
		$('img:first', this).animate({ width: '265px', height: '129px' }, 600);
		$(this).animate({ top: '14px', left: '20px' }, 600, function() {
			
			// Change logo image for ie
			if ( $.browser.msie ) { $(this).attr('src', '/images/silenceoncourt_mini.png') };

			$('#bg').css({ background: 'repeat-y url(\'/images/bg2.png\')' });
			$('#main').css({ background: 'repeat-y url(\'/images/bg2.png\')' });
			$('#headerHC').css({ left: '-500px'}).show().animate({ left: '799px' }, 500, function() {
				$('#headerMask').fadeIn('slow');
				$('#container').fadeIn('slow');
				selectMenuItemAtPath_animated_(currentContentsPath, false);
				$('#footer').fadeIn('slow');
				$('#copyright').fadeIn('slow', function() {
					$('#headerDeco').fadeIn('fast');
					setTimeout('ChangeHeader()', 6000);
				});
			});
			
		});
		
	});
	
	return false;
	
}

currentHeader = 2;
headersCount = 19;
function ChangeHeader()
{
	
	if ( currentHeader == headersCount ) {
		currentHeader = 2;
	} else {
		currentHeader++;
	}
	
	var s = currentHeader.toString();
	s = ( s.length < 2 ) ? '0' + s : s;
	
	var wH = $(window).height();
	$('#headerDeco img:first').animate({ top: (wH - 200) + 'px', opacity: '0.0' }, 2000, function() {
		$(this).css({ top: '-210px', opacity: '1.0' }).attr('src', '/headers/2011a/' + s + '.jpg').animate({ top: '0px', opacity: '1.0' }, 1000, function() {
			setTimeout('ChangeHeader()', 6000);
		});
	});
	
}

Array.prototype.indexOf_ = function (value)
{
	
	var c = this.length;
	for ( var i = 0; i < c; i++ ) {
		if ( this[i] == value ) {
			return i;
		}
	}
	
	return null;
	
};

// Just for the fun

function SimpleDictionary()
{
	this.values = new Object();
}

SimpleDictionary.prototype.valueForKey_ = function (key)
{
	
	if ( this.values[key] === undefined ) {
		return null;
	}
	
	return this.values[key];
	
};

SimpleDictionary.prototype.setValue_forKey_ = function (value, key)
{
	this.values[key] = value;
};
