/* adler.js
 *
 * this file contains all of the jscript for the companychaddick.org site.
 *
 * note that DreamWeaver puts some jscript in the HTML files themselves.
 *
 * known bugs:
 *	1.	ie mac does not show the correct image when opening an artist page with an imageNum specified in the URL
 *	2.	win ie pops in the original image before showing the correct image when opening an artist page with an imageNum specified
 *		in the URL
 */

function setInitialImage(theElem) {
	// set the image to the right one from the URL, if applicable


	var imageElem = document.getElementById(theElem);
	if ( imageElem )
		selectImageFromURL(theElem);
		
	// HACK: safari 1.3v312 has a nasty bug which causes the navigation images to be hidden
	// behind the colorbar divs.
	// HACK: stupid safari hack: we change a property of the navigation div to force a reflow of that
	// div, which fixes a bug in safari in which the navigation images don't show up on a page refresh.
	// we choose a property that we're not actually making use of. In this case, we use the 'color'
	// property. Since there is no text in the navigation div, this won't hurt anything.
	//var elem = document.getElementById('navigation');
	//elem.style.color = 'red';

}


/* preloadImages
 * preload images based on a given image's src. the prototype is:
 *		preloadImages( elementID, image1, image2, image3, ... )
 * where each image is found in the same path as the src attribute for the image element with the given elementID
 */
function preloadImages()
{
	var args = preloadImages.arguments;
	var theElem = args[0];
	var elem = document.getElementById(theElem);
	var path = elem.src;
	path = path.replace(/[^\/]+\.(jpg|gif|png)$/,'');	// peel off the trailing filename, e.g., filename.gif or filename.jpg

	var len = args.length-1;
	var imageArray = new Array(len);
	for(var i=0; i<len; i++)
	{
		imageArray[i] = new Image;
		imageArray[i].src = path+args[i+1];
	}	
}

function preloadLocalImages()
{
	// hack: allow other images to be preloaded by setting the localPreloadImages variable.
	if ( localPreloadImages )
	{
		len = localPreloadImages.length;
		document.imageArray2 = new Array(len);
		for(var i=0; i<len; i++)
		{
			document.imageArray2[i] = new Image;
			document.imageArray2[i].src = localPreloadImages[i];
		}
	}
}


/* setImageNum
 * sets the image src for the given element to the given image number.
 */
function setImageNum(theElem, imageNum)
{
	var elem = document.getElementById(theElem);
	var path = elem.src;
	path = path.replace(/[^\/]+\.(jpg|gif|png)$/,'');	// peel off the trailing filename, e.g., filename.gif or filename.jpg

	// you would think that we could just do this:
	// 	elem.src = path+imageNum+'.jpg';
	// however, Safari has another bug in which it will keep the image dimensions from the old image
	// when the new image is loaded (under certain circumstances). the following code
	// removes the IMG element from the DOM altogether, changes it's src, and then puts it back.
	// we do this weird hack only for safari.
	
	var isSafari = navigator.appVersion.indexOf("Safari") > 0;
	if ( isSafari )
	{
		var par = elem.parentNode; //document.getElementById('artimage'); //elem.parentElement;
		par.removeChild(elem);
		elem.src = path+imageNum+'.jpg';
		par.appendChild(elem);
	}
	else
	{
		elem.src = path+imageNum+'.jpg';
	}
	
	setInfoNum(imageNum);
}

/* setInfoNum
 *	hides all of the artinfo_* divs except for the one specified.
 *
 *  bug: the element prefix ('artinfo_') is hardcoded.
 */
function setInfoNum(num)
{
	// make all info divs hidden, except the one indicated in num
	var i=1;
	while ( true )
	{
		var elem = document.getElementById('artinfo_'+i);
		if ( elem == null )
			break;
		elem.style.display = 'none';
		i++;
	}
	
	i=1;
	while ( true )
	{
		var elem = document.getElementById('artinfo_'+i);
		if ( elem == null )
			break;
		if ( i == num )
		{
			elem.style.display = 'block';
		}
		i++;
	}
}


/* selectImageFromURL
 *	pull the image selector, if any, out of the URL, and set the main image accordingly.
 *	the URL is of the format:
 *	http://blahblahblah/blah?img=1
 *
 *	'img=[0-9]+'	select image <n>, i.e., 'images/image<n>.jpg'
 */
function selectImageFromURL(theElem)
{
	var url = document.URL;
	var num = 1;
	
	var pos = url.search(/\?img=([0-9]+)/);
	if ( pos > 0 )
	{
		num = parseInt(RegExp.$1);
		setImageNum(theElem,num);	
	}

	var elem = document.getElementById(theElem);
	elem.style.visibility = 'visible';
}

/* swapImageFromThumbs
 * used for the artist pages:
 * this routine swaps the source of the main image (passed in as theElem) using
 * the given imageNum.
 */
function swapImageFromThumbs(evt, theElem, imageNum)
{
	setImageNum(theElem,imageNum);	
	
	// Cancel the event.
	var e = ( window.event ) ? window.event : evt; // IE gets the event from window.event, in FF, it must be passed in
	e.cancelBubble = true; // prevent the event from bubbling to other event handlers
	e.returnValue = false; // IE: cancel the event so that <a href> does not fire
	if (e.preventDefault) { // FF: cancel the event so that <a href> does not fire
		e.preventDefault();
	}

}

/*	goToNextImage
 *	used for the artist pages:
 *	navigates to the next or previous image (depending on the fwd flag).
 */
function goToNextImage( evt, theElem, fwd, min, max )
{
	// peel off the current imageNum.
	var elem = document.getElementById(theElem);
	elem.src.search(/([0-9]+)\.(jpg|gif)/);	// sets RegExp global
	// grab the match as an integer
	var imageNum = parseInt(RegExp.$1);

	// increment/decrement, based on the direction, and wrap around
	imageNum += ( fwd ) ? 1 : -1;
	if ( imageNum < min )
		imageNum = max;
	if ( imageNum > max )
		imageNum = min;
	
	// swap the image!
	swapImageFromThumbs( evt,theElem, imageNum );
}


/*	swapImage
 *	used for the artist list.
 *	Pass image name and source values by calling the function elsewhere
 *	Example: swapImage(myImageName, images/myimage.gif)
 */
function swapImage(imageName, imageSrc)
{
	if (document.images)
	{
		var path = document.images[imageName].src;
		path = path.replace(/[^\/]+\.(jpg|gif|png)$/,'');	// peel off the trailing filename, e.g., filename.gif or filename.jpg
		if (imageSrc != "none")
  		{
    		document.images[imageName].src = path + imageSrc;     		
    		document.images[imageName].className = "image";
  		}
  		else
  		{
  			document.images[imageName].src = path + "transparent_thumb.gif";
    		document.images[imageName].className = "imagenoborder";
  		}
  	}
}

