﻿$(document).ready(function() {
    shiftPane = function(jump) {
        var wscroll = $(window).scrollTop()
        var htop = $('#contentsCentral').offset().top
        var rheight = $('#contentsRight').height()
        var mheight = $('#contentsCentral').height()

        var tscroll
        if (rheight >= mheight) { tscroll = 0 } else {
            tscroll = wscroll < htop ? 0 : wscroll - htop + 20
            tscroll = tscroll + rheight < mheight ? tscroll : mheight - rheight
        }
        if (jump) {
            $('#contentsRight').stop().css({ 'marginTop': tscroll + 'px' });
        } else {
            $('#contentsRight').stop().animate({ 'marginTop': tscroll + 'px' }, 500);
        }

        wscroll = htop = rheight = mheight = null;
    }

    $('.fleetVehicles').hide();
    $('.vehiclesHeader').hide();

    $('.fleetCategory').each(function(i) {
        var url = $(this).children('.fleetDescription').children('a[href^="#"]')[0].href
        var section = url.substr(url.indexOf('#') + 1)
        url = null
        $(this).appendTo('#fleetList')
        $(this).after($(".fleetVehicles:has(a#" + section + ")"));
        section = null
    });
    $('ul.fleetCategories').remove()

    $('.fleetDescription a[href^="#"]').bind('click', function(e) {
        var vis = $(this).hasClass('active') ? 0 : 1;
        var obj = $(".fleetVehicles:has(a#" + this.href.substr(this.href.indexOf('#') + 1) + ")")
        if (vis == 1) { obj.stop(1).show("blind", 150) }
        else {
            var container = $('#fleetList');
            container.css('height',container.height() + 'px');

            obj.stop(1).hide("blind", 150, function() {
                $('#fleetList').css('height', 'auto');
                shiftPane(1);
            });
        }

        $(this).text(vis == 1 ? 'Hide Vehicles' : 'View Vehicles').toggleClass('active');

        return (false);
    }).mouseover(function(e) {
        $(this).parents('.fleetCategory').stop(1).animate({ "backgroundColor": "#93FF93" }, 200)
    }).mouseout(function(e) {
        if ($(this).hasClass('active')) {
            $(this).parents('.fleetCategory').stop(1).animate({ "backgroundColor": "#D3FFD3" }, 200)
        } else {
            $(this).parents('.fleetCategory').stop(1).animate({ "backgroundColor": "#FFFFFF" }, 200)
        }
    });

    $(window).scroll(function() {
        shiftPane();
    });
});