var streckenabschnitte = {

	objectID             : 0,

	idContainer          : '',
	idContainerRemoveMe  : '',
	idContainerVon       : '',
	idContainerWeiter    : '',
	idContainerBis       : '',

	rowTemplateVon       : '',
	rowTemplateWeiter    : '',
	rowTemplateBis       : '',

	rowHeightVon         : '',
	rowHeightWeiter      : '',
	rowHeightBis         : '',

	jsonOrte             : [],
	jsonOrteVonName      : {},
	jsonOrteBisName      : {},

	stationData			 : {},
	stationObjects		 : {},
	stationList			 : {},

	hideCounter			 : 0,

	init : function() {

		this.idContainer         = 'adresscontainer_'+this.objectID;
		this.idContainerRemoveMe = 'adresscontainer_removeme_'+this.objectID;
		this.idContainerVon      = 'adresscontainer_von_'+this.objectID;
		this.idContainerWeiter   = 'adresscontainer_weiter_'+this.objectID;
		this.idContainerBis      = 'adresscontainer_bis_'+this.objectID;

		//Template laden
		this.rowTemplateVon      = $(this.idContainerVon).innerHTML;
		this.rowTemplateWeiter   = $(this.idContainerWeiter).innerHTML;
		this.rowTemplateBis      = $(this.idContainerBis).innerHTML;

		//Höhe des Templates bestimmen
		this.rowHeightVon        = $(this.idContainerVon).getHeight();
		this.rowHeightWeiter     = $(this.idContainerWeiter).getHeight();
		this.rowHeightBis        = $(this.idContainerBis).getHeight();

		//Template aus dem Dokument entfernen
		$(this.idContainerRemoveMe).replace('');

	},

	changeStations : function() {



		// 1. Sind stationen Vorhanden?
		var stationCount = this.countStations();
		if (stationCount >= 1) {

			// 2. Daten Pro Station Merken
			this.saveStationData();

			// 3. Statioen ausblenden
			this.hideAllStations(); // Ruft this.makeNewStations() auf um erst weiterzumachen wenn die alten Stationen weg sind
		} else {
			this.makeNewStations();
		}






	},

	makeNewStations : function() {
		// 5. Stationscontainer vorbereiten
		this.prepareStations();

		// 6. Stationen mit Daten füllen
		this.fillStationData();

		// 6.1 Stationen Funktionen Init
		this.initStationRows();

		// 4. Verschoben wegen variabler stationshöhe Container auf neue Höhe Stellen
		var newHeight = this.getNewHeight();
		this.setContainerHeight(newHeight);

		// 7. Blende alle Stationen ein
		this.showAllStations();
	},



	countStations : function() {
		//Zähle die Stationscontainer im Hauptcontainer
		var stationContainer = $(this.idContainer).getElementsByClassName('stationContainer');
		return stationContainer.length;
	},

	getNewHeight : function() {
		var newHeight = 0;

		var stationContainer = $(this.idContainer).getElementsByClassName('stationContainer');
		for (i=0; i<stationContainer.length; i++) {
			newHeight = newHeight+stationContainer[i].getHeight();
		}

		return newHeight;
	},

	setContainerHeight : function(height) {
		// Container auf neue Höhe Stellen
		var morpher = new Effect.Morph(this.idContainer, {style: 'height: '+height+'px',  duration: 0.5, delay: 0.2});
	},

	prepareStations : function() {
		// Stationen Zählen
		var stationCount = 0;
		for (var stationId in this.stationList) {
			stationCount++;
		}

		var i=1;
		for (var stationId in this.stationList) {
			if (i== 1) {
				var row = this.rowTemplateVon.replace(/###LIID###/g, stationId);
				$(this.idContainer).insert(row);

				// Hier muss die klasse von weiter auf bis gestellt werden,
				// da die station am ersten tag die endstation ist und nicht die zwischenstation
				// Wenn es dur eine tour zwischen 2 stationen ist
				if (i == stationCount) {
					$('row_von_nach'+stationId+'_'+this.objectID).removeClassName('weiter');
					$('row_von_nach'+stationId+'_'+this.objectID).addClassName('bis');
				}
			} else if (i==stationCount) {
				var row = this.rowTemplateBis.replace(/###LIID###/g, stationId);
				$(this.idContainer).insert(row);
			} else {
				var row = this.rowTemplateWeiter.replace(/###LIID###/g, stationId);
				$(this.idContainer).insert(row);
			}
			// Element aus den Sichtbaren Bereich schieben
			$('ad_row_template'+stationId+'_'+this.objectID).setStyle({left: '660px'});
			i++;
			var datepickerClassNames = 'format-d-m-y divider-dot input-text w_medium highlight-days-67 range-low-2009-04-01 range-high-2009-10-31';

			// Daterpicker vorbereiten
			if ($('von_datePicker'+stationId+'_'+this.objectID)) {
				$('von_datePicker'+stationId+'_'+this.objectID).addClassName(datepickerClassNames);
			}
			if ($('bis_datePicker'+stationId+'_'+this.objectID)) {
				$('bis_datePicker'+stationId+'_'+this.objectID).addClassName(datepickerClassNames);
			}
		}
	},

	saveStationData : function() {
		//Hole eine Liste aller Stationen
		var stationContainer = $(this.idContainer).getElementsByClassName('stationContainer');
		var stationIds = Array();
		for (i=0; i<stationContainer.length; i++) {
			var stationId = stationContainer[i].id.replace(/ad_row_template/g, "");
			stationId = stationId.replace(/_/g, "");
			stationId = ((stationId*1)-(this.objectID*1))/100;
			stationIds.push(stationId);

			// Speicherobject Für station erstellen
			this.stationData[stationId] = {};

			//Calenderdaten speichern
			if ($('von_datePicker'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['date'] = $('von_datePicker'+stationId+'_'+this.objectID).value;
			}
			if ($('bis_datePicker'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['date'] = $('bis_datePicker'+stationId+'_'+this.objectID).value;
			}

			// Adresse Speichern
			if ($('von_address_form_hotel'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['von_hotelname'] = $('von_address_form_hotel'+stationId+'_'+this.objectID).value;
			}
			if ($('von_address_form_street'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['von_strasse'] = $('von_address_form_street'+stationId+'_'+this.objectID).value;
			}
			if ($('von_address_form_zip'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['von_plz'] = $('von_address_form_zip'+stationId+'_'+this.objectID).value;
			}
			if ($('von_address_form_city'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['von_ort'] = $('von_address_form_city'+stationId+'_'+this.objectID).value;
			}

			if ($('bis_address_form_hotel'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['bis_hotelname'] = $('bis_address_form_hotel'+stationId+'_'+this.objectID).value;
			}
			if ($('bis_address_form_street'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['bis_strasse'] = $('bis_address_form_street'+stationId+'_'+this.objectID).value;
			}
			if ($('bis_address_form_zip'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['bis_plz'] = $('bis_address_form_zip'+stationId+'_'+this.objectID).value;
			}
			if ($('bis_address_form_city'+stationId+'_'+this.objectID)) {
				this.stationData[stationId]['bis_ort'] = $('bis_address_form_city'+stationId+'_'+this.objectID).value;
			}
		}
	},

	fillStationData : function() {
		// Stationen Zählen
		var stationCount = 0;
		for (var stationId in this.stationList) {
			stationCount++;
		}

		var i=1;
		for (var stationId in this.stationList) {
			if (i== 1) {
				$('stationNameVon'+stationId+'_'+this.objectID).innerHTML = this.stationList[stationId]['vonName']
				$('stationNameBis'+stationId+'_'+this.objectID).innerHTML = this.stationList[stationId]['bisName']
			} else if (i==stationCount) {
				$('stationNameBis'+stationId+'_'+this.objectID).innerHTML = this.stationList[stationId]['bisName']
			} else {
				$('stationNameBis'+stationId+'_'+this.objectID).innerHTML = this.stationList[stationId]['bisName']
			}

			// Daten aus dem datenarray holen und funktionen einstellen
			if (typeof(this.stationData[stationId]) == 'object') {
				// Calenderdaten Setzen
				if ($('von_datePicker'+stationId+'_'+this.objectID)) {
					$('von_datePicker'+stationId+'_'+this.objectID).value = this.stationData[stationId]['date'];
				}
				if ($('bis_datePicker'+stationId+'_'+this.objectID)) {
					$('bis_datePicker'+stationId+'_'+this.objectID).value = this.stationData[stationId]['date'];
				}


				// Adresse Setzen

				if ($('von_address_form_hotel'+stationId+'_'+this.objectID) && this.stationData[stationId]['von_hotelname']) {
					$('von_address_form_hotel'+stationId+'_'+this.objectID).value  = this.stationData[stationId]['von_hotelname'];
				}
				if ($('von_address_form_street'+stationId+'_'+this.objectID) && this.stationData[stationId]['von_strasse']) {
					$('von_address_form_street'+stationId+'_'+this.objectID).value = this.stationData[stationId]['von_strasse'];
				}
				if ($('von_address_form_zip'+stationId+'_'+this.objectID) && this.stationData[stationId]['von_plz']) {
					$('von_address_form_zip'+stationId+'_'+this.objectID).value    = this.stationData[stationId]['von_plz'];
				}
				if ($('von_address_form_city'+stationId+'_'+this.objectID) && this.stationData[stationId]['von_ort']) {
					$('von_address_form_city'+stationId+'_'+this.objectID).value   = this.stationData[stationId]['von_ort'];
				}

				if ($('bis_address_form_hotel'+stationId+'_'+this.objectID) && this.stationData[stationId]['bis_hotelname']) {
					$('bis_address_form_hotel'+stationId+'_'+this.objectID).value  = this.stationData[stationId]['bis_hotelname'];
				}
				if ($('bis_address_form_street'+stationId+'_'+this.objectID) && this.stationData[stationId]['bis_strasse']) {
					$('bis_address_form_street'+stationId+'_'+this.objectID).value = this.stationData[stationId]['bis_strasse'];
				}
				if ($('bis_address_form_zip'+stationId+'_'+this.objectID) && this.stationData[stationId]['bis_plz']) {
					$('bis_address_form_zip'+stationId+'_'+this.objectID).value    = this.stationData[stationId]['bis_plz'];
				}
				if ($('bis_address_form_city'+stationId+'_'+this.objectID) && this.stationData[stationId]['bis_ort']) {
					$('bis_address_form_city'+stationId+'_'+this.objectID).value   = this.stationData[stationId]['bis_ort'];
				}
			}


			i++;
		}

		// Datenobject Löschen
		//this.stationData = {};
	},

	initStationRows :function() {
		this.stationObjects = {};
		for (var stationId in this.stationList) {

			if (typeof(this.stationObjects[stationId]) != 'object') {
				this.stationObjects[stationId] = {};
			}

			if ($('von_address'+stationId+'_'+this.objectID)) {
				this.stationObjects[stationId]['von'] = new adresscontainer();
				this.stationObjects[stationId]['von'].objectID = this.objectID;
				this.stationObjects[stationId]['von'].idPrefix = 'von_';
				this.stationObjects[stationId]['von'].idPostfix = stationId+'_'+this.objectID;
				this.stationObjects[stationId]['von'].parentContainer = this.idContainer;
				this.stationObjects[stationId]['von'].init();
			}
			if ($('bis_address'+stationId+'_'+this.objectID)) {
				this.stationObjects[stationId]['bis'] = new adresscontainer;
				this.stationObjects[stationId]['bis'].objectID = this.objectID;
				this.stationObjects[stationId]['bis'].idPrefix = 'bis_';
				this.stationObjects[stationId]['bis'].idPostfix = stationId+'_'+this.objectID;
				this.stationObjects[stationId]['bis'].parentContainer = this.idContainer;
				this.stationObjects[stationId]['bis'].init();
			}
		}

		//Datepicker inititialisieren
		datePickerController.create();
	},

	showAllStations : function() {

		for (var stationId in this.stationList) {
			var morpher = new Effect.Morph('ad_row_template'+stationId+'_'+this.objectID, {
				style: 'left: 0px;',
				duration: 0.6,
				delay: 0.5
			});
		}
	},

	hideAllStations : function() {

		var stationContainer = $(this.idContainer).getElementsByClassName('stationContainer');
		for (i = 0; i < stationContainer.length; i++ ) {

			var stationId = stationContainer[i].id.replace(/ad_row_template/g, "");
			stationId = stationId.replace(/_/g, "");
			stationId = ((stationId*1)-(this.objectID*1))/100;


			//Destructor für die Addressobjekte
			if (typeof(this.stationObjects[stationId]['von']) == 'object') {
				this.stationObjects[stationId]['von'].destruct();
			}
			if (typeof(this.stationObjects[stationId]['bis']) == 'object') {
				this.stationObjects[stationId]['bis'].destruct();
			}


			var deleteStation = function(station, parentThis) {
				$(station).replace('');
				parentThis.hideCounter--;
				if (parentThis.hideCounter == 0) {
					parentThis.makeNewStations();
				}
			}

			var countStation = function(station, parentThis) {
				parentThis.hideCounter++;
			}

			var afterFinishFunc = deleteStation.bind('',stationContainer[i], this);
			var beforeStartFunc = countStation.bind('',stationContainer[i], this);
			var morpher = new Effect.Morph(stationContainer[i], {
				style: 'left: 660px;',
				duration: 0.6,
				delay: 0.0,
				beforeStart : beforeStartFunc,
				afterFinish : afterFinishFunc
			});
		}
	}

}

