var presentation;
var slideWidth;
var slideGroups=[];

var articles;
var articleWidth;
var activeArticleIndex=0;

var taglines;
var activeTaglineIndex=0;

$(document).ready(function() {
	presentation=$('#presentation');
	
	// Presentation
	sizePresentation();
	$(window).resize(sizePresentation);
	$(presentation).mousedown(onPresentationMouseDown);
	
	// Articles
	articles=$('#articles .article');
	sizeArticles();
	$(window).resize(sizeArticles);
	
	var articleButton=$('<a id=article-button>&nbsp;</a>');
	articleButton.appendTo($('#footer'));
	articleButton.click(onArticleButtonClick);
	
	// Taglines
	taglines=$('#taglines .tagline');
	if (taglines.length>0) {
		taglines.css({display:'block',float:'left'});
		sizeTaglines();
		$(window).resize(sizeTaglines);
		
		var taglineButton=$('<a id=tagline-button>&nbsp;</a>');
		taglineButton.appendTo('#footer');
		taglineButton.click(onTaglineButtonClick);
	}
	
	// Vertical center
	centerVertical();
	$(window).resize(centerVertical);
	
	// Key slide
	$(window).keydown(onKeyUp);
	
	// Hash handling
	$('#menu a[href*=#], .square a[href*=#], #footer a[href*=#]').each(function(index,element){
		this.href=this.href.split('#').join('#/');
		$(element).click(onAddressChange);
	});
	$.address.change(onAddressChange);
	
	// Back
	$('.slide.back').click(slidePresentationBack);
	
	// Plus
	$('.plus-overlay').click(togglePlusOverlay);
	
	// Groups
	$('.slide-group').each(function(index, element) {
		slideGroups[index]=$(element);
	});
	colorGroupBackSlides();
	showSlideGroup(0);
});
function sizePresentation() {
	$('#content').width($(window).width()-32);
	var w=$(window).width()-$('#menu').outerWidth()-32;
	presentation.width(w);
	
	sizeWrapper();
}
function onPresentationMouseDown(e) {
	var bw=20;
	var pw=presentation.width();
	if (e.offsetY>presentation.height()-44) {
		if (navigator.platform.indexOf('Mac')>-1) {
			// Mac
			if (e.offsetX>pw-2*bw && e.offsetX<pw-bw) {
				slideLeft();
			} else if (e.offsetX>pw-bw) {
				slideRight();
			}
		} else {
			// Windows, Linux
			if (e.offsetX<bw) {
				slideLeft();
			} else if (e.offsetX>pw-bw) {
				slideRight();
			}
		}
	}
}
function sizeWrapper() {
	var wrapper=$('#slide-wrapper');
	var slides=wrapper.find('.slide');
	var firstSlide=$(slides[0]);
	slideWidth=firstSlide.outerWidth()+10;
	var slidesWidth=slides.length*slideWidth-10;
	var pw=presentation.width();
	var wrapperWidth=slidesWidth+pw-slideWidth;
	wrapper.width(wrapperWidth);
	
	var slides=$('.slide, .slide-group');
	var lastSlide=$(slides[slides.length-1]);
	lastSlide.css('margin-right', pw-slideWidth+'px');
}
function sizeArticles() {
	var w=presentation.width();
	$('#articles').width(w);
	
	
	var numArticles=$('#articles .article').length;
	var numVisibleArticles=1;
	var pad=10;
	
	while (w/numVisibleArticles>slideWidth) {
		numVisibleArticles++;
	}
	articleWidth=w/numVisibleArticles;
	articleWidthNoPad=articleWidth-pad;
	
	articles.width(articleWidthNoPad);
	
	var lastArticle=$(articles[articles.length-1]);
	lastArticle.css('margin-right', 0);
	
	$('#article-wrapper').width((articleWidth*numArticles)-pad);
	$('#article-wrapper').height($(articles[0]).height());
}
function onArticleButtonClick() {
	activeArticleIndex++;
	activeArticleIndex=activeArticleIndex%articles.length;
	$('#articles').stop().animate({scrollLeft:articleWidth*activeArticleIndex});
}
function sizeTaglines() {
	taglines.width(presentation.width());
	$('#tagline-wrapper').width($(taglines[0]).width()*taglines.length);
}
function onTaglineButtonClick() {
	activeTaglineIndex=(activeTaglineIndex+1) % taglines.length;
	$('#taglines').stop().animate({scrollLeft:$(taglines[0]).width()*activeTaglineIndex});
}
function centerVertical() {
	var ch=$('#container').height();
	var wh=$(window).height();
	var p=Math.max(20, wh-ch)/2+'px';
	$('body').css('margin-top', p);
}
function onKeyUp(e) {
	var slideDirection;
	if (e.which==37) { // Left
		slideDirection='left';
	} else if (e.which==39) { // Right
		slideDirection='right';
	}
	if (slideDirection) {
		e.preventDefault();
		slideDirection=='left' ? slideLeft() : slideRight();
	}
}
function slideLeft() {
	var sl=presentation.scrollLeft();
	var activeSlideIndex;
	activeSlideIndex=Math.ceil(sl/slideWidth);
	activeSlideIndex--;
	if (activeSlideIndex>=0) {
		slidePresentationToIndex(activeSlideIndex);
	}
}
function slideRight() {
	var sl=presentation.scrollLeft();
	var activeSlideIndex;
	activeSlideIndex=Math.floor(sl/slideWidth);
	activeSlideIndex++;
	if (activeSlideIndex>=0) {
		slidePresentationToIndex(activeSlideIndex);
	}
}
function slidePresentationToIndex(i) {
	slidePresentationToX(i*slideWidth);
}
function slidePresentationToX(x) {
	if (presentation.scrollLeft()==x) return;
	presentation.stop().animate({scrollLeft:x});
}
function slidePresentationBack() {
	$.address.value('');
	if ($(this).hasClass('mosaic-back')) {
		slidePresentationToX($($('.mosaic')[0]).position().left);
	} else {
		slidePresentationToX(0);
	}
}

function colorGroupBackSlides() {
	var mosaicGroupLinks=$('.mosaic .square a[href*=#]');
	var backSlides=$('.slide.back');
	
	if (mosaicGroupLinks.length!=backSlides.length) return;
	
	var p;
	for (var i=0; i<mosaicGroupLinks.length; i++) {
		p=$(mosaicGroupLinks[i]).parent();
		if (!$(p).is('td')) p=$(p).parent();
		$(backSlides[i]).css('background-color', p.css('background-color'));
	}
}
function showSlideGroup(groupIndex) {
	for (var i=0; i<slideGroups.length; i++) {
		var group=slideGroups[i];
		if (i==groupIndex) {
			$('#slide-wrapper').append(group);
		} else {
			group.detach();
		}
	}
	sizeWrapper();
}
function onAddressChange(e) {
	var names=$.address.pathNames();
	if (names.length==0) return;
	var name=names[0];
	var tg=$('#'+name);
	// Si on n'a pas trouvé la cible c'est peut être un groupe
	if (!tg.length) {
		for (var i=0; i<slideGroups.length; i++) {
			var group=slideGroups[i];
			if (group.prop('id')==name) {
				showSlideGroup(i);
				tg=group;
			}
		}
		if (!tg) return;
	}
	slidePresentationToX(tg.position().left);
}

function togglePlusOverlay(e) {
	$(this).css('opacity', $(this).css('opacity')==1 ? 0 : 1);
}

