var imgPic;
var imageurl;
var wintitle;
var winPic;

AttachBehaviors(fndclass(document, 'pictureBox'));

function AttachBehaviors(el)
{
	if(!el) return;
	
	/* first image will be the main image */
	var imgs = el.getElementsByTagName('DIV');
	/* filter divs keep image divs only */
	var firstImage = true;
	var MainImgEl;
	for(var i=0 ; i<imgs.length ; i++)
	{
		if(imgs[i].id == 'pictureBoxImage')
		{
			if(firstImage)
			{
				MainImgEl = imgs[i];
				imgs[i].onclick = function() {popupImage(this)};
				firstImage = false;
			}
			else
			{
				imgs[i].onclick = function() {swapImage(this, MainImgEl)};
			}
		}
	}
}

function fndclass(node, className)
{
	var els = node.getElementsByTagName('*');
	for(var i=0; i<els.length; i++)
	{
		if(els[i].className.indexOf(className) > -1) return els[i];
	}
	return null;
}

/*  swapImage
    
*/

function swapImage(clickedImg, mainImg)
{
	var curMainSrc = mainImg.style.backgroundImage;
	var curMainAlt = mainImg.alt;
	var startBig;
	var endBig;
	var startSmall;
	var endSmall;
	
	for(i= mainImg.style.backgroundImage.length; i > 0; i--)
	{
		if (mainImg.style.backgroundImage.charAt(i)== '.'){
			endBig=i;
		}
		if (mainImg.style.backgroundImage.charAt(i)== 'W'){
			startBig=i;
			break;
		}
	}
	
	for(i= clickedImg.style.backgroundImage.length; i > 0; i--)
	{
		if (clickedImg.style.backgroundImage.charAt(i)== '.'){
			endSmall=i;
		}
		if (clickedImg.style.backgroundImage.charAt(i)== 'W'){
			startSmall=i;
			break;
		}
	}
	
	/* replace main */
	mainImg.style.backgroundImage = clickedImg.style.backgroundImage.replace(/W\d+H\d+/, mainImg.style.backgroundImage.substring(startBig,endBig));
	mainImg.alt = clickedImg.alt;
	/* replace clicked */
	clickedImg.style.backgroundImage = curMainSrc.replace(/W\d+H\d+/, clickedImg.style.backgroundImage.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"  -->
*/

function popupImage(el)
{
	var start;
	var end;
	// get start and end location for image size information
	for(i= el.style.backgroundImage.length; i > 0; i--)
	{
		if (el.style.backgroundImage.charAt(i)== '.'){
			end=i;
		}
		if (el.style.backgroundImage.charAt(i)== 'W'){
			start=i;
			break;
		}
	}
	
	imageurl = el.style.backgroundImage.replace(el.style.backgroundImage.substring(start,end), 'W800H600');
	wintitle = el.alt;

	imgPic = new Image();
	imgPic.src = imageurl.substring(4,imageurl.length-1);

	winPic = window.open("", "ShowImage", "width=640, height=480, resizable=1, scrollbars=1, menubar=0, status=0, toolbar=0");
	
	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=" + imgPic.src + "></body></html>");
	winPic.document.close();

	setDims();
	
	winPic.focus();
}

function setDims()
{
  var w;
  var h;
  
  if(!imgPic.complete)
  	setTimeout("setDims()",100);
  else {
	  w = screen.availWidth;
	  h = screen.availHeight;
  		
	  if(w >= imgPic.width+26)
		  w = imgPic.width+30;
	  if(h >= imgPic.height+24+20)
		  h = imgPic.height+65;

	  winPic.resizeTo(w,h);
	  winPic.moveTo((screen.availWidth-w)/2,(screen.availHeight-h)/2);
  }
}
