/* Author: 

*/


$(function(){
    $('<img />').load(function(){

        $('body.index #promo-index').css({'display': 'block'});
        if(Modernizr.cssanimations) return;
        
        $('body.index #promo-index article').css({
                'opacity': 0,
                'margin-left': -150
            }).delay(1000).animate({
                'opacity': 1,
                'margin-left': 200
            }, 1000);
    
        if($.browser.msie && $.browser.version > 6 && $.browser.version < 9) return;
        
        $('body.index #promo-index img.left').css({
                'opacity': 0,
                'margin-left': -150
            }).delay(500).animate({
                'opacity': .6,
                'margin-left': 0
            }, 1000, function(){
                $('body.index #promo-index img.left').css({'opacity': 1});
            });
            
       $('body.index #promo-index img.right').css({
                'opacity': 0,
                'margin-left': 150
            }).delay(500).animate({
                'opacity': .6,
                'margin-left': 0
            }, 1000, function(){
                $('body.index #promo-index img.right').hide();
            });
       
    }).attr('src', $('#promo-index img').eq(0).attr('src'));
   
   
    $('#moon').addClass('rotated');
    
});

$(function(){
   
    var trinkTime = function(){ return parseInt(Math.random()*3000 +3000) },
        trinkStar = function(){ return ".trinkle-star"+parseInt(1+Math.random()*3) };
    
    setTimeout(function(){
        $(trinkStar()).animate({
            'opacity': 1
        }, 100, function(){
            $(this).animate({
                'opacity': 0
            }, 10);
        })
        
        setTimeout(arguments.callee, trinkTime());
    }, trinkTime());
    
});


var Animation = function(id, options){
    var that = this;
    this.options = $.extend({
        defaultanimation: 'default',
        animations: {},
        dontbreak: [],
        autoplay: true,
        loop: true,
        vertical: true,
        frameHeight: 0,
        frameWidth: 0,
        height: 64,
        width: 64,
        slidex: 0,
        slidey: 0,
        fps: 25,
        image: '',
        onstart: function(){},
        onstop: function(){}
    }, options || {});
    
    this.frame = $(id);
    this.options.animations['default'] = [];
    this.step = -1;
    
    this.frame.css({
        'width': this.options.width +'px',
        'height': this.options.height +'px',
        'overflow': 'hidden',
        'zoom': 1,
        'position': 'relative'
    });
    
    $('<img />').load(function(){
        
        that.image = $('<img />').attr('src', that.options.image).css({
            'position': 'absolute',
            'top': that.options.slidex + 'px',
            'left': that.options.slidey + 'px'
        });
        
        that.frame.append(that.image);
        
        that.options.frameHeight = that.options.frameHeight || that.options.height;
        that.options.frameWidth = that.options.frameWidth || that.options.width;
        
        that.size = [that.image.width(), that.image.height()];
        that.steps = that.options.vertical?
                    Math.ceil(that.size.y / that.options.frameHeight):
                    Math.ceil(that.size.x / that.options.frameWidth);
                    
        for(var i=0;i<that.steps;i++)
            that.options.animations['default'].push(i);
        
        that.play(null, !that.options.autoplay);
        
    }).attr('src', that.options.image);
}


Animation.prototype.play = function(animation, firstStepOnly){
    var that = this;
    
    this.stop();
    this.currentAnimation = animation || this.options.defaultanimation;
    this.animation = this.options.animations[this.currentAnimation];
    this.options.onstart.call(this);
    
    this.step = -1;
    if(firstStepOnly) this._step();
    else this.timer = setInterval(function(){ that._step.call(that); }, parseInt(1000 / this.options.fps));
    return this;
}


Animation.prototype.addEvent = function(event, animation){
    var that = this;
    this.frame[event](function(){
        that.play.call(that, animation, false)
    });
    return this;
}

Animation.prototype.stop = function(){
    if(this.timer) clearInterval(this.timer);       
    this.currentAnimation = null;
    return this;
}
    
Animation.prototype._step = function(){
    this.step++;
    if(this.step >= this.animation.length)
        if(this.options.loop) this.step = 0;
        else {
            this.options.onstop.call(this);
            return this.stop();
        }
        
    var param = {};
    param[this.options.vertical? "marginTop": "marginLeft"] =
            -this.animation[this.step]*this.options[this.options.vertical? "height": "width"];
    this.image.css(param);
    
    return this;
}
    


