(function($) {

/* config */
var slideshowFadeDuration = 1000;
var slideshowTime = 5000;

/* slideshow */
$(function() {

    var updateDesc = function(o) {
        $(".description", o.target).html($("img:last", o.target).attr("alt"));
    }

    var updateImage = function(direction, o) {
        if (o.fading) { return; }
        o.fading = true;
        if (direction == 1) {
            var next = $("img:first", o.target);
            next.remove().css("display", "none");
            $("img:last", o.target).after(next);
            next.fadeIn(slideshowFadeDuration, function() {
                updateDesc(o);
                o.fading = false;
            });
        } else {
            var current = $("img:last", o.target);
            current.fadeOut(slideshowFadeDuration, function() {
                current.remove().css("display", "inline");
                o.target.prepend(current);
                updateDesc(o);
                o.fading = false;
            });
        }
    }

    $("#photo-browser").each(function() {

        var me = $(this);
        var o = { target: $(this), fading: false };
        updateDesc(o);

        $("a", o.target).bind("click", function(e) {
            var direction = $(e.target).hasClass(".action-next") ? 1 : -1;
            updateImage(direction, o);
            return false;
        });

        setTimeout(function() {
            updateImage(1, o);
            setTimeout(arguments.callee, slideshowTime);
        }, slideshowTime);

    });

});

})(jQuery);

// carousel
(function($) {

    var updateImage = function(direction, o) {
        if (o.fading) { return; }
        o.fading = true;
        if (direction == 1) {
            var next = $("li:first", o.root);
            o.ul.animate({ left: "-=" + o.totalWidth + "px" }, 500, "swing", function() {
                next.remove();
                o.ul.css("left", "-" + o.totalWidth + "px").append(next);
                o.fading = false;
            });
        } else {
            var current = $("li:last", o.root);
            o.ul.animate({ left: "+=" + o.totalWidth + "px" }, 500, "swing", function() {
                current.remove();
                o.ul.css("left", "-" + o.totalWidth + "px").prepend(current);
                o.fading = false;
            });
        }
    }

    $(function() {

        $(".carousel").each(function() {

            var o = { fading: false, root: $(this), ul: $("ul", $(this)), totalWidth: $("li", $(this)).outerWidth(true) };
            o.totalWidth = (o.totalWidth == null ? 0 : o.totalWidth);

            var items = $("li", o.ul).clone();
            var len = items.size();

            if ($(".video", items).size() == 0 && len <= 3) { return; }
            
            for (var i = len; i < 5 && i != 0; i += len)
                o.ul.append(items.clone());

            var current = $("li:last", o.root);
            current.remove();
            o.ul.css("left", "-" + o.totalWidth + "px").prepend(current);

            $(".nav a", o.root).bind("click", function(e) {
                var direction = $(e.target).hasClass("next") ? 1 : -1;
                updateImage(direction, o);
                return false;
            });

            // add image hover description
            $("#images a", $(this)).bind("mouseover", function(e) {
                var t = $(this).attr("title");
                if (t && t != "")
                    $(".description", o.root).text(t);
                return false;
            });

            // add video hover description
            $(".nav a", o.root).bind("click", function(e) {
                var idx = $(e.target).hasClass("next") ? 2 : 0;
                var t = $("li:eq(" + idx + ") a", o.root).attr("title");
                if (t && t != "")
                    $(".description", o.root).text(t);
                return false;
            });
        });

    });

    // project video lightview
    $(".video").live("click", function(e) {
        Lightview.show({
            href: $(this).attr("href"),
            title: $(this).attr("title"),
            options: {
                width: 512,
                height: 312
            }
        });
        return false;
    });

    // make scribble thumbnails on homepage clickable
    $(".scribble-image").live("click", function(e) {
        window.location = $("a", this).attr("href");
    });

    // ctp calculator
    $(function() {
        var ctpSubmit = function(e) {
            var grant = $("#grant").val();
            var ctp = grant / 100 * 11.5;
            var message = "If you are applying for &pound;" + grant + " you will require a CTP Payment of: &pound;" + ctp.toFixed(2);
            $("#result")
                .empty()
                .css("display", "none")
                .addClass("success")
                .html(message)
                .fadeIn();
            return false;
        };
        $("#ctpcalc").bind("submit", ctpSubmit);
        $("#ctpcalc a").bind("click", ctpSubmit);
    });

    // add google map to contact page
/*
    $(function() {
        if ($("#hq").size()) {
                google.load("search", "1");
                google.load("maps", "2");
                google.setOnLoadCallback(function() {
                    if (GBrowserIsCompatible()) {
                        var map = new google.maps.Map2(document.getElementById("hq"));
                        map.addControl(new GLargeMapControl());
                        map.addControl(new GMapTypeControl());
                        map.setCenter(new google.maps.LatLng(54.622978, -2.592773), 5, G_HYBRID_MAP);
                    }
                    $(window).unload(GUnload);
                });
        }

    });
*/

$(function() {

    $("button").bind("focus", function(e) {
        var a = $(this);

        /* adding a class prevents the jolt in ie? also fakes :active */
        a.addClass("active");

        /* remove outline in firefox */
        if (!$.browser.msie) { a.blur(); }

        /* remove focus in ie after short interval as a click whilst focused jolts */
        else { setTimeout(function() { a.blur(); }, 500); }
    });

    $("button").bind("blur", function(e) {
        $(this).removeClass("active");
    });

    /* ignore: disable form submission */
    /*$("form").bind("submit", function(e) {
        e.preventDefault();
    });*/

});

})(jQuery)
