// Hier werden Funktionen definiert
DivControl=function(tours, tooltips, tooltip, list, description, map) {
	for(var i=0; this.name='__divCtrl'+i, window[this.name]; i++);
	window[this.name]=this; // Das aktuelle Fenstern
	// Definition der Funktionen
	this.tours=tours; // Funktion Tours in tours.js
	this.tooltips=tooltips; 
	this.tooltip=tooltip;
	this.list=list;
	this.description=description;
	this.map=map;
	
	// Abfragen ob Variablen gesetzt wurden
	if(map && map.src) this.map_src=map.src;
	this._idSelected='_';
	this.lastTooltipKey='';
	this.regexp={};

	// ein Beschreibender Text wird generiert
	this.writeTooltip=function(key) {
		if(!key) key='empty';
		if(this.timeout) {
			window.clearTimeout( this.timeout );
			delete( this.timeout );
		}
		this.timeout=window.setTimeout(
			'window[\''+this.name+'\'].writeTooltipTimeout(\''+key+'\')'
			,300
		);
	}
	
	// setzt den Timeout f?r einen Tooltip fest
	this.writeTooltipTimeout=function(key) {
		if(this.timeout) delete( this.timeout );
		if(this.tooltip) {
			var t=this.tooltip;
			if( !t.hasChildNodes() ) {
				t.appendChild( document.createTextNode('') );
				this.writeTooltip(key);
			} else if(key!=this.lastTooltipKey) {
				this.lastTooltipKey=key;
				t.style.backgroundImage= key=='_' ? 'none' : '' ;
				t.replaceChild( document.createTextNode(this.tooltips[key]) , t.firstChild );
			}
		}
	}
	
	// Hier werden die TourID's in die Registrierung geschrieben
	this.regexp._id=/^_./;
	this.regexp.headline=/^h/;
	this.regexp.tour_id=/^tour(_.+)$/;
	this.writeTourList=function() {
		if(this.list) {
			var l=this.list;
			if( !l.hasChildNodes() ) {
				for(var _id in this.tours) if( _id.match(this.regexp._id) ) {
					var t=document.createElement('DIV');
					t.id='tour'+_id;
					t.className='tour';
					t.divCtrl=this;
					t.onmouseover=function()	{ divCtrl.onTourOver(		this.id.replace(divCtrl.regexp.tour_id, '$1') , 1 ); };
					t.onmouseout=function()		{ divCtrl.onTourOut(		this.id.replace(divCtrl.regexp.tour_id, '$1') ); };
					t.onclick=function()			{ divCtrl.onTourClick(	this.id.replace(divCtrl.regexp.tour_id, '$1') ); };
					t.appendChild( document.createTextNode(this.tours[_id].name) );
					l.appendChild(t);
				} else if( _id.match(this.regexp.headline) ) {
					var t=document.createElement('DIV');
					t.className='headline';
					t.appendChild( document.createTextNode(this.tours[_id].name) );
					l.appendChild(t);
				}
			}
		}
	}

	this.writeTourDescription=function(_id) {
		if(this.description) {
			if(!_id) _id=this._idSelected;
			var d=this.description;
			if( !d.hasChildNodes() ) {
				d.appendChild( document.createElement('IMG') );
				d.appendChild( document.createElement('P') );
				d.lastChild.appendChild( document.createTextNode('') );
				this.writeTourDescription(_id);
			} else {
				d.childNodes[0].src=this.tours[_id].image_description_src;
				var p=d.childNodes[1];
				p.replaceChild( document.createTextNode(this.tours[_id].description) , p.firstChild );
			}
		}
	}

	this.regexp.insert_over=/$/;
	this.regexp.tour_over=/ tour_over/g;
	this.onTourOver=function(_id, dontScroll) {
		if(this._idOver!=_id) {
			this._idOver=_id;
			this.dontScroll=dontScroll;
			if(this.timeoutOver) window.clearTimeout(this.timeoutOver);
			this.timeoutOver=window.setTimeout('window[\''+this.name+'\'].onTourOverTimeout()', 300);
		}
	}
	this.onTourOverTimeout=function() {
		var _id=this._idOver
				,dontScroll=this.dontScroll
		;
		delete(this.timeoutOver);
		if(this.map) this.map.src=this.tours[_id].image_src;
		if(this.list) {
			var t=document.getElementById('tour'+_id);
			if(t) t.className=t.className.replace(this.regexp.insert_over, ' tour_over');
			var l=this.list;
			if(!dontScroll && l.scrollTop!=undefined && l.scrollHeight!=undefined) {
				var length=0, i=null;
				for(var __id in this.tours) if( __id.match(this.regexp._id) || __id.match(this.regexp.headline) ) {
					if(__id==_id) i=length;
					length++;
				}
				l.scrollTop=Math.round(i*parseInt(l.scrollHeight)/length);
			}
		}
		this.writeTourDescription(_id);
	}
	this.onTourOut=function(_id) {
		if(this._idOver && _id==this._idOver) {
			if(this.timeoutOver) {
				window.clearTimeout(this.timeoutOver);
				delete(this.timeoutOver);
			}
			delete(this._idOver);
		}
		if(this.map) this.map.src=this.map_src;
		if(this.list) {
			var t=document.getElementById('tour'+_id);
			t.className=t.className.replace(this.regexp.tour_over, '');
		}
		this.writeTourDescription( this._idSelected );
	}

	this.regexp.insert_selected=/ tour_selected|$/;
	this.regexp.tour_selected=/ tour_selected/g;
	this.onTourClick=function(_id) {
		if(this.list) {
			var t;
			t=document.getElementById('tour'+this._idSelected);
			if(t) t.className=t.className.replace(this.regexp.tour_selected, '');
			t=document.getElementById('tour'+_id);
			if(t) t.className=t.className.replace(this.regexp.insert_selected, ' tour_selected');
		}
		this._idSelected=_id;
	}

	return this;
}
