//
// thumbchange (c) by Peter Hofer  / 2007 
//  
// all rights reserved
// 
// this is NOT Freeware !!  - contact peter.hofer at ics.at
//

function thumbchange(img) { 
	
	this.images   = new Array();
//	this.id = mid; 	
	this.path = img.src; 
	
	this.currend_id = 2; 
	this.loaded = false; 
	this.img = img; 
	this.timer = 0;
	this.running = false;
	this.pos = 0; 
}

thumbchange.prototype.load = function() {
	var tmp = this.path.split('_');
	
	for (var ix = 0; ix<=3; ix++) {
                this.images[ix] = new Image();
		
		// this.images[ix].src = this.path.replace('_1.jpg','_'+ix+'.jpg');  
		this.images[ix].src = tmp[0]+ '_'+ix+'.jpg'; 
 		//  alert(this.images[ix].src); 
	}
	this.loaded = true; 
}

thumbchange.prototype.starte = function() {
	if(! this.loaded ) {
		this.load(); 
		// log("lade "+this.); 
	}
	// log(this.images[this.currend_id].src); 
	if(this.images[this.currend_id].complete) {
		document.getElementById(this.img.id).src = this.images[this.currend_id].src	; 
	}
	this.currend_id++; 
	if(this.currend_id == 4)  this.currend_id = 0;
	if(! this.running) {  	
		this.timer = setInterval("thumbs["+this.pos+"].laufe()",500); 
		this.running = true; 
	} 

} 

thumbchange.prototype.laufe = function() {
	if(this.images[this.currend_id].complete) {
		if(document.getElementById(this.img.id)){
		document.getElementById(this.img.id).src = this.images[this.currend_id].src; 
		}
	}
	this.currend_id++;
	log(this.currend_id); 
	if(this.currend_id == 4)  this.currend_id = 0;
	
}

thumbchange.prototype.stoppe = function() {
	if(this.running) {
		this.running = false; 
		clearInterval(this.timer); 
	}	 
}

thumbs = Array('A'); 


function log(t) {
	if(document.getElementById('debug')) {
	 	document.getElementById('debug').innerHTML = "<HR>"+t; 
	}
}

function fillthumbs()  {
	var imgs=document.getElementsByTagName('IMG');
	var m = 0; 
	var img; 
	var t = ''; 
	for(var i = 0; i < imgs.length; i++) {
		im = imgs[i]; 
		if(im.id.indexOf('IMG') == 0) { 
			var img = document.getElementById(im.id); 
			thumbs[m] = new thumbchange(img);  		
			thumbs[m].pos = m; 
			document.getElementById(im.id).setAttribute('tump',m); 
			m = thumbs.length; 
		}
	}

}

function fillthumb(id)  {
	obj = document.getElementById(id); 
	var m = thumbs.length;
	thumbs[m] = new thumbchange(obj);  		
	thumbs[m].pos = m; 
	document.getElementById(id).setAttribute('tump',m); 

}



function th_starte(elem) {
	if(elem.getAttribute('tump') != '') {
		var m = elem.getAttribute('tump')
		thumbs[m].starte(); 
	}
}

function th_stoppe(elem) {
	if(elem.getAttribute('tump') != '') {
		var m = elem.getAttribute('tump')
		thumbs[m].stoppe();
	}
}
