/***********************************************************************************************
* File: floatMenu.js
*
* Version Control:
* VERSION              COMMENTS                             AUTHOR                  DATE
* 1.0                  Initial Version                      Matthew Watson          02/06/2010
*
*
* Description:
* Script to enable a menu to float down the page once the page has scrolled past a certain point
*
************************************************************************************************/

/**
* Title: floatMenu()
* 
* Description: 
* Function to float a menu on a page, so it follows the users viewpoint
* 
* Accepts: string (name) the name of the div that we want to float
* Returns: N/A
*/
function floatMenu(name) {

    var menuTop = null;                                                                             //Variable to hold the position of the top of the floating  menu
    var menuBottom = null;                                                                          //Variable to hold the position of the bottom of the floating menu
    var menuTopDefault = null;                                                                      //Variable to hold the default position of the top of the menu
    var menuSize = parseInt($(name).offset().top) + parseInt($(name).height()) + 50;                //Variable to get the hight of the menu, plus the distance to the top of the page
    var footer = parseInt($("#footer").offset().top);                                               //Variable to hold the position of the footer in relation to the top of the page

    if (menuSize < footer) {                                                                        //If the menusize is less then the footer
        $(name).css("position", "absolute");                                                        //Set the CSS of the menu to abosloute positioning
        $(name).css("top", "10px");                                                                 //Set the top of the menu to 10px from the top

        menuTopDefault = $(name).offset().top;                                                      //Set the default top
        menuBottom = parseInt($(name).offset().top + $(name).height() + 50);                        //Set the bottom
        menuTop = parseInt($(name).css("top").substring(0, $(name).css("top").indexOf("px")))       //Set the top

        $(window).scroll(function() {                                                               //If the window is scrolling

            try {                                                                                   //Try the following code
                if ($(document).scrollTop() > menuBottom) {                                         //If the current window is greater than the bottom of the menu

                    var offset = parseInt($(document).scrollTop());                                 //Set the offset to  be the distance to the top of the document
                    offset = offset - parseInt(menuTopDefault);                                     //Less the default position of the menu
                    offset = offset + 30;                                                           //Plus 30

                    if ((offset + menuTopDefault + parseInt($(name).height())) > parseInt($("#footer").offset().top)) {     //If the calculated bottom position is greater than the top of the footer

                        offset = (parseInt($("#footer").offset().top) - parseInt($(name).height()) - menuTopDefault - 30);  //Offset the position as the last position the menu can be in before it crosses over the footer
                    }

                    offset = offset + "px";                                                         //Add the px attribute to the offset
                    $(name).animate({ top: offset }, { duration: 500, queue: false });              //Animate the menu to the position

                }
                else {                                                                              //Else if the window is not greater than the bottom of the menu
                    $(name).animate({ top: "10px" }, { duration: 500, queue: false });              //Set the menu in its default position
                }
            }
            catch (error) {                                                                         //If there was an error
                //Do nothing
            }
        });
    }
}