/*
  ===============================
  Do not remove this header.
  Place this script near the top of your page.
  ===============================
  Image Magnifier code by  
  ShipScript copyright 2006 ISDN*tek
  http://www.isdntek.com/etools.htm
  This code has been tested in 
  Internet Explorer 6, FireFox 1, 
  Netscape 7, and eBay

  The original code was made available under the condition that the
  code not be altered and the above credit remains in place.

  Modifications were made by russell@greatculturalrenaissance.com with
  the permission of isdntek, and *not* tested extensively. This code
  is made available under the same conditions original zip*zoom from
  isdntek.

  There is no support offered for this code, either through myself or
  isdntek.
*/
/*
  default magnification viewer is 50% of the image size. This can be
  changes by giving the image an attribute "tileSize='.XX'"
*/
var magviewersize = .5;
/*
  ===============================
  the following javascript code 
  must remain as one long sentence 
  to function correctly. Place this 
  code before your images, 
  near the top of your page.
  ===============================

  RWY: Made several changes to modify the behavior and add
  functionality. Some are general, some are specific to my site.
  Changes include: automatic startup (no click on anchor needed),
  change or disable magnification, magnify remains visible unless
  turned off, controllable magnify tile size.

  To use: Give the photo to be magnified the class "magnify_this".
  That's it!

  Well, a little more would help. You should set the size of the
  image. You may set an ID, unlike in the original script. There are
  also options possible:

  tileSize=".XX" : sets the magnify tile size to .XX of the image.
	  Default is .5
  promptBefore=Yes : Puts the controls before the image rather than after.

  The sizes for magnification are set in the function "offerChoices"
  at the bottom. I thought of making them settable, but these ones (1,
  2, 4, 8) work for me. 

  Also, the image does not need to be contained in a DIV, any html in
  its container will not be touched. However, to make my page look
  better the following HTML is included in the same DIV as the image.
  This might not be desirable if there is a lot of code. 
  Solutions:
  1)  put a DIV around the image and a little HTML (I put a caption in the DIV)
  2) Change the code in initMagnifier to put the variable POST  outside the 
  	</DIV> label
*/
var quot="'"; 
var Magnifico=new Array(); 
var magnify=false; 
var magStartX=0; 
var magStartY=0; 

function initMagnifier(){ 
  var magViewSize = magviewersize;
  for (im=0; im<document.images.length; im++){ 
	if (document.images[im].getAttribute("magtile"))
	  return;				// page already done
	if (document.images[im].className.indexOf("magnify_this") < 0) 
	  continue;				// not a magnified image
	var nextImage=document.images[im]; 
	var setID = "";
	var id = nextImage.getAttribute("id");
	if (!id) {
	  id = 'bgbaseimg'+im;
	  setID = " id='" + id + "'";
	}
	magViewSize = nextImage.getAttribute("tileSize");
	if (!magViewSize) magViewSize = magviewersize;
	var start = nextImage.parentNode.innerHTML.search(/<img/i);
	var end = nextImage.parentNode.innerHTML.indexOf(">", start);
	var pre = nextImage.parentNode.innerHTML.substring(0, start);
	var thisImageCode = nextImage.parentNode.innerHTML.substring(start, end) + setID + ' galleryimg="false" oncontextmenu="return false;" magtile="Magtile' + im + '">'; 
	var post = nextImage.parentNode.innerHTML.substring(end + 1);
	var precontrol = "";
	var postcontrol = "";
	if (nextImage.getAttribute("promptBefore")) precontrol = offerChoices(im);
	else postcontrol = offerChoices(im);
	if (nextImage.style.cssFloat)
	  var alignment=nextImage.style.cssFloat;
	else if (nextImage.align) 
	  var alignment=nextImage.align;
	else var alignment='none';
	nextImage.parentNode.innerHTML = a = pre +  '<div id="placeholder'+im+'" magtile="Magtile' + im + '" style="position:relative; left:0px; top:0px; float:'+alignment+';'+ ' width:'+nextImage.offsetWidth+'px;"'+ ' onclick="return true">'+ precontrol + '<div id="imgholder'+im+'" style="position:relative; left:0px; top:0px; border:0px solid blue; overflow:hidden" onclick="return true">'+ thisImageCode + '<div id=Magnifier'+im+' style="position:absolute; left:0px; top:0px; '+ ' width:'+(nextImage.offsetWidth*magViewSize)+'px; height:'+(nextImage.offsetHeight*magViewSize)+'px; '+ ' border:1px solid gray; visibility:hidden; overflow:hidden; '+ ' background-color:gray; " title="Move magnifier to see details"'+ ' ondrag="return false" '+ ' onmousedown="showMagnifier('+im+',event.clientX,event.clientY); return false" '+ ' onmousemove="if (magnify==true) {moveMagnifier('+im+',event.clientX,event.clientY)}" '+ ' onmouseup ="magnify=false; return false" '+ '><img id="Magtile'+im+'" src="'+nextImage.src+'" style="position:absolute; '+ ' left:0px; top:0px; width:'+nextImage.offsetWidth+'px; border:0px;" '+ ' onmouseover="this.galleryimg='+quot+'no'+quot+'" oncontextmenu="return false;" origWidth="' + nextImage.offsetWidth + '">'+ '</div></div>' + postcontrol + post + '</div>';
	Magnifico[im]=document.getElementById('Magnifier'+im); 
	document.getElementById(id).style.cssFloat='none'; 
	Magnifico[im].style.left=((document.getElementById('imgholder'+im).offsetWidth/2)-(Magnifico[im].offsetWidth/2))+'px';
	Magnifico[im].style.top=((document.getElementById('imgholder'+im).offsetHeight/2)-(Magnifico[im].offsetHeight/2))+'px';
	setMagtile(im); 
  }
}

