var speed = 0.5;
var nextSection;

/**
 * Init jQuery javascripts when dom is ready
 */
$(function()
{
    // -- make the bottom area of the home page scrollable
    if ($(".scrollable"))
    {
        var api = $(".scrollable").scrollable({
            size      : 1,
            clickable : false,
            keyboard  : true,
            items     : ".panels",
            api       : true,
            speed     : speed * 1000,
            loop      : true
        });
    }

    // -- set up custom scroll bars
    if ($(".scroll-pane"))
    {
        $(".scroll-pane").jScrollPane({
            scrollbarWidth  : 8
        });
    }

    // -- add hover line to feature boxes
    $('.feature').hover(
        function(e){
            $(this).find("hr").css({
                "color"            : "#990000",
                "background-color" : "#990000"
            });
        },
        function(e){
            $(this).find("hr").css({
                "color"            : "transparent",
                "background-color" : "transparent"
            });
        }
    );

    // -- make faceboxes work
    $("a[rel*=facebox]").facebox({
        opacity:0.85,
        closeImage:"/img/closer.png"
    });

    // -- facebox closer
    $("#closer a").live("click", function(e)
    {
        e.preventDefault();
        $(document).trigger('close.facebox');
    });

    if ($.fn.droplet)
    {
        $("select.category").droplet({
            parseSelectedOffset:false,
            fadeSpeed:100,
            change:function(selected, i)
            {
                window.location = selected.value;
            }
        });
    }

    var $team_container = $("#team_container");
    var $images =$("#team_container img");
    var current = 1;
    var showImage = function()
    {
        current++;
        if (current > $images.length) current = 1;
        var $image = $("#office-photo-" + current);
        //console.log(current);
        $image.css("opacity", 0);
        $team_container.append($image);
        $image.animate({opacity:1}, 1000);
    }
    if ($team_container.length)
    {
        setInterval(showImage, 7000);
    }
});

/**
 * Scroll the bottom content area to specific content.
 *
 * @param string section  Section to jump to; based on id's of panels
 * @return void
 */
function gotoContent(section)
{
    if (section == undefined) section = nextSection;
    var api = $(".scrollable").scrollable();
    var index = 0;

    switch (section)
    {
        case "about"   : index = 2; break;
        case "reel"    : index = 1; break;
        case "contact" : index = 3; break;
        default        : index = 0; break;
    }

    $("#navigation ul li").removeClass("active");
    $("#nav_" + section).addClass("active");

    api.seekTo(index);
}

/**
 * Jump to a content section.
 *
 * Automatically scrolls to the content-bottom div and
 * calls the corresponding section animation when complete.
 *
 * @param string section  Section to jump to; based on id's of panels
 * @return void
 */
function gotoSection(section)
{
    var complete = false;
    var targetOffset = $("#content-bottom").offset().top;

    gotoContent(section);
}

/**
 * Handle changes in address.
 *
 * @param Event event  SWFAddress change event
 * @return void
 */
function _handleChange(event)
{
    if (event.path)
    {
        if (event.path.length && event.path != "/")
        {
            var path = event.path;

            if (path.charAt(0) == "/") path = path.substr(1);
            if (path.charAt(path.length - 1) == "/") path = path.substr(0, path.length - 2);

            gotoSection(path);
        }
    }
}

// -- register change handlers
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, _handleChange);

