function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function xWindow(name, w, h, x, y, loc, men, res, scr, sta, too)
{
  var e='',c=',',xf='left=',yf='top='; this.n = name;
  if (document.layers) {xf='screenX='; yf='screenY=';}
  this.f = (w?'width='+w+c:e)+(h?'height='+h+c:e)+(x>=0?xf+x+c:e)+
    (y>=0?yf+y+c:e)+'location='+loc+',menubar='+men+',resizable='+res+
    ',scrollbars='+scr+',status='+sta+',toolbar='+too;
  this.opened = function() {return this.w && !this.w.closed;};
  this.close = function() {if(this.opened()) this.w.close();};
  this.focus = function() {if(this.opened()) this.w.focus();};
  this.load = function(sUrl) {
    if (this.opened()) this.w.location.href = sUrl;
    else this.w = window.open(sUrl,this.n,this.f);
    this.focus();
    return false;
  };
}


Events = {
  add: function(o, type, fn) {
    if (!(o = $(o))) return;
    if (o.addEventListener) o.addEventListener(type, fn, false);
    else if (o.attachEvent) {
      var id = o.eventCacheID;
      if (!id) this.cache[id = o.eventCacheID = this.cache.length] = o;
      if (!fn.eventHandlers) fn.eventHandlers = [];
      var handler = fn.eventHandlers[id];
      if (handler) o.detachEvent('on'+ type, handler);
      else handler = fn.eventHandlers[id] = function(e) {
        return fn.call(Events.cache[id], Events.translate(e, Events.cache[id]));
      }
      o.attachEvent('on'+ type, handler);o = null;
      this.clearCache();
    }
  },
  remove: function(o, type, fn) {
    if (!(o = $(o))) return;
    if (o.removeEventListener) o.removeEventListener(type, fn, false);
    else if (o.detachEvent) {
      var handler = fn, id = o.eventCacheID;
      if (fn.eventHandlers) handler = fn.eventHandlers[id] || fn;
      o.detachEvent('on'+ type, handler);o = null;
      this.clearCache();
    }
  },
  cache: [],
  clearCache: function() {
    for (var i = 0; i < this.cache.length; i++) {
      var o = this.cache[i];
      if (o && /^[13]$/.test(o.nodeType) && !o.parentNode) 
        delete this.cache[i];
    }
  },
  translate: function(e, currentTarget) {
    var getView = function(o) {
      if (o.ownerDocument) return o.ownerDocument.parentWindow;
      return o.parentNode ? gWin(o.parentNode): o.parentWindow;
    }
    return extendIf({}, (e||window.event), {
      target: e.srcElement,
      currentTarget: currentTarget||e.srcElement,
      preventDefault: function() {window.event.returnValue = false;},
      stopPropagation: function() {window.event.cancelBubble = true;},
      timeStamp: (new Date).valueOf(),
      view: getView(e.srcElement||window),
      relatedTarget: 
        /mouseover/i.test(e.type)?e.fromElement:
        /mouseout/i.test(e.type)?e.toElement:null
    });
  }
}

//--[ common ]--------------------------------------//
// simplified for distribution here..
function $(id) {
  return typeof id == 'string' ? document.getElementById(id) : id;
}
function extendIf(o) {
  var a = arguments;
  if (o) for (var i = 1; i < a.length; i++)
    if (a[i]) for (var p in a[i])
        if (!o[p]) o[p] = a[i][p];
  return o;
}

function attachImageDynamics(){
	
	//get the thumbnail ul object
	var oTableThumbs = document.getElementById('thumbs');
	
	//now, get the array of all of the image objects
	var aThumbImages = oTableThumbs.getElementsByTagName('img');
		
	//now, loop through each of the images, attaching actions for the 
	//dynamic behavior for each
	for(var imgIdx = 0; imgIdx < aThumbImages.length; ++imgIdx){
		
		//put the image source in a variable
		var sOrignalImageSource = aThumbImages[imgIdx].src.replace('/thumbs/','/originals/');
		var sImageTitle			= aThumbImages[imgIdx].alt;

		//attach a linked image class to the image
		aThumbImages[imgIdx].className 		= "linkedImage";
		aThumbImages[imgIdx].originalImage 	= sOrignalImageSource;
		aThumbImages[imgIdx].imageTitle 	= sImageTitle;
		
		//if we are at the beginning of the image array
		if(imgIdx == 0){
			aThumbImages[imgIdx].nextImgNode = aThumbImages[imgIdx + 1];
			aThumbImages[imgIdx].prevImgNode = null;
		//if we are at the end of the image array
		}else if((imgIdx + 1) == aThumbImages.length){
			aThumbImages[imgIdx].nextImgNode = null;
			aThumbImages[imgIdx].prevImgNode = aThumbImages[imgIdx - 1];	
		//otherwise, we are in the middle
		}else{
			aThumbImages[imgIdx].nextImgNode = aThumbImages[imgIdx + 1];
			aThumbImages[imgIdx].prevImgNode = aThumbImages[imgIdx - 1];			
		}		
		
		//attach a click event to the image
		Events.add(aThumbImages[imgIdx], 'click', thumbnailClicked);
	}

}

