//============================================================+
// 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_Tmpl = Class.create();
PT_Tmpl.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="slide_#{c}" style="display:none; position:absolute; z-index:#{c};">' +
			'	<a id="slide_img_#{c}" href="javascript:void(0)">' +
			'		<img src="../../../var/image/slide_a/20jahre_a#{c}.png" alt="#{title}" onload="pt.intro.item_ready(\'#{c}\')" style="border:0;" />' +
			'	</a>' +
			'</div>'

		);

	}
}

var PT_Intro = Class.create();
PT_Intro.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
			, false
		);

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

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

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

		/**
		* @var object of the current item
		*/
		this.el_akt=$('slide_'+this.items_c);
		/**
		* @var object of the current item link
		*/
		this.el_akt_img=$('slide_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("pt.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=$('slide_'+this.items_c);
		Event.stopObserving($('slide_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=pt.tmpl.img['c'].evaluate({
			c:this.items_c
			,title:'Slideshow'

		});
		this.stage.el.innerHTML+=html;
		this.el_next=$('slide_'+this.items_c);

		this.to_handler=window.setTimeout("pt.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)?5:c-1;

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

		new Effect.Parallel(
			[
				new Effect.Fade('slide_'+old_c, {sync: true})
				,new Effect.Appear('slide_'+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($('slide_img_'+this.items_c), "click", this.e_play_click);

					}.bind(this)
				}
		);
	}
}


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

/**
* initialises the slideshow objects
* @since 1.0 (2008-10-10)
*/
function init() {
	pt.tmpl= new  PT_Tmpl();
	pt.intro= new  PT_Intro(true);
}


/*
 * Overwrite setUsability() in frontend.js to route to init() afterwards
 */
function setUsability() {
	if (document.cookie) {
		var gespeichert=document.cookie.split("_");
		if (gespeichert.length>1) {
			contrastScheme=gespeichert[0];
			us_schriftgroesse=gespeichert[1];
			us_schriftgroesse=us_schriftgroesse.split(";");
			us_schriftgroesse=us_schriftgroesse[0];
		}
	} else {
		document.cookie=contrastScheme+"_"+us_schriftgroesse;
	}
	if(us_schriftgroesse=="") us_schriftgroesse=0.7;
	document.body.style.fontSize=us_schriftgroesse+'em';
	switch(contrastScheme) {
		case "lc":
			document.getElementById("stylesheet_hc").disabled=true;
			document.getElementById("stylesheet_lc").disabled=false;
		break;
		default:
			document.getElementById("stylesheet_hc").disabled=false;
			document.getElementById("stylesheet_lc").disabled=true;
}
init();
init2();
}

/*
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init);
} else if (window.attachEvent) {
	window.attachEvent("onload", init);
} else {
	window.onload=init;
}
*/