/**
* JavaScripts für Picardie Thueringen
* 
* <i-D> internet & Design GmbH & Co. KG
* Erfurter Str. 35
* 99423 Weimar
* Deutschland
* Fon:     03643 7785 0
* Fax:     03643 7785 29
* E-Mail:  office@i-d.de
* Web:     http://www.i.d.de/
*
* @author Niels Bobogk <niels@i-d.de>
* @copy 2009 <i-D> internet & Design GmbH & Co. KG
*/

// Includes
idTools.Require("prototype-1.6.0.3");
// CSS
document.write('<link rel="stylesheet" type="text/css" href="/de/css/thpi/script-only.css" media="screen"/>');

// --------------------------------------------------------------------------------

// Abschnitt Konfiguration

// ACHTUNG: Slideshow wird in Extra-Datei thpi.config.js konfiguriert !!!
document.write('<script type="text/javascript" src="/de/js/thpi.config.js"></script>');

// Konstanten
var SEARCHBOX_DEFAULT_TEXT = 'Suchbegriff eingeben';         // Standard in Suchbox
var SEARCHBOX_MIN_LENGTH   = 3                               // Mindestlaenge einer Suchfeldeingabe
var CROSSFADE_INTERVAL     = 10;                             // Zeitabstand zwischen Ueberblendungen in sek.
var CROSSFADE_DURATION     = 1.5;                            // Dauer Ueberblendung in sek.
var SLIDESHOW_PATH         = "/de/bilder/thpi/slideshow/";   // Pfad zu den Slideshow-Bildern

// --------------------------------------------------------------------------------

/**
* Suchfeld mit Hinweistext;
* vorm Versenden wird Plausibilitaet geprueft
*/
var SearchBox = Class.create();
SearchBox.prototype = {
	/**
	* Initialisierung
	*/ 
	initialize: function(nodeBox) {
		if (nodeBox.tagName.toLowerCase() == 'input' && nodeBox.type.toLowerCase() == 'text') {
			this.box = nodeBox;
			this.box.value = SEARCHBOX_DEFAULT_TEXT;
			
			Event.observe(this.box, 'focus', this.onfocus.bindAsEventListener(this));
			Event.observe(this.box, 'blur', this.onblur.bindAsEventListener(this));
			Event.observe(this.box.form, 'submit', this.onsubmit.bindAsEventListener(this));
		}
	},
	
	/**
	* bei focus default-text entfernen
	*/ 
	onfocus: function() {
		if (this.box.value.strip() == SEARCHBOX_DEFAULT_TEXT) {
			this.box.value = '';	
		}
	},
	
	/**
	* bei blur default-text setzen, sofern Feld leer ist
	*/
	onblur: function() {
		if (this.box.value.strip().empty()) {
			this.box.value = SEARCHBOX_DEFAULT_TEXT;	
		}
	},
	
	/**
	* vorm Absenden auf plausiblen Suchbegriff pruefen und ggf. Absenden abbrechen
	*
	* @param object Event
	*/
	onsubmit: function(event) {
		if (this.box.value.strip().empty() || this.box.value.strip() == SEARCHBOX_DEFAULT_TEXT || this.box.value.strip().length < SEARCHBOX_MIN_LENGTH) {
			alert('Bitte geben Sie einen Suchbegriff ein!');
			this.box.focus();
			Event.stop(event);
		}
	}
}

// --------------------------------------------------------------------------------

/**
* Slideshow-Element
*/
var SlideshowItem = Class.create();
SlideshowItem.prototype = {
	/**
	* Initialisierung
	* Erstellung der Elemente
	*/ 
	initialize: function(index) {
		this.state = -1;
		
		this.img  = new Image();
		this.text = new Element('p').insert('');
		this.item = new Element('div', {'class' : 'item'}).insert(this.img);
		
		this.load(index);
		
		this.item.insert(this.text);
	},
	
	/**
	* Laden der Item-Daten
	*
	* @param int Index des zu Ladenden Slideshow-Items
	*/
	load: function(index) {
		// wird bereits geladen
		if (this.state = 0) return;
		
		try {
			var el = SlideshowItems[index];			
			if (el) {
				this.state = 0;
				
				this.img.src = SLIDESHOW_PATH + el.file;
				$(this.text).update(unescape(el.text));
				
				$(this.text).removeClassName('footer_de');
				$(this.text).removeClassName('footer_fr');
				$(this.text).addClassName('footer_' + el.lang);
			}
		} catch(err) {};
	},
	
	/**
	* Entladen der Item-Daten
	*/
	unload: function() {
		this.img.src = null;
		$(this.text).update('');
		$(this.text).toggleClassName('');
	},
	
	/**
	* gibt an, ob Element (Bild) geladen ist
	*
	* @return bool
	*/
	readyState: function() {
		// ... wird geladen
		if (this.state == 0 && !this.img.complete) return 0;
		// ...ist fertig geladen
		if (this.state == 0 && this.img.complete) {
			this.state = 1;
		}
		// sonst:
		return this.state;
	}
}

// --------------------------------------------------------------------------------

