(function($, global) {

    $(document).ready(function() {
        var footer = $('.footer'),
			bar = footer.find('.bar'),
			topElement = $(window),
			expandTrigger = bar.find('.footer-triggers a'),
			locateADealer = $('.searchForm'),
			address = locateADealer.find('.address'),
			zipStr = 'ZIP Code',
			hideFooter = footer.css('position') === 'fixed';

        expandTrigger.bind({
            'click': function(e) {
                e.preventDefault();
                footer.trigger('toggle', [false, $(e.target).parent()]);
            }
        });

        locateADealer.bind({
            submit: function(e) {
                if (address.val() == zipStr) {
                    e.preventDefault();
                }
            }
        });

        var footerHeight = 0;
        var supplement = footer.find('.supplement-wrap');
        var supplementHeight = supplement.height();

        footer.find('.interior').each(function(i) {
            $(this).addClass('active');
            var height = footer.height();
            if (height > footerHeight) {
                footerHeight = height;
            }
            $(this).removeClass('active');
        }).each(function(i) {
            var marginBottom = footerHeight - $(this).height() - supplementHeight - supplement.position().top;
            $(this).height($(this).height() + marginBottom);
        });

		$(global).load(function() {
			var biggestInterior = 0;
			footer.find('.interior-full').each(function() {
				if ($(this).height() > biggestInterior) {
					biggestInterior = $(this).height();
				}
			});
			footer.find('.interior-full').each(function() {
				$(this).height(biggestInterior);
			});
		});

        footer.find('.interior').first().addClass('active');
        footer.data('color', expandTrigger.first().data('color'));
        footer.addClass('footer-' + footer.data('color'));

        var barHeight = bar.height() + parseInt(bar.css('paddingTop'), 10),
			extrasWrap = footer.find('.extras-wrap'),
			container = $('.container').first(),
			defaultDuration = 500;
        if (!hideFooter) {
            footer.height(footerHeight);
        }
		footer.find('.interior-full a').bind({
			'focus': function(e) {
				if (footer.hasClass('collapsed')) {
					footer.trigger('expand');
				}
			}
		});
        footer.bind({
            'collapse': function(e, duration) {
                if (hideFooter) {
                    footer.animate({ 'height': barHeight }, duration, function() {
                        $(this).addClass('collapsed').css({ overflow: 'hidden' });
                    });
                    container.animate({ marginBottom: barHeight }, duration);
                    var scroll = topElement.scrollTop() - footerHeight + barHeight;
                    $('body, html').animate({ scrollTop: scroll }, duration);
                    footer.find('input').each(function() {
                        $(this).attr('tabindex', '-1');
                    });
                }
            },
            'expand': function(e, duration) {
                if (hideFooter) {
                    footer.animate({ 'height': footerHeight }, duration, function() {
                        $(this).removeClass('collapsed');
                    });
                    container.animate({ marginBottom: footerHeight }, duration);
                    var scroll = topElement.scrollTop() + footerHeight - barHeight;
                    $('body, html').animate({ scrollTop: scroll }, duration);
                    footer.find('input').each(function() {
                        $(this).attr('tabindex', '');
                    });
                }
            },
            'toggle': function(e, noAnim, target) {
                var duration = noAnim ? 0 : defaultDuration,
					index,
					pane,
					color,
					activeClass = 'active';

                if (target) {
                    index = target.index();
                    pane = $(this).find('.interior-full').find('.interior').eq(index);
                    color = target.data('color');
                };

                if (!target || (target && target.hasClass(activeClass))) {
                    if (target) {
                        target.removeClass(activeClass);
                    }
                    $(this).trigger('collapse', duration);

                } else {
                    if ($(this).hasClass('collapsed')) {
                        $(this).trigger('expand', duration);
                    }
                    $(this).find('.active').removeClass(activeClass);
                    pane.addClass(activeClass);
                    target.addClass(activeClass);

                    $(this).removeClass('footer-' + $(this).data('color'))
						.addClass('footer-' + color)
						.data('color', color);

                }
            }
        });
        footer.trigger('toggle', true);
    });

} (jQuery, this));

