
var isIE=document.all?true:false;
var isDOM=document.getElementById?true:false;
var isNS4=document.layers?true:false;
var ie = (navigator.appName == "Microsoft Internet Explorer");

var isLoaded = false;
var isRequest = false;
var isInit = false;
var LoadDelay = new Date;
var popinfo=false;
var popx=10;
var popy=60;
var prec=-1;
var imgW=800;
var imgH=600;
var diapo=false;
var startdiapo=false;
var nbtim=0;
var cur=0;
var hDiv = 600;
var hFilm = 1000;
var bShow = true;
var bShowFilm = false;
var scroll = 20 ;
var bRun = false;
var scale = 1;
var imHead = null;
var currim = null;
var url = window.location.href;
var bImFirst = true;
var imOff = 1000;
var idx = url.indexOf('?'); /* parameters on this URL? */
var params = new Array(); /* define an array */

if (idx != -1) /* if any params detected */
{
	/* split all the parameters as deliniated by an "&" */
	var pairs = url.substring(idx+1, url.length).split('&');
	/* go through all parameters detected */
	for (var i=0; i<pairs.length; i++)
	{
		/* grab the variable and the value */
		nameVal = pairs[i].split('=');
		params[nameVal[0]] = nameVal[1];
	}
}
if (params["s"] == "1")
	startdiapo=true;
if (params["i"])
	cur=parseInt(params["i"]);

function imgObj(n, path){
	this.num = n;
	this.drop = false;
	this.path = path;
	this.next = null;
}

function backSB(){
	setValues();
	diapo = false;
	//document.body.style.cursor = 'wait';
	beginWait();
	window.location=backPath + sid;
}

function endLoad(){
	if (imHead && imHead.num == cur)
		setImage(imHead.path, getcomment(imHead.num), ratios[imHead.num]);
	if (imHead){
		imHead = imHead.next;
		while (imHead && imHead.drop)
			imHead = imHead.next;
	}
	if (imHead){
		loadImage();
	}
	else {
		End = new Date;
		set_buttons();
		window.status = 'Load finished : '+ (End.getTime() - LoadDelay.getTime()) +' ms';
	}
}

function startFilter(){
	if (ie) {
		setidx();
		var fm = document.getElementById("fimg");
		var nfilter = document.navig.filter.value;
		if (nfilter == 0){
			fm.filters.blendTrans.play();
		}
		else {
			fm.filters.revealTrans.play();
		}
	}
}

function loadImage(){
	if (imHead == null) {
		return;
	}
	window.status = 'Loading image '+ imHead.num + '... ';
	currim = new Image();
	currim.onload = endLoad;
	currim.src = imHead.path;
}

function addImage(n, bClean){
	if (bClean){
		while (imHead) {
			imHead.drop = true;
			imHead = imHead.next;
		}
	}
	var pIm = imHead;
	var path = photoFile(n);
	if (pIm){
		while (pIm.next)
			pIm = pIm.next;
		pIm.next = new imgObj(n, path);
	}
	else{
		imHead = new imgObj(n, path);
		loadImage();
	}
}

function setImage(path, cmt, rt){
	if (isInit) {
		document.images.loading.style.width='0px';
		document.images.loading.style.height='0px';
		document.images.loading.style.display="none";
	}
	if (ie) {
		var fm = document.getElementById("fimg");
		var nfilter = document.navig.filter.value;
		var tm = document.navig.tempo.value;
		var duration = 2;
		if (!document.navig.fader.checked || tm < 5)
			duration = 0;
		if (nfilter == 0) {
			fm.style.filter = "blendTrans(duration="+duration+")";
			fm.filters.blendTrans.apply();
		}
		else {
			--nfilter;
			fm.style.filter = "revealTrans(duration="+duration+",transition="+nfilter+")";
			fm.filters.revealTrans.transition=nfilter;
			fm.filters.revealTrans.apply();
		}
	}
	if (!ie)
		setidx();
	if (rt > 750) {
		w = 600 * 1000 / rt * scale;
		h = 600 * scale;
	}
	else {
		w = 800 * scale;
		h = 800 * rt / 1000 * scale;
	}
	document.images.photo.width = w;
	document.images.photo.height = h;
	document.images.photo.src = path;
	document.images.photo.title = cmt;
	if (isInit) {
		document.images.photo.style.display="block";
		isInit = false;
	}
}