/**
* Slideshow oben links
*/
var Slideshow = Class.create();
Slideshow.prototype = {
	/**
	* Initialisierung
	*/ 
	initialize: function(nodeContainer) {
		// Elternelement
		this.container = nodeContainer;
		this.container.addClassName('jsSlideShowContainer');
		
		// wenn Dateien vorhanden sind
		if (SlideshowItems && SlideshowItems.length > 0) {
			// Gallerie zuruecksetzen
			this.reset();
			this.current = 1;
			
			// Elemente erzeugen
			this.ssi1 = new SlideshowItem(this.getNewIndex());
			this.ssi2 = new SlideshowItem(this.getNewIndex());
			
			// Elemente verstecken
			this.ssi1.item.hide(0);
			this.ssi2.item.hide(0);
			
			// Elemente in DOM einfuegen
			this.container.update(this.ssi1.item);
			this.container.insert(this.ssi2.item);
			
			// 1. Element anzeigen
			this.ssi1.item.appear( { duration: CROSSFADE_DURATION } );
			
			// weiterfuehrendes, periodisches Einblenden
			new PeriodicalExecuter(this.crossfade.bind(this), CROSSFADE_INTERVAL);
		}
	},
	
	crossfade: function() {	
		if (this.current == 1) {
			if (this.ssi2.readyState() != 1) {
				this.ssi2.load();
				return;
			}	
			
			this.ssi1.item.fade( {	
				duration: CROSSFADE_DURATION, 
				object: this,
				afterFinish: function() {					
					this.object.ssi1.load(this.object.getNewIndex());	
				}				
			});
			this.ssi2.item.appear( { duration: CROSSFADE_DURATION } );
			
			this.current = 2;
			
		} else if (this.current == 2) {
			if (this.ssi1.readyState() != 1) {
				this.ssi1.load();
				return;
			}	
			
			this.ssi2.item.fade( {	
				duration: CROSSFADE_DURATION, 
				object: this,
				afterFinish: function() {					
					this.object.ssi2.load(this.object.getNewIndex());					
				}				
			});
			this.ssi1.item.appear( { duration: CROSSFADE_DURATION } );
			
			this.current = 1;
		}
	},
	
	/**
	* (zufaelligen) Index des als naechstes zu zeigenden Elements ermitteln
	*/
	getNewIndex: function() {
		var rand;
		var max = SlideshowItems.length - 1;
		
		if (max <= 0) {			
			return max;
		}
		
		// Arraylaenge gibt Anzahl der Versuche vor
		for (var i = 0; i <= max; i++) {
			rand = this.getRandom(0, max);
			if (SlideshowItems[rand].shown == false) {
				SlideshowItems[rand].shown = true;
				return rand;	
			}
		}
		
		// reset, wenn kein freies Element mehr vorhanden ist..
		this.reset();
		
		// ...und nochmal neu
		return(this.getNewIndex());
	},
	
	/**
	* Zufallszahl innerhalb eines bestimmten Bereiches ermitteln
	*/
	getRandom: function (min, max) {
		if(min > max) return( -1 );
		if(min == max) return( min );
		return(min + parseInt( Math.random() * (max - min + 1)));
	},

	/**
	* alle Slideshow-Items auf "nicht gesehen" setzen
	*/
	reset: function() {
		$(SlideshowItems).each(function(s) {
			s.shown = false;
		});
	}
}

// --------------------------------------------------------------------------------

/**
* Box, welche alternativen Inhalt bei MouseOver anzeigt
*/
var ContentSwitchingBox = Class.create();
ContentSwitchingBox.prototype = {
	/**
	* Initialisierung
	*/ 
	initialize: function(nodeBox) {
		// Hauptbox 
		this.box = nodeBox;
		
		// Status
		this.isOpen = false;
		
		// alle enthaltenen Elemente
		this.desc = $(this.box).descendants();
		
		// Box mit default content
		for (var i = 0; i < this.desc.length; i++) {
			if (this.desc[i].hasClassName('contentDefault')) {				
				this.contentDefault = this.desc[i];
			} else if (this.desc[i].hasClassName('contentRollover')) {
				this.contentRollover = this.desc[i];
			}
		}
		// ggf. Hoehe des alternativen Inhalts anpassen,
		// um Flicker zu vermeiden
		var h = (this.contentDefault.getHeight() > this.contentRollover.getHeight()) ? this.contentDefault.getHeight() + 21 + 'px' : 'auto';
		$(this.contentRollover).setStyle({
			height: h
		});
		
		Event.observe(this.box, 'click', this.switchContent.bindAsEventListener(this));

		// offene Box beim Start
		if (this.box.hasClassName('open')) {
			this.switchContent();
		}
	},
	
	/**
	* wechseln des Inhalts je nach Mouseevent
	*
	* @param object Event-Ausloeser
	* @return void
	*/ 
	switchContent: function(event) {
		// beim Klick auf einen der Links nicht ausführen
		if (event && Event.element(event).tagName.toUpperCase() == 'A') {
			return;	
		}
		
		if (!this.isOpen) {
			// hide() funktioniert nicht, 
			// sofern display-Wert per CSS gesetzt wurde
			$(this.contentRollover).setStyle({
				display: 'block'
			});
			this.contentDefault.hide();
			
			this.isOpen = true;
		} else {
			this.contentDefault.show();
			this.contentRollover.hide();
			this.isOpen = false;
		}
	}	
}

// --------------------------------------------------------------------------------

/**
* Main-Klasse
*/
var Main = Class.create();
Main.prototype = {
	/**
	* Initialisierung
	*/ 
	initialize: function() {
		var self = this;
		
		// Contentwechselboxen initialisieren
		var arrTmpBoxes = $$('.contentSwitchingBox');
		this.arrBoxes = new Array(arrTmpBoxes.length); 		
		for (var i = 0; i < arrTmpBoxes.length; i++) {
			this.arrBoxes[i] = new ContentSwitchingBox(arrTmpBoxes[i]);
		}
		
		// Suchfeld initialisieren
		this.searchBox = new SearchBox($('search'));
		
		// Slideshow initialisieren
		this.slideShow = new Slideshow($('picHeader'));	
		
		// E-Mailadressen decodieren
		decodeEMail();
	}
}

// --------------------------------------------------------------------------------

// Start
Event.observe(window, 'load', function() {
	var main = new Main();
});

