
var s_SemPicsFolder="/planner/seminars/photos/";
var s_SemLinksRoot="/planner/?pg=";
var sa_SemColors=[""], sa_SemLinks=[""], sa_SemCaptions=[""];
var sa_SemPics=[""], sa_SemPics_Loaded=[""]; // _Loaded array holdis list of photos that have 'finished downloading'


var pi_defaultSlideshowInterval=5; // seconds
var pi_SlideshowInterval;
var pi_timerSeminarSlideshow;
var pi_lastSlideshowNum=1; // init


function startSemSlideshow (interval) {
	if (!pi_SlideshowInterval || interval>0) {
		if (interval>0 && interval<100) {pi_SlideshowInterval=interval;}
		else {pi_SlideshowInterval=pi_defaultSlideshowInterval;}
		// add extra time for non-IE browsers because will use a 'slow fade' transition 
		//if (!$$("SeminarImg").filters) pi_SlideshowInterval+=2; 
	}
	stopSemSlideshow();
	pi_timerSeminarSlideshow=setInterval ("showSemPic()", pi_SlideshowInterval*1000 );
}

function stopSemSlideshow () {
	if (pi_timerSeminarSlideshow) {
		clearInterval (pi_timerSeminarSlideshow);
		pi_timerSeminarSlideshow=null;
	}
}

function showSemPic (num) {
	if (sa_SemPics_Loaded.length < 2) return; // no additional pictures loaded yet - SKIP
	var effect="Wheel", interval=0.35; // 'slideshow' transition
	if (num>0) { // user-initiated change
		if (num >=sa_SemPics_Loaded.length) return; // requested picture is not loaded yet - SKIP
		if (!pi_timerSeminarSlideshow) return; // do NOT restart slideshow if it is currently 'stopped'
		if ($$("SeminarImg").filters) {effect="Circle_Out"; interval=0.5;} // IE effect
		else {effect="";} // skip transition for Non-IE
		pi_lastSlideshowNum=num; // slideshow will restart from current image
		startSemSlideshow(); // RESTART slideshow
	} 
	else if (num==-1) { // manual change to 'previous picture'
		if (pi_lastSlideshowNum>1) {pi_lastSlideshowNum--;}
		else {pi_lastSlideshowNum=sa_SemPics_Loaded.length-1;}
		effect=""; // effect="Wipe_Right"; interval=0.1; // Wipe_Up
	}
	else if (num==0) { // manual change to 'next picture'
		if (pi_lastSlideshowNum<sa_SemPics_Loaded.length-1) {pi_lastSlideshowNum++;}
		else {pi_lastSlideshowNum=1;}
		effect=""; // effect="Wipe_Left"; interval=0.1; // Wipe_Down 
	}
	else { // auto-rotate to 'next picture' (slideshow mode)
		if (pi_lastSlideshowNum<sa_SemPics_Loaded.length-1) {pi_lastSlideshowNum++;}
		else {pi_lastSlideshowNum=1;}
	}
	//debug ("pic="+pi_lastSlideshowNum, 1); // DEBUG

	/* OLD METHOD - GET FOLDER FROM 'CURRENT IMAGE'
	var curPic=$("#SeminarImg").src();
	var picFolder=curPic.substr (0, curPic.lastIndexOf ("/")+1);
	*/
	var newPic=sa_SemPics_Loaded [pi_lastSlideshowNum]; // s_SemPicsFolder+
	if (!newPic) {return;} // make sure the pic exists!

	var newLink=s_SemLinksRoot+sa_SemLinks [pi_lastSlideshowNum]+"&link=4";
	var newCaption=sa_SemCaptions [pi_lastSlideshowNum];
	var newColor=sa_SemColors [pi_lastSlideshowNum];
	if (newColor && newColor.substr (0,1) !="#") newColor="#"+newColor;

	$("#SeminarImg").imgChange (newPic, effect, interval); // Pixelate | Wheel | Wipe
	$("#SeminarLink").href (newPic); // link used to 'popup' larger img
	$("#SeminarCaption").html (newCaption).href (newLink);
	$("#SeminarHeading").color (newColor);
	window.defaultStatus=(newCaption) ? "SEMINAR: "+newCaption : "";
	
	// if this is a MANUAL PIC CHANGE, then RESTART Slideshow so 'delay' is reset
	if (typeof num=="number") {startSemSlideshow();}

	return false; // ALWAYS return false because might be called from hyperlink
}

