waiting_img_url = "/t/waiting.gif";

function getAbsolutePosition(element) {
 var r = { x: element.offsetLeft, y: element.offsetTop };
 if (element.offsetParent) { var tmp = getAbsolutePosition(element.offsetParent); r.x += tmp.x; r.y += tmp.y; }
 return r;
};
  
function getRelativeCoordinates(event, reference) {
 var x, y;
 event = event || window.event;
 var el = event.target || event.srcElement;
 if (!window.opera && typeof event.offsetX != 'undefined') {
  var pos = { x: event.offsetX, y: event.offsetY };
  var e = el;
  while (e) { e.mouseX = pos.x; e.mouseY = pos.y; pos.x += e.offsetLeft; pos.y += e.offsetTop; e = e.offsetParent; }
  var e = reference;
  var offset = { x: 0, y: 0 }
  while (e) { 
    if (typeof e.mouseX != 'undefined') { x = e.mouseX - offset.x; y = e.mouseY - offset.y; break; }
    offset.x += e.offsetLeft;
    offset.y += e.offsetTop;
    e = e.offsetParent;
  }
  e = el;
  while (e) { e.mouseX = undefined; e.mouseY = undefined; e = e.offsetParent; }
 }
 else {  var pos = getAbsolutePosition(reference); x = event.pageX  - pos.x; y = event.pageY - pos.y; }
 return { x: x, y: y };
}

function getElementsBySelector(selector){
 var i,selid="",selclass="",tag=selector,f,s=[],objlist=[];
 if(selector.indexOf(" ")>0){ s=selector.split(" ");
	var fs=s[0].split("#");
	if(fs.length==1) return(objlist);
	f=document.getElementById(fs[1]);
	if(f) return(f.getElementsByTagName(s[1]));
	return(objlist);
 }
 if(selector.indexOf("#")>0){  s=selector.split("#"); tag=s[0]; selid=s[1]; }
 if(selid!=""){ f=document.getElementById(selid); if(f) objlist.push(f); return(objlist); }
 if(selector.indexOf(".")>0){ s=selector.split("."); tag=s[0]; selclass=s[1]; }
 var v=document.getElementsByTagName(tag); 
 if(selclass=="") return(v);
 for(i=0;i<v.length;i++){ if(v[i].className.indexOf(selclass)>=0) objlist.push(v[i]); }
 return(objlist);
}

var list_zoom;

