/*
	Parse-time requirements:
	- commons.js
	Page-load-time requirements:
	- variables set: which, picsFldr, thumbsFldr, pageFileSuffix, slides.
	Call-time requirements:
	- slideshow.js
*/

// config variables
var slideshowDelay = 2000; // in milliseconds

// declare global variables
var currentSlideIndex;
var picLink, abovePicLink, pic, capText, capLink, slideshowStarter,
	thumbnailScroller, prevArrow, nextArrow; // element references
var slideshowInterval; // from setInterval()

// Slide class constructor function
function IslandSlide(name, location, imgbase, filebase, size, price, which) {
	this.name = name;
	this.location = location;
	this.filebase = filebase;
	this.imgbase = imgbase;
	this.size = size;
	this.price = price;
    this.which = which;
    
    this.preload = function() {
        this.imgObj = new Image();
        this.imgObj.src = picFldrs[this.which] + this.imgbase + '-1.jpg';
    }
}

function changeSlideTo(slideIndex) {
	var newSlide = slides[slideIndex];
    
    // only do certain things if this isn't the very first slide loaded...
    if (currentSlideIndex != undefined) {
        if (newSlide == slides[currentSlideIndex]) {
            // if this is just a mouseover on the same slide as is loaded currently,
            // do nothing more.
            return;
        } else {
            // otherwise, unhighlight the current slide's thumb (the next one
            // will be highlighted below, regardless of whether a slide
            // is currently loaded).
            setThumbHighlighted(currentSlideIndex, false);
        }
    }
    
    // alert('current style height: '+pic.style.height);
    
    // take off any style to squish height (might be added again later)
    pic.style.height = '';
	
	// write new info/image src
	pic.obj.src = picFldrs[newSlide.which] + newSlide.imgbase + '-1.jpg';
    
    // try to squish the picture if it's too tall (over 225px)
    if (pic.obj.height > 225) {
        // alert('squish.')
        pic.style.height = '225px';
    }
    
	picLink.obj.href =
			abovePicLink.obj.href =
			capLink.obj.href = baseUrl + newSlide.filebase + '.' + pageFileSuffix;
	capLink.obj.innerHTML = newSlide.name+' - '+newSlide.location;
	capText.obj.innerHTML = newSlide.size+' - '+newSlide.price;
	// capLink.obj.innerHTML = pic.obj.height + ' - ' + pic.style.height;

	// highlight the new slide's thumb
	setThumbHighlighted(slideIndex, true);
	
	currentSlideIndex = slideIndex;
	updateArrowsVis();
}

function nextSlide() {
	if (!atLastSlide()) {
		changeSlideTo(currentSlideIndex + 1);
	} else {
		// if at the last slide, loop back to the first slide.
		changeSlideTo(0);
	}
}
function prevSlide() {
	if (!atFirstSlide()) {
		changeSlideTo(currentSlideIndex - 1);
	} else {
        // if at the first slide, loop to the last.
        changeSlideto(slides.length-1);
    }
}

function startSlideshow() {
	if (!slideshowInterval) { // only set new interval if none currently operating.
		// make the first advance immediately.
        nextSlide();
		slideshowInterval = setInterval(nextSlide, slideshowDelay);
		// change to the blinking 'pause' image.
		// slideshowStarter.style.backgroundImage = 'url("images/slideshow/'+which+'/pause.gif")';
		slideshowStarter.obj.className = addClassNameTo(slideshowStarter.obj.className, 'playing');
	} else {
		// if already running, clicking the button stops it.
		stopSlideshow();
	}
}
function stopSlideshow() {
	if (slideshowInterval) {
		clearInterval(slideshowInterval);
		slideshowInterval = undefined;
		// change back from the blinking 'pause' image.
		// slideshowStarter.style.backgroundImage = '';
		slideshowStarter.obj.className = removeClassNameFrom(slideshowStarter.obj.className, 'playing');
	}
}

