/* -----------------------------------------------------------------------------
		Functions lifted from http://www.hesido.com/web.php?page=javascriptanimation
	//Generic Animation Step Value Generator By www.hesido.com
* ------------------------------------------------------------------------------- */
function easeInOut(minValue,maxValue,totalSteps,actualStep,powr){
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp);
}

function doPosChangeMem(elem,startPos,endPos,steps,delay,powr) {
	//Position changer with Memory by www.hesido.com
	if (elem.intervalID) window.clearInterval(elem.intervalID);
	var actStep = 0;
	elem.intervalID = window.setInterval(
		function() {
			elem.currentPos = [
				easeInOut(startPos[0],endPos[0],steps,actStep,powr),
				easeInOut(startPos[1],endPos[1],steps,actStep,powr)
				];
			elem.style.left = elem.currentPos[0]+"px";
			elem.style.top = elem.currentPos[1]+"px";
			actStep++;
			if (actStep > steps) window.clearInterval(elem.intervalID);
		}
		,delay);

}

/* -----------------------------------------------------------------------------
		Theme switcher (aka "slider")
* ------------------------------------------------------------------------------- */
ThemeSlider = function(){
}

ThemeSlider.prototype = {
	_sliderContent: null, // handle to cell-level element holding all the thumbs, it's the element that we're going to animate
	// animation parameters:
	steps: 21, // total steps of animation (default: 12)
	delay: 40, // intervals the animation will be done in miliseconds (default: 30)
	powr: 0.7, // value used for determining ease-in and out. (default: 0.6)
	// info about the strip being moved:
	_cellWidth: 115, /* width (pixels) for each sample thumbnail div */
	_scrollByStep: 3, // number of cells to show (total move is StepSize * CellWidth)
	_totalCells: 0,
	_startCell: 1,// a one-based pointer to first cell currently being displayed...

	setCount: function(value) { this._totalCells = value; },

	init: function(){
		this._sliderContent = document.getElementById("sliderContent");
		this._sliderContent.style.width = (this._totalCells * this._cellWidth) + "px";
		this._sliderContent.currentPos = [0, 0]; // starting position is 0,0, this attribute holds current x, y coordinates
		this._sliderContent.intervalID = null; // handle to the window interval  

		this.attachEvents();
		this.goTo(1);

	}, // init

	goTo: function(cell){
		this._startCell= cell;
		var endPos = new Array(-((this._startCell-1) * this._cellWidth),0);
		doPosChangeMem(this._sliderContent,this._sliderContent.currentPos,endPos,this.steps,this.delay,this.powr);
	}, // goto

	moveBy: function(cellsToMove){
		// one-based index
		var cell = this._startCell + cellsToMove;
		if (cellsToMove>0){
			if (this._startCell == this._totalCells)
				cell = 1;
			else if (cell > this._totalCells)
				cell = this._totalCells;
		} else {
			if (this._startCell == 1)
				cell = this._totalCells;
			else if (cell < 1)
				cell = 1;
		}

		this.goTo(cell);
	},

	left: function(){
		this.moveBy(-this._scrollByStep);
	},
	
	right: function(){
		this.moveBy(this._scrollByStep);
	},

	attachEvents: function(){
		var x = this;
		
		var h= document.getElementById("scrollRightBtn");
		h.href="javascript:void(0);";
		h.onclick = function(){
			x.right();
		}
	
		var h= document.getElementById("scrollLeftBtn");
		h.href="javascript:void(0);";
		h.onclick = function(){
			x.left();
		}

	}
	
} // themeSlider (object)


/* -----------------------------------------------------------------------------
//	Instant Gallery reads HTML and loads the PBTS Photo Gallery
//	It then wipes out the original HTML and loads the Gallery.
//	Format your HTML like this:

<ul id="photoList">
	<li><blockquote>{description}</blockquote>
		<a href="{ignored, for SEO}" title="{ignored, for SEO}">
			<img src="{grabs filename, following last '/'}" 
				alt="{title}" 
				xwidth="{width in pixels}" 
				xheight="{height in pixels}" />
		</a>
		<h6>{extra1}</h6>
	</li>
</ul>
*/

InstantGallery = function(){
}

InstantGallery.prototype = {
	_gallery: null,
	
	get_gallery: function(){ return this._gallery; },

	scrapeHTML: function(){
		var h = document.getElementById("photoList");
		h.style.display = "none";
		var l = h.getElementsByTagName( "li" );
		var img, file, descr, extra1;
		for (var i=0; i < l.length; i++){
			img = l[i].getElementsByTagName( "img" );
			if (img.length > 0){
				descr = l[i].getElementsByTagName( "blockquote" );
				extra1 = l[i].getElementsByTagName( "h6" );
				file = new String(img[0].src);
				file = file.substr(file.lastIndexOf("/")+1);
				this._gallery.addImg( 
					file,
					img[0].alt,
					(descr.length > 0)?descr[0].innerHTML:"",
					img[0].getAttribute('xwidth'),
					img[0].getAttribute('xheight'),
					(extra1.length > 0)?extra1[0].innerHTML:"" );
			}
		}
		h.innerHTML = "";
	},

	next: function(){ this._gallery.gotoNextPic();},
	previous: function(){ this._gallery.gotoPrevPic();},
	help: function(){ this._gallery.showHelp()},

	// ---------------------------------------------------------------------
	// Function: mySlideshow
	// Descr:    a local function wraps the photo gallery's slideshow function
	//           so visual cue (the anchor) can be displayed
	// ---------------------------------------------------------------------
	toggleSlideshow: function(){
		var s= document.getElementById("myslides");
		if (this._gallery.toggleSlideshow()) {
			s.innerHTML="Stop Slideshow";
			s.className="stop";
		} else {
			s.innerHTML="Start Slideshow";
			s.className="start";
		}
	},

	scrollToSelectedThumb: function(idx){
		// todo: scroll
		return;
	},
	
	initialize: function(imgDir,thumbDir){

		// start by loading the pbts Gallery Manager, first shared photo
		this._gallery = new PBTSGallery;
		
		this._gallery.initialize({
			ThumbDir: thumbDir,
			ImgDir: imgDir,
			LoadingImg: "/img/shim.gif",
			Spacer: "/img/shim.gif",
			Rows: 1,
			enableKeys: true,
			PropOrder: ["file","title","description","width","height","extra1","extra2","extra3","extra4","date"],
			ThumbWidth: 100,
			ThumbSpacing: 0,
			ThumbClass: "thumb",
			ThumbSelectedClass: "selected",
			ThumbAll: false,
			ThumbTitles: true,
			defaults: {
				width: 500,
				height: 333
				},
			ids: { 
				pages: "",
				thumbs: "sliderContent",
				image: "thePhoto",
				info: "",
				description: "caption",
				title: "photoTitle",
				Link: "photoPermaLink"
				}
			}
		);

		var x = this;
		this._gallery.set_Callback(
			function(idx){
				x.scrollToSelectedThumb(idx);
			}
		);
		
		// now let's read picture info from Static HTML
		this.scrapeHTML();
		this._gallery.set_Cols(this._gallery.getCount());

		// now blast in the pizza gallery
		this._gallery.writeGallery();	

		var mySlider = new ThemeSlider;
	
		mySlider.setCount(this._gallery.getCount());
		mySlider.init();

	}

}