// -----------------------------------------------------------------------------------
// By Josh Cope
// http://blazingwolf.com
//
// Based on code by Scott Upton
// http://www.uptonic.com | http://www.couloir.org
//
// -----------------------------------------------------------------------------------
// --- version date: 7/28/08 --------------------------------------------------------


// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");

/*--------------------------------------------------------------------------*/

function Slideshow(varName, photoArray, photoDir, photoId, pfx) {
	this.varName = varName;
	this.photoArray = photoArray;
	this.photoDir = photoDir;
	this.photoNum = this.photoArray.length;
	this.pfx = pfx || "";
	this.photoId = photoId;
	this.photo = this.pfx + 'Photo';
	this.outBox = this.pfx + 'OuterContainer';
	this.photoBox = this.pfx + 'PhotoContainer';
	this.prevLink = this.pfx + 'PrevLink';
	this.viewPhoto = this.pfx + 'ViewPhoto';
	this.nextLink = this.pfx + 'NextLink';
	this.captionBox = this.pfx + 'CaptionContainer';
	this.caption = this.pfx + 'Caption';
	this.counter = this.pfx + 'Counter';
}
	
Slideshow.prototype.setSrc = function(element,src) {
	$('#'+element).attr("src",src);
}

Slideshow.prototype.setHref = function(element,href) {
	$('#'+element).attr("href",splitURL[0]+href);
}

Slideshow.prototype.setInnerHTML = function(element,content) {
 $('#'+element).html(content);
}

Slideshow.prototype.setNewPhotoParams = function() {
	// Set source of new image
	this.setSrc(this.photo, this.photoDir + this.photoArray[this.photoId][0]);
	// Set anchor for bookmarking
	this.setHref(this.prevLink, "#" + (this.photoId + 1));
	this.setHref(this.nextLink, "#" + (this.photoId + 1));
	$('#' + this.viewPhoto).attr("href", this.photoDir + this.photoArray[this.photoId][0]);
	$('#' + this.viewPhoto).attr("title", this.photoArray[this.photoId][3]);
};

Slideshow.prototype.setLinkSize = function() {
	$('#'+this.prevLink).width(Math.floor(((650) * 0.33)));
	$('#'+this.viewPhoto).width(Math.floor(((650) * 0.33)));
	$('#'+this.nextLink).width(Math.floor(((650) * 0.33)));
	$('#'+this.prevLink).height(488);
	$('#'+this.viewPhoto).height(488);
	$('#'+this.nextLink).height(488);
}

Slideshow.prototype.showPhoto = function(){
	$('#'+this.photo).fadeIn({duration: 500, queue:"global", scope:"box"});
	$('#'+this.counter).fadeIn({duration: 500, queue:"global", scope:"box"});
	$('#'+this.caption).fadeIn({duration: 500, queue:"global", scope:"box"});
}

Slideshow.prototype.nextPhoto = function(){
	// Figure out which photo is next
	(this.photoId == (this.photoArray.length - 1)) ? this.photoId = 0 : this.photoId++;
	this.initSwap();
}
Slideshow.prototype.prevPhoto = function(){
	// Figure out which photo is previous
	(this.photoId == 0) ? this.photoId = this.photoArray.length - 1 : this.photoId--;
	this.initSwap();
}

Slideshow.prototype.initSwap = function() {
	// Begin by hiding main elements
	$('#'+this.photo).hide();
	$('#'+this.caption).hide();
	$('#'+this.counter).hide();
	// Set new dimensions and source, then resize
	this.setNewPhotoParams();
	this.showLinks();
}

Slideshow.prototype.showLinks = function(){
	this.setLinkSize();
	$('#'+this.prevLink).show();
	$('#'+this.viewPhoto).show();
	$('#'+this.nextLink).show();
	this.showPhoto();
}

Slideshow.prototype.autoplay = function() {
	timeout = setTimeout(this.varName + ".nextPhoto();", 3000);
}


/*--------------------------------------------------------------------------*/

// Establish CSS-driven events via Behaviour script
var createRules = function(varName, photoArray, photoDir, photoId, pfx) {
	var returnRules = {};
	returnRules['#' + pfx + 'Photo'] = function(element) {
		element.onload = function() {
			var tempPhoto = new Slideshow(varName, photoArray, photoDir, photoId, pfx);
			tempPhoto.showPhoto();
			tempPhoto.autoplay();					
		}
	};
	returnRules['#' + pfx +	'PrevLink'] = function(element) {
		element.onclick = function(event) {
			event.preventDefault();
		}
	};
	returnRules['#' + pfx + 'ViewPhoto'] = function(element) {
			element.onclick = function(event) {
				event.preventDefault();
			}
		};
	returnRules['#' + pfx + 'NextLink'] = function(element) {
		element.onclick = function(event){
			event.preventDefault();
		}
	};
	returnRules['a'] = function(element) {
		element.onfocus = function() {
			this.blur();
		}
	};
	return returnRules;
};

