//**	Intro Image Scroller
//**	by OIL, Incorporated
//**	05-2005
//**	J.Vose
//	Creates a panorama of images that slowly scroll across the page.  Intended as an introductory preview of a portfolio of images.
//	Images are sized to fit the height of the scrolling layer.

/*	Updates:
	10-2005		J.Vose		Updated script to scroll continuously, and updated positioning/visible properties, added pause/play button functionality
*/

//	The variable "imgScrollImages" must be defined elsewhere, identifying the images to be used, and their urls.  Defined in the form:
/*
imgScrollImages={
	<imgObjName 01>:{
		url:<url to image>,
		w:<base width of image>,
		height:<base height of image>,
		link:[optional link to another page],
		desc:[optional descriptive text]
		},
	<imgObjName 02>:{
		url:<url to image>,
		w:<base width of image>,
		height:<base height of image>,
		link:[optional link to another page],
		desc:[optional descriptive text]
		}
	...
*/

var resizeWindowOps="";

var imgScrollLays={ImgScrlrCutLay:null,ImgScrlrContentLay:null};
var pausePlayImgs={pauseBttn:null,playBttn:null};
var imgScrollSettings={scrollOntoScreen:false,scrollFrom:null,scrollerWidth:"screen",widthOffset:20,cutLayPosObj:"filmStripAnchor",showSpeed:.2,scroll:true,scrollSpeed:.035,pause:5500,paused:true,spacing:5,maximumScrll:-50000,scrollerVisible:true,visiblePos:null,hiddenPos:null,curImg:null,imgObjs:{}};

//	Builds the scrolling layer, creating the code to display the images.
function buildImageScroller(){
	if(!imgScrollImages&&(typeof imgScrollImages=="object"))return;
	
	var setgs=imgScrollSettings;
	var imgObjs=setgs.imgObjs;
	
	//	Get references to layers in imgScrollLays
	findObj(document,imgScrollLays);
	if(!findObjs_checkFound(imgScrollLays)){return;};
	
	//	Get references to the pause/play buttons, and if found, init the roll over image cache
	findObj(document,pausePlayImgs);
	if(pausePlayImgs.pauseBttn){
		//	create image objects to cache the on state for the buttons
		pausePlayImgs.cache={pause:{on:new Image(),off:new Image()},play:{on:new Image(),off:new Image()}};
		pausePlayImgs.cache.play.off.src="/images/playBttn.gif"
		pausePlayImgs.cache.play.on.src="/images/playBttn_f3.gif";
		pausePlayImgs.cache.pause.off.src="/images/pauseBttn.gif";
		//	Set pause button to on for starters pausePlayImgs.pauseBttn.src playBttn
		pausePlayImgs.pauseBttn.src=pausePlayImgs.cache.pause.on.src="/images/pauseBttn_f3.gif";
		};
	
	//	Get height of scrolling layer, use to set height/width of images
	setgs.scrlrHeight=getHeight(imgScrollLays.ImgScrlrCutLay)-10;
	
	//	Get images and build image code, write to scrolling layer
	var imgScrlrCode="";var imgId=0;var newWidth;var desc;var newHeight=setgs.scrlrHeight;
	for(imgObj in imgScrollImages){
		im=imgScrollImages[imgObj];
		//	image data must be object, and have a url, width, and height
		if(typeof im!="object")continue;
		if(!im.url||!im.w||!im.h)continue;
		//	Calculate scaled height so image will fit into height of scrolling layer
		newWidth=(im.w/im.h)*newHeight;
		//	If description is not defined, use empty string
		if(im.desc)desc=img.desc;
		else desc="";
		//	Build code for this image layer
		imgScrlrCode+=imgScrlStartCode+"imgScrlrImgLay"+imgId+imgScrlMid0Code+newHeight+imgScrlMid1Code+im.url+
					imgScrlMid2Code+"imgScrlImg"+imgId+imgScrlMid3Code+newHeight+imgScrlMid4Code+newWidth+imgScrlMid5Code+desc+imgScrlEndCode;
		//	Create object to track images
		imgObjs["im"+imgId]={url:im.url,link:im.link,id:imgId+"",img:"imgScrlImg"+imgId,layer:"imgScrlrImgLay"+imgId};
		//im.id=imgId+"";
		//im.img="imgScrlImg"+imgId;
		//im.layer="imgScrlrImgLay"+imgId;
		imgId++;
		}
	
	writeToLayer(imgScrollLays.ImgScrlrContentLay,imgScrlrCode);
	
	//	Add function to resize window operations string, will be handled by handleResize() function
	resizeWindowOps+="initImageScroller();";
	
	setTimeout("initImageScroller();",1500);
	};