function zoomSemPic (img, args) {
	var link=$(img.parentNode);
	if (args[0]) {
		link.addClass('PhotoHidden');
		if (pi_timerSeminarSlideshow) stopSemSlideshow();
	} else {
		link.removeClass('PhotoHidden');
		if (sa_SemPics.length>1) {
			showSemPic(); // IMMEDIATELY flip to the next image, then...
			startSemSlideshow(); // restart slideshow (image will change again AFTER 'delay')
		}
	}
}


/**
* DEBUGGING UTILITY - WILL BE TRANSFERRED TO 'global.js' LATER
*/
function debug (sInfo, fAppend) {
	if (!sInfo) {window.defaultStatus="";}
	else if (fAppend) {
		if (window.defaultStatus !="") {window.defaultStatus+=" / ";}
		window.defaultStatus+=sInfo;
	}
	else {window.defaultStatus=sInfo;}
	// check for additional strings passed as argument #3, #4, etc.
	if (arguments.length > 2) {
		for (var i=2; i <arguments.length; i++) {
			window.defaultStatus+=" ~ "+arguments [i];
		}
	}
}


/**************************************************************************************/


/**
* SAMPLE PRELOADER OBJECT - FOR FUTURE REF
*/
function ImagePreloader (arrImages, callback) {
	// store the call-back
	this.callback = callback;
	// initialize internal state.
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array;
	// record the number of images.
	this.nImages = arrImages.length;
	// for each image, call preload()
	for (var i=0; i <this.nImages; i++ ) this.preload (arrImages [i]);
}

ImagePreloader.prototype.preload = function(image) {
	// create new Image object and add to array
	var oImage = new Image;
	this.aImages.push (oImage);

	// set up event handlers for the Image object
	oImage.onload = ImagePreloader.prototype.onload;
	oImage.onerror = ImagePreloader.prototype.onerror;
	oImage.onabort = ImagePreloader.prototype.onabort;

	// assign pointer back to this.
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;

	// assign the .src property of the Image object
	oImage.src = image;
}

ImagePreloader.prototype.onComplete = function() {
	this.nProcessed++;
	if (this.nProcessed==this.nImages) {
		this.callback (this.aImages, this.nLoaded);
	}
}

