$(document).ready(function() {
  $('.btn').removeClass('topselected');
  $('#work').addClass('topselected');
  
  $('#galeries-menu div:first-child a').addClass('selected');
  
  var back = '<div class="bkg"></div>';
  
  $(window).scroll(function() {
    var t = $('#photos').offset();
    var top = t.top;
    $('.bkg').height($('#photos').height());
    $('.bkg').offset({'top':top});
  });
  
  var affiche = '<div id="affiche"></div>';
  
  $('.bouton').click(function() {
    $('#controls').hide('fade', 300, function() {
        $("#affiche img").hide('fade', 300, function() {
          $(".bkg").hide('fade', 500, function() {
            $("#affiche").remove();
            $('.bkg').remove();
          });
        });
      });
      
    $('#loading').show();

    $('.bouton').removeClass('selected');
    $(this).addClass('selected');
    $('#loading').show();
    var book = $(this).attr('rel');

    $.ajax({
                url: 'http://www.brunofournier.com/bf-ajax/script.get_images.php',
                async: true,
                data: ({book:book}),
                success: function(data) {
                  $('#photos').html(data);
                  $('.pic').click(function() {
                    var choix = $(this).attr('src');
                    var index = choix.lastIndexOf('/')+1;
                    var pic = choix.substring(index);
                    var nom = pic.substring(0, pic.lastIndexOf('.'));
                    var chemin = choix.substring(0,index);
                    
                    var courante = $(this);
                    courante.addClass('cur');

                    $(back).insertBefore('#photos');
                    
                    $(affiche).insertBefore('#photos');
                    $('#affiche').html('<div align="center"><img src="' + chemin + 'm_' + nom + '.jpg" id="current" /><div id="titre"></div><div id="controls"><a href="javascript:void(0)" id="prev"><img src="http://brunofournier.com/images/prev.png" style="border:0" /></a><a href="javascript:void(0)" id="zoom"><img src="http://brunofournier.com/images/zoom.png" style="border:0" /></a> <a href="javascript:void(0)" id="remove"><img src="http://brunofournier.com/images/remove.gif" style="border:0" /></a><a href="javascript:void(0)" id="next"><img src="http://brunofournier.com/images/next.png" style="border:0" /></a></div></div>');
                    $('.bkg').show('fade', 600, function() {
                      $('#affiche').show('fade', 1000);
                    });
                    
                    $('#current').click(function() {
                      $('#affiche').hide('fade', 300, function() {
                          $('.bkg').hide('fade', 300);
                          $('#affiche').remove();
                          $('.bkg').remove();
                      });
                    });
                    
                    var book = $(this).attr('rel');
                    $('#titre').load('http://www.brunofournier.com/bf-ajax/script.get_titre.php?pic='+nom+'.jpg&book='+book);
                    
                    $('#prev').click(function() {
                      if($('.cur').attr('src') == $('#photos img:first').attr('src')) {
                        var preced = $('#photos img:last');
                      } else {
                        var preced = $('.cur').prev();
                      }
                      $('.cur').removeClass('cur');
                      preced.addClass('cur');

                      var choice = preced.attr('src');
                      var ind = choice.lastIndexOf('/')+1;
                      var photo = choice.substring(ind);
                      var name = photo.substring(0, photo.lastIndexOf('.'));
                      var path = choice.substring(0, ind);
                      
                      var b = $('.cur').attr('rel');
                      $('#titre').load('http://www.brunofournier.com/bf-ajax/script.get_titre.php?pic='+name+'.jpg&book='+b);

                      $('#current').attr('src', path + 'm_' + name + '.jpg');
                    });

                    $('#next').click(function() {
                      if(!$('.cur').next().length) {
                        var suiv = courante;
                      } else {
                        var suiv = $('.cur').next();
                      }
                      $('.cur').removeClass('cur');
                      suiv.addClass('cur');

                      var choice = suiv.attr('src');
                      var ind = choice.lastIndexOf('/')+1;
                      var photo = choice.substring(ind);
                      var name = photo.substring(0, photo.lastIndexOf('.'));
                      var path = choice.substring(0, ind);
                      
                      var b = $('.cur').attr('rel');
                      $('#titre').load('http://www.brunofournier.com/bf-ajax/script.get_titre.php?pic='+name+'.jpg&book='+b);

                      $("#current").attr('src', path + 'm_' + name + '.jpg');
                    });
                    
                    $('#zoom').click(function() {
                      var cur = $("#current").attr('src');
                      var ind = cur.lastIndexOf('/')+1;
                      var photo = cur.substring(ind);
                      var name = photo.substring(2, photo.lastIndexOf('.'));
                      var path = cur.substring(0, ind);
                      
                      $('<div id="layer" style="width:100%;height:100%;position:fixed;top:0;left:0;z-index:5000;background:#000;text-align:center;cursor:pointer"><img src="' + path + 'g_' + name + '.jpg" style="margin-top:15px" /></div>').appendTo('#wraper');

                      $('#layer').click(function() {
                        $('#affiche').remove();
                        $('.bkg').remove();
                        $(this).hide('fade', 1000, function() {
                          $(this).remove();
                        });
                      });
                    });

                    $('#remove').click(function() {
                      $('#controls').hide('fade', 300, function() {
                        $('#titre').hide('fade', 300, function() {
                          $("#affiche img").hide('puff', 600, function() {
                            $(".bkg").hide('fade', 500, function() {
                              $("#affiche").remove();
                              $('.bkg').remove();
                            });
                          });
                        });
                      });
                    });
                  });
                },
                error: function() { $('#photos').html('Error : please try again later'); },
                complete: function() {
                  $('#loading').hide('fade', 1000);
                  $('#photos').find('.pic');
                }
              });
  });
  
  $('.pic').click(function() {
    var choix = $(this).attr('src');
    var index = choix.lastIndexOf('/')+1;
    var pic = choix.substring(index);
    var nom = pic.substring(0, pic.lastIndexOf('.'));
    var chemin = choix.substring(0,index);
    
    var courante = $(this);
    courante.addClass('cur');
    
    $(back).insertBefore('#photos');
    $(affiche).insertBefore('#photos');
    $('#affiche').html('<div align="center"><img src="' + chemin + 'm_' + nom + '.jpg" id="current" /><div id="titre"></div><div id="controls"><a href="javascript:void(0)" id="prev"><img src="http://brunofournier.com/images/prev.png" style="border:0" /></a><a href="javascript:void(0)" id="zoom"><img src="http://brunofournier.com/images/zoom.png" style="border:0" /></a> <a href="javascript:void(0)" id="remove"><img src="http://brunofournier.com/images/remove.gif" style="border:0" /></a><a href="javascript:void(0)" id="next"><img src="http://brunofournier.com/images/next.png" style="border:0" /></a></div></div>');
    $("#current").css('cursor', 'arrow');
    
    /*if($('#photos').height() > 400) {
      $('.bkg').height($('#photos').height());
    }*/
    
    $('.bkg').show('fade', 600, function() {
      $('#affiche').show('fade', 1000);
    });
    
    var book = $(this).attr('rel');
    $('#titre').load('http://www.brunofournier.com/bf-ajax/script.get_titre.php?pic='+nom+'.jpg&book='+book);
    
    $('#prev').click(function() {
      if($('.cur').attr('src') == $('#photos img:first').attr('src')) {
        var preced = $('#photos img:last');
      } else {
        var preced = $('.cur').prev();
      }
      $('.cur').removeClass('cur');
      preced.addClass('cur');
      
      var choice = preced.attr('src');
      var ind = choice.lastIndexOf('/')+1;
      var photo = choice.substring(ind);
      var name = photo.substring(0, photo.lastIndexOf('.'));
      var path = choice.substring(0, ind);
      
      var b = $('.cur').attr('rel');
      $('#titre').load('http://www.brunofournier.com/bf-ajax/script.get_titre.php?pic='+name+'.jpg&book='+b);
      
      $('#current').attr('src', path + 'm_' + name + '.jpg');
    });
    
    $('#next').click(function() {
      if(!$('.cur').next().length) {
        var suiv = courante;
      } else {
        var suiv = $('.cur').next();
      }
      $('.cur').removeClass('cur');
      suiv.addClass('cur');
      
      var choice = suiv.attr('src');
      var ind = choice.lastIndexOf('/')+1;
      var photo = choice.substring(ind);
      var name = photo.substring(0, photo.lastIndexOf('.'));
      var path = choice.substring(0, ind);
      
      var b = $('.cur').attr('rel');
      $('#titre').load('http://www.brunofournier.com/bf-ajax/script.get_titre.php?pic='+name+'.jpg&book='+b);
      
      $("#current").attr('src', path + 'm_' + name + '.jpg');
    });
    
    $('#zoom').click(function() {
      var cur = $("#current").attr('src');
      var ind = cur.lastIndexOf('/')+1;
      var photo = cur.substring(ind);
      var name = photo.substring(2, photo.lastIndexOf('.'));
      var path = cur.substring(0, ind);

      $('<div id="layer" style="width:100%;height:100%;position:fixed;top:0;left:0;z-index:5000;background:#000;text-align:center;cursor:pointer"><img src="' + path + 'g_' + name + '.jpg" style="margin-top:15px" /></div>').appendTo('#wraper');
      
      $('#layer').click(function() {
        $('#affiche').remove();
        $('.bkg').remove();
        $(this).hide('fade', 1000, function() {
          $(this).remove();
        });
      });
    });
    
    $('#remove').click(function() {
      $('#controls').hide('fade', 300, function() {
        $('#titre').hide('fade', 300, function() {
          $("#affiche img").hide('puff', 600, function() {
            $(".bkg").hide('fade', 500, function() {
              $("#affiche").remove();
              $('.bkg').remove();
            });
          });
        });
      });
    });
  });

});
