/*
	=====================
	Javascript - Slideshow
	from www.tautz-it.de
	=====================

	info:
	1. IE6 has a double click problem. ondblclick="s1.shownext(1);" in the link
	could help but then Gecko has a problem, does shownext twice, means a jump.

	version: 0.16 beta
	version date: 2006-10-04
	copyright: www.tautz-it.de
*/

// *************************************
// class begin
// *************************************
// picdir:  can be with or without slash at the end
// pictures: array of pictures (name, descr)

// thumbfilenameend: if not set large pictures are used for thumbnails
//                   if set e.g. 'i.gif': standard 1.jpg => thumbnail 1i.gif
// noofthumbs: number of thumbnails
function slideshow(slidename,slidedatadir,picdir,pictures,thumbfilenameend) { // -- constructor --
	this.showdebugmessages = true;

	this.sn = slidename;
	this.picdir = picdir;
	this.slidedatadir = slidedatadir;
	this.pictures = pictures;
	this.thumbfilenameend = thumbfilenameend;

	this.n = this.pictures.length;
	this.picpos = 1; // selected picture number
	this.thumbfirstpos = 1; // withnoofpos=4: 1,5,9,13,17,...
	this.noofthumbs = 4;
	this.thumbsperrow = 4;
	this.showpicturelinklist = true; // List of Links for each picture

	// ---------
	// default values
	// if they should be changed: set values after new slidesho(...) and before init
	this.language = 'de';
	// ---------

	// add slash at the end if missing
	if ( this.picdir.substr(this.picdir.length-1,1) != '/' ) this.picdir = this.picdir + '/';
	if ( this.slidedatadir.substr(this.slidedatadir.length-1,1) != '/' ) this.slidedatadir = this.slidedatadir + '/';

	this.tooltips = new Array();
	for (i=0; i<this.n;i++) {
		s = this.pictures[i][1];
		s =	s.replace(/<(.*?)>/ig, '');
		this.tooltips.push(s);
	}

// *************************************
// Call this function after new slideshow
// set param to true to paint the first picture
this.init = function(bSetFirstPicture) {
	// show image for large pictures

	document.getElementById(this.sn+'thumbs').style.display = 'block';

	document.getElementById(this.sn+'mainpicture').style.display = 'inline';
	document.getElementById(this.sn+'topnavi1').style.display = 'block';
	document.getElementById(this.sn+'topnavi2').style.display = 'block';
	document.getElementById(this.sn+'infotext').style.display = 'block';
	document.getElementById(this.sn+'linklist').style.display = 'block';

	this.initthumbs();
	this.setthumbpictures();

	if ( 0 == this.n ) return;

	if ( bSetFirstPicture ) this.showpicture_at_picpos();

}

// *************************************
// step e.g.: +1 -1 +2 -2 etc., neagtive values = step backwards
this.shownext = function(step) {
	if ( 0 == this.n ) return;

	if ( (this.picpos+step) > this.n )
		this.picpos = this.n;
	else if ( (this.picpos+step) < 1 )
		this.picpos = 1;
	else
		this.picpos += step;

	this.showpicture_at_picpos();
}

// *************************************
// Use this function if you click on a thump button
this.buttonshowpic = function(buttonno) {
	picpos = this.thumbfirstpos + buttonno -1;
	if ( (picpos < 1) || (picpos > this.n) ) return; // invalid values
	this.picpos = picpos;
	this.showpicture_at_picpos();
}

// *************************************

this.showpicture = function(pos) {
	if ( (pos<1) || (pos>this.n) ) return;
	this.picpos = pos;
	this.showpicture_at_picpos();
}

// *************************************

this.showpicture_at_picpos = function() {
	// set button previous and next
	if ( this.picpos <= 1 )
		document.getElementById(this.sn+'buttonprevious').src = this.slidedatadir + 'button_previous_noaction.gif';
	else
		document.getElementById(this.sn+'buttonprevious').src = this.slidedatadir + 'button_previous.gif';
	if ( this.picpos >= this.n )
		document.getElementById(this.sn+'buttonnext').src = this.slidedatadir + 'button_next_noaction.gif';
	else
		document.getElementById(this.sn+'buttonnext').src = this.slidedatadir + 'button_next.gif';

	// paint new thumbs if necessary
	if ( this.picpos >= (this.thumbfirstpos+this.noofthumbs) ) {
		this.calcthumbfirstpos();
		//this.thumbfirstpos += this.noofthumbs;
		this.setthumbpictures();
	} else
	if ( this.picpos < this.thumbfirstpos ) {
		this.calcthumbfirstpos();
		//this.thumbfirstpos -= this.noofthumbs;
		this.setthumbpictures();
	}

	if ( (this.picpos > this.n) || (this.picpos < 1) ) return;

	this.highlightthumb();

	fn = this.picdir + this.pictures[this.picpos-1][0];
	document.getElementById(this.sn+'mainpicture').src = fn;
	document.getElementById(this.sn+'infotext').innerHTML = this.pictures[this.picpos-1][1];
	//this.debugprint(this.sn+' ' +fn);

	if ( this.showpicturelinklist ) this.makelinklist();
}

// *************************************
// inserts thumbs as follows:
// <a id="s1thumb1" class="thumb" href="javascript:s1.buttonshowpic(1);"><img id="s1imgthumb1" class="thumb"
// src="../slideshowgeneraldata/thumb_nopic.jpg" width="80" height="60" border="0" alt="" /></a>
this.initthumbs = function() {
	s = '';
	for (i=1; i<=this.noofthumbs; i++) {
		s += '<a id="'+this.sn+'thumb'+i+'" class="thumb" onfocus="'+this.sn+'.removefocusborder(this)" '+
				 'href="javascript:'+this.sn+'.buttonshowpic('+i+');">'+
				 '<img id="'+this.sn+'imgthumb'+i+'" class="thumb" src="'+this.slidedatadir+'" '+
				 ' width="80" height="60" border="0" alt="" /></a>';
		if ( 0 == i%this.thumbsperrow) s += '<div style="clear:left"></div>';
	}
	s += '<div style="clear:left"></div><a id="'+this.sn+'thumbrowsadd" class="rows" style="margin-left: 2px;" href="javascript:'+this.sn+'.thumbrowsadd()" onfocus="'+this.sn+'.removefocusborder(this)" class="alignright"><img src="'+this.slidedatadir+'/thumbrows_add.gif" alt="" border="0" height="10" width="57"></a><a id="'+this.sn+'thumbrowsremove" class="rows" href="javascript:'+this.sn+'.thumbrowsremove()" onfocus="'+this.sn+'.removefocusborder(this)" class="alignright"><img src="'+this.slidedatadir+'/thumbrows_remove.gif" alt="" border="0" height="10" width="57"></a>';

	document.getElementById(this.sn+'thumbs').innerHTML = s;

	// set events
	//for (i=1; i<=this.noofthumbs; i++) {
	//	obj = document.getElementById(this.sn+'thumb'+i);
	//	obj.onMouseDown = this.doit;
		 //obj.onFocus = this.doit();
	//}
}

// *************************************
// change background color (change css name) for selected thumbnail
this.highlightthumb = function() {
	for ( i = 1; i <= this.noofthumbs; i++ ) {
		obj = document.getElementById(this.sn+'thumb'+i);
		if ( this.picpos == (this.thumbfirstpos+i-1) ) {
			if ( 'thumbselected' != obj.className ) obj.className = 'thumb selected';
		} else {
			if ( (this.thumbfirstpos+i-1) <= this.n ) {
				if ( 'thumb' != obj.className ) obj.className = 'thumb';
			} else {
				if ( 'thumb nohover' != obj.className ) obj.className = 'thumb nohover';
			}
		}
	}
}

// *************************************

this.debugprint = function(value) {
	if ( !this.showdebugmessages ) return;
	if ( document.theform )
		if ( document.theform.v1 )
			document.theform.v1.value = value;
}

// *************************************

this.setthumbpictures = function() {
	// set thumbs
	for ( i=1; i<=this.noofthumbs;i++ ) { // starts with 1=first button
		pos = this.thumbfirstpos-1 + i;
		if ( pos > this.n ) {
			fn = this.slidedatadir + 'thumb_nopic.jpg';
			tooltiptext = '';
		} else {
			fn = this.slidedatadir + 'thumb_nopic.jpg'; // clear old pic
			document.getElementById(this.sn+'imgthumb'+i).src = fn;
			tooltiptext = this.tooltips[pos-1];
			// tooltiptext has allready htmlspecialchars
			// title must be without htmlspecialchars means: undo htmlspecialchars
			tooltiptext =	tooltiptext.replace(/&quot;/ig, '"');
			tooltiptext =	tooltiptext.replace(/&amp;/ig, '&');
			tooltiptext =	tooltiptext.replace(/&#039;/ig, "'");
			tooltiptext =	tooltiptext.replace(/&lt;/ig, '<');
			tooltiptext =	tooltiptext.replace(/&gt;/ig, '>');

			if ( '' != this.thumbfilenameend ) {
				arrfn = this.pictures[pos-1][0].split('.');
				arrfn.pop();
				fn = this.picdir + arrfn.join('.')+this.thumbfilenameend;
			} else {
				fn = this.picdir + this.pictures[pos-1][0];
			}
		}
		document.getElementById(this.sn+'imgthumb'+i).src = fn;
		document.getElementById(this.sn+'imgthumb'+i).title = tooltiptext;
	}
}

// *************************************
// removes focus from <a onfocus="s1.removefocusborder(this)">...</a>
this.removefocusborder = function(obj) { // e=event
	//id = e.srcElement.id;
	//this.debugprint('here'+id);
	//obj = e.srcElement;
	if ( obj.blur ) obj.blur();
}

// *************************************

this.thumbblocknext = function() {
	this.shownext(this.noofthumbs);
}

// *************************************

this.thumbblockprevious = function() {
	this.shownext(-this.noofthumbs);
}

// *************************************

this.thumbrowsadd = function() {
	this.noofthumbs += this.thumbsperrow;
	// important: find first pos ob thumb block
	this.thumbfirstpos = Math.floor((this.picpos-1)/this.noofthumbs)*this.noofthumbs + 1;

	this.initthumbs();
	this.setthumbpictures();
	this.highlightthumb();


}

// *************************************

this.thumbrowsremove = function() {
	if ( (this.noofthumbs-this.thumbsperrow) >= this.thumbsperrow ) {
		this.noofthumbs -= this.thumbsperrow;
			// important: find first pos ob thumb block
		this.calcthumbfirstpos();

		this.initthumbs();
		this.setthumbpictures();
		this.highlightthumb();
	}
}

// *************************************

this.calcthumbfirstpos = function() {
	this.thumbfirstpos = Math.floor((this.picpos-1)/this.noofthumbs)*this.noofthumbs + 1;
}

// *************************************

this.makelinklist = function() {
	s = '';
	for (i=1; i<=this.n;i++) {
		tooltiptext = this.tooltips[i-1];
		if ( i == this.picpos ) theclassname = 'sel'; else theclassname = 'nosel';
		s += '<a href="javascript:'+this.sn+'.showpicture('+i+')" class="'+ theclassname +
				 '" onfocus="'+this.sn+'.removefocusborder(this)" alt="'+tooltiptext+'" title="'+tooltiptext+'">'+i+'</a>';
	}
	document.getElementById(this.sn+'linklist').innerHTML = s;
}

// *************************************

}

// *************************************
// class end
// *************************************