jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
    def: 'easeOutQuad',
    swing: function (x, t, b, c, d) {
        return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
    },
    easeInQuad: function (x, t, b, c, d) {
        return c*(t/=d)*t + b;
    },
    easeOutQuad: function (x, t, b, c, d) {
        return -c *(t/=d)*(t-2) + b;
    },
    easeInOutQuad: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t + b;
        return -c/2 * ((--t)*(t-2) - 1) + b;
    },
    easeInCubic: function (x, t, b, c, d) {
        return c*(t/=d)*t*t + b;
    },
    easeOutCubic: function (x, t, b, c, d) {
        return c*((t=t/d-1)*t*t + 1) + b;
    },
    easeInOutCubic: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    },
    easeInQuart: function (x, t, b, c, d) {
        return c*(t/=d)*t*t*t + b;
    },
    easeOutQuart: function (x, t, b, c, d) {
        return -c * ((t=t/d-1)*t*t*t - 1) + b;
    },
    easeInOutQuart: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
        return -c/2 * ((t-=2)*t*t*t - 2) + b;
    },
    easeInQuint: function (x, t, b, c, d) {
        return c*(t/=d)*t*t*t*t + b;
    },
    easeOutQuint: function (x, t, b, c, d) {
        return c*((t=t/d-1)*t*t*t*t + 1) + b;
    },
    easeInOutQuint: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
        return c/2*((t-=2)*t*t*t*t + 2) + b;
    },
    easeInSine: function (x, t, b, c, d) {
        return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
    },
    easeOutSine: function (x, t, b, c, d) {
        return c * Math.sin(t/d * (Math.PI/2)) + b;
    },
    easeInOutSine: function (x, t, b, c, d) {
        return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
    },
    easeInExpo: function (x, t, b, c, d) {
        return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
    },
    easeOutExpo: function (x, t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    },
    easeInOutExpo: function (x, t, b, c, d) {
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    },
    easeInCirc: function (x, t, b, c, d) {
        return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
    },
    easeOutCirc: function (x, t, b, c, d) {
        return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
    },
    easeInOutCirc: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
        return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
    },
    easeInElastic: function (x, t, b, c, d) {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    },
    easeOutElastic: function (x, t, b, c, d) {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
    },
    easeInOutElastic: function (x, t, b, c, d) {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
        return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
    },
    easeInBack: function (x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        return c*(t/=d)*t*((s+1)*t - s) + b;
    },
    easeOutBack: function (x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
    },
    easeInOutBack: function (x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
        return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
    },
    easeInBounce: function (x, t, b, c, d) {
        return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
    },
    easeOutBounce: function (x, t, b, c, d) {
        if ((t/=d) < (1/2.75)) {
            return c*(7.5625*t*t) + b;
        } else if (t < (2/2.75)) {
            return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
        } else if (t < (2.5/2.75)) {
            return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
        } else {
            return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
        }
    },
    easeInOutBounce: function (x, t, b, c, d) {
        if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
        return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
    }
});

var DURATION_NAV = 2000;
var DURATION_PAGE = 1000;
var DURATION_SLIDE = 500;
var DURATION_IMAGE_CHANGE = 4000;
var DURATION_IMAGE_FADE = 350;
var pages = {};
var slideshow_timeout, switchpage_timeout, webcam_timeout;
var image_q = [];
var image_q_running = false;

