﻿//********************** slideshow functions *************************
//user definitions - define at runtime
var numItems=1;
autoplayinterval = 0;
var jumpOnLongSpans = false;

//user definitions - define at design time;
var itemWidth=339;
var scroller = [];
function setScrollArray() {     //this needs to be set with all the elements that will scroll
    scroller[0]=document.getElementById('scroller');
    scroller[1]=document.getElementById('client');
    scroller[2]=document.getElementById('descrip');
}



//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');
	setScrollArray();
	
	//btnRt.style.color='#cccccc';
	for (var i=0; i<scroller.length; i++) {
	    scroller[i].style.width=itemWidth*numItems;
	}
	
	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))) {
		            if (curPos-startPos < (itemWidth-100)) {
		                doMove(curPos+8);
		            } else {
		                doMove(iEnd-(itemWidth-100));
		                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-100)) {
		                doMove(curPos-8)
		            } else {
		                doMove(iEnd+(itemWidth-100));
		                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++) {
	    scroller[i].style.left=pos+'px';
	}
}
