ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
ns6 = (!document.all && document.getElementById)? true:false;
assetPath = 'assets/';

//imageJscript_a will exist on feature pages with more than one image -- it's form is [[imSrc,w,h,caption,opacity],["assets/tdst/idaTest4.jpg",410,478,"Decreasing the lash craziness", 0]];
curFeatureIm = 0; //current feature image which corresponds to its index in imageJscript_a
fade_a = [];
fade_a['in'] = -1; //index of im div to fade in
fade_a['out'] = -1; //ind of im div to fade out
fade_a['ondeck'] = -1; //ind of next im to fade to (if new selection is clicked in the middle of a fade)
imCrossFade = true;
imInit = false; //once initialized this will be set to false (if imCrossFade is enabled)
servicingFade = false;
//fade speed vars
linearFade = false;
step = 2; //used if fade is linear
fps = 60;
timeout = parseInt(1000/fps);
speed = 6; //used if fade is not linear
lt = 2/speed; //lower threshold for non linear fades

function rollOver(itemPrefix, itemNum, mouseOver){
	// itemPrefix is prefix of id for this image, itemNum is index (from 0) of image to act on (itemPrefix+itemNum == image id), mouseOver -- bool 1 = mouseOver, 0 = mouseOut
	var menu_a = ['latest', '2d', '3d', 'web', 'archive', 'guestbook', 'contact'];
	var overSuffix = '_Ov.gif';
	var upSuffix = '_Dn.gif';
	
	var id = itemPrefix+itemNum;
	if (mouseOver){
		document.images[id].src = 'assets/nav/v2/'+menu_a[itemNum]+overSuffix;
	}else{
		document.images[id].src = 'assets/nav/v2/'+menu_a[itemNum]+upSuffix;
	}
}

function layerWrite(text, ltwt) { //ltwt is layer to write to
	if (ns4) {
		var lyr = document.layers[ltwt].document;
		lyr.open();
		lyr.write(text);
		lyr.close();
	}
	else if (ie4) {
		document.all[ltwt].innerHTML = text;
	}
	else if (ns6) {
		var lyr = document.getElementById(ltwt);
		lyr.innerHTML = text;
	}
}

function filetype(text) { //parse filename test
	var t_char = text.lastIndexOf(".", text.length) + 1;
	var ftype = text.slice(t_char);
	return ftype;
}

function winpos(winwidth, winheight) {
	var winx = parseInt((screen.width - winwidth)/2);
	var winy = parseInt(((screen.height - winheight)/5) * 2);
	
	if (winx < 0) {
		winx = 20;
	}
	if (winy < 0) {
		winy = 20;
	}
	
	if (ie4) {
		var upperleft='left='+winx+',top='+winy+',';
	}
	else if (ns4 || ns6) {
		var upperleft='screenX='+winx+',screenY='+winy+',';
	}
	else {
		var upperleft='';
	}//if
	return upperleft;
}

function popUpHtml(url, width, height, options) {
	//options(scrollable,resizable) -- 11 would enable scrollbars and make window resizable
	
	if (url.indexOf('http://') == -1){
		url = assetPath+url
	}
	
	if (options == null) {
		options = '0'
	}
	if (parseInt(options.charAt(0))) {
		var sb = 'scrollbars=yes';
	}else{
		var sb = 'scrollbars=no';
	}
	if (parseInt(options.charAt(1))) {
		var rs = 'resizable=yes';
	}else{
		var rs = 'resizable=no';
	}
	var upperleft = winpos(width, height);
	window.open(url,'pictureWindow',upperleft+'toolbar=no,location=no,directories=no,status=no,'+sb+','+rs+',copyhistory=no,width='+width+',height='+height);
}

