﻿//rollover / rollout functions

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//********************** slideshow functions *************************
//user definitions - define at runtime
var numItems=1;
autoplayinterval = 0;
var jumpOnLongSpans = false;
var itemWidth=338;
var scroller = [];


//set functionality
var iCount=1;
//var btnRt;
//var btnLt;
//var btnPlayPause;
var client;
var descrip;
var myTimeout=0;
var myAutoplayTimeout=0;
var secondaryTimeout = 0;
var turbo;
var isPaused=false;

function onLoad() {
	//btnRt = document.getElementById('scroller_moveright');
	//btnLt = document.getElementById('scroller_moveleft');
	//btnPlayPause = document.getElementById('scroller_btnPlayPause');
	 
	 //add all elements that scroll (passed as arguments to onLoad) to scroller array
    var i,a=onLoad.arguments; 
    for(i=0; i<a.length; i++) {
       scroller[i]=document.getElementById(a[i]);
        if (scroller[i] != null)    scroller[i].style.width=itemWidth*numItems;
    } 
	 	
	//btnRt.style.color='#cccccc';
	
	if (autoplayinterval>0) {
	    myAutoplayTimeout = setInterval("move('left',1);", autoplayinterval);
	}
}

function playpause(elemPlay, elemPause) {
    var ElemPlay = document.getElementById(elemPlay);
    var ElemPause = document.getElementById(elemPause);
    if (ElemPlay.style.display=='none') {
        ElemPlay.style.display='inline';
        ElemPause.style.display='none';
        clearInterval(myAutoplayTimeout);
        isPaused=true;
    } else {
        ElemPlay.style.display='none';
        ElemPause.style.display='inline';
	    myAutoplayTimeout = setInterval("move('left',1);", autoplayinterval);
	    isPaused=false;
    }
}

var lastElement=null;
var lastImage;
var premodCSS_class;
var premodCSS_className;
var lastPos=1;

function thumbnail_click(element, newimage, css, position) {
    //if scrolling, while the user clicks a thumbnail, finish the first scroll then go to the second.  If user clicks a third
    //  thumnail, forget the second and do the third instead (after the first).
    if (myTimeout!=0) {
        if (secondaryTimeout!=0) {
            clearTimeout(secondaryTimeout)
        }
        var func = "thumbnail_click('" + element + "','" + newimage + "','" + css + "'," + position + ")"
        secondaryTimeout = setTimeout(func, 150)
    } else {
        secondaryTimeout = 0;
        
        //restore old attributes
        if (lastElement!=null) {
            lastElement.src = lastImage;
            lastElement.setAttribute("class", premodCSS_class);
            lastElement.setAttribute("className", premodCSS_className);
            //lastElement.setAttribute("onmouseout",'MM_swapImgRestore()');
            lastElement.onmouseout=function () {MM_swapImgRestore();};
        }
        MM_swapImgRestore();
        
        //move to proper item
        if (position != lastPos) {
            if (position > lastPos) { move('left', position-lastPos) } else { move('right', lastPos-position); }
        }
        
        //get information for current item
        var item = document.getElementById(element);
        lastElement = item;
        lastImage = item.src;
        premodCSS_class = item.getAttribute("class");
        premodCSS_className = item.getAttribute("className");
        lastPos = position;
        
        //set information for current item
        item.src = newimage;
        item.setAttribute("class",css);
        item.setAttribute("className",css);
        //item.("onmouseout",'');
        item.onmouseout=function(){};
    }

}

function move(dir,numItemsToMove) {
//clear autoplay and reset at the end of this function, so if someone hits a nav button the autoplay will "start over" on the interval
	if (autoplayinterval>0) {
	    clearInterval(myAutoplayTimeout);
	}

    if (myTimeout==0) {
        if (dir=='right') { 
            if (iCount>1) {
                iCount=iCount-numItemsToMove; 
            } else {
                //we're moving to the right and we're trying to go past the 1st element -- skip to end
                iCount=numItems;
                turbo=true;
            }
        }
        if (dir=='left'){
            if (iCount<numItems) { 
                iCount=iCount + numItemsToMove; 
            } else {
                //we're moving to the left and past the last element -- skip to start 
                iCount=1;   
                turbo=true;
           }
        }
    	    	
    	if (numItemsToMove > 1) {turbo=true;}
        var endPos = (iCount-1) * itemWidth*(-1);
        myTimeout = setInterval("moveElements(" + endPos + ");", 1);
    }

    //reset the autoplay interval
	if ((autoplayinterval>0) && (isPaused==false)) {
	    myAutoplayTimeout = setInterval("move('left',1);", autoplayinterval);
	}
    
}	

var startPos=null;
function moveElements(iEnd)  {
	var curPos = parseInt((scroller[0].style.left).substr(0,(scroller[0].style.left).indexOf('p')));
	if (startPos==null) { startPos=curPos; }       //get the origninal starting position
	if (curPos==iEnd) {
		clearInterval(myTimeout);
		myTimeout=0;
		startPos=null;

	//if we're jumping instead of scrolling the whole list, we really want it to appear to be a short scroll in the opposite direction
	} else if ((jumpOnLongSpans==true) && (turbo==true) & (startPos==0) && (iEnd==(numItems-1) * itemWidth * (-1))) {
		                doMove(iEnd-(itemWidth-8));
		                turbo=false;
		
	//if we're jumping instead of scrolling the whole list, we really want it to appear to be a short scroll in the opposite direction
	} else if ((jumpOnLongSpans==true) && (turbo==true) & (startPos==(numItems-1) * itemWidth * (-1)) && (iEnd==0)) {
		            if (startPos-curPos < (itemWidth-10)) {
		                doMove(curPos-8);
		            } else {
		                doMove(0);
		                turbo=false;
		            }
		
	} else if (curPos < iEnd) {
		if (iEnd-curPos<5) {
		    turbo=false;
			doMove(curPos+1);
		} else if (iEnd-curPos<15) {
		    turbo=false;
			doMove(curPos+2);
		} else if (iEnd-curPos<40) {
		    turbo=false;
			doMove(curPos+3);
		} else if (iEnd-curPos<100) {
			doMove(curPos+6);
		} else {
		    if (turbo==true) {
				doMove(curPos+40);
		    } else {
    			doMove(curPos+8);
    		}    			
		}
	} else if (curPos > iEnd) {
		if (curPos-iEnd<5) {
		    turbo=false;
			doMove(curPos-1);
		} else if (curPos-iEnd<15) {
		    turbo=false;
			doMove(curPos-2);
		} else if (curPos-iEnd<40) {
		    turbo=false;
			doMove(curPos-3);
		} else if (curPos-iEnd<100) {
			doMove(curPos-6);
		} else {
		    if (turbo==true) {
				doMove(curPos-40);
		    } else {
    			doMove(curPos-8);
    		}    			
		}
	}
}	

function doMove(pos) {
	for (var i=0; i<scroller.length; i++) {
        if (scroller[i] != null) scroller[i].style.left=pos+'px';
	}
}



function KillAllAutoSlides() {
    //if a movie is playing, we need to clear all intervals in slideshow
    clearInterval(myTimeout);
    clearInterval(myAutoplayTimeout);
}