//	Initializes the image scrolling layer sizing images 
//	and adding them to the layer, starts scrolling
function initImageScroller(){
	if(!imgScrollImages||(typeof imgScrollImages!="object"))return;
	if(!imgScrollLays.ImgScrlrCutLay)return;
	var setgs=imgScrollSettings;
	var imgObjs=setgs.imgObjs;
	var offset=0;
	var scrollerWidth=setgs.scrollerWidth;
	
	var cutLay=imgScrollLays.ImgScrlrCutLay;
	
	if(setgs.cutLayPosObj){
		if(typeof setgs.cutLayPosObj=="string")setgs.cutLayPosObj=findObj(document,setgs.cutLayPosObj);
		if(findObjs_checkFound(setgs.cutLayPosObj)){
			offset=getAbsLeft(setgs.cutLayPosObj);
			setLeft(cutLay,offset);
			setTop(cutLay,getAbsTop(setgs.cutLayPosObj));
			}
		else{alert("Can't find positioning object ");}
		}
	else alert("no setgs.cutLayPosObj");
	
	//	Calculate the visible size
	if(scrollerWidth=="screen"){
		if(setgs.widthOffset)offset=offset+setgs.widthOffset;
		scrollerWidth=getScreenWidth();
		}
	if(!isNaN(parseInt(scrollerWidth))){
		//alert("setting width of " + cutLay + " to " + (parseInt(setgs.scrollerWidth)-offset));
		setWidth(cutLay,parseInt(scrollerWidth)-offset-17);
		}
	
	setgs.visiblePos=[getLeft(cutLay),getTop(cutLay)];
	
	//	If the scrollIn variable is true, calculate the hidden position of the image scroller
	if(setgs.scrollOntoScreen){
		
		//	Set hidden position depending on which side of page images should scroll in from
		switch(setgs.scrollFrom){
			case "top":
				setgs.hiddenPos=[getLeft(cutLay),(-1*getHeight(cutLay))];break;
			case "bottom":
				setgs.hiddenPos=[getLeft(cutLay),getScreenHeight()];break;
			case "left":
				setgs.hiddenPos=[-1*(getWidth(cutLay)),getTop(cutLay)];break;
			case "right":
				setgs.hiddenPos=[getScreenWidth(),getTop(cutLay)];break;
				}
		}
	
	//	Move into position depending on whether scroller is visible or hidden to start
	if(setgs.scrollerVisible){
		setLeft(cutLay,setgs.visiblePos[0]);
		setTop(cutLay,setgs.visiblePos[1]);
		}
	else{
		setLeft(cutLay,setgs.hiddenPos[0]);
		setTop(cutLay,setgs.hiddenPos[1]);
		}
	
	//	Step through images in image scroller, get sizes, and position to start
	var curImgPos=0;var firstImg=true;
	for(imgObj in imgObjs){
		im=imgObjs[imgObj];
		if(typeof im!="object"){alert("image not object " + imgObj);continue;}
		if(!im.id){alert("Can't find id for image " + imgObj);continue;}
		if(firstImg){setgs.firstImg=im.id;firstImg=false;}
		if(typeof im.img=="string")im.img=findObj(document,im.img);
		if(typeof im.layer=="string")im.layer=findObj(document,im.layer);
		if(!findObjs_checkFound(im.img)||!findObjs_checkFound(im.layer)){alert("Can't find image " + im.id);continue;}
		im.width=getWidth(im.img);
		//	If no width, image not found hide image and continue
		if(im.width==0){hideLayer(im.layer);continue;};
		im.pos=curImgPos;
		setLeft(im.layer,curImgPos);
		curImgPos+=im.width+setgs.spacing;
		setgs.lastImg=im.id;
		}
	
	setgs.numbImgs=setgs.lastImg;
	
	showIntroImageScroller();
	};

