/**
 * loupe.js 1.3 (27-Jul-2007)
 * (c) by Christian Effenberger 
 * All Rights Reserved
 * Source: loupe.netzgesta.de
 * Distributed under NSL
 * License permits free of charge
 * use on non-commercial and 
 * private web sites only
**/

// the path to "loupe.png" must be set before loading "loupe.js" !

if(typeof loupePath=="undefined") var loupePath = "";

var imgreso = new Image;  

function roundTo(val,dig) {
	var num = val;
	if(val > 8191 && val < 10485) {
		val = val-5000;
		num = Math.round(val*Math.pow(10,dig))/Math.pow(10,dig);
		num = num+5000;
	}else {
		num = Math.round(val*Math.pow(10,dig))/Math.pow(10,dig);
	}
	return num;
}

function LoupeMouseDown(event) {
	if (!event) event = window.event;
	document.body.canvas = this;
	this.inDrag = true;
	if(event.pageX) {
		this.startX = event.pageX;
		this.startY = event.pageY;
	}else if (event.clientX) {
		this.startX = event.clientX;
		this.startY = event.clientY;
	}else {
		return;
	}
}

function LoupeMouseUp() {
	if(this.inDrag) {
		this.inDrag = false;
		document.body.canvas = null;
	}
}

function LoupePosition() {
	var ctx = this.getContext("2d");
	var image = document.getElementById(this.iName);
	var left = Math.max(this.xMin,Math.min(this.xMax,Math.round(this.xPos-this.width/2)));
	var top = Math.max(this.yMin,Math.min(this.yMax,Math.round(this.yPos-this.height/2)));
	var xSrc = Math.round(Math.min((left-this.xMin)*this.xMulti,this.cWidth-this.size));
	var ySrc = Math.round(Math.min((top-this.yMin)*this.yMulti,this.cHeight-this.size));
	this.style.left = left + "px"; this.style.top = top + "px";
	ctx.drawImage(image,xSrc,ySrc,this.size,this.size,this.xOff,this.yOff,this.size,this.size);
	if(this.crossHair) {
		ctx.strokeStyle = 'rgba(0,0,255,0.25)'; ctx.lineWidth = 1;
		ctx.beginPath(); ctx.moveTo(this.xOff+this.radius,this.yOff);
		ctx.lineTo(this.xOff+this.radius,this.yOff+this.size); ctx.closePath(); ctx.stroke();
		ctx.beginPath(); ctx.moveTo(this.xOff,this.yOff+this.radius);
		ctx.lineTo(this.xOff+this.size,this.yOff+this.radius); ctx.closePath(); ctx.stroke();
		ctx.strokeStyle = 'rgb(255,255,255)'; ctx.lineWidth = 2;
	}
	ctx.drawImage(imgreso,0,0,this.size,this.size,this.xOff,this.yOff,this.size,this.size);
	ctx.beginPath();
	ctx.moveTo(this.xOff+this.size,this.yOff+this.radius);
	ctx.arc(this.xOff+this.radius,this.yOff+this.radius,this.radius,0,Math.PI*2, false);
	ctx.closePath();
	ctx.stroke();
}

function LoupeDrag(event) {
	if(!event) event = window.event;
	var canvas = this.canvas; 
	if(canvas && canvas.inDrag) {
		var eventX; var eventY;
		if (event.pageX) {
			eventX = event.pageX;
			eventY = event.pageY;
		}else if (event.clientX) {
			eventX = event.clientX;
			eventY = event.clientY;
		}else {
			return;
		}
		canvas.xPos += eventX-canvas.startX;
		canvas.yPos += eventY-canvas.startY;
		canvas.startX = eventX;
		canvas.startY = eventY;
		canvas.position();
	}
}

function LoupeOpacity(ids, opacStart, opacEnd, millisec) {	var speed = Math.round(millisec / 100); var timer = 0;	if(opacStart > opacEnd) {		for(var i = opacStart; i >= opacEnd; i--) {			window.setTimeout("changeLoupeOpacity(" + i + ",'" + ids + "')",(timer * speed));			timer++;		}	}else if(opacStart < opacEnd) {		for(var i = opacStart; i <= opacEnd; i++) {			window.setTimeout("changeLoupeOpacity(" + i + ",'" + ids + "')",(timer * speed));			timer++;		}	}
}
function changeLoupeOpacity(opacity, ids) {	var obj = document.getElementById(ids); 
	obj.style.opacity = (opacity / 100);}
function shiftLoupeOpacity(ids, millisec) {	if(document.getElementById(ids).style.opacity != '') {		var currentOpac = document.getElementById(ids).style.opacity * 100;
	}else {
		var currentOpac = 0;
	}
	if(currentOpac == 0) {
		toggleLoupeVisibility(ids);
		LoupeOpacity(ids, currentOpac, 100, millisec);	}else if(currentOpac > 0) {
		LoupeOpacity(ids, currentOpac, 0, millisec);
		window.setTimeout("toggleLoupeVisibility('"+ids+"')",millisec);	}}

function toggleLoupeVisibility(ids) {
	var obj = document.getElementById(ids); 
	obj.style.visibility=(obj.style.visibility=='hidden'||obj.style.visibility==''?'visible':'hidden');
}
				
function createLoupe(imgname,display,xpos,ypos,ch) {
	var size = 205; var image = document.getElementById(imgname);
	if(image) image.style.cursor = 'default';
	if(image && image.width>=size && image.height>=size)
}
