/// <reference path="../../jquery/jquery-1.4.2.js" />
/// <reference path="../../jquery/jquery-1.4.2-vsdoc.js" />

//  Stephën Miessner acts7@myvideosnapshots.com

//  with help from:
//  http://www.sohtanaka.com/web-design/new-site-new-direction/#comment-9963
//      http://jqueryfordesigners.com/fixed-floating-elements/
//      http://learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links

$(document).ready(function()
{

if ($('#COMPARERECENT').length) { 



    //alert( $('#COMPARERECENT').height() );

    if ($('#COMPARERECENT').offset() == null) return;

    //capture the initial position of the div
    //  grab css attribute marginTop to get the value (returns ex: 20px )
    //  use parseFloat to convert the "20px" to a number we can subtract
    //  finally if we don't have a top-margin 'auto' is returned. So we convert an instance of 'auto' to 0
    var top = $('#COMPARERECENT').offset().top - parseFloat($('#COMPARERECENT').css('marginTop').replace(/auto/, 0));


    //  Capture current scrolled Y position using $(window).scrollTop()
    //  If the div we want to see is above that Y position, add the fixed class
    //  If not - remove that class
    //  Note that since we bind the scroll event to the window object, 
    //  I’ve used $(this).scrollTop() (since this is also the window in this case).

    //	I replaced the default trigger of just scroll because
    //	I noticed if you scrolled down a ways, and refreshed,
    //		you would not see the cart
    //	So I have bound the trigger to window load AND scroll			
    $(window).bind('load scroll', function(event)
    {
        //$(window).scroll(function(event){

        //   DO SOMETHING COOL


        //   what the y position of the scroll is
        var y = $(this).scrollTop();

        //   whether thats below the form
        if (y >= top)
        {
            //   if so, add the fixed class
            $('#COMPARERECENTWrapper').addClass('fixed');
            $('#COMPARERECENT').addClass('rounded clearfix');
            //alert( $('#COMPARERECENT').height() ); 
            $('#NAVbg').css('padding-bottom', '0px');

        } else
        {
            //   otherwise remove it
            $('#COMPARERECENT').removeClass('rounded');
            $('#COMPARERECENTWrapper').removeClass('fixed');
            //alert( $('#COMPARERECENT').height() ); 

            //	PAD THE BOTTOM OF PARENT DIV - WHEN NOT fixed
            //	THIS WILL CAUSE IT TO APPEAR AS IN THE WRAPPER
            var cartheight = $('#COMPARERECENT').height() + 20;
            $('#NAVbg').css('padding-bottom', cartheight);

        }
    });
}	else {
    	//do not affect the left nav - it does not exist
    }
});
