$(document).ready(function(){

	// Determine if the user is on an iPad. If so, use different code to handle the fixed ISI.
    if (is_iPad())
    {
    	$('#importantSafetyCallout').css('top', (window.pageYOffset + window.innerHeight - 116) + 'px');
		
		$(document).bind('scroll', function() {
		  $('#importantSafetyCallout').css('top', (window.pageYOffset + window.innerHeight - 116) + 'px');
		});
    }
    else 
    {
	    // Hide fixed ISI when scrolling.
		jQuery(window).bind('scrollstart', function() {
			//$('#importantSafetyCallout').stop().hide();
			$('#importantSafetyCallout').stop().show().animate({
				opacity: 0
			}, 0);
		});
		
		jQuery(window).bind('scrollstop', function(e) {
			$('#importantSafetyCallout').stop().show().animate({
				opacity: 1
			}, 600);
		});
    }

    var isiCallout = $('#importantSafetyCallout').html();
    var headerHeight = 175; // Default height for the header
    var defaultHeight = "110px"; // Default height of collapsed view
    var resize = true; // set flag of true so we can resize safety information when opened

    if (isiCallout != null) {
        $('body').append($('#importantSafetyCallout'));
        $('#footer').css('margin-bottom', '120px'); // Add enough margin on bottom so you can still see the footer.
    }

    $('#expandSafetyCallout').click(function() {
        if($(this).hasClass('inactiveISI'))
        {
            $('#expandSafetyCallout').removeClass('inactiveISI');
            $('#expandSafetyCallout').addClass('activeISI');

            if(($.browser.msie === true) && ($.browser.version == '6.0'))
            {
                $('select').css("visibility", "hidden");
            }

            var windowHeight = $(window).height();
            var safetyCalloutHeight =  400;
            if (windowHeight - headerHeight < 400) { safetyCalloutHeight = windowHeight - headerHeight; }

            $('.safetyContent').css({'margin-bottom':'15px', 'border-bottom':'1px solid #c6c6c6'});
            $('.safetyContent').animate({
                height: safetyCalloutHeight + 'px'
                }, 500, function(){
                resize = true;
                $(window).resize(resizeSafetyInformation);
                $(window).scroll(resizeSafetyInformation);
            });
            return false;
        }
        else if($(this).hasClass('activeISI'))
        {
            resize = false;
            $('#expandSafetyCallout').addClass('inactiveISI');
            $('#expandSafetyCallout').removeClass('activeISI');

            if(($.browser.msie === true) && ($.browser.version == '6.0'))
            {
                $('select').css("visibility", "visible");
            }

            $('.safetyContent').css({'margin-bottom':'0px', 'border-bottom':'none'});
            $('.safetyContent').animate({
                height: defaultHeight
                }, 500 );
        }

        function resizeSafetyInformation()
        {
            if (resize == false) {return false;}
            var windowHeight = $(window).height();
            var safetyCalloutHeight =  550;
            if (windowHeight - headerHeight < 550) { safetyCalloutHeight = windowHeight - headerHeight; }
            $('.safetyContent').css('height', safetyCalloutHeight);
        }
    });
});

