$(function()
{
    var self = this;
    
    // Common 
    
    this.colorbox = function()
    {
        if( $.fn.colorbox instanceof Function )
        {
            
            $( '.colorbox' ).colorbox( {
                opacity     : 0.4, 
                transition  : 'elastic',
                previous    : '',
                next        : '',
                close       : 'chiudi',
                innerWidth  : 800,
                innerHeight  : 400
            } );
            
            
            $( '.page-colorbox' ).colorbox( {
                opacity     : 0.4, 
                transition  : 'elastic',
                inline      : true,
                href        : function(){
                                return '#' + $( this ).attr( 'rel' )
                              },
                rel         : 'nofollow',
                previous    : '',
                next        : '',
                close       : 'chiudi',
                innerWidth  : 800,
                maxHeight  : 400
            } );
            
        }
        
        
    };

    
    // Page index.php
    
    this.initStructure = function(){
        
        $('h1').animate(
            {
                top : 20
            },
            {
                   queue : false,
                duration : 800,
                  easing : 'easeInOutExpo',
                complete : function(){
                    
                    $('#menu').animate(
                        {
                            width : 720
                        },
                        {
                            queue : false,
                            duration : 600,
                            easing : 'easeInOutExpo',
                            complete : function(){
                                            //openVideoAuguri();
                                            moveMessages();
                                       } 
                        }
                    );
                    
                    self.initOrder();
                        
                }
            }
        );
        
        $('#bottle-group').animate(
                    {
                        right : 0
                    },
                    {
                        queue : false,
                        duration : 1000,
                        easing : 'easeInOutExpo' 
                    }
        );
        
        // Events
        
        $('#label-buy-now').hover(
            
            function(){
                
                $(this).animate(
                    {
                        top : 50
                    },
                    {
                        queue : false,
                        duration : 800,
                        easing : 'easeOutElastic' 
                    }
                );
                    
            },
            
            function(){
                
                $(this).animate(
                    {
                        top : 60
                    },
                    {
                        queue : false,
                        duration : 800,
                        easing : 'easeOutElastic' 
                    }
                );
                
            }
        );
    }
    
    // Page order.php
    
    this.initOrder = function(){
    
        $('#order-form').animate(
            {
                top : 80
            },
            {
                queue : false,
                duration : 800,
                easing : 'easeInOutExpo' 
            }
        );
    }
    
    // Init Structure
    self.initStructure();
    
    //colorbox
    self.colorbox();
        
});


function openVideoAuguri(){
    $.colorbox({
            html:'<iframe width="560" height="315" src="http://www.youtube.com/embed/ufZMb1l2y7w?autoplay=1&rel=0" frameborder="0" allowfullscreen></iframe>',
            innerWidth: 640,
            innerHeight:360,
            initialWidth: 500,
            initialHeight: 300,
            scrolling: false
    });
}

function openImageAuguri(){
    $.colorbox({
            href: 'assets/images/index/rolando-auguri-2012.jpg',
            transition  : 'elastic',
            innerWidth: 882,
            innerHeight:481,
            initialWidth: 500,
            initialHeight: 300,
            scrolling: false
    })
}


function moveMessages(){
    $('.scrolling-text').bind('marquee', function() {
        var ob = $(this);
        var tw = ob.width();
        var ww = ob.parent().width();
        ob.css({ right: -tw, display: 'block' });
        ob.animate({ right: ww }, 200000, 'linear', function() {
            ob.trigger('marquee');
        });
    }).trigger('marquee');
}


// Easings

jQuery.easing.easeInOutExpo = function( x, t, b, c, d )
{
    if (t==0) return b;
    if (t==d) return b+c;
    if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
    return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
}

jQuery.easing.easeOutElastic = function( x, t, b, c, d )
{
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
}

jQuery.easing.easeOutBounce = function( x, t, b, c, d )
{
    if ((t/=d) < (1/2.75)) {
        return c*(7.5625*t*t) + b;
    } else if (t < (2/2.75)) {
        return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
    } else if (t < (2.5/2.75)) {
        return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
    } else {
        return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
    }
};
