/**
 * global variables
 */

window.onload = initGallery;
var currentMedia = 0;

var mediaDivs = new Array();
var playInterval = false;

/**
 * initGallery
 */
function initGallery() {
	// show the switch control
	document.getElementById("switch_control").style.display = 'block';
	document.getElementById("single_control").style.display = 'block';
	
	// hide the big images and show the first one
	divs = document.getElementsByTagName("div");
	var j = 0;
	for( var i = 0; i < divs.length - 1; i++ ) { 
		if( divs[i].className == 'media' ) {
			mediaDivs[j] = divs[i];
			j++;
			divs[i].style.display = "none";
		}
	}
	mediaDivs[0].style.display = "block";
	currentMedia = 0;
	countImages()
}
/**
 * countImages
*/ 
function countImages() {
	myCurrentMedia = currentMedia + 1;
	myCounter = document.getElementById('counter');
	myCounter.innerHTML = myCurrentMedia + ' of ' + mediaDivs.length;
}
/**
 * switchMedia
 */
function switchMedia(target, caller) {
	var myTarget = target.split("#");
	hideMedia();

	new Effect.Pulsate(caller, {from: .7}); 
	for (var i = 0; i < mediaDivs.length; i++) {
		if (mediaDivs[i].id == myTarget[1]) {
			document.getElementById(myTarget[1]).style.display = new Effect.Appear(myTarget[1]);
			currentMedia = i;
		}
	}
	countImages();
	return false;
}
/**
 * hideMedia
 */
function hideMedia() {
	divs = document.getElementsByTagName("div"); 
	for( var i = 0; i < divs.length - 1; i++ ) { 
		if( divs[i].className == 'media' ) {
			divs[i].style.display = "none";
		}
	}
}
/**
 * showAll
 */
function showAll() {

	divs = document.getElementsByTagName("div"); 
	for( var i = 0; i < divs.length - 1; i++ ) { 
		if( divs[i].className == 'media' ) {
			divs[i].style.display = "block";
		}
	}
	return false;
}
/**
 * showOne
 */
function showOne() {
	var i = 1;
	while (document.getElementById('img'+i)) {
		var myImg = 'img' + i;
		if (i == 1) {
			document.getElementById(myImg).style.display = "block";
		} else {
			document.getElementById(myImg).style.display = "none";
		}
		i++;
	};
	document.getElementById("single_control").style.display = "block";
	document.getElementById("all_control").style.display = "none";
	return false;
}
/**
 * indicateThumb
 */
function indicateThumb() {
	// pulsate thumb nail image
//alert("you make me pulsate");
	var myLinks = null;
	links = document.getElementsByTagName("a"); 
	for( var i = 0; i < links.length -1; i++ ) { 
		if (links[i].toString().search("#")  != -1) {
			myLinks = links[i].toString().split("#");
		} else {
			myLinks[1] = null;
		}
		if( myLinks[1] == mediaDivs[currentMedia+1].id ) {
alert("i = " + i);
alert(myLinks[1] +" == "+mediaDivs[currentMedia+1].id);
			new Effect.Pulsate(links[i-1], {from: .7, duration: 1}); 
		}
	}
}
/**
 * showNext
 */
function showNext() {
	
//alert(currentMedia + ' ' + mediaDivs.length);

	hideMedia();
	document.getElementById(mediaDivs[currentMedia+1].id).style.display = new Effect.SlideUp(mediaDivs[currentMedia+1].id); //'block';
	document.getElementById(mediaDivs[currentMedia].id).style.display = new Effect.SlideDown(mediaDivs[currentMedia].id); //'block';
	if(currentMedia+2 == mediaDivs.length) {
		currentMedia = 0;
	} else {
		currentMedia++;
	}
	indicateThumb();
	countImages();
	return false;	
}
/**
 * play
 */
function play() {
	myPlay = document.getElementById('play');
	myPlay.id = 'stop';
//	myPlay.innerHTML = 'stop';
//		myPlay.onclick = stopPlay();
	if (!playInterval) {
		playInterval = setInterval("showNext()", 1000);
	} else { 
		myPlay.onClick = clearInterval(playInterval);
		stopPlay();
	}
	return false;
}
/**
 * stopPlay
 */
function stopPlay() {
	myPlay = document.getElementById('play');
	myPlay.id = 'play';
//	myPlay.innerHTML = 'play';
		playInterval = false;
//		myPlay.onClick = clearInterval(playInterval);
	return false;
}
/**
 * showPrev
 */
function showPrev() {
	hideMedia();
	document.getElementById(mediaDivs[currentMedia-1].id).style.display = new Effect.Appear(mediaDivs[currentMedia-1].id); //'block';
	currentMedia--;

	indicateThumb();
	countImages();
	return false;	
}
/**
 * popup
 */
function popup(url, id, width, height)
{
  newwindow = window.open('popup_pic.shtml?'+url,id,'toolbar=no,scrollbars=yes,location=no,statusbar=no,menubar=no,resizable=yes,width=' + width + ',height=' + height);
  newwindow.focus();
  return false;
}

