/* ---------------------- */
/*     Moving map         */
/* ---------------------- */

var Movingmap = new Class({
	getOptions: function(){
		return {
		};
	},

	initialize: function(texttriggers, details, options){
		this.setOptions(this.getOptions(), options);

		this.texttriggers = texttriggers;
		this.details = details;
		this.detailsList = new Array();
		this.fxList = new Array();
		this.detailHeightsList = new Array();
		this.activeId = "";

		details.each(function(item, index){
			//works only with FF: item.addEvent('load', function(){this.addLocation(item, shapes[index], this.details[index])}.bindWithEvent(this));
			this.addLocation(texttriggers[index],item);
		},this);
	},

	show: function(id){
		this.detailsList[id].setStyle('display', 'inline');
		this.fxList[id].start({'opacity': 1, 'height': this.detailHeightsList[id]});
	},
	hide: function(id){
		this.fxList[id].start({'opacity': 0, 'height': 0}).chain(function(){this.detailsList[id].setStyle('display', 'none')}.bind(this));
	},
	switcher: function(id){
		if(this.activeId != ""){
			this.hide(this.activeId);
		}
		if(id != this.activeId){
			this.show(id);
			this.activeId = id;
		}else{
			this.activeId = "";
		}
	},

	addLocation: function(texttrigger,target){
		var tarHeight = target.offsetHeight;
		var active = false;
		var targetID = target.id;
		this.detailsList[target.id] = target;
		this.fxList[target.id] = new Fx.Styles(target, {duration: 600});
		this.detailHeightsList[target.id] = target.offsetHeight;

		/* Modify Targets (List items) */
		var closeButton = new Element('img',{
			'src': 'http://www.pohl-boskamp.de/shared/nps/close.gif',
			'alt':'close',
			'class':'closelink',
			'events': {
				'click': function(){
					this.switcher(targetID);
				}.bind(this)
			}
		}).injectAfter(target.getChildren()[0]);

		this.fxList[target.id].start({'opacity': 0, 'height': 0});
		this.detailsList[target.id].setStyles({'display': 'none', 'overflow': 'hidden'});

		texttrigger.addEvent('click', function(e){
			e = new Event(e).stop();
			this.switcher(targetID);
		}.bind(this));
	}
});
Movingmap.implement(new Events);
Movingmap.implement(new Options);


/* ------------------------- */
/*      Inline Lexicon       */
/* ------------------------- */

var InlineBox = new Class({
	getOptions: function(){
		return {
		};
	},

	initialize: function(texttriggers, details, options){
		this.setOptions(this.getOptions(), options);
		this.anchorList = new Array();
		this.actives = new Array();
		this.boxList = new Array();
		this.fxListHeight = new Array();
		this.fxListOpacity = new Array();
		this.heightsList = new Array();
		this.activeId = "";

		// scan anchorList for those opening a box
		$A($$('a')).each(function(el){
			// we use a regexp to check for links that
			// have a rel attribute starting with "lexiconlink"
			if(el.rel && el.href && el.rel.test('^inlinebox', 'i')) {
				// el.onclick = this.click.pass(el, this);
				this.anchorList.push(el);
			}
		}, this);

		this.anchorList.each(function(item, index){
			this.addBox(item,index);
		},this);
	},

	addBox: function(item,number){
		this.boxList[number] = new Element('div',{'id':'box'+number, 'class':'inline-lexicon'}).setStyles({'display':'none','overflow':'hidden','height':0});
		if(item.getParent().getStyle('display')=='block'){
			this.boxList[number].injectAfter(item.getParent());
		}else{
			this.boxList[number].injectAfter(item.getParent().getParent());
		}
		this.actives[number] = false;

		this.fxListHeight[number] = new Fx.Styles(this.boxList[number], {duration: 800});
		this.fxListOpacity[number] = new Fx.Styles(this.boxList[number], {duration: 800});
		this.heightsList[number] = 0;

		item.addEvent('click', function(e){
			e = new Event(e).stop();
			this.switcher(number);
		}.bind(this));
	},

	loadContents: function(id) {
		// AJAX call here
		var ajaxCompleted = this.displayContent.pass(id, this);
		var ajaxFailure = this.ajaxFailure.bind(this);
		var updateBox = new Element('div').injectInside(this.boxList[id]);
		var ajaxOptions = {
			method: 		'get',
			update: 		updateBox,
			evalScripts: 	this.options.evalScripts,
			evalResponse: 	this.options.evalResponse,
			onComplete: 	ajaxCompleted,
			onFailure: 		ajaxFailure
		};
		this.ajaxRequest = new Ajax(this.anchorList[id], ajaxOptions).request();
		return false;
	},

	displayContent: function(id){
		this.boxList[id].setProperty('class','inline-lexicon-loaded');
		this.heightsList[id] = this.boxList[id].getChildren()[0].offsetHeight;
		var closeButton = new Element('img',{
			'src': 'http://www.pohl-boskamp.de/shared/nps/close.gif',
			'alt':'close',
			'class':'closelink',
			'events': {
				'click': function(){
					this.switcher(id);
				}.bind(this)
			}
		}).injectBefore(this.boxList[id].getChildren()[0]);
		this.fxListHeight[id].start({'height': this.heightsList[id]});
		// trace(this.fxList[id]+": "+toHeight+20);
	},

	ajaxFailure: function (){
		this.contents.setHTML('');
		this.error.clone().injectInside(this.contents);
		this.nextEffect();
		this.center.setStyle('cursor', 'pointer');
		this.bottom.setStyle('cursor', 'pointer');
		this.center.onclick = this.bottom.onclick = this.close.bind(this);
	},

	switcher: function(id){
		if(this.actives[id] == false){
			if(!this.boxList[id].childNodes[0]){
				this.loadContents(id);
			}
			this.actives[id] = true;
			this.show(id);
		}else{
			this.actives[id] = false;
			this.hide(id);
		}
	},
	show: function(id){
		this.boxList[id].setStyles({'display':'block','opacity':0}); //
		this.anchorList[id].setProperty('class','lexiconlink-open');
		if(this.boxList[id].childNodes[1]){this.fxListHeight[id].start({'height': this.heightsList[id]});}
		this.fxListOpacity[id].start({'opacity': 1});
	},
	hide: function(id){
		this.fxListOpacity[id].start({'opacity': 0}).chain(function(){this.boxList[id].setStyle('display', 'none')}.bind(this));
		this.fxListHeight[id].start({'height': 0});
		// this.boxList[id].setStyle('display', 'none')
		this.anchorList[id].setProperty('class','lexiconlink');
	}
});
InlineBox.implement(new Events);
InlineBox.implement(new Options);

// startup
Window.onDomReady(function(){new InlineBox();});
