/*
	JavaScript Document
	"deadline.js" 
	© montag
	pour paris-musées
	septembre 09
		
*/
var where = 'intro';
var viewsize;
var screenres;
var contentsize;
var menuwidth;
var introtop;
var artists;
var art = 0;
function initDeadline() {
	artists = $$('h2.name');
	artists.each(function(elt) {
		elt.observe('click', selectArt);
		elt.observe('mouseover', function(event) {
			$(elt).addClassName('over');							  
		});
		elt.observe('mouseout', function(event) {
			$(elt).removeClassName('over');							  
		});
	});
	$('home').observe('click', goWhere);
	$('splash').observe('click', closeSplash);
	$('menu').observe('click', closeSplash);
	$('info_pra').observe('click', infoPratic);
	setScreen();
	$('mam').observe('click', function(evt) {
		evt.stop();
		window.open('http://www.mam.paris.fr', 'MAM');
	});	
}
//
function setScreen() {
	viewsize = document.viewport.getDimensions();
	if (viewsize.height > 780) {
		if ($('page').addClassName('medium')) $('page').removeClassName('medium')
		screenres = 'large';
		introtop = -700;
	} else {
		if ($('page').addClassName('large')) $('page').removeClassName('large')
		screenres = 'medium';
		introtop = -546;
	}
	if (where == 'intro') {
		$('main').setStyle('top: '+introtop+'px;');
	} else {
		$('main').setStyle('top: 0px;');
	}
	$('page').addClassName(screenres);
	$('h1img').src = 'assets/img/header_'+screenres+'.gif';
	if (art != 0 && art != 'info') {
		$('art_'+art).removeClassName('active');
	}
	art = 0;
	$('splash').setStyle('left: 0;');
	$('work').update('');
	setFontSize();
}
//
function setFontSize() {
	var introh = parseInt($('intro').getHeight());
	var dispoh = parseInt($('page').getHeight()) - parseInt($('intro').offsetTop);
	var textsize = 20;
	while (introh > dispoh) {
		textsize -= 1;
		$('intro').setStyle('font-size: '+parseFloat(textsize / 10)+'em;');
		introh = parseInt($('intro').getHeight());
	}
	$('debug').update('available height: '+dispoh+'; intro height: '+introh+'; font-size: '+parseFloat(textsize / 10)+'em;');
	// menu width
	$('menu').setStyle('width: auto;');
	menuwidth = $('menu').getWidth();
	$('debug').insert('; menu width: '+menuwidth+';');
	$('menu').setStyle('width: '+menuwidth+'px;');
	contentsize = $('content').getDimensions();
	contentsize.width -= menuwidth;
	$('debug').insert(' content width: '+contentsize.width+'; content height: '+contentsize.height+';');
	$('work').setStyle('width: '+contentsize.width+'px; left: '+parseInt(contentsize.width + menuwidth)+'px;');
	
}
//
window.onresize = function () {
	setScreen();
}
//
function goWhere() {
	if (where == 'intro') {
		goContent();
	} else {
		goIntro();
	}
	$('enter_bt').toggle();
}
//
function goIntro() {
	new Effect.Move('main', {
		y: introtop,
		mode: 'absolute', 
		transition: Effect.Transitions.EaseFromTo,
		beforeStart: function() {
			//
		},
		afterFinish: function() {
			where = 'intro';
			$('enter_bt').update('<img src="assets/img/home1.gif" width="29" height="14" border="0" />');
			$('enter_bt').toggle();
		}
	});
	return false;
}
//
function goContent() {
	new Effect.Move('main', {
		y: 0,
		mode: 'absolute', 
		transition: Effect.Transitions.EaseFromTo,
		beforeStart: function() {
			//
		},
		afterFinish: function() {
			where = 'content';
			$('enter_bt').update('<img src="assets/img/home2.gif" width="29" height="14" border="0" />');
			$('enter_bt').toggle();
		}
	});
	return false;
}
//
function selectArt(evt) {
	evt.stop();
	var id = this.id;
	var elt = this;
	//alert('art: '+art+'; id: '+id);
	if(id != art) {
		if (art != 0 && art != 'info') {
			$('art_'+art).removeClassName('active');
		}
		$('art_'+id).addClassName('active');
	}
	if(art != 0 && art != id) {
		new Effect.Move('splash', {
			x: 0,
			mode: 'absolute', 
			duration: 1,
			transition: Effect.Transitions.EaseFromTo,
			afterFinish: function() {  
				openArt(id);
			}
		});
		new Effect.Move('work', {
			x: menuwidth + contentsize.width,
			mode: 'absolute', 
			duration: 1,
			transition: Effect.Transitions.EaseFromTo,
			afterFinish: function() {  
				//
			}
		});
	} else {
		openArt(id);
	}
	art = id;
}
//
var pl;
var pp = 'media/pictures/';
//
function openArt(id) {
	var cellwmax = parseInt(contentsize.width) - 180;
	//alert(cellwmax);
	$('debug').update('art: '+id);
	$('debug').insert('; contentsize.width: '+contentsize.width);
	$('debug').insert('; cells width: '+cellwmax);
	var nid = '0'+id;
	if (id < 10) {
		nid = '0'+nid
	}
	var mytxt = $('artText_'+id).innerHTML;	
	$('work').update('<div id="nav"><img id="prevbt" src="assets/img/prev.gif" width="24" height="24" /><img  id="nextbt" src="assets/img/next.gif" width="24" height="24" /></div><table border="0" cellspacing="0" cellpadding="0" id="worktable"><tr id="worktr"></tr></table>');
	var html = '<td class="wk"><p class="txt" style="width: '+parseInt(cellwmax - 60)+'px;">'+mytxt+'</p></td>';
	var celltemp = new Template('<td class="wk">#{img}<h5>#{title}</h5><p class="caption">#{caption}</p></td>');
	var workarray = eval('artist_'+id).work;
	if (workarray) {
		var count = 0;
		for (var i = 0; i < workarray.length; i++) {
			//alert($('worktr').donw().getWidth());
			var myimg = '<div class="block" style="width: '+cellwmax+'px;" title="Cliquez sur l\'image pour la déplacer">&nbsp;</div><img src="'+pp+nid+workarray[i][0]+'" style="max-height: '+cellwmax+'px; max-width: '+cellwmax+'px;" />'
			count ++;
			if (workarray[i][0].include('f4v')) {
				var mymedia = workarray[i][0].split('.');
				//alert(mymedia[0])
				myimg = '<script type="text/javascript">swfobject.embedSWF("assets/mtgPlayer_01a.swf", "vid_1", "400", "225", "9.0.115", "assets/expressInstall.swf", {"mfile": "../media/movies/'+workarray[i][0]+'", "mpict": "media/movies/'+mymedia[0]+'.jpg", "mplay": 0, "mbuffer": 5, "mvolume": 0.3, "mratio": "16:9", "ctime": 1, "clength": 1, "cvolume": 1, "cfull": 1, "chide": 1, "ascolor": "996600", "asalpha": 1, "asmode": "normal", "bkcolor": "000000", "bkalpha": 0.7, "bkmode": "multiply"}, {"scale": "showall", "wmode": "transparent", "bgcolor": "#000000", "menu": false, "allowfullscreen": true, "allowscriptaccess": "always", "salign": "tl"}, {"id": "mtgPlayer", "name": "mtgPlayer"})</script><div class="block" style="width: '+cellwmax+'px;" title="Cliquez sur l\'image pour la déplacer">&nbsp;</div><div id="vid_1" style="width: 400px;"><a href="http://www.macromedia.com/go/getflashplayer">Téléchargez "Flash Player"</a> pour voir cette vidéo.</div>';
			}		
			var mywork = {
				img: myimg,
				title: workarray[i][1],
				caption: workarray[i][2]
			}
			html += celltemp.evaluate(mywork);
		}
	}
	$('worktr').update(html);
	//
	new Effect.Move('splash', {
		x: - contentsize.width,
		mode: 'absolute', 
		duration: 1,
		transition: Effect.Transitions.EaseFromTo,
		afterFinish: function() {  
			//
		}
	});
	new Effect.Move('work', {
		x: menuwidth,
		mode: 'absolute', 
		duration: 1,
		transition: Effect.Transitions.EaseFromTo,
		afterFinish: function() {  
			//openArt();
			pl = $$('td.wk');
			pl.each(function(elt) {
				elt.observe('click', moveToP);
				elt.observe('mouseover', function(event) {
					$(elt).addClassName('over');							  
				});
				elt.observe('mouseout', function(event) {
					$(elt).removeClassName('over');							  
				});
			});
		}
	});
}
//
function stopEvent(evt) {
	evt.stop();
}

