/**
 * JavaScript für www.exedis.de
 *
 * @copyright © exedis – Internet Konzeption, Gestaltung, Realisation
 * @copyright http://www.exedis.de
 * @author René Kubach
 */

$(document).ready(function(){
	// Externe Links
	$("a.extern").each(function(){
		$(this).attr({target: '_blank', title: 'Externer Link: '+ this.title + ' [neues Fenster]'
		});
	});
	// Referenzen
	Referenzen.init();
	// Impressum
	$('#impr-strg a').click(function(){
		$('#impr-inhalt').slideToggle( 700, 'easeInOutQuint' );
	});
});

Referenzen = {

	liste: 			[],
	akt:			-1,
	naechste:		0,
	aktiv: 			true,
	slideshowTimer:	{},
	domObj:			{},

	init: function () {
		if ($('.no-css:visible').length) {
			return; // Abbrechen, wenn CSS nicht geladen
		}
		$('#referenzen .ref').each(function(){
			var dom  = $(this);
			var name = dom.find('h3').clone();
			var info = dom.find('div.info').clone();
			var url  = dom.find('div.url').clone();
			var img  = dom.find('div.screen').clone();
			Referenzen.liste.push({ name: name, info: info, url: url, img: img });
		});
		this.domObj = $('#scRef').css({ opacity:0 }).append('<div id="refImg"></div><div id="refText"></div>');
		$('#gehe-zurueck').click(function(){ Referenzen.stopSlideshow(); Referenzen.zurueck(); }).animate({ left: 0, opacity: 1 },{duration: 450, easing: 'easeInOutQuint'});
		$('#gehe-weiter').click(function(){ Referenzen.stopSlideshow(); Referenzen.weiter(); }).animate({ right: 0, opacity: 1 },{duration: 450, easing: 'easeInOutQuint', complete: function(){
			Referenzen.laden();
			Referenzen.startSlideshow();
		} });
		this.tastaturNavigationAktivieren();
	},

	weiter: function () {
		if (!this.aktiv) {
			this.naechste = this.akt+1==this.liste.length ? 0 : this.akt+1;
 			this.tauschen();
 		}
	},

	zurueck: function () {
		if (!this.aktiv) {
			this.naechste = this.akt==0 ? this.liste.length-1 : this.akt-1;
			this.tauschen();
		}
	},

	tauschen: function () {
		this.aktiv = true;
		$('#refImg').animate({ left:'-40px' },{duration: 380, easing: 'easeInOutQuint'});
		this.domObj.animate({ opacity:0 },{duration: 380, easing: 'easeInOutQuint', complete: function(){ Referenzen.laden(); }});
	},

	laden: function () {
		var ref = this.liste[this.naechste];
		$('#refImg').empty().css('left',0).append(ref.img);
		$('#refText').empty().append(ref.name, ref.info, ref.url);
		this.akt = this.naechste;
		window.setTimeout(function(){ Referenzen.einblenden(); },200);
	},

	einblenden: function () {
		this.domObj.animate({ opacity:1 },{ duration: 600, easing: 'easeInOutQuint', complete: function () { Referenzen.aktiv = false; } });
	},

	startSlideshow: function () {
		Referenzen.slideshowTimer = window.setTimeout( "Referenzen.slideshow()", 12000 );
	},

	slideshow: function () {
		this.weiter();
		Referenzen.slideshowTimer = window.setTimeout( "Referenzen.slideshow();", 8000 )
	},

	stopSlideshow: function () {
		window.clearTimeout( Referenzen.slideshowTimer );
		Referenzen.slideshowTimer = window.setTimeout( "Referenzen.slideshow()", 20000 );
	},

	/*  ===== Tastaturnavigation ===== */

	tastaturNavigationAktivieren: function  () {
		$(document).bind('keydown', {}, Referenzen.tastaturEingabe);
	},

/*	tastaturNavigationDeaktivieren: function  () {
		$(document).unbind('keydown');
	},
*/
	tastaturEingabe: function ( e ) {
		var o = e.data.opts;
		var keycode = e.keyCode;
		var escapeKey = 27;
		var key = String.fromCharCode(keycode).toLowerCase();
		if ( keycode==escapeKey ) {
			Referenzen.stopSlideshow();
		} else if ( keycode==37 ) {
			Referenzen.stopSlideshow();
			Referenzen.zurueck();
		} else if ( keycode==39 ) {
			Referenzen.stopSlideshow();
			Referenzen.weiter();
		}
	}



};
