$(document).ready(function(){

// LISTENERS

// NEXT
$('#next2').click(function () { 

// We want to advance the frame forward one, lets pull our position info
var i = parseInt($("#mini-slideshow").data('position').current) + 1;
var n = parseInt($("#mini-slideshow").data('position').total);

if (i > n) { i = 0;}
    advanceSlide(i,n);
});

// PREVIOUS
$('#prev2').click(function () {

// We want to advance the frame forward one, lets pull our position info
var i = parseInt($("#mini-slideshow").data('position').current) - 1;
var n = parseInt($("#mini-slideshow").data('position').total);

//alert('i=' + i + ' | n=' + n);

if (i < 0) { i = n;}
    advanceSlide(i,n);
});
$('div#nav').show();

// GO TO SLIDE #
$('input#position').keypress(function (e) {
    var n = parseInt($("div").data('position').total);
    if (e.which == 13) {
      
      imgPlace = $('input#position').val();
      current = parseInt($("div").data('position').current);
      
      if (isNaN(imgPlace) == true) {
        alert('You may only use numbers.');
        imgPlace = $('input#position').val(current+1);
        return;
      }
      
      viewedImage(current, 0);


    if (imgPlace == '' || imgPlace == '0') {
        imgPlace = '1';
    }
    slideSet(imgPlace, n);
    }
});


}); // END READY

// BEGIN FUNCTIONS

function advanceSlide(i,n) {
    //console.log('We are heading to position: ', i);
    var ppre = i-2;
    var pre = i-1;
    var next = i+1;
    var nnext = i +2;

    if (ppre < 0) {ppre = n}
    if (pre < 0) {pre = n}
    if (next > n) {next = 0; prev = n;}
    savePosition (pre, i, next, n);
    if (nnext > n) {nnext = 0; prev = n;}
    viewedImage(ppre, '0');
    viewedImage(pre, '0');
    viewedImage(next, '0');
    viewedImage(nnext, '0');
    viewedImage(i, '1');
}

function slideSet(imgPlace, n) {

    sStart = imgPlace-1;
        if (sStart > n) {
            sStart = n;
            imgPlace = n+1;
        }
        
    var pre = sStart-1;
    var next = sStart+1;
    
    if (pre < 0) {pre = n}
    if (next > n) {next = 0; prev = n;}
        
    // Lets go ahead and load current image + next + previous
    viewedImage(sStart, 1);
    viewedImage(pre, 0);
    viewedImage(next, 0);
    
    // Write position info into a data div
    savePosition (pre, sStart, next, n)
}

// This function takes the index position of the DIV and processes it for us in advance
function viewedImage(pos, state) {
    if ($('ul#slideshow > li:eq(' + pos + ')').hasClass('viewed') == false) {
        var info = $('ul#slideshow > li:eq(' + pos + ') > div.image').text();
        var pieces = info.split("|");
        $('ul#slideshow > li:eq(' + pos + ') > div.image').html('<img src="' + pieces[0] + '" height="' + pieces[1] + '">');
        $('ul#slideshow > li:eq(' + pos + ') > form > div.links > span.grabUrl').html('<a href="' + location.href + '?img=' + parseInt(pos+1) + '">Image Link</a>');
        $('ul#slideshow > li:eq(' + pos + ') > div.links > span.grabUrl').html('<a href="' + location.href + '?img=' + parseInt(pos+1) + '">Image Link</a>');
        $('ul#slideshow > li:eq(' + pos + ')').addClass('viewed');
    }
    // This is the slide that is turned on
    if (state == '1') {
        $('ul#slideshow > li:eq(' + pos + ')').show();
        $('input#position').val(pos+1);
    }
    else {
        $('ul#slideshow > li:eq(' + pos + ')').hide();
    }
}

function savePosition (pre, current, next, n) {
    //alert('Saving positions: previous = ' + pre + 'current = ' + current + ' next = ' + next + ' total = ' + n);
    $("#mini-slideshow").data("position", { previous:pre, current:current, next:next, total:n });
    //console.log('Position', 'Previous: ' + pre + ' | Current: ' + current + ' | Next: ' + next);
}


function loadGallery() {

// Lets get the number of images, subtract 1 b/c zero-based
var n = $('ul#slideshow > li').size() - 1;

// Default starting position
var sStart = 0;

        // Lets go ahead and flip current image + next + previous
        viewedImage(n, '0');
        viewedImage(0, '1');
        viewedImage(1, '0');
        // Write position info into a data div
        savePosition (n, '0', '1', n)

// Let people know the are on 1 of X images
$('span#total').text(n+1);

}

