
var thisImg = 0;
var millisec = 100;
var playFlag = 0;
var movieFlag = 0;
var loopFlag = 0;
var frameFlag = 0;
pauseID = 0;


var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var tzst = ['AST','EST','CST','MST','PST','AKST'];
var tzdt = ['ADT','EDT','CDT','MDT','PDT','AKDT'];

/**
 * pad 2 digit number specified in num with leading 0 as needed
 */
function timePadZero(num) {
	return num > 9 ? num : '0' + num;
}

/**
  * Retrieve last modify time of file specified in url in background, convert to utc and local date, and place in innerHtml of
  * element specified by id
  */
function getModDate(id,url) {
	var request = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
  	request.open("GET", url, true);
	request.onreadystatechange = function(){
		if (request.readyState == 4 && request.status == 200) {
	
			var lastModStr = request.getResponseHeader('Last-Modified');
			var lastModDate = new Date(lastModStr);
			var timeStr = getUtcLocalDate(lastModDate);
			var doc = document.getElementById(id);
			
			if (doc) {
				doc.innerHTML = timeStr;
			}
					
		}
	}
  	request.send(null);

}

/**
 * Format date as utc and local string
 * given a date object as a parameter 
 */
function getUtcLocalDate(date) {
	var utc = date.getUTCFullYear()+ ' ' + months[date.getUTCMonth()] + ' '+date.getUTCDate()+' '+timePadZero(date.getUTCHours())+':'+timePadZero(date.getUTCMinutes());
	var clientDate = new Date();
	var date1 = new Date(clientDate.getFullYear(), 1, 0, 0, 0, 0);
	var date2 = new Date(clientDate.getFullYear(), 7, 0, 0, 0, 0);			
	var date3 = new Date(clientDate.getFullYear(), clientDate.getMonth(),clientDate.getDate(),0,0,0);
	var haveDst = date1.getUTCHours() - date2.getUTCHours();
	var onDst = date1.getUTCHours() - date3.getUTCHours();
	var tzOffset = clientDate.getTimezoneOffset()/60;
	var tz = '';
	if ((tzOffset > 2) && (tzOffset < 10)) {

		if ((onDst == 0) && (haveDst == 1)){
			tz = tzst[tzOffset - 4];
		} else {
			tz = tzdt[tzOffset - 3];

		}
	}
	
	var local = date.getFullYear()+ ' ' + months[date.getMonth()] + ' '+date.getDate()+' '+timePadZero(date.getHours())+':'+timePadZero(date.getMinutes()) + ' ' + tz;
	var timeStr = utc + ' UTC ('+local+')';	
	return timeStr;
}
function update(){

  if (! playFlag) { aceImg = imageLoading(); }
  thisImg++;
  if (! movieFlag && ! frameFlag) { thisImg = 0; }
  
  if(thisImg == imgCnt) {
     thisImg = -1;
     if (playFlag) clearTimeout(timerID);
  } else {
  		document.images['swimg'].src = aceImg[thisImg].src;
     	timerID = setTimeout('update()', millisec);
  }
  movieFlag = 1;
}

function endUpdate(){

  if (movieFlag) clearTimeout(timerID); 
  if (loopFlag && pauseID != 0) clearTimeout(pauseID);
  loopFlag = 0;
}

function frameAdvance(){

  if (! playFlag) { aceImg = imageLoading(); } 
  thisImg++; 
  if(thisImg == imgCnt) {
      thisImg = 0;
      if (movieFlag) clearTimeout(timerID);
  }

  document.images['swimg'].src = aceImg[thisImg].src;

  
  frameFlag = 1;
}

function frameBack(){

  if (! playFlag) { aceImg = imageLoading(); }
  thisImg--;
  if(thisImg < -1) {
      thisImg = imgCnt-2;
      if (movieFlag) clearTimeout(timerID)
      } else if (thisImg < 0) {
      	thisImg = imgCnt-1;
      if (movieFlag) clearTimeout(timerID);
  } 
  
  document.images['swimg'].src = aceImg[thisImg].src;
  frameFlag = 1;
}

function Loop(){

  if (! playFlag) { aceImg = imageLoading(); } 
  thisImg++;
  if(thisImg == imgCnt) {
      thisImg = -1;
      pauseID = setTimeout('Loop()',1000);
      return; 
  }
  
  document.images['swimg'].src = aceImg[thisImg].src;
  timerID = setTimeout('Loop()',millisec);
  movieFlag = 1;
  loopFlag = 1;
}

function setSpeed(myform){
  idx = document.myform.speed.selectedIndex;
  millisec = document.myform.speed.options[idx].value; 
}

function checkLoop(){
  (loopFlag) ? 'return' : Loop(); 
}     