function _DigiZoom_()
{
 this.init = function () 
 {
  list_zoom = getElementsBySelector("a.DigiZoom");
  if (list_zoom)
  	{
  	 for (i=0; i<list_zoom.length; i++)
  	 	{
		// récupération des information
  	 	 infos_sp = list_zoom[i].rel.split(";");
  	 	 for(i_sp=0; i_sp<infos_sp.length;i_sp++)
  	 	 	{
  	 	 	 subinfos = infos_sp[i_sp].split(":");
  	 	 	 switch (subinfos[0])
  	 	 	 	{
  	 	 	 	 case "box-height" : list_zoom[i].box_height = parseInt(subinfos[1]); break;
  	 	 	 	 case "box-width" : list_zoom[i].box_width = parseInt(subinfos[1]); break;
				}
			}

		// fixation de l'espace utile du selecteur
  	 	 picture = list_zoom[i].getElementsByTagName("img");
  	 	 if (picture)
  	 	 	{
  	 	 	 list_zoom[i].imgheight = list_zoom[i].style.height=parseInt(picture[0].height);
  		 	 list_zoom[i].imgwidth = list_zoom[i].style.width=parseInt(picture[0].width);
			}
		 // init de la zone du zoom
		 list_zoom[i].idimage = i;
		 list_zoom[i].zoomdiv = document.getElementById(list_zoom[i].id+"-zoom");
		 if (!list_zoom[i].zoomheight) list_zoom[i].zoomheight = parseInt(list_zoom[i].zoomdiv.style.height);
		 if (!list_zoom[i].zoomheight) list_zoom[i].zoomheight = 200;
		 list_zoom[i].zoomdiv.style.height = list_zoom[i].zoomheight;
		 if (!list_zoom[i].zoomwidth) list_zoom[i].zoomwidth = parseInt(list_zoom[i].zoomdiv.style.width);
		 if (!list_zoom[i].zoomwidth) list_zoom[i].zoomwidth = 200;
		 list_zoom[i].zoomdiv.className = "DigiZoom_zoom";
		 list_zoom[i].zoomdiv.style.width = list_zoom[i].zoomwidth;
 		 list_zoom[i].zoomimg = document.createElement("IMG");
		 list_zoom[i].zoomimg.src = list_zoom[i].href;
		 list_zoom[i].zoomdiv.appendChild(list_zoom[i].zoomimg);
		 list_zoom[i].zoomimg.idimage = list_zoom[i].idimage;
		 list_zoom[i].loaded = 0;
			
		// définition du cadre de sélection au dessus de l'image normale
		 list_zoom[i].selector = document.createElement("DIV");
		 list_zoom[i].selector.className = "DigiZoom_select";
		 list_zoom[i].selector.id='digizoom-selector-'+i;
		 list_zoom[i].selector.style.width = list_zoom[i].imgwidth;
		 list_zoom[i].selector.style.height = list_zoom[i].imgheight;
		 list_zoom[i].selector.zIndex=5;
  	 	 list_zoom[i].appendChild(list_zoom[i].selector);

 		 list_zoom[i].selector.waitimg = document.createElement("IMG");
		 list_zoom[i].selector.waitimg.src = waiting_img_url;
		 list_zoom[i].selector.waitimg.className = "DigiZoom_waiting";
		 list_zoom[i].selector.appendChild(list_zoom[i].selector.waitimg);
 	 	 list_zoom[i].selector.style.visibility="visible";

 		 list_zoom[i].zoomimg.onload = function()
		 	{
 	 	 	 var id = this.idimage;
   	 	 	 list_zoom[id].selector.style.visibility="hidden";
			 list_zoom[id].totalheight = parseInt(list_zoom[id].zoomimg.height);
			 list_zoom[id].totalwidth = parseInt(list_zoom[id].zoomimg.width);

			 if (list_zoom[id].totalheight<list_zoom[id].zoomheight) 
			 	 list_zoom[id].zoomheight = list_zoom[id].zoomdiv.style.height = list_zoom[id].totalheight;
			 if (list_zoom[id].totalwidth<list_zoom[id].zoomwidth) 
			 	list_zoom[id].zoomwidth = list_zoom[id].zoomdiv.style.width = list_zoom[id].totalwidth;

			 if (!list_zoom[id].box_width) list_zoom[id].box_width = Math.round(list_zoom[id].zoomwidth* list_zoom[id].imgwidth/list_zoom[id].totalwidth);
			 if (!list_zoom[id].box_height) list_zoom[id].box_height = Math.round(list_zoom[id].zoomheight*list_zoom[id].imgheight/list_zoom[id].totalheight);
			 if (list_zoom[id].box_width) list_zoom[id].selector.style.width = list_zoom[id].box_width+"px";
			 if (list_zoom[id].box_height) list_zoom[id].selector.style.height = list_zoom[id].box_height+"px";
			 list_zoom[id].selector.removeChild(list_zoom[id].selector.firstChild);
   	 	 	 list_zoom[id].loaded = 1;
			}
		 list_zoom[i].zoomimg.onload.idimage = list_zoom[i].idimage;

  	 	 
  	 	 list_zoom[i].onmouseover = function ()
  	 	 	{
 	 	 	 var id = this.idimage;
  	 	 	 list_zoom[id].selector.style.visibility="visible";
 	 		 list_zoom[id].zoomdiv.style.visibility="visible";
	 		 list_zoom[id].zoomdiv.style.display="block";
 	 		 if (list_zoom[id].loaded)
 	 		 	{
  	 	 		 this.onmousemove = function(event) 
  	 	 	 		{
  	 	 	 		 var e = event || window.event;
  	 	 	 		 var id = this.idimage;
  	 	 	 		 pos = getRelativeCoordinates(event, list_zoom[id]);
  	 	 	 		 
  	 	 	 		 // déplacement du selecteur
  	 	 	 		 new_pos_x = pos.x - Math.round(list_zoom[this.idimage].box_width / 2);
  	 	 	 		 new_pos_y = pos.y - Math.round(list_zoom[id].box_height / 2);
  	 	 	 		 if ( (new_pos_x + list_zoom[id].box_width) >= list_zoom[id].imgwidth)
  	 	 	 	 		{
  	 	 	 	 		 new_pos_x = list_zoom[id].imgwidth-list_zoom[id].box_width;
						}
  	 	 	 		 if ( (new_pos_y + list_zoom[id].box_height) >= list_zoom[id].imgheight)
  	 	 	 	 		{
  	 	 	 	 		 new_pos_y = list_zoom[id].imgheight-list_zoom[id].box_height;
						}
  	 	 	 		 new_pos_x = (new_pos_x<0)?0:new_pos_x;
  	 	 	 		 new_pos_y = (new_pos_y<0)?0:new_pos_y;

 	 	 		     list_zoom[id].selector.style.left = new_pos_x +'px';
				     list_zoom[id].selector.style.top = new_pos_y +'px';
				     
				     // déplacement du zoom
				     zoom_pos_x = (new_pos_x/list_zoom[id].imgwidth) * list_zoom[id].totalwidth;
				     zoom_pos_y = (new_pos_y/list_zoom[id].imgheight) * list_zoom[id].totalheight;
				     if (zoom_pos_x+list_zoom[id].zoomwidth > list_zoom[id].totalwidth)
			     		{
			     		 zoom_pos_x = list_zoom[id].totalwidth-list_zoom[id].zoomwidth;
						}
				     if (zoom_pos_y+list_zoom[id].zoomheight > list_zoom[id].totalheight)
			     		{
			     		 zoom_pos_y = list_zoom[id].totalheight-list_zoom[id].zoomheight;
						}
				     list_zoom[id].zoomdiv.scrollLeft = zoom_pos_x;
				     list_zoom[id].zoomdiv.scrollTop = zoom_pos_y;
					}
				}
  	 	 	}

  	 	 list_zoom[i].onmouseout = function ()
  	 	 	{
 	 	 	 var id = this.idimage;
 	 		 if (list_zoom[id].loaded)
 	 		 	{
	  	 	 	 list_zoom[id].selector.style.visibility="hidden";
 		 		 list_zoom[id].zoomdiv.style.visibility="hidden";
 		 		 list_zoom[id].zoomdiv.style.display="none";

	  	 	 	 this.onmousemove = function() 
  		 	 	 	{
  	 		 	 	 return false;
					}
				}
			}
		 list_zoom[i].onclick = function ()
		 	{
			 return false;
			}
		}
	}
 }
}


DigiZoom = new _DigiZoom_();
onload = DigiZoom.init;