/*
* Written by Yannick VA
* de Persgroep Publishing
* v1.4 10/03/2009 13:46:49
*/
var InfoWindow = { }
InfoWindow = Class.create({
	initialize: function(element, options) {
		element = $(element);
		this.element = element;
		this.div = null;
		this.options = options;
		this.active = false;
		this.top = 0;
		this.left = 0;
		this.paddingTop = this.options.paddingTop ? this.options.paddingTop : 20;
		this.paddingLeft = this.options.paddingLeft ? this.options.paddingLeft : 0;
		this.leftCorrection = false;

		Event.observe(this.element,'mouseover',this.mouseOver.bindAsEventListener(this));
		Event.observe(this.element,'mouseout',this.mouseOut.bindAsEventListener(this));
	},
	
	mouseOver: function(event){	
		this.createPageDimensions();
		this.elmInfo();

		if(parseInt(this.left) + parseInt(this.options.width) + this.paddingLeft > this.winWidth()){
			this.left = this.winWidth() - this.options.width - 30;
			this.leftCorrection = true;
		}
		
		this.active = true;
		this.createInfo();
		
		if(this.top + this.div.getHeight() + this.paddingTop > this.height){
			this.div.style.top = (this.winHeight() - this.div.getHeight() - this.paddingTop) + 'px';
		}
		else{
			var tmp = this.top + (this.paddingTop-20);
			
			if(this.leftCorrection){
				tmp += this.element.getHeight();
			}
			
			this.div.style.top = tmp+'px';
		}

		if(typeof Effect != 'undefined' && this.options.useEffect){
			Effect.Appear(this.options.id, {duration: 0.5});

		}
		else{
			this.div.style.display = 'block';
		}
		
		Event.stop(event);
	},
	
	mouseOut: function(event){
		if(this.active){
			document.body.removeChild(this.div);
		}
		this.active = false;
		Event.stop(event);
	},
	
	createInfo: function(){
		if(this.active){
			var my_div = document.createElement('div');
			my_div.id = this.options.id;
			my_div.style.position = 'absolute';
			my_div.style.left = (parseInt(this.left) + parseInt(this.element.getWidth())+this.paddingLeft)+'px';
			my_div.style.width = this.options.width+'px';
			my_div.style.display = 'none';
			my_div.className = 'infowindow';
			my_div.innerHTML = this.options.text;
			document.body.appendChild(my_div);
			
			this.div = $(this.options.id);
		}
	},
	
	elmInfo: function(){
		Position.prepare();
	 
		var offsets = Position.positionedOffset(this.element);
		this.top = offsets[1];
		this.left = offsets[0];
	},
	
	winWidth: function(){ 
		return document.body.offsetWidth || window.innerWidth || document.documentElement.clientWidth || 0; 
	},
       
	winHeight: function(){
		return document.body.offsetHeight || window.innerHeight || document.documentElement.clientHeight || 0; 
	},
	
	createPageDimensions: function(){
		var windowWidth, windowHeight;
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
	
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
	
		if(yScroll < windowHeight){
			this.height = windowHeight;
		} else {
			this.height = yScroll;
		}
	
		if(xScroll < windowWidth){
			this.width = windowWidth;
		} else {
			this.width = xScroll;
		}
	}
});