var isload=false;
function repositionneImg(){
$("#photo_ic .photoIC").each(function(){
	var w2 = $(this).parent().width(); $(this).width(w2);
  var h = $(this).height('').height(); var h2 = $(this).parent().height();
  if(h<h2){
    var top = Math.floor((h2-h)/2); $(this).css('margin-top',top);
  }else{
    $(this).height(h2);
  }
});
}

function checkNavPhoto(){

	var el = $("#boxPhotoImage img");
	if(el.is(':hidden')){
		$("#boxPhotoPrev > img").addClass('disabled');
		$("#boxPhotoNext > img").addClass('disabled');
		return;
	}
	var current=el.attr('src');
	var els = $(".imgbox"), find=null;
	for(i=0;i<els.length;i++){
		if( find!=null ) continue;
		if( $(els[i]).attr('href') == current ){
			find=i;
		}
	}

	var prev = $(els[find-1]);
	if(find==null || find-1<0 || prev.length==0 || prev.attr('href')=='javascript:;'){
		$("#boxPhotoPrev > img").addClass('disabled');
	}else{
		$("#boxPhotoPrev > img").removeClass('disabled');
	}
	var next = $(els[find+1]);
	if(find==null || find+1>=els.length || next.length==0 || next.attr('href')=='javascript:;'){
		$("#boxPhotoNext > img").addClass('disabled');
	}else{
		$("#boxPhotoNext > img").removeClass('disabled');
	}

}


function nextPhoto(){
	var el = $("#boxPhotoImage img");
	checkNavPhoto();
	if(el.is(':hidden')) return;
	var current=el.attr('src');
	var els = $(".imgbox"); var find=0;
	els.each(function(){
		if(find==1){
			var href = $(this).attr('href');
			if(href=="javascript:;") return;
			$("#boxPhoto").addClass('load'); 
			$("#boxPhotoImage > img").fadeOut(250,function(){
				var el=$(this); el.attr('src',href);
			});
			find=2;
		}else if(find==0){
			var href = $(this).attr('href');
			if( href == current ) find=1;
		}
	});
	checkNavPhoto();
}
function prevPhoto(){
	var el = $("#boxPhotoImage img");
	checkNavPhoto();
	if(el.is(':hidden')) return;
	var current=el.attr('src');
	var els = $(".imgbox"); var prev=null; var find=0;
	els.each(function(){
		if( find==1 ) return;
		var href = $(this).attr('href');
		if( href == current ){ 
			var href = prev; if(href=="javascript:;" || href==null) return;
			$("#boxPhoto").addClass('load'); 
			$("#boxPhotoImage > img").fadeOut(250,function(){
				var el=$(this); el.attr('src',href);
			});
			find=1;
		}
		prev=href;
	});
	checkNavPhoto();
}
window.onresize = retaillePhotoBox;
function retaillePhotoBox(){

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

  
  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 closeBox(){ $("#boxPhoto").fadeOut(700); $("#boxPhotoBack").hide(); };
var animOA = function(){

  repositionneImg();
  retaillePhotoBox();

  // 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=="37"){ var lien = $("#boxPhotoPrev > a").attr('href'); if(lien!==undefined) window.location.href = lien; }
    if(e.keyCode=="39"){ var lien = $("#boxPhotoNext > a").attr('href'); if(lien!==undefined) window.location.href = lien; }
      });
  $("a.imgbox").not('.nophoto').click(function(){
    var href = $(this).attr('href');
    var el = $("#boxPhotoImage > img");
    if( el.attr('src')!=href ){
      $("#boxPhoto").addClass('load');
      el.fadeOut(400,function(){
        $(this).attr('src',href);
        $("#boxPhoto").fadeIn('750',retaillePhotoBox);
      });
    }else $("#boxPhoto").fadeIn('750',retaillePhotoBox);
    return false;
  });



  
  var el0=$("#boxPhotoImage");
  var href = window.location.href;
  if( href.indexOf('#next')>0 ){
    var right = el0.css('right');
    el0.css('right','450px').animate({right:right},750);
  }else if( href.indexOf('#prev')>0 ){
    var right = el0.css('right');
    el0.css('right','-450px').animate({right:right},750);
  } else {

    var el0=$("#bandeCollec1"); var el1=$("#bandeCollec2");
    var w0 = el0.width(); var w1 = el1.width(); el1.width('0px');
    el0.width('0px').animate({width:w0},800,function(){
      el1.animate({width:w1},400);
    });
    if( jQuery.browser.msie && jQuery.browser.version<7 ){
      // Do nothin'
    }else{
      var el2 = $("#boxPhoto"); var h2 = el2.height();
      el2.height('0px').css('opacity','0').animate({height:h2,opacity:1},750);
    }

  }
  scrollBar();
  $("#scrollBar").css({position:'relative',display:'block'});

  
  isload=true;
  repositionneImg();

};

var loadAnimImg = function(){
	animOA();
	if( jQuery.browser.msie && jQuery.browser.version>=8 ){ 
		setTimeout(retaillePhotoBox,500);
		setTimeout(retaillePhotoBox,600);
		setTimeout(retaillePhotoBox,700);
		setTimeout(retaillePhotoBox,800);
		setTimeout(retaillePhotoBox,900);
	}
};
addOnLoad('loadAnimImg');
