/*
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     Copyright:  (C) Orion Imaging, 2007 et alia  [www.orionimaging.com]
     For the pursuit of life, liberty, and good code...
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	# Name:  js_main.js
	# Use:  main JavaScript
*/
	
	if (top.location.href!=location.href) top.location.href = top.location.href;

	function local()
	{
		if (location.href.substr(0, 17)=='http://127.0.0.1/') return true;
		else return false;
	}


	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 print_r( thing, niceHtml, level )
	// See also:
	//	http://binnyva.blogspot.com/2005/10/dump-function-javascript-equivalent-of.html
	//	http://www.brandnewbox.co.uk/logbook/category/Javascript/
	{
		if (!niceHtml) niceHtml = 0;
		if (!level) level = 0;
		
		var dump = '';
		var padding = ''; //pads beginning of line
		for (j=0; j<level+1; j++) padding += '  ';

		if (typeof(thing)=='object')
		{ //Array/Hashes/Objects
			for (var item in thing)
			{
				var value = thing[item];
				if (typeof(value)=='object')
				{ //If it is an array...
					dump += padding + "'" + item + "' ...\n";
					dump += print_r(value, niceHtml, level+1);
				}
				else
				{
					dump += padding + "'" + item + "' => \"" + value + "\"\n";
				}
			}
		}
		else
		{ //Strings/Chars/Numbers etc...
			dump = '['+typeof(thing)+']<<<'+thing+'>>>';
		}
		
		return dump;
	}
	
	
	function explode( sep, str )
	// Use:  mirrors php's explode
	{
		return str.split(sep);
	}
	
	
	function implode( sep, arr )
	// Use:  mirrors php's implode
	{
		var out = arr[0];
		for (var i=1; i<arr.length; ++i) out += sep+arr[i];
		return out;
	}


	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('portfolioList', 'aboutList');
		
		if (onOff)
		{ //turn things off before on...
			for (i=0; i<allMenuIds.length; ++i)
			{
				if (menuId!=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);
		
	}
	
	
	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) e = window.event;
		if (!e) return;
		else if (e.keyCode==13) location.href = goPage; //13==enter
	}


	function show_elm( elmId, displayType )
	// Use:  to show a particular element by id
	// In:  elmId is the element to show, [optional] displayType is either 'block' or 'inline' or another valid diplay value
	{
		var root = document.getElementById(elmId);
		if (root)
		{
			if (displayType) root.style.display = displayType; //set display...
			else root.style.display = 'block';
			if (displayType) root.style.display = displayType; //set visibility...
			else root.style.visibility = 'visible';
		}
	}
	
	
	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
	}

	
	function toggle_plusminus( elmId, toggleToSrc )
	// 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
	{
		if (!toggleToSrc) var toggleToSrc = 'plus3m1minus1m1.png';
		
		if (document.getElementById(elmId+'_img'))
		{
			imgSrc = document.getElementById(elmId+'_img').src;
			var relSrc = imgSrc.split('/').pop();
			
			if (relSrc != toggleToSrc)
			{ //make minus...
				toggle_display(elmId, 0);
				oldSrc = document.getElementById(elmId+'_img').src; //remember...
				var relSrcRoot = oldSrc.split('/'+relSrc);
				document.getElementById(elmId+'_img').src = relSrcRoot[0]+'/'+toggleToSrc;
			}
			else
			{ //revert to last...
				toggle_display(elmId);
				document.getElementById(elmId+'_img').src = oldSrc;
			}
		}
		return false; //kill link
	}


function lowLight(elm_id)
//USE:  to 'lowlight' text (just bolder [no color change])
//IN:
//OUT:
{
	if (document.getElementById)
	{
		var root = document.getElementById(elm_id);
		weight_holder = root.style.fontWeight;  //sets global var to remember weight
		root.style.fontWeight = "900";
		if (!document.all) root.style.cursor = "pointer";
		else root.style.cursor = "hand";
	}
  return;
}


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()
//OUT:                                                                                **NOTE: use unLowLight() |OR| unLowLightMild() to undo lowLightMild()
{
	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.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;
}


function unLowLight(elm_id)
//USE:  to unhighlight text
//IN:  elm_id is element id
//OUT:
{
	if (document.getElementById)
	{
		var root = document.getElementById(elm_id);
		root.style.fontWeight = weight_holder;
		root.style.textDecoration = "none";
		root.style.cursor = "auto";
	}
  return;
}


function unLowLightMild(elm_id)
//USE:  to unLowLightMild text
//IN:  elm_id is element id
//OUT:
{
	if (document.getElementById)
	{
		var root = document.getElementById(elm_id);
		root.style.textDecoration = "none";
		root.style.color = color_holder;
		root.style.cursor = "auto";
	}
  return;
}


function imageBorder( imageId ) //imageBorder(imageId[, width[, style[, color]]]);
//USE:  to put on, and take off borders, respectively
//IN:  img_name is string for image name, [optional] width, [optional] style, [optional] color
{
	var width = '1px';  var style = 'solid';  var color = '#EEEEEE';
	if (arguments.length>1)
	{
		if (arguments[1]) width = arguments[1];
		if (arguments[2]) style = arguments[2];
		if (arguments[3]) color = arguments[3];
	}
	if (document.getElementById)
	{
		document.getElementById(imageId).style.border = width+' '+style+' '+color;
	}
}
function imageUnborder( imageId ) //imageUnborder(imageId[, width[, style[, color]]]);
{
	var width = '0px';  var style = 'none';  var color = '#000000';
	if (arguments.length>1)
	{
		if (arguments[1]) width = arguments[1];
		if (arguments[2]) style = arguments[2];
		if (arguments[3]) color = arguments[3];
	}
	if (document.getElementById)
	{
		document.getElementById(imageId).style.border = width+' '+style+' '+color;
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 )
	// Use:  nextImage(target)
	//*Note:  remember to set the global 'imageShell' if you need a different container (where imageShell is the server-side image container)
	{
		if (!imageShell) imageShell = 'images/image_ajax.php';
		
		//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 = imageShell+'?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;
				}
			}
		}
		
		return;
	} //end method: nextImage;

} //end class: AjaxImageShow();


function imageABFadeByContainer( target, fadeSpeed, fadeStepSize, responseText )
// Use:  fades one image into another
{
	if (!imageShell) var imageShell = false;
	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);
	
	//might be in cache...
	//replaceById('<p><br />&nbsp;<br />Hello: '+xmlHttp.status+'</p>', target); //**debug
	try { if (xmlHttp.readyState && xmlHttp.status=='200') replaceById(xmlHttp.responseText, target); }
	catch (e) {}
	
	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, fadeSpeed, fadeStepSize, offset, responseText )
 //imageFadeByContainer(target, inOut[, fadeSpeed[, fadeStepSize]])
// In:  [optional int] offset is for AB-fading an image in after fading out a previous one
{
	if (!fadeSpeed) var fadeSpeed = 25;
	if (!fadeStepSize) var fadeStepSize = 3;
	if (!offset) var offset = 0;  else var offset = 1;
	if (!responseText) var responseText = '** Error: no responseText';
	if (!offset) var offset = 0;  else var offset = 1;
	if (!imageShell) var imageShell = false;

	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();
}
