var objLayer;
var layerName="sitemapLayer";//Name des LayerDivs
var loadUrl="http://www.berlinwasser.com/framework/sitemap.inc.php";
var loadingText="...";
var offsetY=20;//menulaenge ist fensterhoehe -offsetY
var insertId="framing1";//Id des Divs hinter das das menu inserted wird
var btnSelector ='a.btnSitemap'; //Selektor des ausloesenden Elements


var evt ={
    setEvents: function(){
        $$(btnSelector).each(function(target){
            target.addEvent('click',function(event){evt.funcInitLayer(target,event);return(false);});
             
        });
        $$('body').addEvent('click',function(){if(objLayer){objLayer.hide();}});
    },
    funcInitLayer:function(target,event){
        if(!objLayer){
            p = target.getPosition();
            x = p.x;
            y = p.y;
            objLayer = new Layer({x:x,y:y});
            
        }
        objLayer.show();
        var ev = new Event(event);
        ev.stopPropagation() ;
    }
}

var Layer = new Class({
    initialize: function(options){
        this.options =options
        this.dv = new Element("div");
        this.id=layerName;
        this.dv.setProperty('id',this.id);
        this.dv.setHTML(loadingText);
        this.state='hide';
        this.offsetY=offsetY;
        this.loadUrl=loadUrl;
        this.load();
    },
    show: function(){
    	if(this.state =='show'){
    		this.hide();
    		return;
    	}
    	var wh = window.getHeight()-(this.options.y + this.offsetY +20);
    	this.state='show';
    	this.dv.injectBefore(insertId);
        this.dv.addClass(layerName); 
        this.dv.setStyles({height:wh +'px', left:this.options.x +"px",top:this.options.y + this.offsetY +'px'});
        this.dv.style.overflow="scroll";
    },
    hide: function(){
    	this.state='hide';
        this.dv.setStyles({left:'-1000px'});
    },
    load: function(){
    	var aj = new Ajax(this.loadUrl +"?url="+window.location.pathname, {method: 'get',update:this.dv});
    	aj.request();
    }
});

 /*

window.onDomReady(function(){
	evt.setEvents();
});
*/
