$(document).ready(function() { //when the document is ready...
    // detect ipads that don't support fixed backgrounds
var isiPad = ((navigator.userAgent.match(/iPad/i) != null) || (navigator.userAgent.match(/Android/i) != null) || (navigator.userAgent.match(/IPhone/i) != null));
    var $window = $(window);
    // get all the content sections (based on an id begining with "section")

    //    var links = $('a[id^="hplink"]').each(
    //    function () {
    //        var $self = $(this)
    //        $self.bind('inview', function (event, visible) {
    //            if (visible == true) {

    //                var whatimage = 'imgsection' + this.id.replace("hplink","");
    //                var onsrc = $('#' + whatimage).attr("src").replace("_off", "_on");
    //                $('#' + whatimage).attr("src", onsrc);
    //            }
    //            else {

    //                var whatimage = 'imgsection' + this.id.replace("hplink", "");
    //                var onsrc = $('#' + whatimage).attr("src").replace("_on", "_off");
    //                $('#' + whatimage).attr("src", onsrc);
    //            }
    //        });
    //    });

    var sections = $('div[id^="section"]').each(
    function() {
        var $self = $(this)
        if (isiPad) {
            $self.css({ 'background-attachment': 'scroll' })
            // fix all background div images as well
            var backgrounds = $('div[class^="bg"]', this).each(
                function() {
                    var parent = $(this).parent();
                    var xpos = parseInt($(this).attr('data-xpos'));
                    var ypos = parseInt($(this).attr('data-ypos'));
                    $(this).css({ 'background-attachment': 'scroll' })
                    $(this).css({ 'backgroundPosition': xpos + 'px ' + ypos + 'px' });
                });
        }

        $self.bind('inview', function(event, visible) {
            if (visible == true) {
                $(this).addClass("inview");

                var sections = $('img[id^="imgsection"]').each(
                function() {
                    var $self = $(this)
                    $self.attr("src", "images/but_pllx_off.png");
                });

                var whatimage = 'img' + this.id;
                var onsrc = $('#' + whatimage).attr("src").replace("_off", "_on");
                $('#' + whatimage).attr("src", onsrc);
            }
            else {
                $(this).removeClass("inview");
                var whatimage = 'img' + this.id;
                var onsrc = $('#' + whatimage).attr("src").replace("_on", "_off");
                $('#' + whatimage).attr("src", onsrc);
            }
        });
    });

    var windowHeight = $window.height(); //get the height of the window

    //function that places the navigation in the center of the window
    function RepositionNav() {
        var windowHeight = $window.height(); //get the height of the window
        var navHeight = $('#nav').height() / 2;
        var windowCenter = (windowHeight / 2);
        var newtop = windowCenter - navHeight;
        $('#nav').css({ "top": newtop, "left": 10 }); //set the new top position of the navigation list
    }

    function backPos(xpos, ypos, content_div, scrollpos, inertia) {
        var divpos = content_div.offset().top;
        var div_scrollfactor = (divpos - scrollpos);
        var newpos = (div_scrollfactor * inertia) + ypos;
        return content_div.offset().left + xpos + "px " + newpos + "px";
    }

    function Move() {
        var pos = $window.scrollTop(); //position of the scrollbar
        $.each(sections, function() {
            if ($(this).hasClass("inview")) {
                if (!isiPad) // only scroll if not an iPad
                    $(this).css({ 'backgroundPosition': backPos(0, 0, $(this), pos, 0.3) });  // background image
                // find any background divs that need to be moved based on a classname begining with "bg"
                var backgrounds = $('div[class^="bg"]', this).each(
                function() {
                    var parent = $(this).parent();
                    var xpos = parseInt($(this).attr('data-xpos'));
                    var ypos = parseInt($(this).attr('data-ypos'));
                    var scrollspeed = parseFloat($(this).attr('data-scrollspeed'));
                    if (!isiPad)
                        $(this).css({ 'backgroundPosition': backPos(xpos, ypos, $(this).parent(), pos, scrollspeed) });
                });
            }
        });
    }

    RepositionNav(); //Reposition the Navigation to center it in the window when the script loads

    $window.resize(function() { //if the user resizes the window...
        Move(); //move the background images in relation to the movement of the scrollbar
        RepositionNav(); //reposition the navigation list so it remains vertically central
    });

    $window.bind('scroll', function() { //when the user is scrolling...
        Move(); //move the background images in relation to the movement of the scrollbar
    });

});