var unprotected = '';
function moveToP(evt) {
	if (unprotected != '') {
		$(unprotected).setStyle('height: 100%;');
		unprotected = '';
	}
	
	elt = evt.element();
	//alert('up: '+elt.up()+'; down: '+elt.down()+'; previous: '+elt.previous()+'; next: '+elt.next().id);
	if (elt.next() != null && elt.next().id == 'mtgPlayer') {
		//alert('TROUVÉ');
		elt.setStyle('height: 0;');
		unprotected = elt;
		$('mtgPlayer').observe('click', stopEvent);
	}
	var myw = parseInt(this.clientWidth);
	var myl = parseInt(this.offsetLeft);
	var a = parseInt(contentsize.width) / 2;
	var b = (myl + (myw / 2));
	var x = a - b - 60;
	new Effect.Move('worktable', {
		x: x,
		mode: 'absolute', 
		duration: 1,
		transition: Effect.Transitions.EaseFromTo,
		afterFinish: function() {  
			//
		}
	});
}
//
function closeSplash(evt) {
	if (art != 0 && art != 'info') {
		$('art_'+art).removeClassName('active');
	}
	if ($('splash').offsetLeft < 0) {
		new Effect.Move('splash', {
			x: 0,
			mode: 'absolute', 
			duration: 1,
			transition: Effect.Transitions.EaseFromTo,
			afterFinish: function() {  
				art = 0;
				$('work').setStyle('left: '+parseInt(menuwidth + contentsize.width)+'px'); 
			}
		});
	}
}
//
function infoPratic(evt) {
	evt.stop();
	if (art != 0) {
		$('art_'+art).removeClassName('active');
		new Effect.Move('splash', {
			x: 0,
			mode: 'absolute', 
			duration: 1,
			transition: Effect.Transitions.EaseFromTo,
			afterFinish: function() {  
				art = 0;
				$('work').setStyle('left: '+parseInt(menuwidth + contentsize.width)+'px'); 
				openInfo();
			}
		});
	} else {
		openInfo();
	}
}
//
function openInfo() {
	art = 'info';
	$('work').update($('info_text').innerHTML);
	//
	new Effect.Move('splash', {
		x: - contentsize.width,
		mode: 'absolute', 
		duration: 1,
		transition: Effect.Transitions.EaseFromTo,
		afterFinish: function() {  
			//
		}
	});
	new Effect.Move('work', {
		x: menuwidth,
		mode: 'absolute', 
		duration: 1,
		transition: Effect.Transitions.EaseFromTo,
		afterFinish: function() {  
			//
		}
	});
}