ImagePreloader.prototype.onload = function() {
	this.bLoaded = true;
	this.oImagePreloader.nLoaded++;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function() {
	this.bError = true;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function() {
	this.bAbort = true;
	this.oImagePreloader.onComplete();
}


/**************************************************************************************/


/**
 * ImageCollection Object
 *
 * @description Can preload, swap and rotate images - optionally using a 'transition effect'
 * @returns ImageCollection object
 * @requires jQuery library (could be replaced easily though)
 */
var ImageCollection = function () {
	this._arrImageNames=[]; // string names of ALL Images - blank names and 'load errors' will be REMOVED!
	this._arrImages=[]; // LOADED IMAGES (Objects)
	this._imgFolder=""; // common folder for all images
	this._keepExactIndex=false; // do we need to 'skip bad indexes'? - NOT CURRENTLY IMPLEMENTED!
	this._numImages=0; // number of LOADED IMAGES - incremented as images load
	this._curImage=0; // index of currently displayed image - updated anytime an image is returned
	this.getFolder = function () {return this._imgFolder;} // ALL Images, loaded or not
	this.getTotalImages = function () {return this._arrImageNames.length;} // ALL Images, loaded or not
	this.getImageCount = function () {return this._numImages;} // LOADED Images
	this.getCurrentImageIndex = function () {return this._curImage;}
};

/* these methods setup and preload the images */

ImageCollection.prototype.setImages = function (sa_Images, s_ImgFolder, f_KeepExactIndex) {
	this._keepExactIndex=(f_KeepExactIndex) ? true : false; // NOT CURRENTLY IMPLEMENTED!
	this._arrImageNames=sa_Images; // init assuming a sting-array is passed
	if (typeof sa_Images=="string") { // delimited string - convert to array
		this._arrImageNames=[sa_Images]; // init assuming a 'single element array'
		// now check for a 'delimited string'
		var sep=[",",";","|"," "]; // all valid separators ('spaces' LAST)
		for (var i=0; i < sep.length; i++) {
			if (sa_Images.indexOf (sep[i]) >0) {this._arrImageNames=sa_Images.split (sep[i]); break;}
		}
	}
	this._imgFolder=(s_ImgFolder) ? s_ImgFolder : "";
	if (s_ImgFolder) {
		this._imgFolder=s_ImgFolder;
		if (s_ImgFolder.substr (s_ImgFolder.length-1) !="/") this._imgFolder+="/";
	}
	this.__preLoad(); // now preload each image - 1 at a time
};

ImageCollection.prototype.__preLoad = function () {
	var s_Img;
	while (this._numImages < this._arrImages.length && !s_Img) {
		s_Img=$.trim (this._arrImageNames [this._numImages]);
		if (!s_Img) this._arrImageNames.splice (this._numImages, 1); // empty name - remove from array - loop again!
	}
	if (s_Img) {
		//var img=document.createElement ("img");
		var img=new Image();
		img.onload=this.__appendImage (img);
		img.onerror=this.__abortImage (this._numImages);
		img.src=this._imgFolder+s_Img;
	}
}

ImageCollection.prototype.__abortImage = function (idx) {
	this._arrImageNames.splice (idx, 1); // remove img from array
	this.__preLoad(); // try the next image - if exists
}

ImageCollection.prototype.__appendImage = function (o_PreloadedImage) {
	this._arrImages.push (o_PreloadedImage);
	this._numImages++;
	this.__preLoad(); // preload the next image - if exists
}

/* these methods return the 'index' or 'filename' of the images */

ImageCollection.prototype.getImageNames = function () {
	if (!this._arrImageNames || !this._arrImageNames.length) return null;
	return this._arrImageNames;
};

ImageCollection.prototype.getImageName = function (idx) {
	if (!this._arrImageNames || !this._arrImageNames.length ||  ! idx >=0) return null;
	return (idx < this._arrImageNames.length) ? this._arrImageNames [idx] : -1; // -1 = idx is out of bounds!
};

ImageCollection.prototype.getImageIndex= function (filename) {
	var idx= -1; // init
	if (typeof filename=="string") { // just to be sure
		for (var i=0, count=this._arrImageNames.length; i <count; i++) {
			if (filename==this._arrImageNames [i]) idx=i; break;
		}
	}
	return idx;
};

/* these methods return a 'preloaded image object(s)' */

ImageCollection.prototype.getAllImages = function () {
	if (this._numImages==0) return null;
	return this._arrImages;
};

ImageCollection.prototype.getFirstImage = function (f_setAsCurrent) {
	if (this._numImages==0) return null;
	if (f_setAsCurrent ) this._curImage=0;
	return this._arrImages [0];
};

ImageCollection.prototype.getLastImage = function (f_setAsCurrent) {
	if (this._numImages==0) return null;
	if (f_setAsCurrent) this._curImage=this._numImages-1;
	return this._arrImages [this._numImages-1];
};

ImageCollection.prototype.getImage = function (idx, f_setAsCurrent) {
	if (this._numImages==0 || ! idx >=0) return null;
	if (idx < this._numImages) idx=this._numImages-1; // return 'last image' if idx is out of bounds!
	if (f_setAsCurrent) this._curImage=idx;
	return this._arrImages [idx];
};

/* these methods replace an image with a preloaded image */

ImageCollection.prototype.swap = function (o_Img, idx, transition, speed) {
	if (typeof idx=="string") idx=this.getImageIndex (idx);
	if (idx>=0 && idx<this._numImages) {
		this._curImage=idx;
		var src=this.getImage (idx).src;
		if (transition) $(o_Img).imgChange (src, transition, speed);
		else $(o_Img).src (src);
	}
};

ImageCollection.prototype.next = function (o_Img, transition, speed) {
	this._curImage++;
	if (this._curImage==this._numImages) this._curImage=0;
	var src=this.getImage (this._curImage).src;
	if (transition) $(o_Img).imgChange (src, transition, speed);
	else $(o_Img).src (src);
};

ImageCollection.prototype.previous = function (o_Img, transition, speed) {
	this._curImage--;
	if (this._curImage<0) this._curImage=this._numImages-1;
	var src=this.getImage (this._curImage).src;
	if (transition) $(o_Img).imgChange (src, transition, speed);
	else $(o_Img).src (src);
};

