;

var adjust_data = {
    delta_max: 186
    , bg_pos: {
        body: {
            y: -186
        }
        , container: {
            x: 376
            , y: 37
        }
    } 
};
if ( $('body.card').length > 0 ) {
    adjust_data.delta_max = 83;
    adjust_data.bg_pos.body.y = -83;
}
if ( $('body.with-flash').length > 0 ) {
    adjust_data.delta_max = 183;
    adjust_data.bg_pos.body.y = -183;
}

var adjust_bg_position = function(){
    if ( $('body.do-not-adjust').length > 0 ) {
        return;
    }

    var container = $('#container');
    var w_height = $(window).height();
    var c_height = container.height();
    var c_padding_top = parseInt( container.css('padding-top'), 10 );

    var set_position = function(d) {
        var b_bg_pos_y = adjust_data.bg_pos.body.y + d + 'px';
        var c_bg_pos_y = adjust_data.bg_pos.container.y + d + 'px';
        var c_pt = 0 + d + 'px';

        container.css('padding-top', c_pt);
        container.css('background-position', adjust_data.bg_pos.container.x + 'px ' + c_bg_pos_y);
        $('body').css('background-position', 'center ' + b_bg_pos_y);
    };

    if (w_height > c_height) {
        var delta = Math.round( (w_height - c_height) / 2 );
        if (Math.abs(c_padding_top - delta) > 5 ) {
            if ( delta > adjust_data.delta_max ) {
                delta = adjust_data.delta_max;
            }
            set_position(delta);
        }
    }
    else {
        set_position(0);
    }
};

/*
 * pulsate :: begin
 */

$.fn.pulsate = function (properties, duration, type, speed, callback) {
    type = type || 'Swing'
    speed = speed || 'Normal';
    this.animate(properties, duration, 'pulsate' + type + speed, callback);
};

function createPulsateSwing (speed) {
    return function (p, n) {
        return (1 + Math.sin(n / speed)) / 2;
    }
}

function createPulsateBounce (speed) {
    speed *= 2;
    return function (p, n) {
        return (
            ((Math.asin(Math.sin(Math.PI * n / speed)) + Math.PI / 2) / Math.PI) *
            (Math.sin(Math.PI * n / speed) + 1) / -2 + 1
        );
    }
}

var speeds = {
    fast: 100
    , normal: 200
    , slow: 400
    , very_slow: 600
}


$.extend(
    jQuery.easing
    , {
        pulsateSwingFast: createPulsateSwing(speeds.fast),
        pulsateSwingNormal: createPulsateSwing(speeds.normal),
        pulsateSwingSlow: createPulsateSwing(speeds.slow),
        pulsateSwingVerySlow: createPulsateSwing(speeds.very_slow),
        pulsateBounceFast: createPulsateBounce(speeds.fast),
        pulsateBounceNormal: createPulsateBounce(speeds.normal),
        pulsateBounceSlow: createPulsateBounce(speeds.slow),
        pulsateBounceVerySlow: createPulsateBounce(speeds.very_slow)
    }
);

/*
 * pulsate :: end
 */

$(document).ready(function(){
    adjust_bg_position();

    $('a[href="#"]').live(
        'click.adwz'
        , function(e){
            e.preventDefault();
        }
    );

    $(window).resize(function(){
        adjust_bg_position();
    });

    if ( undefined === window.ie_lte_8) {
        var forever = 5 * 24 * 60 * 60 * 1000; // 5 days! (Which is forever in Internet time)
        $('#cups-glow').pulsate({opacity: 0.4}, forever, 'Swing', 'Slow');
        $('#cups-glow-2').pulsate({opacity: 0.4}, forever, 'Bounce', 'Slow');

        $('#glow-heart').pulsate({opacity: 0.2}, forever, 'Swing', 'VerySlow');
        $('#glow-heart-2').pulsate({opacity: 0.2}, forever, 'Bounce', 'VerySlow');
    }
    else {
        $('#cups-glow-2').addClass('cups-glow-spritely');
        $('#cups-glow').addClass('cups-glow-spritely').sprite({fps: 10, no_of_frames: 25});

        $('#glow-heart').addClass('glow-heart-spritely');
        $('#glow-heart-2').addClass('glow-heart-spritely').sprite({fps: 9, no_of_frames: 25});
    }
});


