var home = {}
home.accordionCollapsedHeight = 113;
home.headerHeight = 63;
home.defaultMargin = 63;
home.scrollSpeed = 30000;

home.fadeInBackground = function(container, callback) {
    var imageURL = null;
    var imageSize = null;

    container.children('.background').fadeIn();

    // get image size
    imageURL = darwin.getBackgroundImage(container.find(".image"));
    imageSize = darwin.getImageSize(imageURL);

    // animate the background
    var travelDistance = home.accordionCollapsedHeight - imageSize.height;

    // sanity check in case image size doesn't initialize
    if(travelDistance > 0) {
        travelDistance = 0;
    }

    container.find(".image").stop().css({backgroundPosition: '50% 0px'}).animate({
        backgroundPosition: '50% ' + travelDistance + 'px'
    }, home.scrollSpeed, function() {
        if(callback){
            callback(container);
        }
    });
}

home.fadeOutBackground = function(container, callback) {
    container.children('.background').fadeOut(function() {
        if(callback) {
            callback(container);
        }
    });
}

home.toggleAccordionContent = function(container) {
    if(container.hasClass('expanded')) {
        // collapse
        home.hideAccordionContent(container);
        $('#home-accordion').animate({'margin-top': home.defaultMargin + 'px'}, function() {
            $('#content .hover').fadeOut();
        });
    }
    else {
        // expand
        $('#content .hover').fadeIn();
        home.showAccordionContent(container);
    }
}


home.showAccordionContent = function(container) {
    home.fadeOutBackground(container);

    // collapse expanded
    $('.expanded').each(function() {
        home.hideAccordionContent($(this));
    });

    // animate to new margin
    container.addClass('expanded');

    var margin = home.headerHeight - home.accordionCollapsedHeight * parseInt(container.attr('rel'));

    $('#home-accordion').animate({'margin-top': margin + 'px'}, function() {
        container.find('.home-accordion-content').slideDown(1000);
    });
}

home.hideAccordionContent = function(container) {
    container.removeClass('expanded');
    container.find('.home-accordion-content').slideUp(500);
}

$(document).ready(function() {
    // init
    $('.home-accordion-content').hide();
    $('.home-accordion-content-thumbnails').hide();

    // preload background images
    $('.image').each(function() {
        darwin.preloadImage(darwin.getBackgroundImage($(this)));
    });

    // accordion
    $('ul#home-accordion > li').hover(function() {
        // show
        $(this).find('.home-accordion-content-thumbnails').fadeIn();

        if(!$(this).hasClass('expanded')){
            home.fadeInBackground($(this));
        }
    }, function() {
        // hide
        $(this).find('.home-accordion-content-thumbnails').fadeOut();
        home.fadeOutBackground($(this));
    });

    $('.accordion-btn').click(function(event) {
        event.preventDefault();
        home.toggleAccordionContent($(this).parents('li'));
    });
});

