/**
 * @package Roath_Garage
 * @subpackage public
 * @author Dan Bettles <dan@danbettles.net>
 */

jQuery(function () {
    jQuery("a[rel='help']").fancybox({
        'width': '75%',
        'height': '75%',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });

    //@url http://dextrose.be/2008/11/10/jquery-background-position-and-internet-explorer/
    jQuery.fn.backgroundPosition = function () {
        var oThisEl = jQuery(this),
            backgroundPosition = oThisEl.css('background-position');

        if (typeof(backgroundPosition) === 'undefined') {
            return oThisEl.css('background-position-x') + ' ' + oThisEl.css('background-position-y');
        }

        return backgroundPosition;
    };

    /**
     * Replaces the header photo at regular intervals
     * 
     * Returns TRUE if successful, or FALSE otherwise
     * 
     * @param {Boolean} p_defer
     * @return {Boolean}
     */
    (function rotateBanners(p_defer) {
        var numBanners = 3,  /*...In the background image*/
            interval = 6000,  /*Six seconds*/
            oHeaderEl = {}, 
            oldBackgroundPosition = '',
            aBgPosMatch = [],
            headerWidth = 0,
            newLeft = 0;

        if (! (p_defer || false)) {
            oHeaderEl = jQuery('div#header-inner');
            oldBackgroundPosition = oHeaderEl.backgroundPosition();

            if (! oldBackgroundPosition) {
                return false;
            }
    
            aBgPosMatch = oldBackgroundPosition.match(/^(-?[0-9]+)(px -?[0-9]+px)$/);
    
            if (! jQuery.isArray(aBgPosMatch)) {
                return false;
            }
    
            headerWidth = oHeaderEl.innerWidth();
            newLeft = Math.abs(parseInt(aBgPosMatch[1], 10)) + headerWidth;
    
            if (newLeft === (headerWidth * numBanners)) {
                newLeft = 0;
            }
    
            oHeaderEl.css('background-position', (-1 * newLeft) + aBgPosMatch[2]);
        }

        window.setTimeout(rotateBanners, interval);

        return true;
    }(true));
});
