//function which hides/shows scrollbuddy div
function doScroll($ScrollBuddy){

    //if cookie is turned on, scrollbuddy only runs once
    var cookieOn = true;
    
    //if cookie doesn't exist
    if(!$.cookie('stop_scrollbuddy')){

        //get scroll position
        var scrollTop = $(window).scrollTop();
    
        //set scrollbuddy div position on page
        $ScrollBuddy.css({
            position:   'absolute',
            top:        $(window).height() + scrollTop - $ScrollBuddy.outerHeight()  + 'px',
            left:       ($(window).width()/2) - ($ScrollBuddy.outerWidth()/2) + 'px'
        });
    
        //if user scrolls more than 0px, or all page content is in view
        if(scrollTop > 0 || $(window).height() > $('body').outerHeight()){
        
            //hide scrollbuddy div
            $ScrollBuddy.stop().animate({opacity:0}, 'slow', function(){

                $ScrollBuddy.css('top','-999px');

                //if the cookie is on, stop scrollbuddy running again
                if(cookieOn){
                    $.cookie('stop_scrollbuddy','yes');
                }
            });
        
        }
    
        //otherwise
        else {
        
            //re-position scrollbuddy to current window scroll location
            if($ScrollBuddy.css('opacity') == 0){
                $ScrollBuddy.css('top', $(window).height() + scrollTop - $ScrollBuddy.outerHeight() - 180 + 'px');
            }
        
            //show scrollbudy div
            $ScrollBuddy.stop().animate({opacity:1, top:$(window).height() + scrollTop - $ScrollBuddy.outerHeight()+'px'}, 'slow');
        
        }

    }

    //otherwise, the cookie exists, and it should be hidden
    else{
        $ScrollBuddy.remove();
    }
}

//cookie plugin
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
  
//when page has loaded  
$(document).ready(function() {
    
    //if scrollbuddy doesn't exist, create it.
    if(!$('#ScrollBuddy').length){

        //create and append scrollbuddy div to page
        $('<div><img src="/Images/background/scrollBuddy.png" alt="scroll for more content" /></div>')
            .attr('id', 'ScrollBuddy')
            .css('opacity', 0)
            .appendTo('body');

    }

    //otherwise, make sure it is it's opacity is set to 0 for it to be faded in
    else {
        $('#ScrollBuddy').css('opacity',0);
    }
    
    //cache scrollbuddy div
    $ScrollBuddy = $('#ScrollBuddy');
    
    //add event handlers for window scrolling and re-size to show/hide scrollbuddy div
    $(window).bind({
        scroll:function(){doScroll($ScrollBuddy);},
        resize:function(){doScroll($ScrollBuddy);}
    });
    
    //initiate scrollbuddy
    setTimeout(function(){doScroll($ScrollBuddy);},1000);
});

