// author: Sven Rhinow		website: http://www.sr-tag.de
// gkh_layer is MIT-Licensed

    var myLayer = new Class({
	
	Implements: [ Options ],
	
	options:{
	    parentEl : $('middle'),    
	    layerID : 'layer',    
	    closeID : 'closelayer',    
	    contentID : 'layercontent', 
	    mlIframe : 'mlIframe',
	    closeTxt:'close',   
	    duration : 100,
	    //    da der Layer obere Bereiche überdecken soll
	    //    Höhe vom Headimage + Höhe der Hauptnavigation
	    topheight : 305+50,
	    itemcount : 0,
	    parentsize : '',
	    initiframe : false  //false erstellt nur ein leeres div wenn true wird das iFrame direkt beim laden der Seite aufgerufen
	},
	
	initialize: function(options){
	    
	    //overwrite options
	    this.setOptions(options);
	    this.parentEl = this.options.parentEl;
	    this.layerID = this.options.layerID;
	    this.closeID = this.options.closeID;
	    this.contentID = this.options.contentID;
	    this.mlIframe = this.options.mlIframe;
	    this.closeTxt = this.options.closeTxt;
	    this.duration = this.options.duration;
	    this.topheight = this.options.topheight;
	    this.itemcount = this.options.itemcount;
	    this.parentsize = this.options.parentsize;
	    this.initiframe = this.options.initiframe;
// 	    console.log(this.options);
	    //URL-parameter zur sofortigen Darstellung auswerten
	    var myURI = new URI(document.location);
	    var showif = myURI.getData('showif', 'query');
	    if(showif==1) this.initiframe = true; 
 	    
	    
	    //alle Links mit rel="openlinks" mit Click-Event ausstatten
	    var links = $$("a").filter(function(el) {
		return el.rel && el.rel.test(/^openlayer/i);
	    });
	    links.each(function(item,index){
		
		this.itemcount++;
		
		item.addEvent('click', function(event){
		    event.stop(); //Prevents the browser from following the link.
		    window.scrollTo(0, 0);
		    this.open(item);
		    		     
		}.bind(this));
		    
	     }.bind(this));
	     
	     //nur erstellen wenn relevante Links auf dieser Seite existieren
	     if(this.itemcount > 0){	
		 this.createHtml();
		 if(this.initiframe) {
// 		     console.log(this.initiframe);
		       $(this.mlIframe).tween('opacity',1);
		       $(this.layerID).tween('opacity',1).setStyle('display', 'block');		 
		 }else{
		     $(this.layerID).setStyle('display', 'none');
		 }
	     }
	
	},
	
	createHtml: function(){	    
	     	    
	    //hoehe des Hauptbereichs holen    
	    var parentsize = this.parentEl.getSize();
            //global setzen
            this.parentsize = parentsize;
            
            //hoehe des Hauptbereichs holen    
	    var parentsize = this.parentEl.getSize();

	    // Layer-Hintergrund erstellen
	    var Layer = new Element('div', {id: this.layerID, html: ''});
	    Layer.setStyles({
		width : parentsize.x,
		height : parentsize.y + this.topheight,
		top: -this.topheight 
	    });
	    Layer.inject(this.parentEl,'top');
	    
	    // Schliessen-Button erstellen
	    var CloseLink = new Element('a', {id: this.closeID, html: this.closeTxt, href: '.'});
	    CloseLink.addEvent('click', function(event){
		    event.stop(); //Prevents the browser from following the link.
		    this.close();	
	    }.bind(this));
	    CloseLink.inject(Layer,'top'); 
	    
	    // Content-Container erstellen
	    var Content = new Element('div', {id: this.contentID, html: ''});
	    Content.inject(Layer,'bottom');
	    
	    // Inhalts-Iframe erstellen (div-Platzhalter)	
	       
	    if(this.initiframe){
	    
		var ifr = new Element('iframe',{
		    src:'map.html',
		    id:this.mlIframe ,
		    width:parentsize.x-30,
		    height:parentsize.y-40,
		    backgroundColor: '#F2f2f2',
		    frameborder:0
		    });
		ifr.setStyles({
// 		    paddingTop : 40,
// 		    paddingLeft: 30
		});
	    
	    }else{
		var ifr = new Element('div', {id: this.mlIframe, html: ''}); 			    
	    }
            ifr.inject($(this.contentID),'bottom');
	},
	close: function(){
	   $(this.mlIframe).tween('opacity',0);
	   $(this.layerID).tween('opacity',0);
	   $(this.layerID).setStyle('display', 'none');
	},
	
	open: function(el){
           
           //eventuell existierendes iframe loeschen
           $(this.mlIframe).destroy();
           
	    // Inhalts-Iframe erstellen	    
	    var ifr = new Element('iframe',{
		src:el.href,
		id:this.mlIframe ,
		width:this.parentsize.x-30,
		height:this.parentsize.y-40,
		frameborder:0
		});
	    ifr.setStyles({
// 		paddingTop : 40,
// 		paddingLeft: 30
	    });
	    ifr.inject($(this.contentID),'bottom');	
	               
//            console.log($(this.mlIframe).href);
           
// 	   $(this.mlIframe).tween('opacity',1);
	   $(this.layerID).tween('opacity',1).setStyle('display', 'block');    
	}
	
    });    
    //var ml = new  myLayer();