$(document).ready(function(){
    // Move selected page indicator to selected page
    $("#nav_current").css(
        {top:$.inArray($("#nav li.selected")[0], $("#nav li:visible")) * 18 + 12}
    );
    // Animate selected page indicator
    $("#nav li").mouseenter(function(){
        var li = this;
        $("#nav_current").animate(
            {top:$.inArray(li, $("#nav li:visible")) * 18 + 12},
            {duration:DURATION_NAV, easing:'easeOutElastic', queue:false}
        );

    });
    $("#nav").mouseleave(function(){
        $("#nav_current").animate(
            {top:$.inArray($("#nav li.selected")[0], $("#nav li:visible")) * 18 + 12},
            {duration:DURATION_NAV, easing:'easeOutElastic', queue:false}
        );
    });
    // Handle navigation clicks
    $("#nav li").click(function(){
        $("#nav li.selected").removeClass("selected");
        $(this).addClass("selected");
        $ul = $(this).find("ul");
        if($ul.length){
            $("#nav ul").not($ul).slideUp(DURATION_SLIDE);
            $ul.slideDown(DURATION_SLIDE);
        } else {
            $("#nav ul").not($(this).parents("ul")).slideUp(DURATION_SLIDE);
        }
        loadPage($(this).attr("id").substr(4));
        $(this).find("li").each(function(){
            var id = $(this).attr("id").substr(4);
            loadPage(id, true);
        });
        return false;
    });
    /* Webcam images */
    $("#webcam_images li").click(function(){
       var img = $("#webcam img")[$.inArray(this, $("#webcam_images li"))];
       $("#webcam img").not(img).fadeOut();
       $(img).fadeIn();
    });
});
$(window).load(function(){
    $("#nav > li").each(function(){
        var id = $(this).attr("id").substr(4);
        loadPage(id, true);
    });
});
function loadPage(id, cacheOnly){
    /* If page is not in cache or already being cached */
    if(!pages['page-' + id]){
        pages['page-' + id] = 'loading';
        if(!cacheOnly){
            $("#loading").show();
        }
        /* Get page JSON data */
        $.get('/json/' + id, function(page) {
            if(!cacheOnly){
                $("#loading").hide();
            }
            /* Cache page */
            pages['page-' + page.id] = page;
            if(page.images.length){
                var img = page.images[0];
                if(!cacheOnly){
                    image_q.push(img);
                }
                else {
                    image_q.unshift(img);
                }
                cacheImages();
            }
            if(!cacheOnly){
                switchPage(id);
            }
        },
        'json');
    }
    else if(!cacheOnly){
        switchPage(id);
    }
}
function switchPage(id){
    if(slideshow_timeout){
        window.clearTimeout(slideshow_timeout);
    }
    if(switchpage_timeout){
        window.clearTimeout(switchpage_timeout);
    }
    if(pages['page-' + id] == 'loading'){
        $("loading").show();
        switchpage_timeout = window.setTimeout(
            "switchPage(" + id + ")",
            100
        );
        return;
    }
    $("loading").hide();
    $("#contentwrapper").animate(
        {top:504},
        {duration:DURATION_PAGE, queue:false, complete:function(){
                 p = pages["page-" + id];
                 if(p.content){
                    $("#content").html(p.content);
                    if(p.type == "t"){
                        $("#content").attr("class", p.type);
                        $("#contentwrapper")
                            .attr("class", p.type)
                            .animate({
                            top:426
                        }, {duration:DURATION_PAGE});
                    }
                    else {
                        $("#content").attr("class", p.type);
                        $("#contentwrapper")
                            .attr("class", p.type)
                            .animate({
                            top:0
                        }, {duration:DURATION_PAGE});
                    }
                }
                if(p.images.length){
                    switchImage(0, p)
                }
                $("#webcam_images li").click(function(){
                   var img = $("#webcam img")[$.inArray(this, $("#webcam_images li"))];
                   $("#webcam img:visible").not(img).fadeOut();
                   $(img).fadeIn();
                });
            }
        }
    );
    pageTracker._trackPageview($("nav-" + id).find("> a").attr("href"));
}
function switchImage(ix, p){
    if(slideshow_timeout){
        window.clearTimeout(slideshow_timeout);
    }
    if($("#" + p.images[ix].id + ".loading").length){
        $("#loading").show();
        slideshow_timeout = window.setTimeout(
            "switchImage(" + ix + ", pages['page-" + p.id + "'])",
            100
        );
        return;
    }
    $("#loading").hide();
    $("img.pageimage:visible").fadeOut(DURATION_IMAGE_FADE);
    $("#" + p.images[ix].id).fadeIn(DURATION_IMAGE_FADE);
    if(p.images.length > 1){
        if(++ix >= p.images.length){
            ix = 0;
        }
        image_q.push(p.images[ix]);
        cacheImages();
        slideshow_timeout = window.setTimeout(
            "switchImage(" + ix + ", pages['page-" + p.id + "'])",
            DURATION_IMAGE_CHANGE
        );
    }

}
function cacheImages(){
    if(!image_q_running){
        image_q_running = true;
        processQueue();
    }
}
function processQueue(){
    debugWrite("Queue running: " + image_q.length);
    img = image_q.pop();
    if(!img){
        debugWrite("Stopping queue: " + image_q.length);
        image_q_running = false;
        return;
    }
    if(!$("#" + img.id).length){
        $('<img/>')
            .hide()
            .addClass("pageimage")
            .addClass("loading")
            .attr(
                { alt: img.title,
                  src: img.src,
                  id: img.id }
            ).load(function(){
                $(this).removeClass("loading").addClass("loaded");
                debugWrite("Image loaded: " + img.id);
                processQueue();
            }).appendTo("#page");
    }
    else{
        debugWrite("Image exists: " + img.id);
        processQueue();
    }
}
function debugWrite(s){
    return;
    if(!$("#debug").length){
        $("<div></div>").attr("id", "debug").appendTo("body");
    }
    $("#debug").html(s + "<br />" + $("#debug").html());
}


