// JavaScript Document
// Accordeon-Galerie script
// (c) / written by Bert Riecken (www.tonbildtext.de) anno 2009 / all rights reserved
// dont use without link/comment to www.tonbildtext.de

// make changes here for debug and path of this script
var debug = 0;		// debug = 1: info-outputs in document
var thisPath = "../accordeon/";	// relative path of this script

// dont make changes down after here, if you do not know what to do
var accordeonCount = 0;	// count of accordeons in document (auto-value)

//instance new Image
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

// timeout-function
function idle (ms) {
	if (ms > 0) window.setTimeout("idle(0)",ms);
}

function nothing () {
	//nothing
}

// loading and naming all pics (pics named like: name_nn.jpg, where nn=[01..99], firstPicName=name_01.jpg)
function loadPictures (cnt, path, firstPicName, viewLarge) {
	var Bilder = new Array();
	var pic,img,source;
	file = firstPicName.substring(0,firstPicName.length-6);	// name_01.jpg -> name_
	if (debug) document.write('(loadPictures) file: ' + file + '<br>\n');
	for (i=0; i < cnt; i++) {
		pic = i + 1;
		if (pic < 10) pic = '0' + pic;
		img = file;
		source = path + img + pic + '.jpg '; // = path/name_nn.jpg
		if (debug) document.write('(loadPictures) source(' + i + '): ' + source + '<br>\n');
		Bilder[i] = new Image();
		Bilder[i].src = source;
		Bilder[i].name = "pic_" + accordeonCount + "_" + pic;  // = name_01_nn
		Bilder[i].height = Bilder[i].naturalHeight;
		Bilder[i].width = Bilder[i].naturalWidth;
		if (viewLarge) {
			source = path + "big/" + img + pic + '.jpg '; // = path/big/name_nn.jpg
		}
		Bilder[i].oSrc = source;
	}
	return Bilder;
}

// Build the accordeon
function buildGalerie (cnt, path, firstPicName, smallWidth, height, viewLarge, lastPicName) {
	var Bilder = new Array();
	var lastImg;
	var firstWidth,firstHeight,lastWidth,lastHeight,width;
	var name='';
	
	// if there are more than one; for pic-ids and -names
	accordeonCount++;
	
	// slash in path checking
	if(path.substring(path.length-1,1) != "/") path = path + '/';
	if(thisPath.substring(thisPath.length-1,1) != "/") thisPath = thisPath + '/';
	
	if (lastPicName) {
		lastImg = newImage(path+lastPicName);
	}
	
	// load accordeon-pics
	Bilder = loadPictures(cnt, path, firstPicName, viewLarge);

	// Maximal width and height of pics
	var maxWidth = 0, maxHeight = 0;
	for (i=0; i < cnt; i++) {
		if (Bilder[i].width > maxWidth) maxWidth = Bilder[i].width;
		if (Bilder[i].height > maxHeight) maxHeight = Bilder[i].height;
	}
	
	// width of last pic if given, else width of largest pic (so every pic will fit on last place)
	if (lastPicName) {
		lastWidth = lastImg.width;
	} else {
		lastWidth = maxWidth;
	}
	
	if (lastWidth == 0) lastWidth = 320;
	
	// total width of accordeon
	width = (cnt * smallWidth) + lastWidth;
	
	// debug-output
	if (debug) {
		document.write('(buildGalerie) lastWidth:' + lastWidth + '; height:' + lastHeight + '; file:' + lastPicName + '<br>\n');
		document.write('(buildGalerie) maxWidth:' + maxWidth + '; maxHeight:' + maxHeight + '<br>\n');
		document.write('(buildGalerie) width:' + width + '; height:' + height + '; smallWidth:' + smallWidth + '; cnt:' + cnt + '; accordeonCount:' + accordeonCount + '<br>\n');
	}
	
	document.write('<p><div class="accordeon-galerie">\n');
	document.write('<ul class="accordeon" id="accordeon" style="width:' + width + 'px; height:' + height + 'px;">\n');
	
	// the accordeon of pics
	galId = "gal" + accordeonCount;
	if (viewLarge) tclass = 'class="thickbox" rel="' + galId + '"';
	else tclass = 'class="small" ';
	for (i=0; i < cnt; i++) {
		if (viewLarge) thref = 'href="' + Bilder[i].oSrc + '" ';
		else thref = 'href="javascript:nothing();"';
		document.write('<li><a ' + thref + tclass + ' >\n');
		document.write('<img src="' + Bilder[i].src + '" name="' + Bilder[i].name + '" /></a></li>\n');
	}
	
	// the last pic
	$name = "last_" + accordeonCount;
	if (lastPicName) {
		document.write('<li><a class="last">\n');
		document.write('<img class="last" src="' + path + lastPicName + '" name="' + $name + '" id="' + $name + '" style="width:' + lastWidth + 'px;" /></a></li>\n');
	} else {
		document.write('<li><a class="last">\n');
		document.write('<img class="last" src="' + thisPath + 'pix.gif" name="' + $name + '" id="' + $name + '" style="width:' + lastWidth + 'px;" /></a></li>\n');
	}

	document.write('</ul>\n');
	document.write('</div></p>\n');
	
}