var elThumbsContainer = null;
var elThumbsArrowLeft = null;
var elThumbsArrowRight = null;
var elLargeImageSource = null;
var elGalleryCaption = null;
var elGalleryDescLong = null;
//var largeImageLocation = null;
var scrollTimerID = null;
var scrollDirection = null;
var toSubtract = 483;
var thumbWidth = 86;

// autoplay and audio vars
var playing = false;
var done = false;
var index = 0; // start @ first feature

var imgTimerID = 0; // for the mouseover and mouseout switch image

var playGalleryTimerID = null;

var pic = Array();

function init()
{
  if(!elThumbsContainer) 
  { 
    elThumbsContainer = document.getElementById("thumbsContainer"); 
    elThumbsArrowLeft = document.getElementById("arrow_left"); 
    elThumbsArrowRight = document.getElementById("arrow_right"); 
    elLargeImageSource =  document.getElementById("largeImageSource");
    elGalleryCaption =  document.getElementById("galleryCaption");
    elGalleryDescLong =  document.getElementById("galleryDescLong");
    elGalleryPopupList =  document.getElementById("featuresPopup");
    elGalleryPopupListLink =  document.getElementById("featureListLink");
  }
}

function scroll()
{
  init();
  if(scrollDirection == "right")
  {
    elThumbsContainer.scrollLeft += 5;
  } else{
    elThumbsContainer.scrollLeft -= 5;
  }
  checkArrows();
  scrollTimerID = setTimeout("scroll()", 10);
}

function checkArrows()
{
  elThumbsArrowLeft.style.visibility = (elThumbsContainer.scrollLeft > 0) ? "visible" : "hidden";
  elThumbsArrowRight.style.visibility = (elThumbsContainer.scrollLeft < elThumbsContainer.scrollWidth - elThumbsContainer.clientWidth) ? "visible" : "hidden";
}

function scrollStart(direction)
{
  scrollDirection = direction;
  scrollTimerID = setTimeout("scroll()", 10);
}

function scrollStop()
{
  clearTimeout(scrollTimerID);
}

function parentOffset(obj) {
  var x = obj.offsetLeft;
  while (obj = obj.offsetParent) { x += obj.offsetLeft; }
  return x
} 
// Moves the Thumbs if one is selected outside of the container
function moveThumbs()
{
  //var thumbLocation = parentOffset(document.getElementById("thumb_"+curImage)) - toSubtract;  
  var thumbLocation = parentOffset(document.getElementById("thumb_"+FID)) - toSubtract;
  var maxScrollWidth = elThumbsContainer.offsetWidth + elThumbsContainer.scrollLeft - thumbWidth;
  if(thumbLocation > maxScrollWidth) { elThumbsContainer.scrollLeft = thumbLocation - elThumbsContainer.offsetWidth + (thumbWidth +(thumbWidth/2)); }
  if(thumbLocation < elThumbsContainer.scrollLeft) { elThumbsContainer.scrollLeft = thumbLocation - (thumbWidth/2); }
  checkArrows();
//  debug("<br />thumbLocation=" + thumbLocation + "<br />maxScrollWidth=" + maxScrollWidth + "<br />"+document.getElementById("thumb_"+FID).offsetWidth);
}

// The next two functions are no longer used - JM 2007-04-06
var loadLongDesc = function(oXML)
{
  elGalleryDescLong.innerHTML = oXML.responseText;
}
  
getLongDesc = function()
{
  var get = "";
  //get += "model=" + modelName;
  //get += "modelSection="+modelSection;
  //get += "&modelYear="+modelYear;
  get += "image="+curImage;
  get += "&fid="+FID;
  HTTPRequestConn.connect("/20"+modelYear+"/ajax.gallery.php", "GET", get, loadLongDesc);
}

function switchTheImage(image, fid)
{
	doPassVar('slideshow', 'stop'); // ask skip-to-image actions such as clicking thumb, link or popup link will cause slideshow to stop.	
	clearTimeout(playGalleryTimerID);
    showTheImage(image, fid);
}

