Europolgaz.mapAnimation = function() {
    this.initialize();
};
                       
Europolgaz.mapAnimation.prototype = {
    current: 0,
    delegate: null,
    durationPumpFade: 100,
    durationPumpTxtFade: 750,
    durationLine: 1500,
    animations: [],
    initialize: function() {
        if($("div.map-container").length == 0) {
            return false;
        }
        $('div.map-no-line').show();
        $('div.map-line').show();
        this.delegate = this.animate.createDelegate(this);
        this.createAnimation();
        this.animate();
    },
    animate: function() {
        if(this.current >= this.animations.length) {
            return false;
        }
        var animation = this.animations[this.current];
        if(typeof(animation.length) != 'undefined') {
            for(var i=0;i<animation.length;i++) {
                this.doAnimation(animation[i]);
            }
        } else {
            this.doAnimation(animation);
        }
        this.current++;
    },
    doAnimation: function(animation) {
        switch(animation.type) {
            case 'fadeIn':
                $(animation.object).fadeIn(animation.duration, animation.callback?this.delegate:null);
                break;
            case 'fadeOut':
                $(animation.object).fadeOut(animation.duration, animation.callback?this.delegate:null);
                break;
            case 'css':
                $(animation.object).animate(animation.properties, animation.duration, animation.callback?this.delegate:null);
                break;
            case 'show':
                $(animation.object).show();
                break;
            case 'end':
                this.endAnimation();
        }
    },
    endAnimation: function() {
        for(var i=1;i<=5;i++) {
            this.attachTxts(i);
        }
    },
    attachTxts: function(idx) {
        $('div.map-pump' + idx + '-mid').show();
        $('div.map-pump' + idx + '-mid').css('background', 0);
        $('div.map-pump' + idx + '-mid').css('zIndex', 11);
        $('div.map-pump' + idx + '-txt').css('zIndex', 10);
        var pump = $('div.map-pump' + idx + '-mid');
        pump.mouseenter(function(e){
            if($('div.map-pump' + idx + '-txt:hidden').length > 0) {
                $('div.map-pump' + idx + '-txt').show();
            }
        });        
        pump.mouseleave(function(e){
            if($('div.map-pump' + idx + '-txt:visible').length > 0) {
                $('div.map-pump' + idx + '-txt').hide();
            }
        });
        pump.css('cursor', 'pointer');
        pump.click(function(e) {
            document.location.href = '/gazociag/tlocznie_gazu/' + idx + '.html';
        });
    },
    getPumpAnim: function(idx, size, fadeIn) {
        return {
            type: fadeIn?'fadeIn':'fadeOut',
            duration: this.durationPumpFade,
            object: 'div.map-pump' + idx + '-' + size,
            callback: true
        };
    },
    getLineAnim: function(width) {
        return this.getLine(width, 'div.map-no-line');
    },                                                
    getLine: function(width, objectName) {
        return {
                type: 'css',
                properties: {width: width},
                duration: this.durationLine,
                object: objectName,
                callback: true
        };
    },                                                
    addPumpCircleAnim: function(idx, doFirst) {
        if(doFirst) {
            this.animations.push(this.getPumpAnim(idx, 'sml', true));
        }
        this.animations.push(this.getPumpAnim(idx, 'mid', true));
        this.animations.push(this.getPumpAnim(idx, 'big', true));
        this.animations.push(this.getPumpAnim(idx, 'big', false));
    },
    getPumpTxt: function(idx, fadeIn, callback) {
        return {
            type: fadeIn?'fadeIn':'fadeOut',
            duration: this.durationPumpTxtFade,
            object: 'div.map-pump' + idx + '-txt',
            callback: callback
        }
    },
    addPumpSwitchIn: function(width, idx) {
        this.animations.push([this.getLineAnim(width), this.getPumpTxt(idx, true, false)]);
    },
    addPumpSwitchOut: function(idx, idx2) {
        this.animations.push([this.getPumpTxt(idx, false, false), this.getPumpAnim(idx2, 'sml', true)]);
    },
    
    createAnimation: function() {
        this.animations.push(this.getLineAnim('582px'));   
        this.addPumpCircleAnim(5, true);
        this.addPumpSwitchIn('496px', 5);
        this.addPumpSwitchOut(5, 4);
        this.addPumpCircleAnim(4, false);
        this.addPumpSwitchIn('396px', 4);
        this.addPumpSwitchOut(4, 3);
        this.addPumpCircleAnim(3, false);
        this.addPumpSwitchIn('276px', 3);
        this.addPumpSwitchOut(3, 2);
        this.addPumpCircleAnim(2, false);
        this.addPumpSwitchIn('110px', 2);
        this.addPumpSwitchOut(2, 1);
        this.addPumpCircleAnim(1, false);
        this.addPumpSwitchIn('0px', 1);
        this.animations.push(this.getPumpTxt(1, false, true));
        
        var last = [];
        last.push({type: 'show', object: 'div.map-new-line-bg'});
        last.push({type: 'show', object: 'div.map-new-line'});
        last.push(this.getLine('0px', 'div.map-new-line-bg'));
        this.animations.push(last);
 
        this.animations.push({type:'end'});
    }
}
