/*
 *
*/

function debug(message){if(typeof console == 'object') console.log(message);}


// on cache l'image avant son chargement
// n'est pas mis en CSS, sinon ceux qui ne JS pas ne verront rien
// l'image sera réaffiché pas when_resize
function init_element(){ 
	debug("bigImage hide");
//	$("#bigImage").hide();
	$("#bigImage").css("display","none");
$('head').append('<style>#bigImage \{display:none;\}</style>');
}
function init_element_ready(){ 
   img_scroll_stop();
   $("#imgBig").mouseout(function(e){ img_scroll_stop(); });
   $("#imgBig").mousemove(function(e){
   	var x = (e.pageX- this.offsetLeft);

   	var y = (e.pageY-this.offsetTop);
//   	  debug(px +', '+ py);

    var cible = calc_cible(x,y);
    //var cible = calc_cible2(x,y);
	  img_scroll(cible.x_cible,cible.x_vit,cible.y_cible,cible.y_vit);
  	  //debug(x_cible+':'+x_vit +', '+ y_cible+':'+y_vit );
   }); 
   $(window).resize(function(){debug('resize'); when_resize(); });
   $("#bigImage").load(function(){ debug('load'); when_resize();  });
   debug('init_element_ready when resize'); when_resize();
	//$("body").css("background-color","#AAA");		
}


// Déplace l'image du côté ou la souris chatouille    
function calc_cible(x,y){
	var s=50; // sensibilité
   	var cw = $("#imgBig").outerWidth();
   	var ch = $("#imgBig").outerHeight();
   	var px = Math.round(x / cw * s * 2 - s); 
   	var py = Math.round(y / ch * s * 2 - s);

    var iw = $("#bigImage").outerWidth();
    var il = $("#imgScrollH").position().left;

	var ih = $("#bigImage").outerHeight();
    var it = $("#imgScrollV").position().top;

	var x_cible = il; var x_vit=0;
	if(iw > cw) {
      if(px > 2) 
      {
      	var x_cible = cw - iw;
	    var x_vit = px - 2;
      } else if(px < -2) 
      { 
      	var x_cible = 0;
	    var x_vit = (px + 2)*-1;
	  }      	
	}
	var y_cible = it; var y_vit=0;
	if(ih > ch) {
      if(py > 2) 
      {
      	var y_cible = ch - ih;
	    var x_vit = py - 2;
      } else if(py < -2) 
      { 
      	var y_cible = 0;
	    var y_vit = (py + 2)*-1;
	  }      	
	 }
	x_vit=5; y_vit=5;
	return {x_cible:x_cible,x_vit:x_vit,y_cible:y_cible,y_vit:y_vit};
}

// Déplace l'image en position relative à la position de la souris
// Si la souris est en haut, on voit le haut de l'image, si elle est en bas, on vois le bas
function calc_cible2(x,y){
	var cw = $("#imgBig").outerWidth();
   	var px = x / cw * 1.2 - 0.1;
   	var ch = $("#imgBig").outerHeight();
   	var py = y / ch * 1.2 - 0.1;

    var iw = $("#bigImage").outerWidth();
    var il = $("#imgScrollH").position().left;

	var ih = $("#bigImage").outerHeight();
    var it = $("#imgScrollV").position().top;
	
	var x_cible = il; var x_vit=0;
	if(iw > cw) {
	  var x_min = 0; var x_max = cw - iw;
	  x_cible = Math.round((x_max - x_min) * px);
	}
	var y_cible = it; var y_vit=0;
	if(ih > ch) {
	  var y_min = 0; var y_max = ch - ih;
	  y_cible = Math.round((y_max - y_min) * py);
	}
	 x_vit=10; y_vit=10;
	return {x_cible:x_cible,x_vit:x_vit,y_cible:y_cible,y_vit:y_vit};
}


function img_scroll_stop(){
//	$("#bigImage").stop();
		$("#imgScrollH").stop(); 
		$("#imgScrollV").stop(); 
		var il = $("#imgScrollH").position().left;
    	var it = $("#imgScrollV").position().top;
   	  	debug('stop at '+il+':'+it);
		$.x_cible=il;
    	$.x_vit=0;
		$.y_cible=it;
    	$.y_vit=0;
}
function img_scroll_fin(){
    var il = $("#imgScrollH").position().left;
    var it = $("#imgScrollV").position().top;
   	  debug('fin at '+il+':'+it);
}
function img_scroll(x_cible,x_vit,y_cible,y_vit){
   if(x_cible != $.x_cible || x_vit != $.x_vit  )
   {
    var il = $("#imgScrollH").position().left;
    var temps=Math.abs(x_cible - il) * 5 / x_vit ;
   	  debug("actuel:"+il+" to:"+x_cible+" - tps:"+temps+" - x_vit:"+x_vit);
		$("#imgScrollH").stop().animate(  {left:x_cible+"px"}, temps, 'linear',img_scroll_fin); 
		$.x_cible=x_cible;
    	$.x_vit=x_vit;
    }
    //debug('ancien cible:'+$.y_cible+' - demandé:'+y_cible);
   if(y_cible != $.y_cible || y_vit != $.y_vit  )
   {
    var it = $("#imgScrollV").position().top;
    var temps=Math.abs(y_cible - it)* 5 / y_vit;
   	  debug("actuel:"+it+" to:"+y_cible+" - tps:"+temps+" - y_vit:"+y_vit);
		$("#imgScrollV").stop().animate(  {top:y_cible+"px"}, temps, 'linear',img_scroll_fin); 
		$.y_cible=y_cible;
    	$.y_vit=y_vit;
    }
}

function when_resize() {
	var marge=5;
	var itop = $("#hmenu").position().top + $("#hmenu").outerHeight()+marge;
    var ibot = $("#imgminis").position().top - marge;
   debug("win heigh"+$(window).height()+" - top:"+itop+" - bot:"+ibot);
	//var mt = $(window).height() -170;
	//var mt = $(window).height() - itop - ibot;
	var mt = ibot - itop;
   	$("#imgBig").css("top",itop+"px"); 
	$("#imgBig").height(mt);
	var cw = $("#imgBig").outerWidth();
   	var ch = $("#imgBig").outerHeight();
    var iw = $("#bigImage").outerWidth();
    var ih = $("#bigImage").outerHeight();
	// si l'image n'est pas chargé, on s'arrete
	// la fct sera réappelé par l'evenement nload
	if(iw == 0 && ih == 0) return;

   	var nl = Math.round((cw - iw) / 2);
   	var nt = Math.round((ch - ih) / 2);

  debug("container "+cw+"x"+ch+" - image "+iw+"x"+ih);

		$("#imgScrollH").css("left",nl+"px"); 
		$("#imgScrollV").css("top",nt+"px"); 

  debug("center at "+nl+":"+nt);
//   	$("#imgBig").children().center();
// on réaffiche l'image en fadeIn (caché par init_element()
 debug('fadein');
 $("#bigImage").fadeIn();
   	
  //  var ct = $("#bigImage").position().top;
//    var cl = $("#bigImage").position().left;

   	var ch = $("#imgBig").outerHeight();
    var iw = $("#bigImage").outerWidth();
    var ih = $("#bigImage").outerHeight();

//var it=nt; var il=nl;
    var it = $("#bigImage").position().top;
    var il = $("#bigImage").position().left;

    $.x_cible=il;
    $.y_cible=ih;
    $.x_vit=0;
    $.y_vit=0;
  debug("("+mt+")"+cw +'x'+ ch + ' - ' + iw+'x'+ih+'->'+il+':'+it);
}
