
function Kujjuk_reticule(reticule, zoomImage, zommHolder,waitingObj, zoomAnchor){
	// attributs
	this.view = reticule
	this.zoomedHolder = zommHolder
	this.zoomedHolderPos = Kujjuk_getPos(zommHolder);
	if(zoomAnchor != null)
		this.zoomAnchor = zoomAnchor;
	this.zoomedImage = zoomImage
	this.zoomedImage.style.top = 0;
	this.zoomedImage.style.left = 0;
	this.waitingItem = waitingObj;
	//methodes
	// les evennements ne sont pas enregistré ici!
	this.OnMouseDown = Kujjuk_reticule_OnMouseDown;
	this.show = Kujjuk_show;
	this.hide = Kujjuk_hide;
	this.move = Kujjuk_move;
	this.actualisePosition = kujjuk_actualise_position;
	this.actualise = kujjuk_actualise;
	this.onMouseMove = Kujjuk_MouseMouse
	
	this.view.controler = this;
	
	//permet de retrouver cet objet via l'imagen notament ds la gestion d'evennements.
	this.view.onmousedown = this.OnMouseDown;
	this.view.onmousemove = this.onMouseMove;
	this.view.onmouseout = this.onMouseMove;
	this.view.style.position = "absolute";
	this.actualisePosition();
    return this;
}
function kujjuk_actualise_position(){
	if(this.zoomAnchor == null)
		return;
		this.zoomAnchorPos = Kujjuk_getPos(this.zoomAnchor)
		this.zoomedHolder.style.position= "absolute";
		this.zoomedHolder.style.top = this.zoomAnchorPos.t+"px";
		this.zoomedHolder.style.left = this.zoomAnchorPos.l+"px";
}
function kujjuk_actualise(e,kujjuk360){
	this.actualisePosition();
	this.show(kujjuk360);
	
	var tab = e.target.src.split('/');
	
	// IE7 do not suport indexOf on string because for IE7 string are not Array  (anyway Ie7 do not support indexOf on array  ...
var searchKujjuk=new RegExp(tab[tab.length-2], "g");
	var adr = e.target.src.substring(0,e.target.src.search(searchKujjuk)) //kujjukIndexOf(e.target.src,tab[tab.length-2]));
	adr +="jpgfull/"+tab[tab.length-1];
	
			
	// checker si c est different
	if(this.zoomedImage.src != adr){
		if(this.waitingItem != null ){
			var oldImage = kujjukZoomImage;
			kujjukZoomImage = document.createElement("img");
			kujjukZoomImage.id = oldImage.id;
			oldImage.parentNode.replaceChild(kujjukZoomImage,oldImage)
			this.zoomedImage = kujjukZoomImage;
			this.zoomedImage.style.marginTop = oldImage.style.marginTop;
			this.zoomedImage.style.marginLeft = oldImage.style.marginLeft;
	
			this.waitingItem.style.visibility = "visible";
			this.waitingItem.style.display = "block";
		}
		this.zoomedImage.src = adr; 
		this.zoomedImage.onload = kujjukSetVisibleImage;
	}
	this.move(e.posX,e.posY)
	//console.log(e.target.width);
}
function kujjukSetVisibleImage(){
	
	if(kujjukSelecteur.waitingItem != null ){
			kujjukSelecteur.waitingItem.style.visibility = "hidden";
			kujjukSelecteur.waitingItem.style.display = "none";
		}
		
	kujjukSelecteur.zoomedImage .style.visibility = "visible";
	kujjukSelecteur.zoomedImage .style.display = "block";
}

function Kujjuk_reticule_OnMouseDown(e){
	
		this.controler.actualImage.OnMouseDown(e,this.controler.actualImage.kujjukImage);
		this.controler.hide();
	    onselectstart = function () { return false; };
        ondragstart = function() { return false; };
        return false;
    
}


function Kujjuk_show(kujjuk360){
	kujjukZoomVisible = true;
	
	this.view.style.cursor = kujjuk360.kujjukImage.style.cursor;
	this.actualImage = kujjuk360
	this.actualImagePos = Kujjuk_getPos(kujjuk360.kujjukImage);
	this.view.style.visibility = "visible";
	this.view.style.display = "block";
	this.zoomedImage .style.visibility = "visible";
	this.zoomedImage .style.display = "block";
	this.zoomedHolder .style.visibility = "visible";
	this.zoomedHolder .style.display = "block";
}
function Kujjuk_hide(){
	kujjukZoomVisible = false;
	this.actualImage = null;
	this.view.style.visibility = "hidden";
	this.view.style.display = "none";
	this.zoomedImage .style.visibility = "hidden";
	this.zoomedImage .style.display = "none";
	
	this.zoomedHolder .style.visibility = "hidden";
	this.zoomedHolder .style.display = "none";
}

function Kujjuk_MouseMouse(e){
	e=Kujjuk_getMousePositionInEvent(e)
	
	this.controler.move(e.posX,e.posY)
}
function Kujjuk_move(xx,yy){
	if(this.actualImage == null)
	return;
	var zoomedImgSize = Kujjuk_getPos(this.zoomedImage)
	var viewSize = Kujjuk_getPos(this.view)
	
	var relativeXX = (xx-viewSize.w/2 - this.actualImagePos.l)/this.actualImagePos.w;
	var relativeYY = (yy-viewSize.h/2 - this.actualImagePos.t)/this.actualImagePos.h;
	
	yy = yy-viewSize.h/2;
	xx = xx-viewSize.w/2;
	
	
	var testXX = (xx - this.actualImagePos.l)/this.actualImagePos.w;
	var testYY = (yy - this.actualImagePos.t)/this.actualImagePos.h;
	
	if(testXX<0 || testYY<0 || testXX>1 || testYY>1)
	return this.hide();
	
	var depX = zoomedImgSize.w 
	var depY = zoomedImgSize.h
	this.zoomedImage.style.marginLeft = -1*depX*relativeXX+this.zoomedHolderPos.w/2+"px";
	this.zoomedImage.style.marginTop = -1*depY*relativeYY+this.zoomedHolderPos.h/2+"px";
	this.view.style.top = yy+"px";
	this.view.style.left = xx+"px";
	
	
}
