/*
 * autor: Marc Dix
 * company: mediaman technology
 * 
 * Position fixed is not supported in mobile Safari. This is a small workaround that
 * deactivates fixed items on scroll and reactivates them on scrollstop, with the
 * correct position.
 * 
 * If you have to manually trigger the reactivation you can trigger the 'scrollstop' event that
 * is bound to the document > $(document).trigger('scrollstop');
 */

// check dependencies first
if (('undefined' === typeof(jQuery.event.special.scrollstart)) || ('undefined' === typeof(jQuery.event.special)) || ('undefined' === typeof(jQuery))) {
    if ('undefined' !== typeof(console)) {
        console.log('Dependencies for mobile.safariFixedFix.js not met - needs jquery and jquery.scroll.events plugin.');
    } else {
        alert('Dependencies for mobile.safariFixedFix.js not met - needs jquery and jquery.scroll.events plugin.');
    }
} else {
    if ((navigator.userAgent.match(/iPad/i) !== null) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
        (function safariFixedFix() {
            var config = {};
            config.headerId               = '#navpanel';
            config.footerId               = '#footer';
            config.footerHeight           = 30;
            config.disableIfIdExists      = '#supersized-fullscreen-overlay'; // disable header and footer fadeIn if this id exists (used for fullscreen)
            config.fadeInDurationMs       = 200;
            config.watchOrientationChange = true;

            /**
              * Removes the header & the footer when scrolling
              */
            $(document).bind('scroll', function() { 
                $(config.headerId + ', ' + config.footerId).css('display', 'none');
            })

            /**
              * Checks on scrollstop if in fullscreen mode (we don't want the navigation here),
              * calculates the top and the bottom of the window, moves the
              * header & the footer to the correct position and fades them in.
              */
            $(document).bind('scrollstop', function() {
                if (0 === $(config.disableIfIdExists).length) {
                    var fromTop = $(window).scrollTop();
                    var bottom  = $(window).scrollTop() + $(window).height() - config.footerHeight;

                    $(config.headerId).css({'position' : 'absolute', 'top' : fromTop});
                    $(config.footerId).css({'position' : 'absolute', 'top' : bottom})

                    $(config.headerId + ', ' + config.footerId).fadeIn(200);
                }
            })
        })();
        
        if (config.watchOrientationChange) {
            window.onorientationchange = function() {
                $(document).trigger('scrollstop');
            }
        }
    }
}
