
function backgroundImage(args){
    for(i in args){
    bgSrc = args[i]
    var winheight = $(window).height();
//    var winheight = winheight-50
//  console.log('windowheight = '+winheight);
    var imgcount = args.length
//  console.log('imgcount = '+imgcount);

    $('div#bgwrapper').append('<div id="bg'+i+'"class="bg"> </div>');
//  console.log('i = '+i);

    if (imgcount>1){
        $('div#bgNavNumbers').append('<a id="bgNav'+i+'"class="bgNav" href="#">'+(parseInt(i)+1)+'</a>');
//  console.log('made bgNavNumbers');
    };
    
    $('#bg'+i).css({
        'background-image'  : 'url(/photos/'+bgSrc+')',
        'height'            : winheight
        });
    };

};

function hidenav(current){
    current = (parseInt(current));
    imgcount = photos.length
    
    if(current<1){
        $('div#bgNavHandles a#bgPrevious').hide();
        }
        else{
            $('div#bgNavHandles a#bgPrevious').show();
     };
     
     if(current>=parseInt(imgcount)-1){
         $('div#bgNavHandles a#bgNext').hide();
         }
         else{
             $('div#bgNavHandles a#bgNext').show();
    };
    
};

$(document).ready(function() {
 if(typeof(photos) != "undefined"){
    backgroundImage(photos);
    $('#bgNavHandles a').animate({
        'opacity':'1'
    })    .delay('1500').animate({
             'opacity':'0'
         });
    $('.bg:not(#bg0)').hide();
    $('#bg0').addClass('bgActive');
    $('#bgNav0').addClass('bgNavActive');

    var imgcount = photos.length
    var current = 0 
    var previous = current-1
    var next = current+1


//Run hidenav function
        hidenav(current)

//when a number is clicked, find that image and fade it in, fade the rest out.
     $('div#bgNavNumbers a.bgNav').click(function(){
         
          var num = ($(this).text()-1);
          var bgnum = '#bg'+ num;

          $('.bg:not('+bgnum+')').fadeOut(800).removeClass('bgActive');
          $(bgnum).fadeIn(800).addClass('bgActive');
          $('.bgNav').removeClass('bgNavActive');
          $(this).addClass('bgNavActive');
          current = num
          hidenav(current);
          console.log(current)
          return false
      });

//Next
      $('div#bgNavHandles a#bgNext').click(function(){

          var next = current+1
          
          $('.bgActive').fadeOut(800).removeClass('bgActive');
          $('#bg'+next).fadeIn(800).addClass('bgActive');
          $('.bgNav').removeClass('bgNavActive');
          $('#bgNav'+next).addClass('bgNavActive');
          current++   // = (parseInt(current)+1)
          hidenav(current);
        console.log(current)
        if(current>=(imgcount-1) | current == 0 ){
    		$("#nav").animate({
    				left: 0
    			  }, 300);  
        }else{
            $("#nav").animate({
      				left: -230
      			  }, 300);            
        }
          return false
      });
      
//Previous
//      $('div#bgNavHandles a#bgPrevious').click();
$('div#bgNavHandles a#bgPrevious').click(function(){

            var previous = current-1

            $('.bgActive').fadeOut(800).removeClass('bgActive');
            $('#bg'+previous).fadeIn(800).addClass('bgActive');
            $('.bgNav').removeClass('bgNavActive');
            $('#bgNav'+previous).addClass('bgNavActive');
            current--   // = (parseInt(current)-1)
            hidenav(current);
            console.log(current)
            $("#nav").animate({
      				left: -230
      			  }, 300);
            
          if(current>=(imgcount-1) | current == 0 ){
      		$("#nav").animate({
      				left: 0
      			  }, 300);  
          }
        });

      //Allows for left and right keys to browse images.
      $(document.documentElement).keyup(function (event) {
  	  if (event.keyCode == 37){	                // Previous 
  	     if(current == 0){
   	         return false
  	     }else{
  	         $('a#bgPrevious').trigger('click', 'click_bgPrev');
  	     }
  	  };
        if (event.keyCode == 39 ){
            if(current>=(imgcount-1)){
                return false
            }else{
                $('a#bgNext').click();
            }
        };
  	});

    }

            



//When the window resizes, resize the background image and place the vignette at the bottom.
        $(window).resize(function() {
            
            var winheight = $(window).height();
//            var winheight = winheight-50
            
          $('.bg').css({
              'height'  : winheight
          });
          $('#body_vignette').css({
              'top': (winheight-600)
              });
          });





});

