/* start zipped version Do Mär 18 11:46:23 CET 2010 */
// foto.js
/**
 * Foto Detailpage script
 */

jQuery( function() {
	var jThumbs = jQuery('img.thumb'),		// thumbails
		jFullsize = jQuery('.mfsizeb a');	// fullsize button link
	if (!jThumbs.length) return;
	self.diGal = {
			IMG_BASEURL: document.location.href, //jThumbs[0].style.backgroundImage.match(/^url\((.+?)\d+\)$/)[1],
			FULLSIZE_BASEURL: document.location.href, // jFullsize[0].href.match(/^(.+?)\d+(?:\?.*)?[ ]*$/)[1],
			FULLSIZE_URL: jFullsize[0].href,
			jFullsize: jFullsize,
			jPrev: jQuery('#fprev'),
			jNext: jQuery('#fnext'),
			jPlay: jQuery('#fplay'),
			current: 0,
			pics: [],
			currPicIndex: -1,
			playTimer: null,
			PLAY_INTERVAL: 2500,
			jMidifoto: jQuery('#midifoto'),
			next: function(e) {
				e = !!e;
				if (e) diGal.stop();
				if (diGal.currPicIndex==diGal.pics.length-1 && e) return false;
				diGal.switchTo( (diGal.currPicIndex+1) % diGal.pics.length, !e);
				return false;
			},
			prev: function(e) {
				diGal.stop();
				if (diGal.currPicIndex==0) return false;
				diGal.switchTo(diGal.currPicIndex-1);
				return false;
			},
			play: function() {
				diGal.playTimer = setInterval(function(){diGal.next(0)}, diGal.PLAY_INTERVAL);
				var img = diGal.jPlay.one('click', diGal.stop)[0];
				img.src = img.src.replace('play.gif','pause.gif');
				diGal.next(0);
			},
			stop: function() {
				if (!diGal.playTimer) return;
				clearInterval(diGal.playTimer);
				diGal.playTimer = 0;
				var img = diGal.jPlay.one('click', diGal.play)[0];
				img.src = img.src.replace('pause.gif','play.gif');
			},
			switchTo: function(idx, dontStop) {
				if (!dontStop) diGal.stop();
				diGal.currPicIndex = idx;
 
				//diGal.jFullsize.attr('href', diGal.FULLSIZE_BASEURL + diGal.pics[idx].fullID);
                //http://www.docinsider.de/detailsPage/imageOverlay/2186532?KeepThis=true&TB_iframe=true&width=600&height=600
				diGal.jFullsize.attr('href', "/detailsPage/imageOverlay/"+diGal.pics[idx].fullID+"?KeepThis=true&TB_iframe=true&width=600&height=600" );
				diGal.jMidifoto.attr('src', diGal.pics[idx].midiUrl);
				diGal.jNext.css('opacity', (diGal.currPicIndex==diGal.pics.length-1)? 0.2 : 1);
				diGal.jPrev.css('opacity', (diGal.currPicIndex==0)? 0.2 : 1);
			},
			init: function() {
				var currMidiID = this.jMidifoto.attr('src').match(/\/(\d+)\//)[1];	// id of current midsize image

				// iterate thumbnails...
				jThumbs.each( function(i,img) {
					img.ownIndex = diGal.pics.length;		// attach index (*not* ID!) directly to <img/> element
					/^[^\d]+(\d+).(\d+)$/.exec(img.name);	// parse midsize+ large image's id
                    var pic = {
						midiID: RegExp.$1,
						fullID: RegExp.$2,
						id: img.style.backgroundImage.match(/\/(\d+)\//)[1],
                        fullUrl: img.parentNode.href,
                        midiUrl: img.parentNode.href//.replace(this.fullID, this.midiID)
					};
                    pic.midiUrl = pic.midiUrl.replace(pic.fullID, pic.midiID); 


					
					diGal.pics[img.ownIndex] = pic;
					if (pic.midiID == currMidiID) {
						diGal.currPicIndex = this.ownIndex;	// thumb for currently displayed midsize found?
					}
					
				}).bind('click', function() {			// bind click handlers to thumbs...
					diGal.switchTo(this.ownIndex);
					return false;
				});
				if (this.currPicIndex<0 || !this.pics.length) return;
				this.jPrev.bind('click',this.prev);
				this.jNext.bind('click',this.next);
				this.jPlay.one('click', this.play);
				if (this.pics.length>1) {
					this.jFullsize.bind('click', this.stop);
					this.jPlay.show();
					jQuery('#detailreiter li a').bind('click', this.stop);	// switching tabs stops autoplay
				}
				this.switchTo(this.currPicIndex);
			}
		};
	
	 diGal.init();
});
/* end zipped version Do Mär 18 11:46:23 CET 2010 */
