// shows select boxes
function showSelectBoxes()
{
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// hides select boxes
function hideSelectBoxes()
{
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// shows flash objects and embeded flash
function showFlash()
{
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) 
	{
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) 
	{
		flashEmbeds[i].style.visibility = "visible";
	}
}

// hides flash objects and embeded flash
function hideFlash()
{
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) 
	{
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) 
	{
		flashEmbeds[i].style.visibility = "hidden";
	}

}

//
//	Configuration
//
var overlayOpacity = 0.8;	// controls transparency of shadow overlay

var animate = true;			// toggles resizing animations
var resizeSpeed = 7;		// controls the speed of the image resizing animations (1=slowest and 10=fastest)

if(animate == true){
	overlayDuration = 0.2;	// shadow fade in/out duration
	if(resizeSpeed > 10){ resizeSpeed = 10;}
	if(resizeSpeed < 1){ resizeSpeed = 1;}
	resizeDuration = (11 - resizeSpeed) * 0.15;
} else { 
	overlayDuration = 0;
	resizeDuration = 0;
}

// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Help from Ran Bar-On [ran2103@gmail.com]
//

function pause(ms)
{
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}

function resizeDataContainer(dataContainerId,dataId,Width,Height) 
{
	// get curren width and height
	var widthCurrent = $(dataContainerId).getWidth();
	var heightCurrent = $(dataContainerId).getHeight();

	// scalars based on change from old to new
	var xScale = ( Width / widthCurrent) * 100;
	//xScale = xScale>100?100:xScale;
	xScale=100;
	var yScale = ( Height / heightCurrent) * 100;

	// calculate size difference between new and old image, and resize if necessary
	var wDiff = widthCurrent - Width;
	var hDiff = heightCurrent - Height;

	if( hDiff != 0){ new Effect.Scale($(dataContainerId), yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
	if( wDiff != 0){ new Effect.Scale($(dataContainerId), xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

	// if new and old image are same size and no scaling transition is necessary, 
	// do a quick pause to prevent image flicker.
	if((hDiff == 0) && (wDiff == 0))
	{
		if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);} 
	}

	$(dataContainerId).setStyle({'width':Width.toString()+'px'});
	new Effect.Appear($(dataId), { duration: resizeDuration, queue: 'end'});
	new Effect.Appear($('bottom'), { duration: resizeDuration, queue: 'end'});
}

function ajaxRequest(url,dataId,outerId,dataContainerId,param)
{
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	$('loading').setStyle({'top':((ns ? pageYOffset : document.documentElement.scrollTop + 10).toString())+'px'});
	new Effect.Appear($(outerId), { duration: overlayDuration, from: 0.0, to: overlayOpacity });
	
	$('loading').show();
	var marginsLR=10;		// left and right margins
	var marginsTB=10;		// top and bottom margins
	var marginBottom=0
	var width=0;
	var height=0;
	hideSelectBoxes();
	hideFlash();
	$(dataId).hide();
	
	imgPreloader=new Image();
	imgPreloader.onload=function()
	{
		$(dataId).setStyle({'width':(imgPreloader.width).toString()+'px'});
		$(dataId).setStyle({'height':(imgPreloader.height).toString()+'px'});
		$(dataId).setStyle({'margin':'0 auto 0 auto'});
		
		$(dataId).update('<img src="'+imgPreloader.src+'" alt="" style="margin-top:.3em;cursor:pointer;" onclick="closeBox(\''+dataId+'\',\''+dataContainerId+'\',\''+outerId+'\');clearTimeout(tId);" />');
		
		width=imgPreloader.width;
		height=imgPreloader.height;
		if (param!="")
		{
			var params=param.split('~');
			makeRequest('GET',params[1],params[2],params[0]);
			marginBottom=10;						//font dimension
		}
		$(dataContainerId).setStyle({'width':(width+marginsLR*2).toString()+'px'});
		$(dataContainerId).setStyle({'height':(height+marginsTB*2+marginBottom).toString()+'px'});
		imgPreloader.onload=function(){};
		showItems(dataContainerId,dataId,width,marginsLR*2,height,(marginsTB*2+marginBottom));
	}
	imgPreloader.src=url;

	function showItems(dataContainerId,dataId,width,marginsLR,height,marginsTB)
	{
		pause(500);
		resizeDataContainer(dataContainerId,dataId,width+marginsLR,height+marginsTB);

		$(dataContainerId).setStyle({'left':((document.documentElement.scrollWidth-width-marginsLR)/2).toString()+'px'});
		$(outerId).setStyle({'height':document.documentElement.scrollHeight.toString()+'px'});
		
		$('loading').hide();
		FloatTopDiv(dataContainerId);
		$(dataContainerId).show();
	}
	
}
var tId;
function FloatTopDiv(id)
{
	var startX = 0;
	var startY = 10;
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
	window.stayTopLeft= function()
	{
		var pY = ns ? pageYOffset : document.documentElement.scrollTop;
		
		if (pY + startY+$(id).scrollHeight<=document.documentElement.scrollHeight)
		{
			$(id).setStyle({'top':((pY + startY).toString())+'px'});
		}
		tId=setTimeout("stayTopLeft('"+id+"')", 100);
	}
	stayTopLeft(id);
}
function closeBox(dataId,dataContainerId,outerId) 
{
	$('loading').hide();
	$(dataId).update('&nbsp;');
	new Effect.Fade($(dataContainerId), { duration: overlayDuration});
	new Effect.Fade($(outerId), { duration: overlayDuration});
	showSelectBoxes();
	showFlash();
}
