jQuery(document).ready(function() {
	
	var bannerProperties = {};
	bannerProperties.bannerContainerID = '#homepageBannerContainer';
	bannerProperties.bannerClass = 'div.banner';
	bannerProperties.navigationContainerID = '#homepageBannerNav';
	bannerProperties.navigationLeftID = '#homepageBannerNavLeft';
	bannerProperties.navigationRightID = '#homepageBannerNavRight';
	bannerProperties.navigationButtonClass = 'div.homepageBannerSelect';

	bannerProperties.timerLength = 8000;
	bannerProperties.transitionLength = 750;
	
	var owenSoundBanner = new BannerController(bannerProperties);
	owenSoundBanner.initialize();

});

/* - banner controll constructor; initializes properties - */
function BannerController(bannerProperties)
{
	// initial properties
	this.hasDataError = false;
	// check for supplied properties 
	if(bannerProperties){
		// declarations
		// - properties
		this.timerIndex;
		this.previousBannerIndex = 0;
		this.currentBannerIndex = 0;
		this.bannerProperties = (bannerProperties) ? bannerProperties : {};
		this.timerLength = (this.bannerProperties.timerLength) ? this.bannerProperties.timerLength : 5000;
		this.transitionLength = (this.bannerProperties.transitionLength) ? this.bannerProperties.transitionLength : 1000;
		// - dom objects
		this.bannerContainer = jQuery(this.bannerProperties.bannerContainerID).get(0);
		this.banners = jQuery(this.bannerProperties.bannerContainerID + ' ' + this.bannerProperties.bannerClass);
		this.navContainer = jQuery(this.bannerProperties.navigationContainerID).get(0);
		this.navRight = jQuery(this.bannerProperties.navigationContainerID + ' ' + this.bannerProperties.navigationRightID).get(0);
		this.navLeft = jQuery(this.bannerProperties.navigationContainerID + ' ' + this.bannerProperties.navigationLeftID).get(0);
		this.navButtons = jQuery(this.bannerProperties.navigationContainerID + ' ' + this.bannerProperties.navigationButtonClass);
		// error check properties
		this.errorCheckProperties();
	} else {
		alert('[BannerController] Error: Banner properties not provided.');
		this.hasDataError = true;
	}
}

/* - method to set the initial state of the banner and define interaction functionality - */
BannerController.prototype.initialize = function()
{
	if(!this.hasDataError){
		var i, banner, bannerNavButtonContent, bannerController = this, navButton;
		// prep banner container
		jQuery(this.bannerContainer).css('position', 'relative');
		// prep banner frames
		for(i = 0; i < this.banners.length; i++){
			banner = this.banners[i];
			jQuery(banner).css({'position': 'absolute', 'top': '0', 'left': '0'});
			if(i > 0){
				jQuery(banner).toggle(false);
			}
		}
		// prep nav banner buttons
		// - remove excess nav buttons
		for(i = this.navButtons.length - 1; i > 0; i--){
			navButton = this.navButtons[i];
			jQuery(navButton).remove();
		}
		// - clone left over nav button to match number of banner frames
		for(i = this.banners.length - 1; i > 0 ; i--){
			navButton = this.navButtons[0];
			jQuery(navButton).clone().insertAfter(navButton).find('a').attr('title', (i + 1)).html('' + (i + 1));
		}
		// - reset navButton array
		this.navButtons = jQuery(this.bannerProperties.navigationContainerID + ' ' + this.bannerProperties.navigationButtonClass);
		// - setup nav buttons
		for(i = 0; i < this.navButtons.length; i++){
			navButton = this.navButtons[i];
			// store button's index number on each button
			navButton.index = i;
			// - apply first/selected/last classes
			if(i == 0){
				jQuery(navButton).addClass('bannerSelectFirst');
				jQuery('a', navButton).addClass('selected').attr('title', '1').html('1');
			} else if (i == this.navButtons.length - 1){
				jQuery(navButton).addClass('bannerSelectLast');
			}
			
			// - add click functionality
			jQuery(navButton).click(function(event){
				event.preventDefault();
				bannerController._navButtonClick(this);
			});
		}
		// prep nav left/right buttons
		// - add click functionality
		jQuery(this.navRight).click(function(event){
			event.preventDefault();
			bannerController._navRightClick(this);
		});
		jQuery(this.navLeft).click(function(event){
			event.preventDefault();
			bannerController._navLeftClick(this);
		});
		// - dynamically position buttons
		var navContainerWidth = jQuery(this.navContainer).width() - parseInt(jQuery(this.navLeft).css('width')) - parseInt(jQuery(this.navRight).css('width'));
		var buttonWidth = isNaN(parseInt(jQuery(this.navButtons[0]).outerWidth())) ? 0 : parseInt(jQuery(this.navButtons[0]).outerWidth());
		var buttonMarginLeft = isNaN(parseInt(jQuery(this.navButtons[0]).css('margin-left'))) ? 0 : parseInt(jQuery(this.navButtons[0]).css('margin-left'));
		var buttonMarginRight = isNaN(parseInt(jQuery(this.navButtons[0]).css('margin-right'))) ? 0 : parseInt(jQuery(this.navButtons[0]).css('margin-right'));
		var navButtonCombinedWidth = (buttonWidth + buttonMarginLeft + buttonMarginRight) * this.navButtons.length;
		var navContainerExtraSpace = Math.round((navContainerWidth - navButtonCombinedWidth) / 2);
		//jQuery(this.navLeft).css('margin-right', Math.round(navContainerExtraSpace / 2));
		jQuery(this.navLeft).css('margin-right', navContainerExtraSpace);
		
		// start banner animation
		function timerHandler()
		{
			bannerController.showNextBanner(bannerController);
		}
		this.timerIndex = setTimeout(timerHandler, this.timerLength);
	}
}

