
window.onresize = retaillePhotoBox;
function retaillePhotoBox(){

  var el = $("#boxPhoto");
  var w = el.width(); var h = el.height();

  if( el.is(':visible') ){
	  $("#boxPhotoBack").css('left','-5000px');
	  var left = ($(window).width()-960)/2 *-1;
	  $("#boxPhotoBack")
		.show()
		.css('left',left)
		.width( $(window).width() )
		.height( $(window).height() )
		.click( closeBox );
  }

  var el = $("#boxPhotoImage > img");
  var LargeurPhoto = (w - $("#boxPhotoLeft").width());
  var HauteurPhoto = (h - 30);
  el.parent().css('right',0);
  el.width(LargeurPhoto).height('');

  if(el.height()<HauteurPhoto){
    el.height(HauteurPhoto).width('');
    var right = (LargeurPhoto - el.width())/2;
    el.parent().css('right',right);
  }

}

function retaillePhotoThumb(){
  $(".thumbs_level img").each(function(){
    var el = $(this); var h = el.height();
    if(h<125) el.width('').height(125);
    var marginT = Math.round( (125-el.height())/2 );
    el.css('margin-top',marginT);
    var marginL = Math.round( (125-el.width())/2 );
    el.css('margin-left',marginL);
  });
}

function closeBox(){ $("#boxPhoto").fadeOut(700, retaillePhotoBox); $("#boxPhotoBack").hide(); };
var animOA = function(){


  retaillePhotoThumb();
  
  // BOX
  $("#boxPhotoImage > img").bind('load',function(){ retaillePhotoBox(); $(this).fadeIn(500, function(){ $("#boxPhoto").removeClass('load'); retaillePhotoBox(); }); });
  $("#boxPhotoClose").css('cursor','pointer').click(closeBox);
  $(document).keyup(function(e){ if(e.keyCode=="27") closeBox(); if(e.keyCode=="37") prevBox(); if(e.keyCode=="39") nextBox(); });

  // navigation Box
  var nbPhoto=0; var photoArray=new Array(); var CurrentPhoto=0; var currentAnim=false;
  function verifNavig(){

    var next=false; var prev=false;
    if(CurrentPhoto<(photoArray.length-1)) next=true;
    if(CurrentPhoto>0) prev=true;
    if(!next && GalSuiv) next=true;
    if(!prev && GalPrev) prev=true;

    if(next) $("#boxPhotoNext > img").removeClass('disabled').css('cursor','pointer');
    else $("#boxPhotoNext > img").addClass('disabled').css('cursor','default');

    if(prev) $("#boxPhotoPrev > img").removeClass('disabled').css('cursor','pointer');
    else $("#boxPhotoPrev > img").addClass('disabled').css('cursor','default');

  }
  var prevBox = function(){
    if(!currentAnim && $("#boxPhoto").is(':visible')){
      if(CurrentPhoto>0){
        CurrentPhoto = CurrentPhoto-1;
        var el = photoArray[CurrentPhoto];
        var href = el.attr('href');
        var title = el.attr('title');
        showBox(href,title);
        verifNavig();
      }else if(GalPrev){
        document.location.href = LinkPrev;
      }
    }
    return false;
  };
  var nextBox = function(){
    if(!currentAnim && $("#boxPhoto").is(':visible')){
      if(CurrentPhoto<(photoArray.length-1)){
        CurrentPhoto = CurrentPhoto+1;
        var el = photoArray[CurrentPhoto];
        var href = el.attr('href');
        var title = el.attr('title');
        showBox(href,title);
        verifNavig();
      }else if(GalSuiv){
        document.location.href = LinkSuiv;
      }
    }
    return false;
  };
  $("#boxPhotoPrev > img").click(prevBox);
  $("#boxPhotoNext > img").click(nextBox);
  function showBox(href,title){
    var el = $("#boxPhotoImage > img"); currentAnim=true;
    $("#boxPhotoLegend").fadeOut(500,function(){ $(this).html(title).fadeIn(500, retaillePhotoBox); });
    if( el.attr('src')!=href ){
      $("#boxPhoto").addClass('load');
      el.fadeOut(400,function(){
        $(this).attr('src',href);
        $("#boxPhoto").fadeIn('750',function(){ currentAnim=false; retaillePhotoBox(); });
      });
    }else $("#boxPhoto").fadeIn('750',function(){ currentAnim=false; retaillePhotoBox(); });
  }
  $("a.imgbox").not('.nophoto').each(function(){ $(this).attr('rel',nbPhoto); photoArray[nbPhoto]=$(this); nbPhoto++; }).click(function(){
    CurrentPhoto = Number($(this).attr('rel'));
    var href = $(this).attr('href');
    var title = $(this).attr('title');
    showBox(href,title);
    verifNavig();
    return false;
  });



  var el0 = $("#GalThumb");
  var href = window.location.href;
  var Anim=true;
  if( href.indexOf('#boxNext')>0 ){

    var el = photoArray[CurrentPhoto];
    var href = el.attr('href');
    var title = el.attr('title');
    showBox(href,title);
    verifNavig(); Anim=false;
	retaillePhotoBox();

  }else if( href.indexOf('#boxPrev')>0 ){

    CurrentPhoto = photoArray.length-1;
    var el = photoArray[CurrentPhoto];
    var href = el.attr('href');
    var title = el.attr('title');
    showBox(href,title);
    verifNavig(); Anim=false;
	retaillePhotoBox();

  }else if( href.indexOf('#next')>0 ){

    var right = 156; var width = el0.width();
    $("#GalThumb > div").css({'left':-width}).animate({left:0},750);
    el0.width('0px').css({left:'auto',right:(right-69)}).animate({right:right,width:width},750);

  }else if( href.indexOf('#prev')>0 ){

    var left = el0.css('left'); var width = el0.width();
    el0.width('0px').css('left','265px').animate({width:width,left:left},750)

  } else {

    var nb=0; $(".thumbs_level > div").hide().width('0px').height('0px').each(function(){
      var temp0=300; var temp=nb*temp0; nb++;
      var t = $(this); setTimeout(function(){ t.animate({width:125, height:125},temp0); },temp);
    });

  }

  if(Anim){
    var el = $("#GalTitre > h2"); var w = el.width()+10;
    var el2 = $("#GalLigne"); var w2 = el2.width();
    var el3 = $("#GalLigne2"); var w3 = 500;
    var el4 = $("a#GalNext,a#GalPrev");
    var el5 = $("#GalNav"); var h5 = el5.height(); el5.height('1px');
    el.hide(); el2.width('0px'); el3.width('0px');
    el4.css('opacity','0.1'); el5.animate({height:h5},1000);
    el2.animate({width: w2},500,function(){
      el3.css({right:'auto',left:w});
      w3-=w; el3.animate({width:w3},600);
      el.fadeIn(600);
      el4.animate({opacity:1},800);
    });
  }


  $("#GalNav .categorie a, #GalNav .categorie").click(function(){
    if( $(this).is('.categorie') ) var el = $(this).next();
    else var el = $(this).parent().next();
    var temps = 600;
    $("#GalNav .separe").not(el).slideUp(temps);
    el.slideDown(temps);
    return false;
  }).css('cursor','pointer');


  
};

addOnLoad('animOA');

