/* orangeSlides
 * Creates slideshow presentations with jQuery
 *
 * @author David Bongard  (mail@bongard.net | www.bongard.net)
 * @version 0.9 - 20071028
 * @licence  http://www.opensource.org/licenses/mit-license.php MIT License
 *
 * Copyright (c) 2007 David Bongard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

//configuration
jSlides = {
	displayTime: 7600, //in milliseconds
	dynamicDisplayTime: false, //use a display time corresponding to the content-length of each slide
	dynamicDisplayFactor: 100, //milliseconds of display time per content character
	playbackMode: 1, //0 = pause, 1 = play
	transitionSpeed: 400, //in milliseconds
	currentSlide: 0,
	previousSlide: null,
	playSymbol: '&nbsp;&nbsp;- Abspielen -',
	pauseSymbol: '<!--<img src="images/tiny_red.gif" border="0" width="10" />-->&nbsp;&nbsp;&nbsp;- Anhalten -',
	containerId: '.jslide', // divs to transform to slides
	panelId: '#jslidePanel', // create a div with this id to use controls
	navigationId: '#jslideNavigation', // create a div with this id and add title-attributes to container-divs to use a navigation
	controlPanel: '<div id="jSlides_progress"><div id="jSlides_progressInner">&nbsp;</div></div>'
};

/*
 *	Find all slides and transform
 */
jSlides.init = function()
{
	// get number of slides
	this.numSlides = $(this.containerId).size();
	navigation = '';

	$(this.containerId).each(function(i)
	{
		// add class and hide
		$(this).addClass('jSlide_'+i).hide();

		// build navigation
		if($(this).attr('title') != '' && this.navigationId != ''){
			navigation = navigation+'<li id="jNav_'+i+'" class="clearfix"><a href="javascript:jSlides.goById('+i+');">'+$(this).attr('title')+'</a></li>';
			$(this).removeAttr('title')
		}
	});

	// add navigation
	if(this.navigationId != ''){
		$(this.navigationId).html('<ul>'+navigation+'</ul>');
	}

	// controls
	$(this.panelId).html(this.controlPanel);

	if(this.playbackMode == 1){
		this.play();
		this.progressTimer_run();
		$("#jSlides_switch").html(this.pauseSymbol); // pause symbol
	}else{
		$("#jSlides_switch").html(this.playSymbol); // play symbol
		this.progressTimer_reset();
	}

	// how number of slides
	$("#jSlides_all_slides").html(this.numSlides);

	// initialize
	this.first();
};

/*
 * calculate next slide
 */
jSlides.go = function(number)
{
	if(this.currentSlide+number <= this.numSlides-1 && this.currentSlide+number >= 0){
		this.previousSlide = this.currentSlide;
		this.currentSlide = this.currentSlide+number;
	}else if(this.currentSlide+number < 0){
		this.last();
	}else if(this.currentSlide+number > this.numSlides-1){
		this.first();
	}
	// go to slide
	this.transition();
};

/*
 * go to a certain slide
 */
jSlides.goById = function(id)
{
	if(id <= this.numSlides-1 && id >= 0){
		this.previousSlide = this.currentSlide;
		this.currentSlide = id;
	}
	// go to slide
	this.transition();
};

/*
 * transition between slides
 */
jSlides.transition = function()
{
	// get display time
	if(this.dynamicDisplayTime == true){
		var text = $(".jSlide_"+this.currentSlide).html();
		this.currentTime = Math.round(text.length*this.dynamicDisplayFactor);
	}else{
		this.currentTime = this.displayTime;
	}

	// change slides
	if(this.previousSlide != this.currentSlide){
		$(".jSlide_"+this.previousSlide).css('position','absolute').fadeOut(this.transitionSpeed, function(){
			$(".jSlide_"+jSlides.currentSlide).css('position','relative').fadeIn(jSlides.transitionSpeed);
		});
	}else{
		$(".jSlide_"+this.currentSlide).fadeIn(this.transitionSpeed);
	}

	// update controls
	$(this.navigationId + ' li').removeClass('current');
	$("#jNav_" + this.currentSlide).addClass('current');
	$("#jSlides_current_slide").html(jSlides.currentSlide+1);

	// start auto-playback
	if(this.playbackMode==1){
		clearTimeout(this.playbackTimeout);
		clearTimeout(this.playbackTrigger);
		this.progressTimer_run();
		this.play();
	}
};

/*
 * jump to first slide
 */
jSlides.first = function()
{
	this.go(eval('-'+this.currentSlide));
};

/*
 * jump to last slide
 */
jSlides.last = function()
{
	this.go(jSlides.numSlides-this.currentSlide-1);
};

/*
 * auto-playback
 */
jSlides.play = function()
{
	if(this.currentSlide < jSlides.numSlides-1){
		jSlides.playbackTimeout = setTimeout('jSlides.go(1);',this.currentTime);
	}else{
		jSlides.playbackTimeout = setTimeout('jSlides.first();',this.currentTime);
	}
};

/*
 * switch playback on or off
 */
jSlides.togglePlayback = function()
{
	if(this.playbackMode==0)
	{
		this.play(); this.progressTimer_run();
		$("#jSlides_switch").html(this.pauseSymbol); // pause symbol
		this.playbackMode=1;
	}
	else
	{
		$("#jSlides_switch").html(this.playSymbol); // play symbol
		clearTimeout(this.playbackTimeout);
		clearTimeout(this.playbackTrigger);
		this.playbackMode=0;
		this.progressTimer_reset();
	}
};

/*
 * show elapsing time while running auto-playback
 */
jSlides.progressTimer_run = function()
{
		this.progressTimer_reset();
		setTimeout(function(){
			$("#jSlides_progressInner").stop().animate({
				left: "0"
			},jSlides.currentTime-100,'linear');
		},Math.round(this.transitionSpeed/3+100));
};

/*
 * reset timer
 */
jSlides.progressTimer_reset = function()
{
	$("#jSlides_progressInner").stop().animate({
			left: "-169px"
	},Math.round(this.transitionSpeed/3),'linear');
};

/*
 * start slideshow
 */
$(document).ready(function(){
	jSlides.init();
	$('a').click(function(){
		this.blur();
	});
});