function setMagnifier(idm){
  Magnifico[idm].style.visibility='visible'; 
  Magnifico[idm].style.cursor = 'move'; 
} 

function setMagtile(idm){ 
  MagImg=document.getElementById('Magtile'+idm);
  xx=(MagImg.offsetWidth-Magnifico[idm].offsetWidth)/ (document.getElementById('imgholder'+idm).offsetWidth-Magnifico[idm].offsetWidth);
  yy=(MagImg.offsetHeight-Magnifico[idm].offsetHeight)/(document.getElementById('imgholder'+idm).offsetHeight-Magnifico[idm].offsetHeight);
  //alert(MagImg.offsetHeight + ", " + Magnifico[idm].offsetHeight + " : " + document.getElementById('imgholder'+idm).offsetHeight + ", " + Magnifico[idm].offsetHeight + " : " + xx + ", " + yy);
  MagImg.style.left=(0-(parseInt(Magnifico[idm].style.left))*xx)+'px' ;
  MagImg.style.top=(0-(parseInt(Magnifico[idm].style.top))*yy)+'px' ; 
}

function showMagnifier(idm,X,Y){ magnify=true; magStartX=X; magStartY=Y; } 

function moveMagnifier(idm,X,Y){ 
  Magnifico[idm].style.left=(parseInt(Magnifico[idm].style.left)+X-magStartX)+'px'; 
  Magnifico[idm].style.top =(parseInt(Magnifico[idm].style.top) +Y-magStartY)+'px'; 
  magStartX=X; 
  magStartY=Y; 
  if (Magnifico[idm].offsetLeft<=0 ){
	magStartX=magStartX-Magnifico[idm].offsetLeft; 
	Magnifico[idm].style.left='0px';
  } 
  if (Magnifico[idm].offsetTop<=0 ) {
	magStartY=magStartY-Magnifico[idm].offsetTop; 
	Magnifico[idm].style.top='0px';
  } 
  var rtlimit=document.getElementById('imgholder'+idm).offsetWidth-Magnifico[idm].offsetWidth; 
  var btlimit=document.getElementById('imgholder'+idm).offsetHeight-Magnifico[idm].offsetHeight; 
  if (Magnifico[idm].offsetLeft>=rtlimit ) {
	magStartX=magStartX-(Magnifico[idm].offsetLeft-rtlimit);
	Magnifico[idm].style.left=rtlimit+'px';
  }
  if (Magnifico[idm].offsetTop>=btlimit ) {
	magStartY=magStartY-(Magnifico[idm].offsetTop-btlimit);
	Magnifico[idm].style.top=btlimit+'px';
  }
  setMagtile(idm); 
}

function hideMagnifier(idm){ 
  Magnifico[idm].style.visibility="hidden"; 
  Magnifico[idm].style.cursor = 'default'; 
  magnify=false; 
} 

// Following are my additions
function changeMag(mag, num) {
  if (!mag) 
	hideMagnifier(num);
  else {
	setMagnifier(num);
	//  var width = /[0-9]+/.exec(Magnifico[num].parent.parent.style.width);
	var image = Magnifico[num].firstChild;
	var width = image.getAttribute("origWidth");
	Magnifico[num].innerHTML = Magnifico[num].innerHTML.replace(/width: *[0-9]+px/i, "WIDTH:" + width*mag + "px");
	// This puts the magnifier back in the same place
	var idm = Magnifico[num].getAttribute("id").substr(9);
	moveMagnifier(idm, magStartX, magStartY);
  }
  return false;
}
function doerror() {
  /asdasd/.asdad();
}

// This replaces a prompt like "Click here for magnification" with
// 3 radioboxes for changing magnification. It is designed to be called 
// automatically from the initialization code, and to work for multiple
// images per page.
// ID is an ID of an element containing (only) the text to be replaced.
// PHOTO is an ID of the image to be magnified
function offerChoices(num) {
  return 'Magnification: off<input type="radio" checked  name="magnify' + num + '" onclick="javascript:changeMag(0, ' + num + ')">low<input type="radio" name="magnify' + num + '" onclick="javascript:changeMag(2, ' + num + ')"> medium<input type="radio" name="magnify' + num + '" onclick="javascript:changeMag(4, ' + num + ')"> high<input type="radio" name="magnify' + num + '" onclick="javascript:changeMag(8, ' + num + ')">';
}