// returns true iff current slide is the last
function atLastSlide() {
	return currentSlideIndex == (slides.length-1);
}
// returns true iff current slide is the first
function atFirstSlide() {
	return currentSlideIndex == 0;
}

// hides/shows arrows appropriately if at the last/first slide.
// called by each changeSlideTo().
function updateArrowsVis() {
	if (atFirstSlide()) {
		prevArrow.style.visibility = 'hidden';
	} else {
		prevArrow.style.visibility = 'visible';
	}
	if (atLastSlide()) {
		nextArrow.style.visibility = 'hidden';
	} else {
		nextArrow.style.visibility = 'visible';
	}
}

// (un)highlights a thumbnail, given a boolean argument.
function setThumbHighlighted(thumbIndex, highlighted) {
	var thumbEl = new getObj('thumbnail_'+thumbIndex);
	// thumbEl.style.border = (highlighted ? '1px solid red' : '');
	// var oldClassName = new String(thumbEl.obj.className);
	if (highlighted) {
		thumbEl.obj.className = addClassNameTo(thumbEl.obj.className, 'highlighted');
		// alert(oldClassName + ' becomes ' + thumbEl.obj.className);
	} else {
		thumbEl.obj.className = removeClassNameFrom(thumbEl.obj.className, 'highlighted');
		// alert(oldClassName + ' becomes ' + thumbEl.obj.className);
	}
}

function picClick() {
	stopSlideshow();
	mainWinGo(picLink.obj.href, false); // false = keep popup open
    return false; // this is assigned to be the onclick function for the picture
                  // and its caption text, so it must return false so that the
                  // HREF url doesn't load into this window.
}

// function swapToMapWin()

function multishowInit() {
    var i;
    
	// assign element references
	picLink = new getObj('picLink');
	pic = new getObj('pic');
	abovePicLink = new getObj('abovePicLink');
	capText = new getObj('capText');
	capLink = new getObj('capLink');
	slideshowStarter = new getObj('slideshowStarter');
	thumbnailScroller = new getObj('thumbnailScroller');
	prevArrow = new getObj('prevArrow');
	nextArrow = new getObj('nextArrow');
	
	// assign click actions that open back in the main window
	picLink.obj.onclick =
				abovePicLink.obj.onclick =
				capLink.obj.onclick = picClick;
	// assign caption click action
	capLink.obj.onclick = picClick;
	
	// write thumbnails
	var thumbnailsHtml = '';
	for (i = 0; i < slides.length; i++) {
		thumbnailsHtml += '<a href="'+baseUrl+slides[i].filebase+'.'+pageFileSuffix+'" onclick="return picClick();" id="thumbnail_'+i+'" onmouseover="changeSlideTo('+i+');stopSlideshow();"><img src="'+thumbFldrs[slides[i].which]+slides[i].imgbase+'.jpg" alt="thumbnail" /></a>\n';
	}
	thumbnailScroller.obj.innerHTML = thumbnailsHtml;
    
    // preload the slide for mouseover smoothness
    for (i = 0; i < slides.length; i++) {
        slides[i].preload();
    }

	// reset current slide index & load the first slide
    setTimeout("changeSlideTo(0);", 200);
}

addLoadEvent(multishowInit);

// =============================================================================

/*** element className management functions ***/
function addClassNameTo(classNameStr, newClassName) {
	// alert(typeof(classNameStr)+ ': ' + classNameStr + ' becomes ' + typeof(newClassName) + ': ' + newClassName);
	var names = (classNameStr) ? classNameStr.split(/ /) : new Array();
	names[names.length] = newClassName;
	return names.join(' ');
}
function removeClassNameFrom(classNameStr, classNameToRemove) {
	// alert(typeof(classNameStr)+ ': ' + classNameStr);
	if (classNameStr) {
		var names = classNameStr.split(/ /);
		for (var i=0; i<names.length; i++) {
			if (names[i] == classNameToRemove) {
				names.splice(i, 1);
			}
		}
		return names.join(' ');
	} else {
		return '';
	}
}