function popUp(title, imageurl, imagewidth, imageheight, imagebgcolor, vars) {
	//vars is a string for passing extra vars (for quicktime only at the moment);
	
	imageurl = assetPath+imageurl;
	var ftype = filetype(imageurl); //returns the file extension
	if(imagebgcolor == null) {
		imagebgcolor='#ffffff';
	}//if
	
	
	var astr ='<html><head><title>' + title + '</title>';
	astr +='</head><body bgcolor="' + imagebgcolor + '" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" style="overflow:hidden;">';
	
	switch (ftype) {
		case ('dcr'):
		astr +='<object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000"';
		astr +=' codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,0,0,0"';
		astr +=' ID=shockmovie width=' + imagewidth + ' height=' + imageheight + '><param name=src value="' + imageurl + '">';
		astr +='<param name=swRemote value="swSaveEnabled=\'false\' swVolume=\'false\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'false\' ">';
		astr +='<param name=swStretchStyle value=none>';
		astr +='<PARAM NAME=bgColor VALUE=' + imagebgcolor + '><embed src="' + imageurl + '" bgColor=' + imagebgcolor + '  width=' + imagewidth + ' height=' + imageheight;
		astr +='swRemote="swSaveEnabled=\'false\' swVolume=\'false\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'false\' " swStretchStyle=none';
		astr +='type="application/x-director" pluginspage="http://www.macromedia.com/shockwave/download/"></embed></object>';
		break;
		case ('swf'):
		astr +='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" ';
		astr +='width="'+imagewidth+'" height="'+imageheight+'"><param name="movie" value="'+imageurl+'"><param name="base" value="."><param name="quality" value="high">';
		astr +='<param name="bgcolor" value="'+imagebgcolor+'"><embed src="'+imageurl+'" quality="high" bgcolor="'+imagebgcolor+'" width="'+imagewidth+'" height="'+imageheight+'" ';
		astr +='type="application/x-shockwave-flash" pluginspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" base=".">';
		astr +='</embed></object>';
		break;
		case ('mov'):
		//popUp(title, qtmovieurl, moviewidth, movieheight, bgcolor, [autoplay(bool), loop(bool), controller(bool)]);
		if (vars == undefined) {
			vars = 'false,false,true';
		}
		vars_a = vars.split(",");
		if (vars_a[2] == 'true') {
			//want to display controller so must change window height
			imageheight = parseInt(imageheight) + 15; //add control panel height in pixels
		}
		
		astr +='<embed src="'+imageurl+'" width="'+imagewidth+'" height="'+imageheight+'" autoplay="'+vars_a[0]+'" loop="'+vars_a[1]+'" controller="'+vars_a[2]+'">';
		break;
		default: //it's an image
		astr += '<img src="'+imageurl+'" width="'+imagewidth+'" height="'+imageheight+'" border="0">';
	}//switch
	
	astr +='</body></html>';
	
	
	var upperleft = winpos(imagewidth, imageheight);
	var windowwidth = imagewidth;
	var windowheight = imageheight;
	var sb = 'scrollbars=no';
	
	if (screen.width < imagewidth) {
		var sb = 'scrollbars=yes';
		var windowwidth = screen.width - 60;
	}
	if (screen.height < imageheight) {
		var sb = 'scrollbars=yes';
		var windowheight = screen.height - 60;
	}
	
	aPopup = window.open('','pictureWindow', upperleft + 'toolbar=no,location=no,directories=no,status=no,'+sb+',resizable=no,copyhistory=no,width=' + windowwidth + ',height=' + windowheight);
	aPopup = window.open('','pictureWindow', upperleft + 'toolbar=no,location=no,directories=no,status=no,'+sb+',resizable=no,copyhistory=no,width=' + windowwidth + ',height=' + windowheight);
	var ndoc= aPopup.document;
	
	ndoc.write(astr)
	ndoc.close()
	
	//self.pictureWindow = aPopup
}

function replaceAll (string, replacer, replacee) { 
	var st = string;
	if (replacer.length == 0)
		return st;
	var idx = st.indexOf(replacer);
	while (idx >= 0){
		st = st.substring(0,idx) + replacee + st.substr(idx+replacer.length);
		idx = st.indexOf(replacer);
	}
	return st;
}

