var depart = {'x':-1, 'y':-1,'image' : null,'zone' : null};
 var arrivee = {'x':-1, 'y':-1,'image' : null,'zone' : null};
 nbImages += 10;

 
 Array.prototype.indexOf = function( v ) {
	 for( var i = 0 ; i <  this.length; i++ ) {
	  if( this[i]==v  ) { return i; }
	 }
	 return -1;
	};

 
 function findScroll() {
	 var dsocleft = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft; 
	 var dsoctop = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
	 if (!dsocleft) dsocleft=0;
	 if (!dsoctop) dsoctop=0;

	 return {'x':dsocleft, 'y':dsoctop};
 }	  
 
 function showZoom(e) {
  $("bigCarte").style.display = "";
  $("viseur").style.display = "";
  $("bandeaubas").style.display = "";
  $("echelle").style.display = "";
  op_ghost = 0;
  ghost(true);
 }	
 function hideZoom(e) {
	  $("bigCarte").style.display = "none";
	  $("viseur").style.display = "none";
	  $("bandeaubas").style.display = "none";
	  $("echelle").style.display = "none";
 } 

 var coord = {'x':0, 'y':0};

 var op_ghost = 5;
 var timeOn = null;
 
 function ghost(noKeep) {
  if (op_ghost<10) {	 
   setOpacity("echelle",op_ghost);
   setOpacity("bandeaubas",op_ghost);
   op_ghost++;
   if (noKeep)
	return setTimeout("ghost(true);",100);
   else
    return setTimeout("timeOn=ghost();",100);
  } 
 }
 
 function zoom(e) {
	 if (window.event) e = window.event;
	 var pos = findPos($("imgMini"));
	 var scroll = findScroll();
	 coord.x=e.clientX-(pos.x-scroll.x);
     coord.y=e.clientY-(pos.y-scroll.y);

     if (coord.x<32)
    	 coord.x=32;
     if (coord.y>240)
    	 coord.y=240;
     
     $("imgZoom").style.marginLeft=(parseInt(277/2)-parseInt((coord.x)*4.33213)) + "px";
     $("imgZoom").style.marginTop=(parseInt(262/2)-parseInt((coord.y)*4.33213)) + "px";

     if (timeOn)
      clearTimeout(timeOn);
     if (op_ghost>=5) {
      op_ghost = 5;
      timeOn=ghost();
     } 

    // $("imgMini").setAttribute("title","{"+coord.x+';'+coord.y+'}');
 }
 
 function createMarker(e) {
	 if (window.event) e = window.event;	 
	 var scroll = findScroll();

     var zoneEnCours = zoneOfPoint(coord);
	 if (!zoneEnCours) return;
	 
  if (!depart.image) {
	  depart.image = new Image();
	  depart.image.src="images/dmarker.png";
	  document.body.appendChild(depart.image);
	  depart.image.style.position = "absolute";
	  depart.image.style.left =( e.clientX-9+scroll.x) + "px";
	  depart.image.style.top =( e.clientY-25+scroll.y) + "px";
	  depart.zone = zoneEnCours;
	  depart.image.onmousemove= function (e) {
		  zoom(e);
	  };
	  depart.image.onmouseover= function (e) {
		  this.className = "markeron";
		  showZoom(e);
	  };	
	  depart.image.onmouseout= function (e) {
		  this.className = "marker";
		 
	  };
	  depart.image.onclick= function (e) {
		  createMarker(e);   
		 
	  };	
	  
	  
  } else if (!arrivee.image) {
	  arrivee.image = new Image();
	  arrivee.image.src="images/amarker.png";
	  document.body.appendChild(arrivee.image);
	  arrivee.image.style.position = "absolute";
	  arrivee.image.style.left =( e.clientX-9+scroll.x) + "px";
	  arrivee.image.style.top =( e.clientY-25+scroll.y) + "px";
	  arrivee.zone = zoneEnCours;
	  
	  arrivee.image.onmouseover= function (e) {
		  this.className = "markeron";
		  showZoom(e);
	  };	
	  arrivee.image.onmouseout= function (e) {
		  this.className = "marker";
		 
	  };	
	  
	  $("formulaire").style.display = "";
	  if (depart.zone.id==arrivee.zone.id) bonsZone = 1;
	  else if (depart.zone.limitrophes.indexOf(arrivee.zone.id)>-1) bonsZone = 1.5;
	  else  bonsZone = 3;
	  var unite = "bon";
	  if (bonsZone>1) unite  = "bons";
	  $('bonbase').innerHTML = bonsZone +" " + unite;
	  total();
  }	  
	   
 }	 
 clearMap = function () {
	 if (depart.image) {
      document.body.removeChild(depart.image);
      depart.image = null;
	 }
	 if (arrivee.image) {
	      document.body.removeChild(arrivee.image);
	      arrivee.image = null;
		 }
	$("formulaire").style.display = "none"; 
	$("echellepoids").title = "0kg";
	$('divcurs').style.left = "";
	poids = 0;
	$('ck1').checked=true;
	$('ck1').src="images/check.png";
	for (var i=2;i<6;i++) {
	  $('ck'+i).checked=false;
	  $('ck'+i).src="images/uncheck.png";
	}  
	
 }	 	 
