$(function() {
    
    /* page setup */
    
    $('#title, #navigation, #onwards, #jamesjarvis1, #jamesjarvis2, #downloads, #biography').hide();
    $('#title').fadeIn(1000);
    swfobject.embedSWF($('#onwards-video').attr('href'), 'onwards-video', '980', '550', '8', null, null, {allowFullScreen: 'true', wmode: 'transparent'}, {});
    
    /* navigation */
    
    var videoShown = false;
    function showVideo() {
        $('#navigation li').removeClass('selected');
        $('#onwards-link').addClass('selected');
        $('#navigation, #header .title, #footer').fadeIn(1000);
        $('#title').fadeOut(1000);
        $('#biography').hide();
        $('#onwards').show();
        videoShown = true;
        return false;
    }

    $('#title, #onwards-link').click(showVideo);
    
    $('#bio-link').click(function() {
        $('#navigation li').removeClass('selected');
        $('#bio-link').addClass('selected');
        $('#biography').show();
        return false;
    });
       
    /* show video after time delay */
    
    setTimeout(function() {
        if (!videoShown) showVideo();
    }, 3000);

    /* wallpaper gallery */
    
    var FADE = (document.location.search.indexOf('fade') > - 1);
    var SPEED = document.location.search.indexOf('speed');
    if (SPEED == -1) {
        SPEED = 50;
    } else {
        SPEED = parseInt(document.location.search.substr(SPEED + 6));
    }
    
    var THUMBWIDTH = 245;
    var THUMBHEIGHT = 185;
    var PREVWIDTH = 980;
    var PREVHEIGHT = 554;
    
    var currentTarget = null;
    var shuffleTag = 0;

    function shuffle(a) { 
        for(var i = 0; i < a.length; i++) {
            var j = Math.floor(Math.random() * a.length);
            var temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        };
        return a;
    };

    function swapImage(image, src, width, height, left, top) {
        image.attr({src: src}).css({
            width: width,
            height: height,
            marginLeft: left,
            marginTop: top,
            opacity: FADE? 0 : 1
        })
        if (FADE) image.animate({
            opacity: 1
        });
    }
    
    function swapImages(e, image) {
        
        var images = $('#wallpapers li img');
        
        var indices = [];
        for (i = 0 ; i < images.length; i++) {
            indices.push(i);
        }
        indices = shuffle(indices);

        var imageSrc = '';
        if (typeof(image) != 'undefined') {
            imageSrc = $(image).parent('a').attr('href').replace('wallpapers','thumbnails');
            images.each(function(i) {
                //ensure selected tile flips first
                if (this == image) {
                    var selectedIndex = indices.indexOf(i);
                    var temp = indices[0];
                    indices[0] = indices[selectedIndex];
                    indices[selectedIndex] = temp;
                }
            });
        }
        
        var tag = ++shuffleTag;
        
        var index = 0;
        function fadeNext() {
            if (tag == shuffleTag) {
                var i = indices[index];
                if (!imageSrc) {
                    var tempSrc = $(images).eq(i).parent('a').attr('href').replace('wallpapers','thumbnails');
                    swapImage(images.eq(i), tempSrc, THUMBWIDTH, THUMBHEIGHT, 0, 0);
                } else {
                    swapImage(images.eq(i), imageSrc, PREVWIDTH, PREVHEIGHT, -(i % 4) * THUMBWIDTH, -Math.floor(i / 4) * THUMBHEIGHT);
                }
                if (++index < images.length) {
                    setTimeout(fadeNext, SPEED);
                }
            }
        };
        setTimeout(fadeNext, 0);
        return false;
    }
    
    $('#wallpapers li a img').hover(
        function(e) {
            return swapImages(e, this);
        },
        swapImages
    );

});