BannerController.prototype.errorCheckProperties = function()
{
	if(!this.bannerContainer){
		alert('[BannerController] Error: Banner container not found.');
		this.hasDataError = true;
	}
	if(!this.banners || this.banners.length == 0){
		alert('[BannerController] Error: Individual banners not found.');
		this.hasDataError = true;
	}
	if(!this.navContainer){
		alert('[BannerController] Error: Navigation container not found.');
		this.hasDataError = true;
	}
	if(!this.navRight){
		alert('[BannerController] Error: Navigation right button not found.');
		this.hasDataError = true;
	}
	if(!this.navLeft){
		alert('[BannerController] Error: Navigation left button not found.');
		this.hasDataError = true;
	}
	if(!this.navButtons || this.navButtons.length == 0){
		alert('[BannerController] Error: Individual banner navigation buttons not found.');
		this.hasDataError = true;
	}
}

/* - method to transition to the next banner - */
BannerController.prototype.showNextBanner = function(bannerController)
{
	var i, navButton;
	// set new banner
	bannerController.currentBannerIndex = (bannerController.currentBannerIndex == bannerController.banners.length - 1) ? 0 : bannerController.currentBannerIndex + 1;
	// animate banners
	jQuery(bannerController.banners[bannerController.previousBannerIndex]).stop(true, true).fadeOut(bannerController.transitionLength);
	jQuery(bannerController.banners[bannerController.currentBannerIndex]).stop(true, true).fadeIn(bannerController.transitionLength);
	// update selected nav button
	for(i = 0; i < bannerController.navButtons.length; i++){
		navButton = bannerController.navButtons[i];
		if(i == bannerController.currentBannerIndex){
			jQuery('a', navButton).addClass('selected');
		} else {
			jQuery('a', navButton).removeClass('selected');
		}
	}
	// set previous banner to current banner
	bannerController.previousBannerIndex = bannerController.currentBannerIndex
	// wait to change to next banner
	function timerHandler()
	{
		bannerController.showNextBanner(bannerController);
	}
	bannerController.timerIndex = setTimeout(timerHandler, bannerController.timerLength);
}

/* - navigation handlers - */
BannerController.prototype._navRightClick = function(button)
{
	// stop current timer
	clearTimeout(this.timerIndex);
	// set previous banner index
	this.previousBannerIndex = this.currentBannerIndex;
	// show banner
	this.showNextBanner(this);
}

BannerController.prototype._navLeftClick = function(button)
{
	// stop current timer
	clearTimeout(this.timerIndex);
	// set previous banner index
	this.previousBannerIndex = this.currentBannerIndex;
	// set current banner index
	this.currentBannerIndex -= 2;
	if(this.currentBannerIndex < 0){
		this.currentBannerIndex += this.banners.length;
	}
	// show banner
	this.showNextBanner(this);
}

BannerController.prototype._navButtonClick = function(button)
{
	// stop current timer
	clearTimeout(this.timerIndex);
	// set previous banner index
	this.previousBannerIndex = this.currentBannerIndex;
	// set current banner index
	this.currentBannerIndex = button.index - 1;
	if(this.currentBannerIndex < 0){
		this.currentBannerIndex += this.banners.length;
	}
	// show banner
	this.showNextBanner(this);
}

