/*
	Name:  js_main.js
	Created by:  Orion Imaging(TM) [www.OrionImaging.net],  Developer: Lawrence Duncan
*/

	function isset( varStr )
	// In:  [string] varName is variable to check existence of
	// Out:  [boolean] state of varName's existence
	{
		return (typeof(window[varStr])!='undefined') ? true : false;
	}


	function toggle_menu( menuId, onOff )
	// In:  [string] menuId is id of menu container div,  [int] onOff is state to toggle to
	// Out:  toggled menu
	{
		//turn off other menus...
		if (!isset('allMenuIds')) allMenuIds = new Array('phlebStationsList', 'urinePTsList', 'partsList');
		if (onOff)
		{ //turn things off before on...
			for (i=0; i<allMenuIds.length; ++i)
			{
				if (menuId!=allMenuIds[i])
				{
					if (document.getElementById(allMenuIds[i])) document.getElementById(allMenuIds[i]).style.display = 'none';
				}
			}
		}
		
		root = document.getElementById(menuId);
		if (isset('timeOut')) clearTimeout(timeOut);

		if (onOff) root.style.display = 'block';
		else timeOut = setTimeout("root.style.display = 'none'", 400);
	} //end function: toggle_menu();
	
	
	function catch_tab_nav( e, goPage )
	// Use:  does tab / enter navigation
	// In:  e is standard event
	// Note:  this is actually just a dummy function with some verbage, which somehow make IE happy with tab-enter stuffs...
	{
		if (e.keyCode==13) location.href = goPage; //13==enter
	}


	function toggle_display( elmId )
	// In:  [string] elmId is id of element to toggle,  [int] onOff is state to toggle to (future dev...)
	// Out:  toggled element
	{
		root = document.getElementById(elmId);
		if (root.style.display=='block' || (arguments.length>1 && !arguments[1])) root.style.display = 'none';
		else root.style.display = 'block';
		return false; //kill link
	} //end function: toggle_menu();

	
	function toggle_plusminus( elmId )
	// In:  [string] elmId is id of element to toggle,  [int] onOff is state to toggle to (future dev...)
	//	[implicit.string]  also inverts plus/minus state of elmId + "_img"
	// Out:  toggled element & inverted plus/minus
	{
		toggle_display(elmId);
		if (document.getElementById(elmId+'_img'))
		{
			imgSrc = document.getElementById(elmId+'_img').src;
			var relSrc = imgSrc.split('/').pop();
			
			if ( relSrc != 'plus3m1minus1m1.png')
			{ //make minus...
				oldSrc = document.getElementById(elmId+'_img').src; //remember...
				var relSrcRoot = oldSrc.split('/'+relSrc);
				document.getElementById(elmId+'_img').src = relSrcRoot[0]+'/plus3m1minus1m1.png';
			}
			else
			{ //revert to last...
				document.getElementById(elmId+'_img').src = oldSrc;
			}
		}
		return false; //kill link
	} //end function: toggle_plusminus();


function lowLightMild( elm_id )
//USE:  to 'lowlight' text    {undelines & changes color to 'whitesmoke' (if not already)
//IN:  elm_id is element id, [color is optional color to highlight to]                 **A milder effect than lowLight()
{
	if (document.getElementById)
	{
		var root = document.getElementById(elm_id);
		color_holder = root.style.color;  //sets global var to remember element color
		weight_holder = root.style.fontWeight;
		//root.style.fontWeight = "bold";
		root.style.textDecoration = "underline";
		if (arguments.length==2) root.style.color = arguments[1];
		else root.style.color = "whitesmoke";
		if (!document.all) root.style.cursor = "pointer"; //pointer
		else root.style.cursor = "hand";
	}
	
	return false;
}

