
(function () {

    /**
    Close any open elements
    @param HTMLelement element
        anchor element
    */
    function close(element) {
        var open_elements, x

        open_elements = $(".inspect a.open");
        for(x=0; x<open_elements.length; x++) {
            if( element !== open_elements[x] )
                animate(open_elements[x]);
        }
    }

    /**
    Get container from supplied element
    @param HTMLelement element
        Current element
    @return HTMLelement
        Container element
    */
    function getContainer( element ) {
        var container;

        // If carousel container passed in return
        if(element.className === 'inspect')
            return element;

        container = $(element).parents('.inspect');
        if(container.length < 1) {
            console.warn('Inspector: Container not found.');
            return;
        }
        return container[0];
    }

    /**
    Get explination from supplied element
    @param HTMLelement element
        Current element
    @return HTMLelement
        explination element
    */
    function getExplanation( element ) {
        var container, explanation;

        container = getContainer(element);
        explanation = $(container).children('.explanation');
        if(explanation.length < 1) {
            console.warn('Inspector: Explantion not found.');
            return;
        }
        return explanation[0];
    }

    /**
    Get overlay from supplied element
    @param HTMLelement element
        Current element
    @return HTMLelement
        overlay image element
    */
    function getOverlay( element ) {
        var container, overlay;

        container = getContainer(element);
        overlay = $(container).children('img');
        if(overlay.length < 1) {
            console.warn('Inspector: Overlay not found.');
            return;
        }
        return overlay[0];
    }

    /**
    Animate Inspectors
    @param HTMLelement element
        anchor element
    */
    function animate(element) {
        var explanation;

        // Expland explanation text
        explanation = getExplanation(element);
        $(explanation).toggle("slow");

        overlay = getOverlay(element);
        $(overlay).animate({"opacity": "toggle"}, 800);

        if( $(element).hasClass('open') ) {
            $(element).removeClass('open');
            $(element).addClass('closed');
        }
        else if( $(element).hasClass('closed') ) {
            $(element).removeClass('closed');
            $(element).addClass('open');
        }
    }

    /**
    Utility function
    @param HTMLelement this
        anchor element
    */
    function inspect() {

        // Close any open inspectors
        close(this);

        // Animate element
        animate(this);
    }

    function navInspect() {
        var target;

        target = $(this).attr('data-target');
        $('#'+target+' a').click();
    }

    function init() {

        $(".inspect a").addClass('closed');
        $(".inspect a").click(inspect);

        $(".subnavigation.home a").click(navInspect);
    }

    jQuery(init);

}());

