function setupLinks(arg) {
	if (arg) $('nav').fade('hide');
	var links = $('nav').getElements('a'),
		widths = [],
		margin = 464;
	for (var i = 0, len = links.length; i < len; i++) {
		widths.push(links[i].getSize().x);
		margin -= links[i].getSize().x;
	}
	margin = Math.floor(margin / (links.length - 1));
	for (var j = 0; j < len; j++) {
		links[j].parentNode.style.width = widths[j] + 'px';
		links[j].parentNode.name = widths[j] - 2;
		if (j < (len - 1)) {
			links[j].parentNode.style.marginRight = margin + 'px';
		}
		links[j].style.width = '0';
		links[j].parentNode.onmouseover = function () {
			var duree = this.name * 4;
			this.childNodes[0].set('tween', {duration: duree});
			this.childNodes[0].tween('width', this.name);
		};
		links[j].parentNode.onmouseout = function () {
			this.childNodes[0].tween('width', 0);
		};
	}
}

function startShow() {
	$('loading').fade('out');
	$('loadtxt').fade('out');
	window.setTimeout(function () {
		document.getElements('h1')[0].fade('in');
	}, 500);
	window.setTimeout(function () {
		for (var i = 0, len = KORBAN.intro.length; i < len; i++) {
			fakeimgs[i] = document.createElement('img');
			fakeimgs[i].className = 'fakeimg';
			fakeimgs[i].id = 'fakeimg' + i;
			fakeimgs[i].src = KORBAN.intro[i];
			$('canvas').appendChild(fakeimgs[i]);
			//fakeimgs[i].set('tween', {'duration': 1000});
			//fakeimgs[i].fade('hide');
		}
		$$('.fakeimg').set('tween', {'duration': 1000});
		$$('.fakeimg').fade('hide');
		$('fakeimg0').fade('in');
		interval = window.setInterval(function () {
			$('fakeimg' + counter%KORBAN.intro.length).fade('out');
			counter++;
			$('fakeimg' + counter%KORBAN.intro.length).fade('in');
			$('nav').fade('in');
		}, 3000);
	}, 1000);
	var all_srcs = [];
	for (var i = 0, ilen = KORBAN.portfolio.length; i < ilen; i++) {
		all_srcs.push(KORBAN.portfolio[i]);
	}
	for (j = 0, jlen = KORBAN.press.length; j < jlen; j++) {
		all_srcs.push(KORBAN.press[j].image);
	}
	var preloadimgs = new Asset.images(all_srcs);
}

var clutch = 0;
function setupIntro() {
	var all_srcs = [];
	/*for (var i = 0, ilen = KORBAN.portfolio.length; i < ilen; i++) {
		all_srcs.push(KORBAN.portfolio[i]);
	}
	for (j = 0, jlen = KORBAN.press.length; j < jlen; j++) {
		all_srcs.push(KORBAN.press[j].image);
		all_srcs.push(KORBAN.press[j].large);
	}*/
	for (k = 0, klen = KORBAN.intro.length; k < klen; k++) {
		all_srcs.push(KORBAN.intro[k]);
	}
	all_srcs.push('images/bio.gif', 'images/contact.jpg');
	all_srcs.push('images/warrowb.gif', 'images/warrowbmo.gif', 'images/barrowb.gif', 'images/barrowbmo.gif', 'images/warrowf.gif', 'images/warrowfmo.gif', 'images/barrowf.gif', 'images/barrowfmo.gif');
	var loading = document.createElement('div');
	loading.id = 'loading';
	$('main').appendChild(loading);
	var loadtxt = document.createElement('p');
	loadtxt.id = 'loadtxt';
	loadtxt.innerHTML = 'loading';
	$('main').appendChild(loadtxt);
	var preloadimgs = new Asset.images(all_srcs, {
		onProgress: function (c, i) {
			var percentage = Math.ceil((i / all_srcs.length) * 100),
				width = Math.floor(percentage * 2.9);
			$('loading').tween('width', (width > clutch) ? width : clutch);
			//$('loadtxt').innerHTML = percentage;
			clutch = (width > clutch) ? width : clutch;
		},
		onComplete: function () {
			$('loading').tween('width', 290);
			window.setTimeout(function () {
				startShow();
			}, 500);
		}	
	});
}

function setupCarousel() {
	//var duree = (pagename == 'portfolio') ? 2500 : 1250;
	var duree = (pagename == 'portfolio') ? 1800 : 1050;
	pan = new Fx.Scroll($('main'), {
		duration: duree,
		//transition: Fx.Transitions.Sine.easeInOut
		transition: Fx.Transitions.Quad.easeInOut
	});
	pan.set(0, 0);
}

function doTransition(hex, color) {
	$('canvas').fade('hide');
	if ($('carousel')) {
		$('carousel').fade('hide');
	}
	document.getElements('body')[0].morph({
		'backgroundColor': hex
	});
	window.setTimeout(function () {
			document.getElements('body')[0].className = color;
			$('canvas').fade('in');
			if ($('carousel')) $('carousel').fade('in');
	}, 250);
}

function revenir () {
	if (!ieFix) {
		$('cartouche').tween('opacity', 1);
		$('main').onclick = '';
		$('main').style.cursor = 'default';
		window.setTimeout(function () {
			$('main').style.backgroundImage = '';
		}, 500);
	}
}
	
function dispatch(unit) {
	if (pagename == 'portfolio') {
		var lv = 580 * unit; //580 * (unit + 1),
		//pan.start(lv, 0);
	}
	if (pagename == 'press') {
		var lv = 0;
		for (var i = 0; i <= unit; i++) {
			lv += Math.floor((KORBAN.press[i].width / KORBAN.press[i].height) * 275);
			lv += 50;
		}
		lv -= 580;
		//pan.start(lv, 0);
	}
	window.setTimeout(function () {
		pan.start(lv, 0);
	}, 0);
}
function parrow() {
	current = current - 1;
	if (current < 0) current = total;
	dispatch(current);
	//$('cartouche').tween('opacity', 1);
	return false;
}
function narrow() {
	current = current + 1;
	if (current > total) current = 0;
	dispatch(current);
	//$('cartouche').tween('opacity', 1);
	return false;
}

function blowUp(source) {
	if (!ieFix) {
		$('main').style.backgroundImage = 'url(' + source + ')';
		$('cartouche').tween('opacity', 0);
		window.setTimeout(function () {
			$('main').onclick = revenir;
			$('main').style.cursor = 'pointer';
		}, 1000);
	}
}