function hideIntroImageScroller(){
	imgScrollSettings.scrollerStopped=true;
	stopIntroImageScroller();
	animateLayerInALine(imgScrollLays.ImgScrlrCutLay, imgScrollSettings.hiddenPos, imgScrollSettings.showSpeed, finishHidingImageScroller);
	};
function finishHidingImageScroller(){
	imgScrollSettings.scrollerVisible=false;
	};
function showIntroImageScroller(){
	imgScrollSettings.scrollerStopped=false;
	showLayer(imgScrollLays.ImgScrlrCutLay);
	if(!imgScrollSettings.scrollerVisible)animateLayerInALine(imgScrollLays.ImgScrlrCutLay, imgScrollSettings.visiblePos, imgScrollSettings.showSpeed,finishShowingImageScroller);
	else finishShowingImageScroller();
	//setTimeout("startIntroImageScroller()",imgScrollSettings.pause);
	};
//	Creates slight pause between scrolling intro 
//	image onto screen, and starting to scroll images
function finishShowingImageScroller(){
	imgScrollSettings.scrollerVisible=true;
	setTimeout("startIntroImageScroller()",imgScrollSettings.pause);
	}

//	Called to start scrolling, including after pause or stop.
function startIntroImageScroller(){
	if(imgScrollSettings.scrollerStopped)return;
	if(!imgScrollSettings.scroll)return;
	var setgs=imgScrollSettings;
	var imgObjs=setgs.imgObjs;
	
	if(!setgs.curImg){
		// start scrolling
		setgs.curImg=setgs.firstImg;
		}
	else if(!setgs.curStopPos){
		//	paused on image, set stop position to next image, if past 
		//	total number of images, reset to start at first image obj
		setgs.curImg=(parseInt(setgs.curImg)+1)+"";
		if(setgs.curImg>setgs.numbImgs)setgs.curImg=setgs.firstImg;
		}
	//	Otherwise scrolling was stopped, so just start again at current position
	setgs.curStopPos=-1*imgObjs["im"+setgs.curImg].pos-imgObjs["im"+setgs.curImg].width;
	//if(setgs.curStopPos<setgs.maximumScrll)setgs.curStopPos=setgs.maximumScrll;
	
	//	Set pause/play buttons if they exist
	if(pausePlayImgs.cache){
		pausePlayImgs.playBttn.src=pausePlayImgs.cache.play.on.src;
		pausePlayImgs.pauseBttn.src=pausePlayImgs.cache.pause.off.src;
	}
	setgs.paused=false;
	
	animateLayerInALine(imgScrollLays.ImgScrlrContentLay, [setgs.curStopPos,0], setgs.scrollSpeed, pauseIntroImageScroller);
	};

