$(document).ready(function() { //perform actions when DOM is ready

    function swapFirstLast(isFirst,id,z) {
        var inAnimation = false; //flag for testing if we are in a animation
        if(inAnimation) return false; //if already swapping pictures just return
        else inAnimation = true; //set the flag that we process a image

        var processZindex, direction, newZindex, inDeCrease; //change for previous or next image

        if(isFirst) {
            processZindex = z;
            direction = '-';
            newZindex = 1;
            inDeCrease = 1;
        } //set variables for "next" action
        else {
            processZindex = 1;
            direction = '';
            newZindex = z;
            inDeCrease = -1;
        } //set variables for "previous" action
        
        $('#'+id+' img').each(function() { //process each image
            if($(this).css('z-index') == processZindex) { //if its the image we need to process
                $(this).animate({
                    'top' : direction + $(this).height() + 'px'
                }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
                    $(this).css('z-index', newZindex) //set new z-index
                    .animate({
                        'top' : '0'
                    }, 'slow', function() { //animate the image back to its original position
                        inAnimation = false; //reset the flag
                    });
                });
            } else { //not the image we need to process, only in/de-crease z-index
                $(this).animate({
                    'top' : '0'
                }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
                    $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
                });
            }
        });

        return false; //don't follow the clicked link
    }
    
    function installZGallery(id){
        var z = 0; //for setting the initial z-index's

        $('#'+id+' img').each(function() { //set the initial z-index's
            z++; //at the end we have the highest z-index value stored in the z variable
            $(this).css('z-index', z); //apply increased z-index to <img>
        });

        $('#'+id+'_next a').click(function() {
            return swapFirstLast(true,id,z); //swap first image to last position
        });

        $('#'+id+'_prev a').click(function() {
            return swapFirstLast(false,id,z); //swap last image to first position
        });
    }
    
    installZGallery('pictures');
    installZGallery('picturesa');
    installZGallery('picturesr');
  
    //Youtube
  
    var current = 1; //flag for testing if we are in a animation
    $('#nexty a').click(function() {
        return swapFirstLastnext(); //swap first image to last position
    });

    $('#prevy a').click(function() {
        return swapFirstLastprev(); //swap last image to first position
    });
  
    function swapFirstLastnext() {
    
        $("#youtube"+current).fadeOut("slaw", function() {
            current = parseInt(current)+1;
            if(current > parseInt($("#youtube_count").html())) {
                current = 1;
            }
            $("#youtube"+current).fadeIn("slaw");
        });

        return false; //don't follow the clicked link
    }

    function swapFirstLastprev() {
    
        $("#youtube"+current).fadeOut("slaw", function() {
            current = parseInt(current)-1;
            if(current == 0) {
                current = parseInt($("#youtube_count").html());
            }
            $("#youtube"+current).fadeIn("slaw");
        });

        return false; //don't follow the clicked link
    }
    
    //left sidebar
    $(window).load(function()
    {
        var scroller = new StickyScroller("#left_wrap",
        {
            start: 192,
            end: $('.container').height()-500,
            interval: 0,
            range: 100,
            margin: 0
        });
                
        var opacity = .25;
        var fadeTime = 300;
        var current;

    });
  
  
});