function unLowLightMild( elm_id )
//USE:  to unLowLightMild text  **NOTE: undoes underlining AND color change, but NOT font-weight
//IN:  elm_id is element id
{
	if (document.getElementById)
	{
		var root = document.getElementById(elm_id);
		root.style.textDecoration = "none";
		root.style.color = color_holder;
		//root.style.cursor = "auto";
	}
	
	return false;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ begin: Ajax things...
//In: [array] images is array of image src's
//Out: fading images, recirculates to [0] image
function AjaxImageShow( images )
{
	//variables...
	var imageCount = 1; //**change this to zero to include first image in array...
	var readyState = 0;
	var responseText = 'Loading...';
	
	//properties...
	this.images = images;
	this.fadeSpeed = 9; //25;
	this.fadeStepSize = 1; //3;
	
	//methods...
	this.nextImage = nextImage;

	function nextImage( target ) //nextImage(target[, footContent])  where footContent=='footerFilname.php?get1=something' ==> draw in next gal footer...
	{
		//set globals, avoid loss of scope...
		fadeSpeed = this.fadeSpeed;
		fadeStepSize = this.fadeStepSize;
		if (imageCount>=this.images.length) imageCount = 0; //reset the show...

		var xmlHttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		if (xmlHttp)
		{ //ajax is go...
			var content = 'images/image_ajax.php?image='+this.images[imageCount];
			xmlHttp.open("GET", content, true); 
			xmlHttp.send(null);
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState==4)
				{
					if (xmlHttp.status=='200')
					{
						imageABFadeByContainer(target, fadeSpeed, fadeStepSize, xmlHttp.responseText); //AB-fade images...
					}
					else replaceById('** Error loading object', target);
					
					++imageCount;
				}
			} //end function: onreadystatechange();
		} //end if: ajax is go;
		return;
	} //end method: nextImage;

} //end class: AjaxImageShow();


function imageABFadeByContainer(target, fadeSpeed, fadeStepSize, responseText)
// Use:  fades one image into another
{
	imageFadeByContainer(target, 'out', fadeSpeed, fadeStepSize, 1, responseText); //fading out image & swap...
	
	//replaceById(responseText, target);
	//setOpacityByContainer(target, 0);
	
	//imageFadeByContainer(target, 'in', fadeSpeed, fadeStepSize, 1); //fade in image...
} //end function: imageABFadeByContainer();


function ajaxFile(content, target) //ajaxFile(content, target[, int spaceBefore, int spaceAfter])
{
	var readyState = 0;
	var responseText = '';
	var spaceBefore = 0;  var spaceAfter = 0; //for padding the in-between animation...
	if (arguments.length>2)
	{
		if (arguments[2].substr(0,12)=='spaceBefore=') spaceBefore = arguments[2].substr(12);
		if (arguments[3].substr(0,11)=='spaceAfter=') spaceAfter = arguments[3].substr(11);
	}

	var xmlHttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	xmlHttp.open("GET", content, true); //get new doc...
	xmlHttp.send(null);
	
	if ('readyState' in xmlHttp && xmlHttp.readyState==4)
	{ //content is already here (cached perhaps)...
		 replaceById(xmlHttp.responseText, target);
		 setTimeout('phlebStatVidControls();', 200); //special auto-play for phlebStatVid
		 return;
	}
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState==4)
		{
			if (xmlHttp.status=='200') responseText = xmlHttp.responseText;
			else responseText = '**Error loading object';
			replaceById(responseText, target);
			setTimeout('phlebStatVidControls();', 200); //special auto-play for phlebStatVid
		}
	}
	return;
} //end function: ajaxFile();


function getOpacityByContainer(target) //opacity := [0,100]
{
	var opacity = '';
	target = document.getElementById(target).getElementsByTagName('img')[0];
	
	if (target.style.MozOpacity!=undefined && target.style.MozOpacity!='')
	 opacity = target.style.MozOpacity*100;
	else if (target.style.KhtmlOpacity!=undefined)
	 opacity = target.style.KhtmlOpacity*100;
	else if (target.filters && target.filters.alpha!=undefined)
	 opacity = target.filters.alpha.opacity;
	else if (target.style.opacity!=undefined && target.style.opacity!='')
	 opacity = target.style.opacity*100;
	else opacity = 100;

	return opacity;
} //end method: getOpacityByContainer();


function setOpacityByContainer(target, opacity) //opacity := [0,100]
{
	target = document.getElementById(target).getElementsByTagName('img')[0];
	target.style.opacity = opacity/100;
	target.style.MozOpacity = opacity/100;
	target.style.KhtmlOpacity = opacity/100;
	target.style.filter = 'alpha(opacity='+opacity+')';
	//alert('target.filters.alpha.opacity == '+target.filters.alpha.opacity);
} //end method: setOpacityByContainer();