function switchFeatureIm(switchTo){
	//switches images on features (maybe some crossfade, if I get fancy) -- switchTo is an index in imageJscript_a
	
	var imDiv = ''; //im(Index)Div
	
	if (imCrossFade){
		if (!imInit){
			//init this thing
			for (i=0;i<imageJscript_a.length;i++){
				imDiv = 'im'+i+'Div';
				var opacity = imageJscript_a[i][4];
				if (ie4) {
					document.getElementById(imDiv).style.filter="alpha(opacity="+opacity+")";
				}else{
					document.getElementById(imDiv).style.MozOpacity = opacity/100;
				}
				//document.getElementById(imDiv).style.display = 'block';
			}
			updateImDivDisplay();
			imInit = true; //thou hast been initialized
		}
		
		if (switchTo != curFeatureIm || (fade_a['in'] != -1 && switchTo != fade_a['in'])){
			fade_a['ondeck'] = switchTo;
			if (servicingFade == false) serviceFade();
		}
		
	}else{
		for (i=0;i<imageJscript_a.length;i++){
			imDiv = 'im'+i+'Div';
			if (i == switchTo){
				document.getElementById(imDiv).style.display = 'block';
			}else{
				document.getElementById(imDiv).style.display = 'none';
			}
		}
	}
}

function updateImDivDisplay(){
	//all images with opacity (imageJscript_a[ind][4]) set to 0 have their div display set to none
	
	for (i=0;i<imageJscript_a.length;i++){
		imDiv = 'im'+i+'Div';
		var opacity = imageJscript_a[i][4];
		if (opacity == 0){
			document.getElementById(imDiv).style.display = 'none';
		}else{
			document.getElementById(imDiv).style.display = 'block';
		}
	}
}

function serviceFade(){
	//crossfades images in fade_a['in'] and fade_a['out']
	
	if (fade_a['in'] != -1 || fade_a['out'] != -1){
		
		//in target is 100, out is 0
		
		servicingFade = true;
		
		if (fade_a['in'] != -1){
			var inDiv = 'im'+fade_a['in']+'Div';
			var inInd = fade_a['in'];
			var endIn = false;
			
			if (linearFade){
				var inDelta = step;
				if (imageJscript_a[inInd][4]+inDelta >= 100) endIn = true;
			}else{
				var inDelta = (100-imageJscript_a[inInd][4])/speed;
				if (Math.abs(inDelta) <= lt) endIn = true;
			}
			
			imageJscript_a[inInd][4] = imageJscript_a[inInd][4] + inDelta;
			
			if (endIn){
				imageJscript_a[inInd][4] = 100;
				curFeatureIm = inInd;
				fade_a['in'] = -1;
			}
			
			if (ie4) document.getElementById(inDiv).style.filter = "alpha(opacity=" + imageJscript_a[inInd][4] + ")";
			else document.getElementById(inDiv).style.MozOpacity = imageJscript_a[inInd][4]/100;
		}
		
		
		if (fade_a['out'] != -1){
			var outDiv = 'im'+fade_a['out']+'Div';
			var outInd = fade_a['out'];
			var endOut = false;
			
			if (linearFade){
				var outDelta = -step;
				if (imageJscript_a[outInd][4]+outDelta <= 0) endOut = true;
				imageJscript_a[outInd][4] = imageJscript_a[outInd][4] + outDelta;
			}else{
				if (fade_a['in'] != -1){
					imageJscript_a[outInd][4] = 100 - imageJscript_a[inInd][4];
				}else{
					endOut = true
				}
			}
			
			if (endOut){
				imageJscript_a[outInd][4] = 0;
				fade_a['out'] = -1;
			}
			
			if (ie4) document.getElementById(outDiv).style.filter = "alpha(opacity=" + imageJscript_a[outInd][4] + ")";
			else document.getElementById(outDiv).style.MozOpacity = imageJscript_a[outInd][4]/100;
		}
		
		setTimeout('serviceFade()', timeout);
		
	}else{
		if (fade_a['ondeck'] != -1){
			fade_a['in'] = fade_a['ondeck'];
			fade_a['out'] = curFeatureIm;
			fade_a['ondeck'] = -1;
			setTimeout('serviceFade()', timeout);
			
			document.getElementById('im'+fade_a['in']+'Div').style.display = 'block';
			document.getElementById('im'+fade_a['out']+'Div').style.display = 'block';
		}else{
			servicingFade = false;
			updateImDivDisplay();
		}
	}
}

