/**
 * Constructor for mainExhibitPager, which controls the image rotation for the main exhibits on the home page
 *
 * @param descClass the class name for the queued descriptions
 * @param imageClass the class name for the queued images
 * @param pagerBtns id of the pager button being used
*/
function slideshow(slideContainer, pagerBtns){
	this.PAGER_INTERVAL = 10000; //milliseconds between each page transition
	this.IMAGE_ANIMATION_TIME = 1000; //milliseconds for image fades
	this.SLIDE_WIDTH = 684;
	this.START_PAGE = 1;
	
	this.slideContainer = slideContainer;
	this.pagerBtns = pagerBtns;
	
	this.numPages = $(this.pagerBtns + ' ul li').length;
	
	this.currentPage = null;
	this.nextPage = null;
	
	this.interval = null;
	
	this.init();
}

slideshow.prototype = {
	/**
	 * starts the page transitions and assigns click handlers to pager
	 *
	*/
	init: function(){
		var instance = this;
		
		$(this.slideContainer).css('width', this.SLIDE_WIDTH * this.numPages + 'px');
		
		this.showPage(this.START_PAGE); //show the first page as defined above
		
		$(this.pagerBtns + ' a').click(function(){ //define click handler for pager buttons
			instance.showPage($(this).attr('rel'), true);
			return false;
		});
	},
	
	/**
	 * Method to trigger a page transition
	 *
	 * @param page the page to be shown
	 * @param clicked an optional boolean to determine whether the page was arrived to via a pager button click
	*/
	showPage: function(page, clicked){
		var instance = this;
		
		//clear the slideshow interval if a pager button click occured
		if (clicked == true){
			this.stopInterval();
		}
		
		//indicate the correct pager button
		$(this.pagerBtns + ' a').parents('li').removeClass('on');
		$(this.pagerBtns + ' a[rel=' + page + ']').parents('li').addClass('on');
		
		var travelDistance = -((page - 1) * this.SLIDE_WIDTH);
		
		$(this.slideContainer).animate({left: travelDistance + 'px'}, this.IMAGE_ANIMATION_TIME);
		this.currentPage = page;
		if (instance.interval == null){
			instance.interval = setInterval(function(){
				instance.showPage(instance.getNextPage(instance.getNextPage(parseInt(instance.currentPage)+parseInt(1))));
			}, instance.PAGER_INTERVAL);
		}
	},
	
	/**
	 * Calculate the next page for the setInterval slideshow
	 *
	 * @param page the page to be shown
	 * @return integer of the page to be shown
	*/
	getNextPage: function(page){
		//make sure the next page doesnt exceed the number of pages, if so set back to beginning
		if (page > this.numPages){
			return 1;
		}
		else{
			return page;
		}
	},
	
	getCurrentPage: function(){
		return this.currentPage;
	},
	
	stopInterval: function(){
		clearInterval(this.interval);
		this.interval = null;
	}
}

var slideshowObj = new slideshow('div.slideContainer', 'div.slideNav');
