/* SCRIPT SETUP */

var div_ids = [];

document.observe("dom:loaded", function() { 
	if (identifier)
	{
		//loadCaptionLinks();
	}
});

/* GLOBAL FUNCTIONS FOR THE SCRIPT */

function doSlide(timeline, img_id, img_file, img_w, img_h, start_x, start_y, caption)
{
	timeline_local = 0;
	var moverDiv = 'slide' + timeline;
	
	var newObj = document.createElement("div");
	newObj.setAttribute('id', moverDiv);
	newObj.style.display = 'block';
	newObj.style.position = 'absolute';
	newObj.style.width = '1500px';
	newObj.style.height = '1500px';
	newObj.style.left = start_x + 'px';
	newObj.style.top = start_y + 'px';
	newObj.style.zIndex = 21;
	$("slide").appendChild(newObj);

//	setTimeout("printOut('" + timeline + "  :  " + img_id + " <b>slide</b>')", (timeline_local * 1000));
	setTimeout("createImg($('" + moverDiv + "'), '" + img_id + "', '" + img_w + "', '" + img_h + "', '" + img_file + "')", (timeline_local * 1000));

	timeline_local = (timeline_local + 0);
	new Effect.Appear('slide', { duration: 3.0, from: 0.0, to: 1.0, delay: timeline_local });

	timeline_local = (timeline_local + 0);
	new Effect.Appear(moverDiv, { duration: 3.0, from: 0.0, to: 1.0, delay: timeline_local });

	timeline_local = (timeline_local + 3);
	setTimeout("$('caption').innerHTML = '" + caption + "'", (timeline_local * 1000));
	
	new Effect.Appear('caption', { duration: 2.0, from: 0.0, to: 1.0, delay: timeline_local });

	timeline_local = (timeline_local + 4);
	new Effect.Appear('caption', { duration: 3.0, from: 1.0, to: 0.0, delay: timeline_local });

	timeline_local = (timeline_local + 4);
	new Effect.Appear('slide', { duration: 3.0, from: 1.0, to: 0.0, delay: timeline_local });

	timeline_local = (timeline_local + 3);
	setTimeout("removeDiv('" + moverDiv + "')", (timeline_local * 1000));
}	
	
function hideDivs(ele, divs, hideType)
{
//	printOut('test', '<hr />go');

	if (hideType == 'exclusive')
	{
		var toHide = div_ids;
		var exceptions = divs;
	}
	else{
		var toHide = divs;
		var exceptions = [];
	}	
	
	for (j=0; j<=toHide.length; j++)
	{
//		printOut('test', toHide[j]);
		if (exceptions.indexOf(toHide[j]) == -1)
		{
//			printOut('test', 'Fade:  ' + toHide[j]);
			new Effect.Fade(toHide[j], { duration: 3.0 });
		}
	}
}

function hideDivsdd(ele, exceptions_arr)
{
	var parent = (ele) ? $(ele) : document.getElementsByTagName("body").item(0);
	var divs = parent.childElements();
	printOut('test', 'go');
	for (j=1; j<=divs.length; j++)
	{
		if (typeof divs[j] != 'undefined')
		{
			printOut('test', divs[j].id);
			if (exceptions_arr.indexOf(divs[j].id) == -1)
			{
				printOut('test', divs[j].id);
				new Effect.Fade(divs[j].id, { duration: 3.0 });
			}
		}
	}
}

function clearDiv(ele)
{
	$(ele).innerHTML = '';
}
	
function removeDivs(ele_arr)
{
	ele_arr.each(function(r) {
		Element.remove(r);
	});
}
	
function removeDiv(ele)
{
	Element.remove(ele);
}
	
function printDivs(parentDiv)
{
	if (typeof parentDiv == 'undefined')
	{
		parentDiv = document.getElementsByTagName("body").item(0);
	}
	
	printOut('test', '<hr />');
	for (i=0; i<parentDiv.childNodes.length; i++)
	{
		if (typeof parentDiv.childNodes[i].id != 'undefined')
		{
			printOut('test', parentDiv.id + ':  ' + parentDiv.childNodes[i].id + '  ::  ' + parentDiv.childNodes[i].style.zIndex);
		}
	}
}

function createDiv(parentDiv, id, x, y, w, h, img, color, display, zIndex)
{
	//registerElement(id);

	if (parentDiv == '')
	{
		parentDiv = document.getElementsByTagName("body").item(0);
	}
	else{
		parentDiv = $(parentDiv);
	}
	
	var newObj = document.createElement("div");
	newObj.setAttribute('id', id);
	newObj.style.display = (display) ? display : 'none';
	newObj.style.position = 'absolute';
	newObj.style.overflow = 'hidden';
	newObj.style.height = h + 'px';
	newObj.style.width = w + 'px';
	newObj.style.top = y + 'px';
	newObj.style.left = x + 'px';
	//newObj.style.border = '1px solid';
	newObj.style.zIndex = (zIndex) ? zIndex : 20;
	if (img) { newObj.style.background = 'url(' + img + ') no-repeat'; }
	if (color) { newObj.style.background = color; }
	parentDiv.appendChild(newObj);
}

function createImg(parentDiv, id, w, h, img)
{
	registerElement(id);

	if (parentDiv == '')
	{
		parentDiv = document.getElementsByTagName("body").item(0);
	}
	else{
		parentDiv = $(parentDiv);
	}

	var newImg = document.createElement("img");
	newImg.setAttribute('id', id);
	newImg.src = img;
	newImg.border = 0;
	newImg.style.position = 'absolute';
	if (h) { newImg.height = h; }
	if (w) { newImg.width = w; }
	parentDiv.appendChild(newImg);
}