function photoFile(n){
	if (bPass)
		return "?d=" + path + "&j=" + photos[n] + sid;
	else if (bPhpTest)
		return pre + path + photos[n] + ".php";
	else
		return pre + path + photos[n];
}

function setPhoto(n, sidx){
	if (!sidx) {
		bShow = false;
		showImage(n);
	}
	beginWait();
	bShow = sidx;
	diapo = false;
	cur = n;
	// document.body.style.cursor = 'wait';
	LoadDelay = new Date;
	addImage(cur, true);
	addImage((cur<photos.length-1) ? cur+1 : 0, false);
	addImage((cur>0) ? cur-1 : photos.length-1, false);
	if (popinfo) info();
	set_buttons();
}

function showImage(n){
	if (prec != n) {
		if (prec != -1)
			document.images['t_'+prec].className = '';
		if (bShow == true) {
			y = document.images['t_'+n].offsetParent.offsetTop;
			y += 50 - (hDiv * scale / 2);
			if (y < 0) y = 0;
			getFilm().scrollTop=y;
		}
		prec = n;
		document.images['t_'+n].className = 'board_sel';
	}
	bShow = true;
}

function getFilm(){
	if (!ie) film = document.getElementById("film");
	return film;
}

function fillFilm(){
	var txt = "<table id=\"tf\" width="+(thumbSize+10)+">\n";
	for (i=0;i<photos.length;i++) {
		txt+="<tr id=\"im_"+i+"\"><td><a href=\"javascript:setPhoto("+i+", false)\"><img name=\"t_"+i+"\" title=\""+comments[i]+"\"src=\""+preinfo+path+photos[i]+"\"></a></td></tr>\n";
	}
	txt += "</table>\n";
	getFilm().innerHTML = txt;
}

function showBoard(force){
	show = document.navig.board.checked;
	if (!force) {
		if (bShowFilm == show)
			return;
	}
	bShowFilm = show;
	if (!ie)
		tf = document.getElementById('tf');
	if (tf) {
		hFilm = tf.offsetHeight;
	}
	sFilm = document.getElementById('film').style;
	if (show) {
		sFilm.visibility = "visible";
		w = thumbSize+12;
		if (hFilm > (hDiv * scale) + 1)
			w += scroll;
		sFilm.width = w+'px';
	}
	else {
		sFilm.visibility = "hidden";
		sFilm.width = '0px';
	}
	resize();
}

function doResize(){
	resize();
	if (bRun)
		showBoard(true);
}

