var width = 0;
$(document).ready( function() {
    $('#mar2010-slider').scrollTo( '0', 1, {axis:'x'} );
    $("#leftArrow, #rightArrow").fadeTo(1,0); 
    width = $("#mar2010-slider").width()+42;
    $("#mar2010-gallery div[id^=slide]").each( function() {
        var id = $(this).attr('id');
        id = id.substr(5); 
        slideLoad(id);   
    });
});
$("#leftArrow, #rightArrow").mouseenter( function() { $(this).fadeTo(150,1); });
$("#leftArrow, #rightArrow").mouseleave( function() { $(this).fadeTo(150,0); });
$("#leftArrow, #rightArrow").click( function() {
    if ( $(this).hasClass('left') ) {
        var scrollWidth = '-'+Math.abs(width); 
    } else if ( $(this).hasClass('right') ) {
        var scrollWidth = Math.abs(width);
    } else {
        alert("error:  not sure where to go.");
    }
    loadNext(scrollWidth, moveSlideContent);
});
function loadNext(direction, f) {
    // Find what ID should be loaded
    var thisID = $("#mar2010-gallery .active").attr('id');
    thisID = thisID.substr(5);  
    if ( direction < 0 ) {
        var minID   = 1;
        var firstID = $("#mar2010-gallery .slide:first").attr('id');
        firstID = firstID.substr(5);
        if (thisID == firstID) {
            loadID = $("#mar2010-gallery .slide:last").attr('id');
            loadID = loadID.substr(5);
            $("#mar2010-gallery .slide:first").before( $("#mar2010-gallery .slide:last") );
            $('#mar2010-slider').scrollTo( "+="+Math.abs(direction), 0, {axis:'x'} );     
        } else if (minID < thisID) {
            loadID = parseInt(thisID) - 1;
        } else if (minID == thisID) {
            var maxID = thisID;
            var slideIDs = $('#mar2010-gallery .slide'); 
            for(var i = 0; i < slideIDs.length; i++) {
              var idNo = $(slideIDs[i]).attr('id');
              idNo = parseInt(idNo.substr(5));
              if (idNo > maxID)
              {
                maxID = idNo;
              }
            }
            loadID = maxID;
        }
    } else if ( direction > 0 ) {
        var maxID = thisID;
        var slideIDs = $('#mar2010-gallery .slide'); 
        for(var i = 0; i < slideIDs.length; i++) {
          var idNo = $(slideIDs[i]).attr('id');
          idNo = parseInt(idNo.substr(5));
          if (idNo > maxID)
          {
            maxID = idNo;
          }
        }
        var lastID = $("#mar2010-gallery .slide:last").attr('id');  
        lastID = lastID.substr(5);
        if (thisID == lastID) {
            loadID = $("#mar2010-gallery .slide:first").attr('id');
            loadID = loadID.substr(5);
            $("#mar2010-gallery .slide:last").after( $("#mar2010-gallery .slide:first") );
            $('#mar2010-slider').scrollTo( "-="+Math.abs(direction), 0, {axis:'x'} );               
        } else if (maxID > thisID) {
            loadID = parseInt(thisID) + 1;
        } else if (maxID == thisID) {
            loadID = 1;
        }    
    }
    $("#mar2010-gallery .active").removeClass('active');
    $("#slide"+loadID).addClass('active');
    // check if slide is loaded
    slideLoad(loadID);
    f(direction);
}
function moveSlideContent(direction) {
    if ( direction < 0 ) {
        // move left; ie, move slide content right
        $('#mar2010-slider').scrollTo( "-="+Math.abs(direction), 600, {axis:'x'} );
        
    } else if (direction > 0) {
        // move right; ie, move slide content left
        $('#mar2010-slider').scrollTo( "+="+Math.abs(direction), 600, {axis:'x'} );   
    } 
}
function slideLoad(ID) {
    var slideContent = $("#slide"+ID).html();
    if ( slideContent == "loading..." ) {
        var requestString = "&mode=load&slide="+ID;
        $.ajax({  
            type: "POST",  
            url: "./march2010/functions.php",  
            data: requestString, 
            success: function(returnText) {
                $("#slide"+ID).html(returnText).hide(1).fadeIn(350);
            },
            error: function() {
                $("#slide"+ID).html("content not loaded. Please refresh the page and try again.");          
            }  
        }); 
    } else {
        // slide is loaded, do nothing  
    }
}
