

AttachBehaviors(fndclass(document, 'pictureBox'));

function AttachBehaviors(el)
{
	if(!el) return;
	
	/* first image will be the main image */
	var imgs = el.getElementsByTagName('IMG');
	var MainImgEl = imgs[0];
	if(MainImgEl)
	{
		MainImgEl.onclick = function() {popupImage(this)};
		/* others get swap behavior */
		for(var i=1 ; i<imgs.length ; i++)
		{
			imgs[i].onclick = function() {swapImage(this, MainImgEl)};
		}
	}
}

function popupImageUrl(el, title)
{
	imageurl = el;
	wintitle = title;
	
	imgPic = new Image();
	imgPic.src = imageurl;
	setDims();
}


function popupImage(el)
{
	var start;
	var end;
	// get start and end location for image size information
	for(i= el.src.length; i > 0; i--)
	{
		if (el.src.charAt(i)== '.'){
			end=i;
		}
		if (el.src.charAt(i)== 'W'){
			start=i;
			break;
		}
	}
	
	imageurl = el.src.replace(el.src.substring(start,end), 'W0H0');
	wintitle = el.alt;
	
	imgPic = new Image();
	imgPic.src = imageurl;
	setDims();
}

function swapImage(clickedImg, mainImg)
{
	var curMainSrc = mainImg.src;
	var curMainAlt = mainImg.alt;
	var startBig;
	var endBig;
	var startSmall;
	var endSmall;
	
	for(i= mainImg.src.length; i > 0; i--)
	{
		if (mainImg.src.charAt(i)== '.'){
			endBig=i;
		}
		if (mainImg.src.charAt(i)== 'W'){
			startBig=i;
			break;
		}
		
	}
	
	for(i= clickedImg.src.length; i > 0; i--)
	{
		if (clickedImg.src.charAt(i)== '.'){
			endSmall=i;
		}
		if (clickedImg.src.charAt(i)== 'W'){
			startSmall=i;
			break;
		}
		
	}
	
	
	
	/* replace main */
	mainImg.src = clickedImg.src.replace(/W\d+H\d+/, mainImg.src.substring(startBig,endBig));
	mainImg.alt = clickedImg.alt;
	/* replace clicked */
	clickedImg.src = curMainSrc.replace(/W\d+H\d+/, clickedImg.src.substring(startSmall,endSmall));
	clickedImg.alt = curMainAlt;
}

/*  popupImage

    This function will open a new window (minus a toolbar, menubar, and statusbar) and display the -->
    image specified by the 'imageurl' parameter. The window is automatically resized to fit the -->
    image's dimensions. The 'wintitle' parameter allows you to specify text for the window's titlebar; -->
    if this parameter is left empty, it will automatically be assigned the string value "Image Window"  -->
*/

var imgPic;
var imageurl;
var wintitle;
var imgPic2;
var imageurl2;
var wintitle2;
var idTMP2 ;

function setDims()
{
  if(!imgPic.complete)
  	setTimeout("setDims()",100);
  else {
	var winPic;
  	winPic = window.open("", "ShowImage", "width=" + screen.availWidth + ", height=" + screen.availHeight  + ", scrollbars=auto, menubar=no, status=no, toolbar=no");
	
	winPic.document.open();
	winPic.document.writeln("<html><head><title>" + wintitle + "</title></head><body>");
	winPic.document.writeln('<div style="position:absolute;width:' + imgPic.width + 'px; height:' + imgPic.height + 'px; left:0px; top:0px">');
	winPic.document.writeln("<img src=" + imageurl + "></div></body></html>");
	winPic.document.close();
	winPic.resizeTo(screen.availWidth,screen.availHeight);
	w = screen.availWidth;
	h = screen.availHeight;
	
	if(w >= imgPic.width+26)
	  w = imgPic.width+6;
	if(h >= imgPic.height+24+20)
	  h = imgPic.height+34;
	
	winPic.resizeTo(w,h);
	winPic.moveTo((screen.availWidth-w)/2,(screen.availHeight-h)/2);
	winPic.focus();
  }
}
			