function moveDiv(elem, x, y)
{
	if (x != '') { $(elem).style.left = x + 'px'; }
	if (y != '') { $(elem).style.top = y + 'px'; }
}

function resizeDiv(elem, w, h)
{
	if (w != '') { $(elem).style.width = w + 'px'; }
	if (h != '') { $(elem).style.height = h + 'px'; }
}

function registerElement(id)
{
	div_ids.push(id);
	//printOut('test', 'Element Reg: ' + id);
}

function zoomImage(elem, pixel_magnification, zoom_dir)
{
	//$(elem).style.position = "absolute";
	$(elem).interval = setInterval("doZoomImage('" + elem + "', " + pixel_magnification + ", '" + zoom_dir + "')", 30);
}

function doZoomImage(elem, pixel_magnification, zoom_dir)
{
	w = $(elem).width;
	h = $(elem).height;
	//printOut(elem + ' : ' + w + '  ' + h);

	if (zoom_dir == 'in')
	{
		if (w < h)
		{
			if ($(elem).width < pixel_magnification)
			{
				ratio = (w / h);
				h = $(elem).height + 4;
				$(elem).height = h;
				$(elem).width = Math.round(ratio * h);
				//printOut('w less than  ' + w + '  ' + h);
			}
			else{
				clearInterval($(elem).interval);
			}
		}
		else{
			if ($(elem).width < pixel_magnification)
			{
				ratio = (h / w);
				w = $(elem).width + 4;
				$(elem).width = w;
				$(elem).height = Math.round(ratio * w);
				//printOut('h less than  ' + w + '  ' + h);
			}
			else{
				clearInterval($(elem).interval);
			}
		}
	}
	else{
		if (w < h)
		{
			if ($(elem).width > pixel_magnification)
			{
				ratio = (w / h);
				h = $(elem).height - 4;
				$(elem).height = h;
				$(elem).width = Math.round(ratio * h);
				//printOut('w less than  ' + w + '  ' + h);
			}
			else{
				clearInterval($(elem).interval);
			}
		}
		else{
			if ($(elem).width > pixel_magnification)
			{
				ratio = (h / w);
				w = $(elem).width - 4;
				$(elem).width = w;
				$(elem).height = Math.round(ratio * w);
				//printOut('h less than  ' + w + '  ' + h);
			}
			else{
				clearInterval($(elem).interval);
			}
		}
	}
}

function soundToggle(m3uFile)
{
	if ($('soundstatus').innerHTML == 'enabled')
	{
		Sound.stop(m3uFile);
		Sound.disable();
		$('soundstatus').innerHTML = 'disabled';
	}
	else{
		Sound.enable();
		Sound.play(m3uFile, { loop: false });
		$('soundstatus').innerHTML = 'enabled';
	}
}

function startSound(m3uFile, loop)
{ 
	var objBody = document.getElementsByTagName("body").item(0);
	var id = m3uFile.replace(/[\./]+/g, '_');
	var div = id + '_div';
	printOut('startSound:  ' + id + '  :  ' + div);
	
	if ($('soundstatus').innerHTML != 'disabled')
	{
		objBody.innerHTML += '<embed id="' + id + '" src="' + m3uFile + '" hidden="true" autostart="true" loop="' + loop + '" height="45" width="170" type="' + getMimeType() + '" mastersound><\/embed>\n'; 
	}
}


function stopSound(m3uFile)
{
	var objBody = document.getElementsByTagName("body").item(0);
	var div = m3uFile.replace(/[\./]+/g, '_') + '_div';
	printOut('stopSound:  ' + div);
	objBody.removeChild($(id));
}

function getMimeType()
{ 
	var mimeType = "application/x-mplayer2"; //default 
	var agt=navigator.userAgent.toLowerCase(); 
	if (navigator.mimeTypes && agt.indexOf("windows")==-1) 
	{ 
	//non-IE, no-Windows 
	  var plugin=navigator.mimeTypes["audio/mpeg"].enabledPlugin; 
	  if (plugin) mimeType="audio/mpeg" //Mac/Safari & Linux/FFox 
	}//end no-Windows 
	return mimeType 
}

function advanceTime(timeline, incr, event)
{
	timeline_local = 0;
	
	//printOut('timeline', timeline + " :  " + incr + " = " + event);
	timeline = timeline + incr;
	return timeline;
}

function printTime()
{
	$('time').interval = setInterval("incrementTime()", 1000);
}

function incrementTime()
{
	var t = eval($('timekeeper').innerHTML);
	t = (t+1);
	$('timekeeper').innerHTML = t;
	$('time').innerHTML = Math.floor(t / 60) + ':' + roundSeconds(t % 60);
}

function roundSeconds(num)
{
	var num_new = Math.round(num);
	if (num_new < 10)
	{
		return '0' + num_new;
	}
	else{
		return num_new;
	}
}

function stopTime(timeline)
{
	clearInterval($('time').interval);
	$('time').style.color = 'red';
}

function quickLoadImages(images)
{
	var aImages = [];
	
	// for each image, call preload()
	for ( var i = 0; i < images.length; i++ )
	{
		var oImage = new Image;
		aImages.push(oImage);
		oImage.src = images[i];
	}
	
	startExhibit(aImages);
}

var print = 'yes';
var code = 'no';
var printCount = 0;

function printOut(ele, msg)
{
	color = (printCount % 2 == 0) ? '#CCFFFF' : '#CCFF99';
	
	if (print == 'yes')
	{
		msg = (code == 'yes') ? msg.escapeHTML() : msg;
		$(ele).style.display = 'block';
		Element.insert(ele, { top: '<div class="printOut" style="background-color: ' + color + ';">' + msg + '</div>\n' });
	}
	
	printCount++;
}