function thumbnailClicked(e){
	//find the image node that was calling this onclick
	var node = (e) ? e.target : window.event.srcElement;
	
	//create the popup url, it is just the url of THIS page, replacing _thumbs with _single
	var popupUrl = document.location.href.replace('_thumbs','_single');
	
	if(popUpWin){
		popUpWin.close();
	}
	
	//call the popup function, passing the url for this artist
	popUpWin = new xWindow(
	  'popupWindow',          	// target name
	  650,           			// width
	  750,      				// height - m is a 'fudge-factor' ;-)
	  screen.width/2 - 325, 50,                  	// position: left, top
	  0,                      // location field
	  0,                      // menubar
	  1,                      // resizable
	  0,                      // scrollbars
	  0,                      // statusbar
	  0);                     // toolbar

	//load the url in the popup
	popUpWin.load(popupUrl);

	//add the dynamics to the popup window to allow for image navigation
	createPopupNavigator(popUpWin, node);
}
var currentImageNode = null;


function createPopupNavigator(windowHandle, imgNode){
	var winObj = windowHandle.w;
	//alert(winObj);
	
	currentImageNode = imgNode;
	
	var popupDoc = winObj.document;
		
	//Events.add(winObj, "load", function(){
		//alert('chaddnging image');		
		//changeImage(winObj, imgNode);
	//});
}


function changeImage(winObj,imgNode){

		//get the main image
		var imgContainer = winObj.document.getElementById('image-container');
		
		//clear out the old image
		imgContainer.innerHTML = '';
		
		//create a new image
		var newImg 	= winObj.document.createElement('img');
		newImg.src 	= imgNode.originalImage;
		newImg.alt	= imgNode.imageTitle;
		newImg.title = imgNode.imageTitle;
		
		//create a new header for the image
		var imgHeader = winObj.document.createElement('h1');
		imgHeader.innerHTML = imgNode.imageTitle;
		imgHeader.className = "image-title";
		
		//put the image header in the image container
		imgContainer.appendChild(imgHeader);
		
		//put the new image in the image container
		imgContainer.appendChild(newImg);
		
		//now fix the image navigation links so that they reflect the right images
		var prevImageLink = winObj.document.getElementById('previous-image');
		var nextImageLink = winObj.document.getElementById('next-image');
		prevImageLink.href = "#";
		nextImageLink.href = "#";

		if(imgNode.prevImgNode != null){
			prevImageLink.onclick = function(){changeImage(winObj, imgNode.prevImgNode);};
			prevImageLink.style.visibility = 'visible';
		}else{
			prevImageLink.style.visibility = 'hidden';
		}

		if(imgNode.nextImgNode != null){
			nextImageLink.onclick = function(){changeImage(winObj, imgNode.nextImgNode);};
			nextImageLink.style.visibility = 'visible';
		}else{
			nextImageLink.style.visibility = 'hidden';
		}
}


function preLoadImages(){
	MM_preloadImages('/images/nav/02about_over.jpg','/images/nav/04film_over.jpg','/images/nav/05contact_over.jpg','/images/nav/03events_over.jpg','/images/gallery/prev_over.jpg','/images/gallery/bio_over.jpg','/images/gallery/next_over.jpg','/images/nav/01gallery_over.jpg','/images/nav/06home_over.jpg');
}



var popUpWin = null;

Events.add(window,'load',attachImageDynamics);
Events.add(window,'load',preLoadImages);
Events.add(window,'unload',function(){popUpWin.close()});

