// draggable popup functions
isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

function ddInit(e){
  topElement=isIE ? "BODY" : "HTML";
  whichElement=isIE ? document.all.theLayer : document.getElementById("theLayer");  
  hotElement=isIE ? event.srcElement : e.target;  
  while (hotElement.id!="popup"&&hotElement.tagName!=topElement){
    hotElement=isIE ? hotElement.parentElement : hotElement.parentNode;
  }  
  if (hotElement.id=="popup"){
    offsetx=isIE ? event.clientX : e.clientX;
    offsety=isIE ? event.clientY : e.clientY;
    nowX=parseInt(whichElement.style.left);
    nowY=parseInt(whichElement.style.top);
    ddEnabled=true;
    document.onmousemove=dd;
  }
  else {
  	hideMe();
  }
  return false;
}

function dd(e){
  if (!ddEnabled) return;
  whichElement.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; 
  whichElement.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
  return false;  
}

function ddN4(whatElement){
  if (!isN4) return;
  N4=eval(whatElement);
  N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  N4.onmousedown=function(e){
    N4.captureEvents(Event.MOUSEMOVE);
    N4x=e.x;
    N4y=e.y;
  }
  N4.onmousemove=function(e){
    if (isHot){
      N4.moveBy(e.x-N4x,e.y-N4y);
      return false;
    }
  }
  N4.onmouseup=function(){
    N4.releaseEvents(Event.MOUSEMOVE);
  }
}

function hideMe(){
  if (isIE||isNN) whichElement.style.visibility="hidden";
  else if (isN4) document.theLayer.visibility="hide";
}

function showMe(){
	var imageSource;
	var imageText;
	imageSource = this.src.replace("_sml", "");
	imageText = '<div align="left" style="float:left;cursor:move;">  <a href="#" onClick="hideMe();return false"><font color=#ffffff size=2 face=arial  style="text-decoration:none">X</font></a> </div>'+this.alt;
	if (isIE||isNN) {
		var theImage = new Image;
		theImage.src = imageSource;
		document.getElementById('popup').innerHTML = '<img src="'+imageSource+'"><div class="caption">'+imageText+'</div>';
		whichElement.style.visibility="visible";
	}
	else if (isN4) {
		document.getElementById('popup').innerHTML = '<img src="'+imageSource+'">';
		document.theLayer.visibility="show";
	}
	return false;
}

document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");

// set popup function
function setPopups() {
	var gallery = document.getElementById('gallery');
	var images = gallery.getElementsByTagName('IMG');
	for (var i=0; i<images.length; i++) {
		if (images[i].src.indexOf("_sml") != -1) {
			images[i].onclick = showMe;
		}
	}
}