function imageFadeByContainer(target, inOut) //imageFadeByContainer(target, inOut[, fadeSpeed[, fadeStepSize]])
// In:  [optional int] offset is for AB-fading an image in after fading out a previous one
{
	if (arguments[2]==undefined) var fadeSpeed = 25;  else var fadeSpeed = arguments[2];
	if (arguments[3]==undefined) var fadeStepSize = 3;  else var fadeStepSize = arguments[3];
	if (arguments[4]==undefined) var offset = 0;  else var offset = 1;
	if (arguments[5]==undefined) var responseText = '** Error: no responseText';  else var responseText = arguments[5];

	var opacity = getOpacityByContainer(target);
	var timerCount = 0;
	var count = 0;
	fadeId = new Array(); //global array for holding setTimeout id's...

	if (inOut=='in')
	{ //fade image in...
		if (offset)
		{
			replaceById(responseText, target);
			setOpacityByContainer(target, 0);
			opacity = 0;
		}

		do
		{
			if (offset) timeOffset = countPrevious*fadeSpeed; // * (fadeSpeed*timerCount) / 2;
			else timeOffset = 0;
			
			opacity = Math.min(100, opacity+fadeStepSize/1.5);
			fadeId[count] = setTimeout('setOpacityByContainer(\''+target+'\', '+opacity+')', (fadeSpeed*timerCount)+timeOffset);
			++timerCount;  ++count;
		} while (opacity<100);
		
		if (offset)
		{ //initiate next slide...
			setTimeout("imageShow.nextImage('"+target+"')", timeBetweenSlides + (fadeSpeed*timerCount) + timeOffset);
		}
	}
	else
	{ //fade image out...
		do
		{
			opacity = Math.max(0, opacity-fadeStepSize);
			fadeId[count] = setTimeout('setOpacityByContainer(\''+target+'\', '+opacity+')', fadeSpeed*timerCount);
			
			//if (offset && opacity<5) break;
			
			++timerCount;  ++count;
		} while (opacity>0);

		if (offset)
		{ //replace image once faded out, then fade in the next...
			//fadeId[count] = setTimeout("replaceById('"+responseText+"', '"+target+"')", fadeSpeed*(timerCount));
			//fadeId[count+1] = setTimeout("setOpacityByContainer('"+target+"', 0)", fadeSpeed*(timerCount+1));
			fadeId[count] = setTimeout("imageFadeByContainer('"+target+"', 'in', '"+fadeSpeed+"', '"+fadeStepSize+"', 1, '"+responseText+"')", fadeSpeed*(timerCount)); //fade in image...
		}
		
		countPrevious = count; //*Note: global
	}

} //end function: imageFade();


function replaceById(content, target)
{
	if (document.getElementById)
	{
		document.getElementById(target).innerHTML = content;
		return true;
	}
	return false;
} //end function: replaceById();


//flash video things...
function getFlashMovieObject( movId )
{
	if (document.embeds && document.embeds[movId])
	{
		return document.embeds[movId];
	}
	  
	else if (document.getElementById(movId))
	{
		return document.getElementById(movId);
	}
	
	else return false;

/* //inspired by the following (found on the net @ http://www.permadi.com/tutorial/flashjscommand/)...
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
*/
}

function SendDataToFlashMovie( movId, command )
{
    var movie = getFlashMovieObject(movId);
    if (!movie) alert('No movie.');
	else movie.SetVariable("/:message", command);
}


function StopFlashMovie( movId )
{
	var movie = getFlashMovieObject(movId);
    if (!movie) alert('No movie.');
	else movie.StopPlay();
}


function PlayFlashMovie( movId )
{
	var movie = getFlashMovieObject(movId);
    if (!movie) alert('No movie.');
	else movie.Play();
	//embed.nativeProperty.anotherNativeMethod();
}


function phlebStatVidControls()
// Use:  to control the phlebotomy stations video
{
	if (!isset('playing') || !playing)
	{ //play...
		PlayFlashMovie('phlebStatVid');
		var replaceText = 
			'<span id="phlebStatVidWrap">'
			+'<a id="vidControlsLink" href="javascript:;" title="Pause Video" onclick="phlebStatVidControls();" style="color: #999; font-size: 10px;">Playing...</a>'
			+'</span>';
		replaceById(replaceText, 'vidControlsWrap');
		playing = true;
	}
	else
	{ //stop...
		StopFlashMovie('phlebStatVid');
		var replaceText = 
			'<span id="phlebStatVidWrap">'
			+'<a id="vidControlsLink" href="javascript:;" title="Play Video" onclick="phlebStatVidControls();" style="font-size: 10px;">Paused.</a>'
			+'</span>';
		replaceById(replaceText, 'vidControlsWrap');
		playing = false;
	}
}

