/* [version]2007-10-22 15:30[/version] */
/**
Benötigt die Module event.js, geometry.js
*/
var Flomp = {};
Flomp.offset_x = 4;
Flomp.offset_y = 8;
Flomp.visible = false;
Flomp.target = null;
Flomp.box = null;
Flomp.contentBox = null;
Flomp.opacity = 0.8;
this.box_total_width = 0;

Flomp.setBox = function( pBox ){
	this.box = pBox;
	this._computeBoxTotalWidth();
	this._setOpacity();
};

Flomp.setContentBox = function( pBox ){
	this.contentBox = pBox;
	this._setOpacity();
};

Flomp.setTarget = function( pTarget ){
	if( pTarget ){
		this.target = pTarget;
	}
};

Flomp._computeBoxTotalWidth = function(){
	var box = this.box;
	if( box ){
		if( box.currentStyle ){	// IE
			this.box_total_width = parseInt( box.currentStyle.width );
			this.box_total_width += parseInt( box.currentStyle.paddingLeft );
			this.box_total_width += parseInt( box.currentStyle.paddingRight );
			var borderWidth = parseInt( box.currentStyle.borderWidth );
			if( !isNaN(borderWidth) ){
				this.box_total_width += borderWidth * 2;
			}
		}else if( window.getComputedStyle ){
			var styles = window.getComputedStyle( box, null );
			this.box_total_width = parseInt( styles.width );
			this.box_total_width += parseInt( styles.paddingLeft );
			this.box_total_width += parseInt( styles.paddingRight );
			var borderWidth = parseInt( styles.borderWidth );
			if( !isNaN(borderWidth) ){
				this.box_total_width += borderWidth * 2;
			}
		}else{
			this.box_total_width = 0;
		}
	}else{
		this.box_total_width = 0;
	}
};

Flomp.show = function(){
	if( this.box && !this.visible ){
		this.box.style.display = '';
		this.visible = true;
	}
};

Flomp.hide = function(){
	if( this.box && this.visible ){
		this.box.style.display = 'none';
		this.visible = false;
	}
};


Flomp.moveTo = function( x, y ){
	var left = this._computeLeftPosition( x );
	var top = this._computeTopPosition( y );

	// Soll nach links aufgeklappt werden?
	if( this.target && this.target.flapToTheLeft ){
		//alert( this.box_total_width );
		left -= this.box_total_width;
		//alert( left );
	}

	this.box.style.left = left + 'px';
	this.box.style.top  = top  + 'px';
}

Flomp.setContent = function( content ){
	if( !this.contentBox ){
		var container = document.createElementNode( 'div' );
		container.id = 'flomp_content';
		this.contentBox = container;
		this._setOpacity();
	}
	this.contentBox.innerHTML = content;
};

Flomp._computeLeftPosition = function ( x ){
	return (x + this.offset_x + Geometry.getHorizontalScroll());
}

Flomp._computeTopPosition = function ( y ){
	return (y + this.offset_y + Geometry.getVerticalScroll());
};

Flomp._setOpacity = function(){
	if( this.box ){
		setOpacity( this.box, this.opacity );
	}
	if( this.contentBox ){
		setOpacity( this.contentBox, 1 );	// Der Content (speziell der Text) soll nicht durchscheinen.
	}
};




function displayFlomp( e ){
	if( !e ) e = window.event;	// IE Event Model.

	if( !Flomp.visible ){
		var target = this.target;
		if( target ){
			Flomp.target = target;
			Flomp.setContent( target.text );
			Flomp.moveTo( e.clientX, e.clientY );
			Flomp.show();
		}
		this.className = 'flomp_target_hot';
	}
}

function moveFlomp( e ){
	if( !e ) e = window.event;	// IE Event Model.
	var target = this.target;
	if( target ){
		Flomp.moveTo( e.clientX, e.clientY );
	}
}

function hideFlomp( e ){
	Flomp.hide();
	this.className = 'flomp_target';
}

/**
Die Flomps müssen in der Reihenfolge registriert werden, in der sie
am Bildschirm angeordnet sind, damit jedes dritte Bild korrekt erkannt werden kann.
*/
function registerFlomps(){
	var box = document.getElementById( 'flomp' );
	var contentBox = document.getElementById( 'flomp_content' );
	Flomp.setBox( box );
	Flomp.contentBox = contentBox;

	for( var i=0; i<arguments.length; i++ ){
		var loc_id = arguments[i];
		var target = {};

		target.loc_id = loc_id;
		target.text = document.getElementById( 'flomp_text_' + loc_id ).innerHTML;
		// Der dritte und letzte Container einer Zeile poppt den Hilfetext nach links.
		target.flapToTheLeft = ((i%3)==2) ? true : false;

		var element = document.getElementById( 'flomp_target_' + loc_id );
		//element.setAttribute( 'target', target );
		element.target = target;

		registerEventLevel0( element, 'mouseover', displayFlomp );
		registerEventLevel0( element, 'mousemove', moveFlomp );
		registerEventLevel0( element, 'mouseout', hideFlomp );
		registerEventLevel0( element, 'click', displayFlomp );
		var onclick_handler = function(e){
			// this bezieht sich bei einem Handler auf das Element, nicht auf die Funktion.
			var target = this.target;
			if( target ){
				location.href = 'locations.php?l=' + target.loc_id + '&p=' + pageNumber;
			}
		};
		registerEventLevel0( element, 'click', onclick_handler );
	}
}


/**
Opacity wird als Dezimalwert angegeben.
*/
function setOpacity( element, opacity ){
	if( element && element.style ){
		var style = element.style;
		var ie_opacity = Math.round( opacity * 100 );
		style.opacity = "" + opacity;
		style.filter = "alpha(opacity=" + ie_opacity + ")";
	}
}
