$(document).ready(function() {
    var speed = 300;
    $("li.animated .banner").css("bottom", "-35px");
    $("h2.gallery .arrow").hide();
    $("li.text-only .background, h2.gallery .background").hide();
    $("li.animated").hover(
            function() {
                $(this).find(".banner").animate({bottom: "7px" }, speed, "swing");
            },
            function() {
                $(this).find(".banner").animate({bottom: "-35px" }, speed, "swing");
            });
    $("h2.gallery").hover(
            function() {
                $(this).find(".arrow").fadeIn(speed);
            },
            function() {
                $(this).find(".arrow").fadeOut(speed);
            });
    $("li.text-only, h2.gallery").hover(
            function() {
                $(this).find(".background").fadeIn(speed);
            },
            function() {
                $(this).find(".background").fadeOut(speed);
            });

    var velocity = 20;
    /* px/sec */

    function nextBoolean() {
        return Math.round(Math.random()) == 0;
    }

    function nextTarget(target, direction) {
        function flip(z) {
            function pick(a, b) {
                return nextBoolean() ? a : b;
            }
            switch (z) { case -1: return pick(0, 1); case 0: return pick(-1, 1); case 1: return pick(0, -1); }
        }
        return direction ? { x: target.x, y: flip(target.y) } : { x: flip(target.x), y: target.y };
    }

    function swingBack(frame) {
        var img = $(frame.find("img").get(0));
		var left = (frame.width() - img.width()) / 2;
		var top = img.width() / img.height() > 0.8 ? (frame.height() - img.height()) / 2 : -20;
        img.stop();
        img.animate({ left: left,  top: top }, speed, "swing")
    }

    function panAround(frame, target, direction) {
        var img = $(frame.find("img").get(0));
        var pos = img.position();
        var from_x = pos.left;
        var from_y = pos.top;
        var to_x;
        var to_y;
        switch (target.x) {
            case -1:
                to_x = 0;
                break;
            case 0:
                to_x = (frame.width() - img.width()) / 2;
                break;
            case 1:
                to_x = frame.width() - img.width();
                break;
        }
        switch (target.y) {
            case -1:
                to_y = 0;
                break;
            case 0:
                to_y = (frame.height() - img.height()) / 2;
                break;
            case 1:
                to_y = frame.height() - img.height();
                break;
        }
        var dist = Math.sqrt((to_x - from_x) * (to_x - from_x) + (to_y - from_y) * (to_y - from_y));
        img.animate({ left: to_x, top: to_y },
        { duration: (dist / velocity) * 1000, easing: "easeInOutSine", complete: function() {
            panAround(frame, nextTarget(target, !direction), !direction);
        }});
    }
    var timeout = undefined;
    $("ul.photo-frames li").hover(function() {
        var frame = $(this);
        timeout = setTimeout(function() {
            var direction = nextBoolean();
            panAround(frame, nextTarget({ x: 0, y: 0 }, direction), direction);
        }, 800);
    }, function() {
        clearTimeout(timeout);
        swingBack($(this));
    });
});

if ($.browser.msie) {

    /* clear alt attributes from images in IE<8, avoiding annoying tooltips */
    /* http://msdn.microsoft.com/en-us/library/cc288472(VS.85).aspx#ctl00_rs1_mainContentContainer_ctl10 */

    if ($.browser.version < "8.0") {
        $(function() {
            $("img").attr("alt", "");
        });
    }

    /* emulate CSS3 :target */

    $(function() {
        var hash = $($(location).attr("hash"));
        if (hash.size() != 0) {
            hash.addClass("target");
            // todo: add blink effect 
        }
    });

    /* todo: monitor clicks to #anchor on the page and change target accordingly */

}