//	Function called by animation routine when 
//	image scroller has scrolled to next image
function pauseIntroImageScroller(){
	if(imgScrollSettings.scrollerStopped || imgScrollSettings.paused)return;
	if(!imgScrollSettings.scroll)return;
	var setgs=imgScrollSettings;
	
	if(setgs.curStopPos<=setgs.maximumScrll){resetPositions();};//setgs.scroll=false;
	
	setgs.curStopPos=null;
	
	//	Cycle image (Move image that has scrolled off screen to last image on right side)
	imgToMove=setgs.imgObjs["im"+setgs.curImg];
	imgToMove.pos=setgs.imgObjs["im"+setgs.lastImg].pos+setgs.imgObjs["im"+setgs.lastImg].width+3;
	setLeft(imgToMove.layer,imgToMove.pos);
	setgs.lastImg=setgs.curImg;
	
	pauseMovement();
	
	if(setgs.pause>0) setTimeout("startIntroImageScroller()",setgs.pause);
	else startIntroImageScroller();
	};

function pauseMovement(){
	if(imgScrollSettings.paused)return;
	
	animateLayerInALine_stop(imgScrollLays.ImgScrlrContentLay);
	
	//	Set pause/play buttons if they exist
	if(pausePlayImgs.cache){
		pausePlayImgs.playBttn.src=pausePlayImgs.cache.play.off.src;
		pausePlayImgs.pauseBttn.src=pausePlayImgs.cache.pause.on.src;
	}
	imgScrollSettings.paused=true;
	};
function startMovement(){
	if(!imgScrollSettings.paused)return;
	startIntroImageScroller();
	}

//	Called to top the scroller, called when it is hidden.
function stopIntroImageScroller(){
	if(!imgScrollSettings.scroll)return;
	
	animateLayerInALine_stop(imgScrollLays.ImgScrlrContentLay);
//	clearInterval(animateIntevalObj);
	};

//	Indicates whether the film strip has been paused, used
//	by pause/play buttons
function isFilmPaused(){
	return imgScrollSettings.paused;
	// stopBttn playBttn
	};

//	Called during pause when image container has scrolled past maximum scroll distance,
//	resets position of container to 0 and moves all images their relative distance
//	to maintain the current appearance.
function resetPositions(){
	var setgs=imgScrollSettings;
	var imgObjs=setgs.imgObjs;
	var leftPos;
	//	move image container layer back to zero, save position
	var stepBack=getLeft(imgScrollLays.ImgScrlrContentLay);
	hideLayer(imgScrollLays.ImgScrlrContentLay);
	//	Step through images and subtract the left position
	//	that the container layer had moved to from the 
	//	current position, to maintain relative positions
	for(imgObj in imgObjs){
		im=imgObjs[imgObj];
		leftPos=getLeft(im.layer);
		setLeft(im.layer,leftPos+stepBack);
		im.pos=leftPos+stepBack;
		}
	setLeft(imgScrollLays.ImgScrlrContentLay,0);
	showLayer(imgScrollLays.ImgScrlrContentLay);
	};

var imgScrlStartCode="<DIV ID=\"";
var imgScrlMid0Code="\" STYLE=\"position:absolute; visibility:inherit; overflow:visible; height:";
var imgScrlMid1Code="; left:0; top:0px; z-index:90;\"><p><img src=\"";
var imgScrlMid2Code="\" id=\"";
var imgScrlMid3Code="\" alt=\"\" height=\"";
var imgScrlMid4Code="\" width=\"";
var imgScrlMid5Code="\" border=\"0\"><BR><span style=\"font-size:10px;\">";
var imgScrlEndCode="</span></p></DIV>";


//	Function to handle resize, reloading and re-intializing 
//	layers once resizing is finished for a set period of time
var waitingOnResizing=null;
//	specify string that will be run if window is resized, functions may be added to this string
//	by other scripts to enable their own resize operations to happen
var resizeWindowOps="";
//	Starts a wait to reload the page, if resize happens during the wait, first wait is replaced with a 
//	new wait, only once a resize event doesn't fire during the wait does the browser actually reload
function handleResize(){
	var rWindowOps=resizeWindowOps;
	var resizeWait=200;
	//	If MacIE, reload document on resize
	waitingOnResizing=setTimeout(rWindowOps,resizeWait);
	};