function resize(){
	var w = 0, h = 0;
	if (typeof(window.innerWidth) == 'number'){
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if (document.documentElement &&
		(document.documentElement.clientWidth || document.documentElement.clientHeight)){
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)){
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	tH = document.getElementById('gFrame').offsetHeight;
	tW = document.getElementById('gFrame').offsetWidth;
	gC = document.getElementById('gCell');
	var off = gC.offsetWidth - imgW;
	if (off < imOff)
		imOff = off;
	var dH = 0;
	var dW = 0;
	if (tW > w){
		dW += w - tW;
	}
	dW += gC.offsetWidth - imgW;
	dH = h - tH;

	imgH += dH;
	imgW += dW - imOff;
	if (imgH > 600)
		imgH = 600;
	else if (imgH < 20)
		imgH = 20;
	if (imgW > 800)
		imgW = 800;
	else if (imgW < 20)
		imgW = 20;

	scaleh = imgH / 600;
	scalew = imgW / 800;
	scale = (scalew < scaleh) ? scalew : scaleh;
	fm.style.width = (12 + (800 * scale)) + 'px';
	fm.style.height = (12 + (600 * scale)) + 'px';
	rt = ratios[cur];
	if (rt > 750) {
		imgW = 600 * 1000 / rt * scale;
		imgH = 600 * scale;
	}
	else {
		imgW = 800 * scale;
		imgH = 800 * rt / 1000 * scale;
	}
	document.images.photo.width = imgW;
	document.images.photo.height = imgH;
	imgH = 600 * scale;
	imgW = 800 * scale;
	getFilm().style.height = imgH+'px';
}

function doOptions(){
	var sel=-1;
	for (var i=0; i<OptTime.length; i++) {
		var Opt = new Option(OptTime[i],OptTime[i]);
		if (OptTime[i] == nDelay) sel = i;
		document.navig.tempo.options[i] = Opt;
	}
	document.navig.tempo.selectedIndex = sel;
	for (var i=0; i<OptFilter.length; i++) {
		var Opt = new Option(OptFilter[i],i);
		document.navig.filter.options[i] = Opt;
	}
	document.navig.filter.selectedIndex = nTrans;
	document.navig.board.checked = bBoard;
	document.navig.cycle.checked = bRepeat;
	document.navig.fader.checked = bFader;
}

function getcomment(num){
	return comments[cur].replace(/&quot;/g, '"');
}

function initim(){
	beginWait();
	fm = document.getElementById("fimg");
	ie = (fm.style.filter != null);
	getValues();
	doResize();
	window.onresize = doResize;
	doOptions();
	if (ie){
		document.images.photo.onload = startFilter;
		document.images.photo.onerror = startFilter;
		document.images.photo.onabort = startFilter;
	}
	else {
		document.navig.fader.disabled = true;
		document.navig.filter.disabled = true;
	}
	fillFilm();
	showBoard(true);
	isLoaded = true;
	isInit = true;
	var pos = cur;
	addImage(pos, true);
	addImage((cur<photos.length-1) ? cur+1 : 0, false);
	addImage((cur>0) ? cur-1 : photos.length-1, false);
	set_buttons();
	bRun = true;
	elm=document.getElementById("info");
	elm.style.left=popx+'px';
	elm.style.top=popy+'px';
	if (popinfo) {
		popinfo = false;
	    info();
	}
	if (startdiapo)
		diaporama(0);
}

function fullImage() {
	if (bPass)
		imglarge = "?d=" + path + "&l=" + photos[cur] + sid;
	else if (bPhpTest)
		imglarge = prelarge+path+photos[cur] + ".php";
	else
		imglarge = prelarge+path+photos[cur];

	window.open(imglarge, "ImgLarge", "resizable=yes,scrollbars=yes,menubar=no,status=no,toolbar=no");
}

function showInfo(){
	if (popinfo)
		hideinfo();
	else
		info();
	set_buttons();
}

function first(){
	setPhoto(0, true);
}

function last(){
	setPhoto(photos.length-1, true);
}

function backward(){
	if (document.navig.cycle.checked) {
		LoadDelay = new Date;
		if (cur>0)
			cur--;
		else
			cur = photos.length-1;
		addImage(cur, true);
		if (cur>0) {
			addImage(cur-1, false);
		}
		else {
			addImage(photos.length-1, false);
		}
	}
	else {
		if (cur>0) {
			LoadDelay = new Date;
			cur--;
			addImage(cur, true);
			if (cur>0) {
				addImage(cur-1, false);
			}
		}
	}
	if (popinfo) info();
	set_buttons();
}

function forward(){
	if (document.navig.cycle.checked) {
		LoadDelay = new Date;
		if (cur<photos.length-1)
			cur++;
		else
			cur = 0;
		addImage(cur, true);
		if (cur<photos.length-1) {
			addImage(cur+1, false);
		}
		else {
			addImage(0, false);
		}
	}
	else {
		if (cur<photos.length-1){
			LoadDelay = new Date;
			cur++;
			addImage(cur, true);
			if (cur<photos.length-1) {
				addImage(cur+1, false);
			}
			else {
				addImage(0, false);
			}
		}
		else {
			if (diapo) {
				diaporama(0);
				window.status='End of gallery';
			}
		}
	}
	if (popinfo) info();
	set_buttons();
}

function next_diapo(){
	--nbtim;
	if (nbtim == 0) {
		if (diapo) {
			if (imHead == null) {
				forward();
				tm = document.navig.tempo.value;
				setTimeout("next_diapo()", tm*1000);
			}
			else {
				setTimeout("next_diapo()", 1000);
			}
			nbtim = 1;
		}
	}
	set_buttons();
}

function diaporama(imm){
	diapo = !diapo;
	if (diapo) {
		if (imm)
			forward();
		tm = document.navig.tempo.value;
		setTimeout("next_diapo()", tm*1000);
		++nbtim;
	}

	set_buttons();
	window.status='';
}

function beginWait(){
	document.body.style.cursor = 'wait';
	var tags = document.getElementsByTagName('A');
	for (var i = 0; i < tags.length; i++) {
		tags[i].style.cursor = 'wait';
	}
}

function endWait(){
	document.body.style.cursor = 'default';
	var tags = document.getElementsByTagName('A');
	for (var i = 0; i < tags.length; i++) {
		tags[i].style.cursor = 'pointer';
	}
}

function set_buttons(){
	if (diapo) {
		document.navig.b_first.disabled = true;
		document.navig.b_prev.disabled = true;
		document.navig.b_next.disabled = true;
		document.navig.b_last.disabled = true;
		document.navig.b_anim.value="Stop";
		document.navig.b_anim.disabled = false;
	}
	else{
		var bEnd = (imHead == null);
		if (bEnd)
			endWait();
		else
			beginWait();
		if (bEnd && cur > 0)
			document.navig.b_first.disabled = false;
		else
			document.navig.b_first.disabled = true;
		if (bEnd && (document.navig.cycle.checked || cur > 0))
			document.navig.b_prev.disabled = false;
		else
			document.navig.b_prev.disabled = true;
		if (bEnd && (document.navig.cycle.checked || cur < photos.length-1))
			document.navig.b_next.disabled = false;
		else
			document.navig.b_next.disabled = true;
		if (bEnd && cur < photos.length-1)
			document.navig.b_last.disabled = false;
		else
			document.navig.b_last.disabled = true;
		document.navig.b_anim.value="Slide Show";
		if (bEnd && (document.navig.cycle.checked || cur < photos.length-1))
			document.navig.b_anim.disabled = false;
		else
			document.navig.b_anim.disabled = true;
	}
}

function settxt(id, txt){
	if (isDOM) re = document.getElementById(id);
	if (isIE) re = eval("document.all."+id);
	if (isNS4) return;
	if (re) re.innerHTML = txt;
}

function setidx(){
	showImage(cur);
	txt = (cur+1)+'&nbsp;/&nbsp;'+photos.length;
	settxt("idx", txt);
	st = unescape(photos[cur]);
	txt = dates[cur];
	settxt("titre", txt);
	if (bComment){
		var cmt= getcomment(cur).replace(/\n/g, ' - ');
		if (cmt=="") cmt="&nbsp;";
		settxt("comment", cmt);
	}
}

function info(){
	url = preinfo + path + photos[cur] + ".html";
	text='<div style="border:0;text-align:right"> --Move-- </div><iframe id="if" src="'+url+'" width="300" height="340" scrolling="yes" frameborder="1" bgcolor="#FFFFE7"></iframe>';
	if (isIE){ // IE5.0+
		document.all.info.innerHTML=text;
		if (!popinfo){
			document.all.info.style.visibility="visible";
			document.all.info.onmousedown=moveinfo;
		}
	}
	else if (isNS4){ // Netscape 4.x
		document.info.document.write('<layer id="info" width="300" height="300" bgColor="#FFFFE7" style="border:1px solid black;font-size:12px;">'+text+'</layer>');
		document.info.document.close();
		document.info.left=popx;
		document.info.top=popy;
		document.info.visibility="show";
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown=moveinfo;
	}
	else if(isDOM){ // Netscape 6.0+
		elm=document.getElementById("info");
		elm.innerHTML=text;
		if (!popinfo){
			elm.style.visibility = "visible";
			elm.addEventListener("mousedown",moveinfo, false);
		}
	}
	popinfo=true;
	document.navig.b_full.value=" X ";
}

function hideinfo(){
	if (isIE) // IE5.0+
		is = document.all.info.style;
	else if (isNS4) // Netscape 4.x
		is = document.info;
	else if(isDOM) // Netscape 6.0+
		is = document.getElementById("info").style;
	is.visibility="hidden";
	popinfo=false;
	document.navig.b_full.value=" ? ";
	set_buttons();
}

function moveinfo(e){
	if (isIE) {
		window.lastX=event.clientX;
		window.lastY=event.clientY;
		document.onmousemove=draginfo;
		document.onmouseup=enddraginfo;

	}
	else if (isNS4) {
		window.lastX=e.pageX;
		window.lastY=e.pageY;
		document.captureEvents(Event.MOUSEMOVE)
		document.onmousemove=draginfo;
		document.captureEvents(Event.MOUSEUP)
		document.onmouseup=enddraginfo;
	}
	else if (isDOM) {
		window.lastX=e.clientX;
		window.lastY=e.clientY;
		window.onmousemove=draginfo;
		window.onmouseup=enddraginfo;
	}
}

function draginfo(e){
	if (isIE) {
		var is = document.all.info.style;
		var difX=event.clientX-window.lastX;
		var difY=event.clientY-window.lastY;
		popx = parseInt(is.left)+difX;
		popy = parseInt(is.top)+difY;
		is.left=popx+"px";
		is.top=popy+"px";
		window.lastX=event.clientX;
		window.lastY=event.clientY;
	}
	else if (isNS4) {
		var is = document.layers.info;
		var difX=e.pageX-window.lastX;
		var difY=e.pageY-window.lastY;
		popx = parseInt(is.left)+difX;
		popy = parseInt(is.top)+difY;
		is.left=popx;
		is.top=popy;
		window.lastX=e.pageX;
		window.lastY=e.pageY;
	}
	else if (isDOM) {
		var is = document.getElementById("info").style;
		var difX=e.clientX-window.lastX;
		var difY=e.clientY-window.lastY;
		popx = parseInt(is.left)+difX;
		popy = parseInt(is.top)+difY;
		is.left=popx+"px";
		is.top=popy+"px";
		window.lastX=e.clientX;
		window.lastY=e.clientY;
	}
}

function enddraginfo(e){
	if (isIE || isNS4){
		document.onmousemove=null;
	}
	else if (isDOM){
		window.onmousemove=null;
	}
}

function setValues() {
	var Val = '';
	Val += (document.navig.board.checked ? '1' : '0');
	Val += ',' + (document.navig.fader.checked ? '1' : '0');
	Val += ',' + (document.navig.cycle.checked ? '1' : '0');
	Val += ',' + document.navig.tempo.value;
	Val += ',' + document.navig.filter.value;
	Val += ',' + (popinfo ? '1' : '0');
	Val += ',' + popx;
	Val += ',' + popy;
	Val += '; path=' + base;
	document.cookie = 'wfbbs_vals' + '=' + Val;
}
 
function getValues() {
	var Val = '';
	var posC = document.cookie.indexOf('wfbbs_vals' + '=');
	if (posC != -1) {
		var pos = posC + ('wfbbs_vals' + '=').length;
		var end = document.cookie.indexOf(';', pos);
		if (end != -1) Val = document.cookie.substring(pos, end);
		else Val = document.cookie.substring(pos);
		var values = Val.split(',');
		bBoard  = (values[0] == 1) ? true : false;
		bFader  = (values[1] == 1) ? true : false;
		bRepeat = (values[2] == 1) ? true : false;
		nDelay  = values[3];
		nTrans  = values[4];
		popinfo = (values[5] == 1) ? true : false;
		popx    = values[6];
		popy    = values[7];
	}
	return (Val);
}

/*
bug = window.open("","bug","status=0 toolbar=0");
bug.document.write("<HTML><HEAD><TITLE>debug</TITLE></HEAD>");
bug.document.write("<BODY><h1>debug output</h1><hr><PRE>");

function debug(text) {
	var str = '';
	date = new Date();
	nH = date.getHours();
	if (nH < 10) str += "0";
	str += nH + ":";
	nM = date.getMinutes();
	if (nM < 10) str += "0";
	str += nM + ":";
	nS = date.getSeconds();
	if (nS < 10) str += "0";
	str += nS + " - ";
	bug.document.writeln(str+'cur='+cur+' - '+text);
}
*/
