/*
 * author: Michal Wrzesniewski
 * for: softomasz.pl
 */
var ChristmasPanel =  {

    numberOfSnowflakes:10,
    snowflakeImage: '/images/star.png',
    snowflakes:[],

    panel : null,
    panelW : 400,
    panelH : 400,
    dx :[],
    xp :[],
    yp :[],
    am :[],
    stx:[],
    sty:[],

    init:function(){
        this.createWindow();
        this.createSnowFlakes()
        this.snow()
    },
    createWindow: function(){

        this.panel = new Element('DIV',{
            id:'christmas-panel'
        });
        this.panel.setStyle({
            position:'absolute',
            top:70+'px',
            zindex:1000,
            overflow:'hidden',
            background:'url(/images/christmas.jpg) no-repeat 0 0',
            border:'2px solid #FFFFFF',
            width:this.panelW+'px',
            height:this.panelH+'px'
        });

        var textContainer = new Element('P',{});

        var text = 'Radosnych \u015awiąt Bożego Narodzenia<br/>oraz pomyślności w Nowym Roku<br/>';
        text += '<b>Wszystkim naszym aktualnym i przysz\u0142ym Klientom</b>';
        text += '<br/><br/>\u017cyczy Zespół AQUAMO';
        textContainer.update(text);
        textContainer.setStyle({
            color:'#FFFFFF',
            margin:0,
            paddingTop:100+'px',
            fontWeight:'normal',
            lineHeight:30+'px',
            fontStyle:'italic',
            fontFamily:' tahoma,sans-serif',
            letterSpaceing:0+'px',
            wordSpaceing:0+'px',
            fontSize:15+'px',
            cursor: 'hand',
            cursor: 'pointer'
        });

        this.panel.insert({
            top:textContainer
        });


        this.setLeft();
        var bar = this.createBottomClosingBar();
        this.panel.insert({
            bottom:bar
        });
        document.body.insert({
            top:this.panel
        });
    },
    createBottomClosingBar:function(){
        var bar = new Element('div',{
            id:'christmas-panel-closing-bar'
        });
        bar.setStyle({
            display:'block',
            position:'absolute',
            height:50+'px',
            width:this.panelW+'px',
            bottom:0,
            textAlign:'center',
            border:'0px solid red'
        });
        var button = new Element('div',{
            id:'christmas-panel-closing-bar-button'
        });
        button.setStyle({
            display:'block',
            marginTop:'10px',
            height:20+'px',
            paddingTop:5+'px',
            border:'0px solid black',
            cursor: 'hand',
            cursor: 'pointer',
            width:100+'px'
        });

        
        button.setStyle({
            marginLeft:parseInt(this.panelW/2-((parseInt(button.style.width))/2))+'px'
        })
        this.panel.observe('click',this.onCloseButtonClick.bindAsEventListener(this), true);
        button.update('<strong>Zamknij</strong>');
        bar.insert(button);
        return bar;
    },
    setLeft:function(){
        this.panel.setStyle({
            left:parseInt(($(document.body).measure('width')/2-(this.panelW/2)))+'px'
        })
    },

    onCloseButtonClick:function(event){
        this.snowflakes.each(function(snowflake ,i ){
            snowflake.remove();
        })
        this.snowflakes = [];
        $(this.panel.id).remove();
    },
    createSnowFlakes:function(){


        for(var i = 0; i<this.numberOfSnowflakes; i++){
            this.dx[i] = 0;                        // set coordinate variables
            this.xp[i] = Math.random() * parseInt(this.panel.style.width)-50;  // set position variables
            this.yp[i] = Math.random() * parseInt(this.panel.style.height);
            this.am[i] = Math.random()*20;         // set amplitude variables
            this.stx[i] = 0.02 + Math.random()/10; // set step variables
            this.sty[i] = 0.7 + Math.random();     // set step variables
            

            var image = new Element('IMG',{
                id:'snowflake_'+i
            })
            image.setAttribute('src',this.snowflakeImage)


            image.setStyle({
                position:'absolute',
                zindex:i,
                visibility: 'visible',
                top: 15+'px',
                left: 15+'px'
            })
            this.panel.insert({
                top:image
            })
            this.snowflakes.push(image);
        }
    },

    snow: function() {
        
        this.snowflakes.each(function(snowflake ,i ){
            this.yp[i] += this.sty[i];

            if (this.yp[i] > parseInt(this.panel.style.height)-50) {
                this.xp[i] = Math.random()* parseInt(this.panel.style.width)-this.am[i]-30;
                this.yp[i] = 0;
                this.stx[i] = 0.02 + Math.random()/10;
                this.sty[i] = 0.7 + Math.random();
            }
            this.dx[i] += this.stx[i];


            $(snowflake.id).style.top = parseInt(this.yp[i])+'px';
            $(snowflake.id).style.left = parseInt(this.xp[i] + this.am[i]*Math.sin(this.dx[i]))+'px';
            
        }.bind(this))
        setTimeout(function(){
            ChristmasPanel.snow()
        }, 10);
    }

    
}

