
// Name:  js_main.js
// Use:  main JavaScript for HansetCorp.com

	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
	{
		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 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') 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();
	
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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; //for padding the in-between animation...
	var spaceAfter = 0;
	
	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);
	xmlHttp.onreadystatechange = function()
	{
		//alert('readyState == '+readyState);
		readyState = xmlHttp.readyState;
		if (readyState==4)
		{
			if (xmlHttp.status=='200') responseText = xmlHttp.responseText;
			else responseText = '**Error loading object';
			replaceById(responseText, target);
		}
	}
	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();