/*
 function check(ck) {
  if (!ck.checked) {
	  ck.checked=true;
	  ck.src="images/check.png";
	  if (ck.id=="ck1" && $("ck2").checked) check($("ck2"));
	  else if (ck.id=="ck2" && $("ck1").checked) check($("ck1"));
  } else {
	  ck.checked=false;
	  ck.src="images/uncheck.png";
  }  
  total();	  
 }	 
 */
 function check(ck) {
	  if (!ck.checked) {
		  ck.checked=true;
		  ck.src="images/check.png";
		  if (ck.id=="ck1" && $("ck2").checked) check($("ck2"));
		  else if (ck.id=="ck2" && $("ck1").checked) check($("ck1"));
		 
	  } else {
		  ck.checked=false;
		  ck.src="images/uncheck.png";
		  if (ck.id=="ck2" && !$("ck1").checked) check($("ck1"));
		  
	  }  
	  total();	  
	 }	 
 
 var poids = 0;
 
 function placePoids(e) {
  if (window.event) e = window.event;	 
  var scroll = findScroll();
  var pos = findPos($("echellepoids"));
  $('divcurs').style.left =( e.clientX-pos.x-5+scroll.x) + "px";
  poids = Math.round(((e.clientX-pos.x-3) / 17.4)*100)/100;
  if (poids > 10) poids=10;
  $("echellepoids").title = poids + "kg";
  total();
 }

 var bonsZone = 1;
 
 function total() {
  var res = bonsZone;
	for (var i=1;i<6;i++)
	  if ($('ck'+i).checked)
		  res += parseInt($('ck'+i).getAttribute('valeur'));

  if (poids>4 && poids<=10)
	  res+= parseInt(poids/4);
  else if (poids>10)
	  res+= 3;	  
	  
  var unite = "BON";
  if (res>1) unite  = "BONS";
  $('totalbon').innerHTML = res+ " " + unite;	   
  	  
 }	 

 var dernierclick='';
 function clickZone(zone) {
	 dernierclick = zone;	 
 }	 

 function Zone(id) {
  this.id = id;
  this.coords = new Array();	  
  this.limitrophes = new Array();
  this.precoords = new Array();
  this.makeCoords = function() {
   this.coords = new Array();	  
   for (var i=0;i<this.precoords.length;i+=2)
	this.coords.push({'x':this.precoords[i], 'y':this.precoords[i+1]}); 
   	  
  }
 }	 

 var zoneA = new Zone('A');
 zoneA.precoords = new Array(52,52,60,52,66,53,69,53,71,56,74,62,78,63,77,67,91,90,92,89,94,84,102,81,120,92,122,91,123,90,126,89,129,89,134,88,138,89,142,91,146,88,148,91,152,91,152,93,151,95,150,95,149,98,151,99,150,102,153,102,154,111,153,111,152,114,150,118,149,119,149,122,150,124,152,124,153,130,158,132,161,133,161,137,156,137,154,137,153,139,149,138,148,141,148,144,147,146,145,147,140,147,139,147,137,149,135,149,133,151,130,151,129,151,129,153,129,155,122,155,122,156,121,157,119,157,118,158,111,159,109,161,107,163,104,163,103,163,103,167,101,167,101,186,100,186,98,185,92,184,87,181,86,178,85,175,73,165,72,163,65,163,61,161,61,158,65,158,66,155,68,151,74,145,74,141,71,138,73,134,74,134,76,135,78,133,79,128,81,123,83,117,86,99,81,100,81,99,85,95,83,94,81,95,81,92,78,93,77,91,60,65,64,63,64,61,57,60,56,59,67,59,65,55,54,55,52,52);
 zoneA.makeCoords();
 zoneA.limitrophes = new Array("B", "C", "D", "E");
 var zoneB = new Zone('B');
 zoneB.precoords = new Array(47,47,48,45,55,45,60,41,55,31,59,26,65,24,68,23,68,20,66,18,71,18,74,19,77,21,78,15,80,12,81,16,81,22,79,27,86,32,87,29,88,23,90,21,92,19,93,19,94,19,97,22,100,26,105,27,110,28,118,30,124,32,131,34,151,39,151,44,149,47,148,49,148,51,146,52,146,57,145,58,144,61,142,62,142,66,138,69,140,72,137,71,137,76,134,76,135,78,134,79,133,80,131,80,132,84,129,86,129,88,128,89,127,90,122,90,121,91,120,93,118,92,116,91,103,82,99,82,97,83,94,84,93,86,93,88,92,89,89,85,86,82,86,80,78,67,77,63,75,63,72,60,71,57,70,53,69,53,66,53,61,53,60,52,57,52,54,47,45,48,46,46);
 zoneB.makeCoords();
 zoneB.limitrophes = new Array("A", "C");
 var zoneC = new Zone('C');
 zoneC.precoords = new Array(128,89,129,85,132,82,132,80,134,80,134,76,137,76,138,72,140,72,138,69,141,65,142,62,144,60,147,53,149,51,148,48,151,41,153,40,154,43,157,43,158,41,162,41,163,40,164,43,165,45,165,46,176,45,177,49,178,46,180,47,182,46,183,50,186,47,190,42,195,41,201,35,203,38,204,39,206,41,206,42,209,43,211,45,211,46,214,47,236,77,239,78,239,82,241,83,245,86,246,87,241,88,240,90,242,92,242,93,248,98,248,101,247,101,247,105,247,107,245,109,250,114,249,116,246,117,238,117,235,116,231,115,228,114,222,115,222,118,219,118,219,119,217,119,217,120,213,120,200,129,199,128,198,125,195,124,185,131,175,132,175,136,178,137,177,139,176,140,175,139,168,139,165,139,165,138,163,137,161,136,162,133,158,133,158,131,156,131,155,131,154,131,152,129,151,127,152,124,152,123,150,122,150,124,148,123,148,119,150,118,152,113,155,110,154,108,153,106,152,102,150,102,151,100,151,99,149,98,149,95,151,94,153,93,152,91,148,91,147,89,145,88,144,88,143,90,142,91,140,90,138,89,136,88,131,88,129,89,128,89);
 zoneC.makeCoords();
 zoneC.limitrophes = new Array("B", "A", "D");
 var zoneD = new Zone('D');
 zoneD.precoords = new Array(125,155,129,155,129,153,128,153,129,151,133,151,134,149,138,149,139,147,145,147,147,146,148,144,148,140,149,138,153,139,155,137,160,137,162,137,166,139,172,139,175,139,177,139,175,136,175,133,176,131,185,131,195,124,197,124,200,130,214,121,217,120,219,120,220,119,222,118,222,114,230,114,231,115,235,116,240,118,245,118,249,117,250,120,250,121,247,124,248,126,248,128,245,129,245,133,244,134,244,137,241,142,246,144,246,145,243,145,244,148,250,151,250,155,248,156,248,165,244,167,242,177,232,186,231,189,236,200,233,199,224,199,223,198,222,200,221,201,217,202,209,201,208,203,205,203,201,201,196,200,193,198,190,199,189,198,184,197,184,195,183,189,184,188,181,186,171,183,160,183,158,180,159,179,160,176,161,176,162,174,161,172,159,173,158,169,156,168,153,169,152,171,150,172,149,172,144,169,141,170,140,171,140,169,139,168,139,165,125,156);
 zoneD.makeCoords();
 zoneD.limitrophes = new Array("A", "C", "E");
 var zoneE = new Zone('E');
 zoneE.precoords = new Array(95,195,95,193,96,190,99,190,98,187,94,184,98,184,101,187,101,168,103,167,103,165,103,163,107,163,111,159,117,158,119,158,120,157,122,157,122,155,124,155,139,166,139,168,140,170,143,170,147,171,150,173,152,170,155,169,156,168,159,173,161,173,161,174,159,176,158,180,159,183,162,183,171,183,174,184,178,185,183,187,184,188,183,190,183,191,183,194,183,196,186,197,189,197,191,199,194,198,197,201,201,201,204,203,208,203,210,201,217,202,216,206,215,208,213,209,208,210,206,210,201,210,200,211,197,211,197,212,195,212,192,217,184,216,182,217,181,219,179,219,179,220,174,221,175,222,172,223,171,226,166,226,165,228,162,229,162,231,162,232,161,235,160,237,160,237,156,235,153,234,151,232,150,234,149,235,147,233,140,229,138,230,137,231,134,231,129,237,123,231,120,234,114,234,112,237,112,238,111,239,106,232,105,231,104,230,104,226,104,218,100,218,103,215,105,214,101,213,103,210,105,210,106,208,106,203,102,203,101,202,100,201,102,198,99,198,99,195,95,195);
 zoneE.makeCoords();
 zoneE.limitrophes = new Array("A", "D");


 var zones = new Array(zoneA,zoneB,zoneC,zoneD,zoneE);

 function zoneOfPoint(p) {
  for (var i=0;i<zones.length;i++)
	  if (pointInMap(p,zones[i].coords))
		  return zones[i];
  return null;
 }
 
 function isLeft(P0,P1,P2)
 {
	    return ( (P1.x - P0.x) * (P2.y - P0.y)
	            - (P2.x - P0.x) * (P1.y - P0.y) );
 }
	
 function pointInMap(P,V)
 {
     var wn = 0;    

     
     for (var i=0; i<V.length; i++) {  
         var i2 = i+1; 
         if (i2==V.length) i2 = 0;
         if (V[i].y <= P.y) {         
             if (V[i2].y > P.y)      
                 if (isLeft( V[i], V[i2], P) > 0)  
                     wn++;            
         }
         else {                       
             if (V[i2].y <= P.y)     
                 if (isLeft( V[i], V[i2], P) < 0)  
                     wn--;            
         }
     }
     return (wn!=0);
 }
 
 lastIMG.push({'src':'images/cartegrande.png','id' : 'imgZoom'});
 lastIMG.push({'src':'images/echellepoids.png','id' : 'echellepoids'});
 lastIMG.push({'src':'images/curseurpoids.png','id' : 'curseurpoids'});
