
var Pkf = {
    init: function(){
        this.locations = document.id('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.Tween(this.areaTitle,{'property':'opacity','duration':1000});
        this.currentArea = null;
        this.locations.addEvent('mouseover',this.imgMouseOver.bind(this));
        document.id('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.set('html',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);
    }
}           

var ToolTickerbox = new Class({
    Implements: [Options],
    options:{
        
    },
    initialize: function(options) {
        this.setOptions(options);
        if (this.options.counter){
            this.setupCounter();
            this.attachCounter();
        }
        if (this.options.ticker){
            this.setupTicker();
            this.startTicker();
        }
    },
    setupCounter: function() {
        var endDate;
        if (matches = this.options.counter.end.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)) {
            //SQL Date
            endDate = new Date(
                matches[1].toInt(),
                matches[2].toInt()-1,
                matches[3].toInt(),
                matches[4].toInt(),
                matches[5].toInt(),
                matches[6].toInt()
            );
        }
        else {
            endDate = Date.parse(this.options.counter.end);
        }
        if (!endDate) {
            return;
        }
        this.endTime = (endDate.getTime() / 1000).toInt();
        this.counterElement = this.options.counter.element || $(this.options.box).getElement('.counter');
        this.msMinutes = 60;
        this.msHours = this.msMinutes * 60;
        this.msDays = this.msHours * 24;
    },
    attachCounter: function() {
        this.setCounter();
        this.counterTimer = this.setCounter.periodical(1000,this); 
    },
    detachCounter: function() {
        $clear(this.counterTimer);
    },
    setCounter: function() {
        var now = ($time() / 1000).toInt();
        if (this.endTime <= now){
            this.renderCounter(0,0,0,0);
            this.detachCounter();
            return;
        }
        var diff = this.endTime - now, remaining;
        var days = Math.floor(diff / this.msDays); remaining = diff - days * this.msDays;
        var hours = Math.floor(remaining / this.msHours); remaining = remaining - hours * this.msHours;
        var minutes = Math.floor(remaining / this.msMinutes); remaining = remaining - minutes * this.msMinutes;
        var seconds = Math.floor(remaining);
        this.renderCounter(days, hours, minutes, seconds);
    },
    renderCounter: function(days, hours, minutes, seconds){
        if (!this.counterElement){
            return;
        }
        var params = {
            'days': days || 0,
            'hours': ((hours || 0)+'').padLeft(2,'0'),
            'minutes': ((minutes || 0)+'').padLeft(2,'0'),
            'seconds': ((seconds || 0)+'').padLeft(2,'0')
        };
        var template = this.options.template || '{days} Tage {hours}:{minutes}:{seconds}';
        this.counterElement.set('html',template.substitute(params));
    },
    setupTicker: function(){
        this.tickerTextElement = this.options.tickerTextElement || $(this.options.box).getElement('.ticker-text');
        this.tickerContainerElement = this.options.tickerContainerElement || $(this.options.box).getElement('.ticker-container');
        this.tickerWidth = this.tickerTextElement.offsetWidth;
        this.tickerInitialLeft = this.tickerContainerElement.getStyle('width').toInt();
        this.tickerLeft = this.tickerInitialLeft;
        this.tickerStep = this.options.ticker.step || 1;
        this.tickerSpeed = this.options.ticker.speed || 50;
        if (Browser.Engine.trident && Browser.Engine.Version <= 4){
            var styles = this.tickerContainerElement.getStyles('border-left-width','border-right-width','padding-left','padding-right');
            var offset = 0, parentElement = this.tickerContainerElement.getParent();
            offset += (styles['border-left-width'] || 0).toInt();
            offset += (styles['border-right-width'] || 0).toInt();
            offset += (styles['padding-left'] || 0).toInt();
            offset += (styles['padding-right'] || 0).toInt();
            parentElement.setStyle('width',(parentElement.getStyle('width').toInt()-offset)+'px');
            alert((parentElement.getStyle('width').toInt()-offset)+'px')
        }
    },
    startTicker: function() {
        this.tickerTextElement.setStyle('left',this.tickerInitialLeft);
        this.tickerLeft = this.tickerInitialLeft;
        this.tickerTimer = this.setTicker.periodical(this.tickerSpeed,this);
    },
    stopTicker: function() {
        $clear(this.tickerTimer);
        this.tickerTextElement.setStyle('left',0);
    },
    setTicker: function(){
        this.tickerLeft -= this.tickerStep;
        if (this.tickerWidth + this.tickerLeft <=0){
            this.tickerLeft = this.tickerInitialLeft;
        }
        this.tickerTextElement.setStyle('left',this.tickerLeft);
    }
});

window.addEvent('load',Pkf.init.bind(Pkf));