function showTheImage(image, fid)
{
  init();
  //document.getElementById("thumb_"+curImage).className = "";
  //document.getElementById("desc_"+curImage).className = "";
  //document.getElementById("desc_popup_"+curImage).className = "";
  document.getElementById("thumb_"+FID).className = "";
  document.getElementById("desc_"+FID).className = "";
  document.getElementById("desc_popup_"+FID).className = "";
  curImage = image;
  FID = fid;  
  //document.getElementById("thumb_"+curImage).className = "cur";
  //document.getElementById("desc_"+curImage).className = "cur";
  //document.getElementById("desc_popup_"+curImage).className = "cur";
  document.getElementById("thumb_"+FID).className = "cur";
  document.getElementById("desc_"+FID).className = "cur";
  document.getElementById("desc_popup_"+FID).className = "cur";
  elGalleryPopupList.className = ""; 
  elGalleryPopupListLink.className = ""; 
  //elGalleryCaption.innerHTML = document.getElementById("desc_"+curImage).innerHTML;
  elGalleryCaption.innerHTML = document.getElementById("desc_"+FID).innerHTML;
  moveThumbs();
  
  // get current index
  //index = getIndexFromImageName(image);
  index = getIndexFromFID(FID);

  elLargeImageSource.src = elLargeImageSource.src.replace(/\/sr_[0-9]+\//, "/sr_"+image_year[index]+"/").replace(/\/[^\/]+$/, "/"+curImage);	
  //getLongDesc();
  elGalleryDescLong.innerHTML = gallery_desc[index];
  // use this to play audio headers as well as audio descriptions.
  // Flash will detect if audio_header is set, and play that first if it is.
  // doPassVar('audio_header', audio_header_name[index]); // send audio info to flash
  if ( audio == "on" ) {
    doPassVar('audioYearShort', audio_year[index]); // send audio info to flash
    doPassVar('audio', audio_name[index]); // send audio info to flash
    doPassVar('audioStatus', 'play');
  } else {
    doPassVar('audioStatus', 'stop');
    slideshow = doGetVar('slideshow');
    if ( slideshow == 'play' ) {
      playGalleryTimerID = setTimeout('playGallery()', 10000);
    }
  }
  //imagePreload(index);
}

function swapTheImage() {
  if ( elLargeImageSource.src.indexOf("/views/") > -1 ) {
    elLargeImageSource.src = elLargeImageSource.src.replace(/\/views\//, "/lite/");
    elLargeImageSource.className = "gallerySmallImage";
    SetCookie("UserPrefBandwidth", "low", 365);
  } else {
    elLargeImageSource.src = elLargeImageSource.src.replace(/\/lite\//, "/views/");
    elLargeImageSource.className = "";
    SetCookie("UserPrefBandwidth", "high", 365);
  }
}

function switchTheImagePopup(image, fid)
{
	togglePopup();
	switchTheImage(image, fid);
}

function togglePopup()
{
  init();
  //If there's a classname, then close it by removing the class | if there's no class, then we want to open it
  elGalleryPopupList.className = elGalleryPopupList.className ? "" : "open"; 
  elGalleryPopupListLink.className = elGalleryPopupListLink.className ? "" : "open"; 
}

function debug(str)
{
  var text = "";
  text += "Scrollwidth = " +elThumbsContainer.scrollWidth + "<br>";
  text += "Scrollwidth = " +elThumbsContainer.scrollWidth + "<br>";
  //document.getElementById("thumb_"+curImage).offsetLeft
  text += "elThumbsContainer.offsetWidth = " +elThumbsContainer.offsetWidth + "<br>";
  text += "SrcollLeft = " + elThumbsContainer.scrollLeft + "<br>";
  text += str + "<br>";
  
  document.getElementById("debug").innerHTML = text;
}

// == auto play functions ==
function playGallery()
{
	if ( playing ) { // dont start counting if we haven't started playing
		index += 1;
	}
	
	if ( index >= image_name.length ) {
		done = true;
	}
			
	if( done && next_page) { location.href = next_page; }
	playing = true;
			
	showTheImage(image_name[index], feature_id[index]);
	//showImage(image_name[index]);
}
window.onload = playGallery;


/*function getIndexFromImageName(image)
{	
	for(i=0;i<image_name.length;i++){
		if ( image_name[i] == image ) {
			index = i;
			return index;
		}
	}
	return 0;
}*/


function getIndexFromFID(fid)
{	
	for(i=0;i<feature_id.length;i++){
		if ( feature_id[i] == fid ) {
			index = i;
			return index;
		}
	}
	return 0;
}

function switchImageStart(img, fid)
{
  var curImg = img;
  var curFID = fid;  
  //imgTimerID = setTimeout("switchTheImage('"+curImg+"', '"+curFID+"')", 1800);
}

function switchImageStop()
{
  clearTimeout(imgTimerID);
}

//Super Flash Variable Passer
function doPassVar(varName, varValue){
	var flashMovie = getFlashMovieObject("controller")
	flashMovie.SetVariable(varName, varValue);
}

function doGetVar(varName) {
  var flashMovie = getFlashMovieObject("controller");
  return flashMovie.GetVariable(varName);
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
  	return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function setAudioStatus(status) {
  index = getIndexFromFID(FID);
  image = image_name[index];
  if ( status == "play" ) {
    audio = "on";
    //document.getElementById('toggle_audio').innerHTML = "Audio: On";
    SetCookie("UserPrefAudio", "on", 365);
    showTheImage(image, FID);
  } else {
    audio = "off";
    //document.getElementById('toggle_audio').innerHTML = "Audio: Off";
    SetCookie("UserPrefAudio", "off", 365);
  }
  clearTimeout(playGalleryTimerID);
}

function imagePreload(index) {
  for (i=index+1; i<image_name.length; i++) {
    pic[i] = new Image();
    pic[i].src = "http://"+location.hostname+"/_m/sr_"+image_year[i]+"/gallery/images/views/"+image_name[i];
  }
}