var Pkf = {
    init: function(){
        this.locations = $('deutschlandkarte');
        if (this.locations) {
            this.initImagemap();
        }
    },
    initImagemap: function(map){
        this.locationsPos = this.locations.getPosition();
        this.locationDiv = new Element('div',{
            'class':'area-over',
            'styles':{
             },
             'events':{
                'click': this.mapAreaClick.bind(this)
             }
        }).inject(document.body);
        this.areaTitle = new Element('div',{
            'class':'area-title',
            'styles':{
                'display':'none'
            }
        }).inject(document.body);
        this.fxTitle = new Fx.Style(this.areaTitle,'opacity',{'duration':1000});
        this.currentArea = null;
        this.locations.addEvent('mouseover',this.imgMouseOver.bind(this));
        $('deutschlandkarte.map').getElements('area').each(function(el){
            el.addEvent('mousemove',this.mapAreaMouseMove);
            el._title = el.title.replace(/\\n/g,'<br />');
            el.title = '';
            el.alt = '';
        }.bind(this));
    },
    imgMouseOver: function(event){
        this.hideAreas();
    },
    mapAreaMouseMove: function(ev){
        if (Pkf.currentArea != null) return;
        Pkf.showArea.bind(Pkf)(this);
    },
    mapAreaClick: function(){
        if (this.currentArea.target == "_blank"){
            window.open(this.currentArea.href);
        }
        else if (this.currentArea.href){
            window.location.href=this.currentArea.href;
        }
    },
    showArea: function(area){
        this.locationsPos = this.locations.getPosition();
        this.currentArea = area;
        this.locationDiv.setStyle('display','block');
        var coords = area.coords.split(',');
        var x = parseInt(coords[0]);
        var y = parseInt(coords[1]);  
        var offX = Math.round((coords[2]-x-Pkf.locationDiv.offsetWidth)/2);
        var offY = Math.round((coords[3]-y-Pkf.locationDiv.offsetHeight)/2);
        this.locationDiv.setStyles({
            'left':(Pkf.locationsPos.x+x+offX)+'px',
            'top':(Pkf.locationsPos.y+y+offY)+'px',
            'display':'block'
        }); //.highlight('#C0C0FF');
        this.areaTitle.setHTML(area._title).setStyles({
            'left':(Pkf.locationsPos.x+x+offX-30)+'px',
            'top':(Pkf.locationsPos.y+y+offY+20)+'px',
            'display':'block',
            'visibility':'hidden'
        });
        this.fxTitle.start(0,1);
    },
    hideAreas: function(){
        this.locationDiv.setStyle('display','none');
        this.currentArea = null;
        this.areaTitle.setStyle('display','none');
        //this.fxTitle.start(1,0);
    }
}           
window.addEvent('load',Pkf.init.bind(Pkf));
