//============================================================+
// File name   : pt_slide.class.js
// Begin       : 2008-10-10
// Last Update : 2008-10-11
// Author      : Tobias Prinz - tobias.prinz@ponton-lab.de - http://www.ponton-lab.de
// Version     : 1.0
// 	----------------------------------------------------------------------------
//  Copyright (C) 2008  Tobias Prinz - Ponton-Lab GmbH
//  ----------------------------------------------------------------------------
//
// Description : This is a javascript Object for display a slideshow using single graphic files
// 				 depending on the prototype framework with scriptacolous extension.
//============================================================+

var PT_Tmpl2 = Class.create();
PT_Tmpl2.prototype = {
	/**
	* initializeses templates.
	* @since 1.0 (2008-10-10)
	*/
	initialize: function() {
		/**
		* @var array for image related templates
		*/
		this.img= new Array();

		/**
		* @var object for slide template
		*/
		this.img['c'] = new Template(
			'<div id="slide2_#{c}" style="display:none; position:absolute; z-index:#{c};">' +
			'	<a id="slide2_img_#{c}" href="javascript:void(0)">' +
			'		<img src="../../../var/image/slide_b/20jahre_b#{c}.png" alt="#{title}" onload="pt2.intro.item_ready(\'#{c}\')" style="border:0;" />' +
			'	</a>' +
			'</div>'

		);

	}
}

var PT_Intro2 = Class.create();
PT_Intro2.prototype = {

	/**
	* initializeses slideshow.
	* @param bool autoplay Sets autoplaymode
	* @since 1.0 (2008-10-10)
	*/
	initialize: function(autoplay) {

		/**
		* @var array with item status
		*/
		this.items=new Array(
			false
			, false
			, false
			, false
			, false
			, false
			, false
			, false
			, false
			, false
			, false
			, false
			, false
		);

		/**
		* @var points to current item
		*/
		this.items_c=0;

		/**
		* @var number of items to display
		*/
		this.items_t=5;

		/**
		* @var object slideshow container
		*/
		this.stage2 = {
			el:$('stage2')
			,dim:$('stage2').getDimensions()
			,pos:Position.page($('stage2'))
		}

		/**
		* @var object of the current item
		*/
		this.el_akt=$('slide2_'+this.items_c);
		/**
		* @var object of the current item link
		*/
		this.el_akt_img=$('slide2_img_'+this.items_c);
		/**
		* @var binds as event listener
		*/
		this.e_play_click = this.play_click.bindAsEventListener(this);
		Event.observe(this.el_akt_img, "click", this.e_play_click);

		/**
		* @var object of the next item
		*/
		this.el_next=null;
		/**
		* @var timeout for autoplay. currently not in use
		*/
		this.t_play=6000;
		/**
		* @var handler for autoplay timeout. currently not in use
		*/
		this.to_handler=null;

		if(autoplay)this.to_handler=window.setTimeout("pt2.intro.play()", this.t_play);
	}

	/**
	* Routes click event.
	* @since 1.0 (2008-10-10)
	*/
	,play_click: function() {
		window.clearTimeout(this.to_handler);
		this.play();
	}

	/**
	* Gets click event, prepares new slide. stops old slide click event
	* @since 1.0 (2008-10-10)
	*/
	,play: function() {
		this.el_akt=$('slide2_'+this.items_c);
		Event.stopObserving($('slide2_img_'+this.items_c), "click", this.e_play_click);

		this.items_c++;
		if(this.items_c>=this.items_t) this.items_c=0;

		var html=pt2.tmpl.img['c'].evaluate({
			c:this.items_c
			,title:'Slideshow'

		});
		this.stage2.el.innerHTML+=html;
		this.el_next=$('slide2_'+this.items_c);

		this.to_handler=window.setTimeout("pt2.intro.play()", this.t_play);
	}

	/**
	* gets called onload by slide graphic. starts transition between old and new slide. deletes old slide
	* @param int c Number of the clicked slide
	* @since 1.0 (2008-10-10)
	*/
	,item_ready: function(c) {
		if(this.items[c]==true) return;

		var old_c=(c==0)?4:c-1;

		this.items[c]=true;
		this.items[old_c]=false;

		new Effect.Parallel(
			[
				new Effect.Fade('slide2_'+old_c, {sync: true})
				,new Effect.Appear('slide2_'+c, {sync: true, scaleX:false, scaleY:true})
			]
				,{
					duration: 1
					,afterFinish: function(effect) {
						Element.remove(effect.effects[0].element);

						this.e_play_click = this.play_click.bindAsEventListener(this);
					Event.observe($('slide2_img_'+this.items_c), "click", this.e_play_click);

					}.bind(this)
				}
		);
	}
}


/**
* namespace
* @since 1.0 (2008-10-10)
*/
var pt2=new Object();

/**
* initialises the slideshow objects
* @since 1.0 (2008-10-10)
*/
function init2() {
	pt2.tmpl= new  PT_Tmpl2();
	pt2.intro= new  PT_Intro2(true);
}