/*
 * Common JavaScript functions and behaviors for uControl
 * $Rev: 7089 $
 * $Date: 2008-02-01 13:51:47 -0800 (Fri, 01 Feb 2008) $
 * $URL: https://svn.mediatemple.net/dev/apps/ucontrol/tags/2008-07-16-01/js/ucontrol.js $
 */

$(document).ready(function() {
    // fade out success message
    if ( $('.uc-alert').filter('.success').size() ) {
        $('.uc-alert').hide();
        $('#uc-body').block(
            $('.uc-alert').html(),
            { 'width'                 : '50%',
              'padding'               : '15px 5px',
              'color'                 : '#555',
              'border'                : '1px solid #ccc',
              'background'            : '#b4efac '
                                        + "url('_img/topshine.png') "
                                        +' repeat-x',
              '-moz-border-radius'    : '10px',
              '-webkit-border-radius' : '10px'
            },
            {  'position_message' : function( $el ) {
                   // requires jquery.dimensions plugin
                   $el.css({ 'top'  : 100,
                             'left' : ( $el.offsetParent().innerWidth()
                                        - $el.outerWidth()              ) / 2
                                        // horizontally centered
                   });
               }
            }
        );
        $('#uc-body-top').block();
    
        // after 1 second fade out success message then unblock body
        $('#uc-body').children('.blockUI-message')
            .fadeTo(500, 1)
            .animate({opacity: "hide"}, 2000, 'easeInCubic',
                     function() { $('#uc-body, #uc-body-top').unblock(); }
            );
     }

    //
    // replace submit input element with a save settings link
    // fragilly assumes only one form, and one :submit per page
    //
    $(document.createElement('a'))
        .attr({ 'href'  : '#',
                'id'    : 'uc-submitlink',
                'class' : 'uc-submitbttn'
        })
        .text('Save Settings')
        .focus(function(event) {        // visual cue that link has focus
            $(this).css('background-position', '-119px top');
        })
        .blur(function(event) {
            $(this).css('background-position', 'left top');
        })
        .click(function(e) {
            $('form').submit(); 
            e.preventDefault();
        })
        .insertAfter(':submit')
        ;
    
    $(':submit')
        .css({ 'position' : 'absolute', // hide by position off screen
                'left'     : '-900px'   // ..so <enter> still submits in IE 6
        })
        .focus(function(e) {            // pass focus to the link
            $('#uc-submitlink').focus();
        })
        ;

    //
    // fade background color of error rows from light red to their
    //  normal color
    $('.uc-formrow.error').each(function() {
        doBGFade(this,
                [255, 221, 221], // start color #ffdddd
                [246, 246, 246], // color at end of animation #f6f6f6
                'transparent',  // finalColor, after animation
                 75, 20, 4);
    });
});

/*
 * cubic easing from the easing plugin version 1.3
 * http://gsgd.co.uk/sandbox/jquery/easing/
 */
jQuery.extend( jQuery.easing,
{
    easeInCubic: function (x, t, b, c, d) {
        return c*(t/=d)*t*t + b;
    }
});

/*
 * Background color fade. One of the jQuery examples from
 * http://www.codylindley.com/blogstuff/js/jquery/ 
 */
function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
    var delta = maxValue - minValue;
    var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
    return Math.ceil(stepp)
}
function addFade() {
    doBGFade(this,[255,255,100],[255,255,255],'transparent',75,20,4);
 }
//
// @param dom   object     elem
// @param array startRGB   animate from this color
// @param array endRGP     animate to this color
// @param mixed finalColor after animation is complete set background to this
//                         color
// @param int   steps      number of "frames" in the animation
// @param int   intervals  miliseconds between each step of the animation
// @param int   powr       ?
// I believe that duration of the entire animation is steps * intervals
//
function doBGFade(elem,startRGB,endRGB,finalColor,steps,intervals,powr) {
    if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
    var actStep = 0;
    elem.bgFadeInt = window.setInterval(
        function() {
            elem.style.backgroundColor = "rgb("+
                easeInOut(startRGB[0],endRGB[0],steps,actStep,powr)+","+
                easeInOut(startRGB[1],endRGB[1],steps,actStep,powr)+","+
                easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)+")";
            actStep++;
            if (actStep > steps) {
                elem.style.backgroundColor = finalColor;
                window.clearInterval(elem.bgFadeInt);
            }
        }
        ,intervals)
}

