/**
 * @author Boston Interactive
 * @version 1.0
 */
if (navigator.appVersion.indexOf("Mac")!=-1) {
var Slideshow = (function(){
	var indexh = 0;
	if(rand) {
		indexh = rand;
		writeURL();
	}
		var browserName=navigator.appName;

	/**
	 * Activates the main program logic.
	 */
	function initialize() {
	//for legacy IE support
		if(!document.addEventListener) {
			document.attachEvent('onkeydown', onDocumentKeyDown, false);
			window.attachEvent('hashchange', onWindowHashChange, false);
		} else {
			document.addEventListener('keydown', onDocumentKeyDown, false);
			window.addEventListener('hashchange', onWindowHashChange, false);
		}
		
		// Read the initial state of the URL (hash)
		readURL();
	}
	/**
	 * Handler for slide navigation click events
	 */
	/*$('.changeSlide').click(function(event) {
		switch( this.getAttribute('id') ) {
				case 'prevSlideBtn': navigateLeft(); break; // left
				case 'nextSlideBtn': navigateRight(); break; // right
			}
			slide();
			
			event.preventDefault();
	});*/

	/**
	 * Handler for the document level 'keydown' event.
	 * 
	 * @param {Object} event
	 */
	function onDocumentKeyDown( event ) {
		
		if( event.keyCode == 37 || event.keyCode == 39 ) {
			
			switch( event.keyCode ) {
				case 37: navigateLeft(); break; // left
				case 39: navigateRight(); break; // right
			}
			
			slide();
			
			//event.preventDefault();
			
		}
	}
	
	/**
	 * Handler for the document level 'mousedown' event.
	 * 
	 * @param {Object} event
	 */
	function onDocumentKeyDown( event ) {
		
		if( event.keyCode >= 37 && event.keyCode <= 40 ) {
			
			switch( event.keyCode ) {
				case 37: navigateLeft(); break; // left
				case 39: navigateRight(); break; // right
			}
			
			slide();
			
			//event.preventDefault();
			
		}
	}
	/**
	 * Handler for the window level 'hashchange' event.
	 * 
	 * @param {Object} event
	 */
	function onWindowHashChange( event ) {
		readURL();
	}
	
	/**
	 * Updates one dimension of slides by showing the slide
	 * with the specified index.
	 * 
	 * @param {String} selector A CSS selector that will fetch
	 * the group of slides we are working with
	 * @param {Number} index The index of the slide that should be
	 * shown
	 * 
	 * @return {Number} The index of the slide that is now shown,
	 * might differ from the passed in index if it was out of 
	 * bounds.
	 */
	function updateSlides( selector, index ) {
		
		// Select all slides and convert the NodeList result to an array
		if (browserName!="Microsoft Internet Explorer"){
			var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) );
		}
		else {
			var slides = [];
			$( selector ).each(function (i) {
				slides[i] = this;
				if(i < index) {
					//past
					slides[i].setAttribute('class', 'past');
				} else {
					//future
					slides[i].setAttribute('class', 'future');
				}
			});
		}

		//var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) );
		
		if( slides.length ) {
			// Enforce max and minimum index bounds
			index = Math.max(Math.min(index, slides.length - 1), 0);
			
			slides[index].setAttribute('class', 'present');

			
			// Any element previous to index is given the 'past' class
			if (browserName!="Microsoft Internet Explorer"){
				slides.slice(0, index).map(function(element){
					element.setAttribute('class', 'past');
				});
			
				// Any element subsequent to index is given the 'future' class
				slides.slice(index + 1).map(function(element){
					element.setAttribute('class', 'future');
				});
			}//end ie
		}
		else {
			// Since there are no slides we can't be anywhere beyond the 
			// zeroth index
			index = 0;
		}
		
		document.body.setAttribute('class', slides[index].getAttribute('id') );
		
		return index;
		
	}
	
	/**
	 * Updates the visual slides to represent the currently
	 * set indices. 
	 */
	function slide() {
		indexh = updateSlides( '#splash>section', indexh );
		writeURL();
	}
	
	/**
	 * Reads the current URL (hash) and navigates accordingly.
	 */
	function readURL() {
		// Break the hash down to separate components
		var bits = window.location.hash.slice(2).split('!');
		
		// Read the index components of the hash
		indexh = bits[0] ? parseInt( bits[0] ) : 0;
		
		navigateTo( indexh );
	}
	
	/**
	 * Updates the page URL (hash) to reflect the current
	 * navigational state. 
	 */
	function writeURL() {
		var url = '!';
		
		// Only include the minimum possible number of components in
		// the URL
		if( indexh > 0 ) url += indexh
		
		window.location.hash = url;
	}
	
	/**
	 * Triggers a navigation to the specified indices.
	 * 
	 * @param {Number} h The horizontal index of the slide to show
	 * @param {Number} v The vertical index of the slide to show
	 */
	function navigateTo( h ) {
		indexh = h === undefined ? indexh : h;
		
		slide();
	}
	
	function navigateLeft() {
		indexh --;
		slide();
	}
	function navigateRight() {
		indexh ++;
		slide();
	}
	
	// Initialize the program. Done right before returning to ensure
	// that any inline variable definitions are available to all
	// functions 
	initialize();
	
	// Expose some methods publicly
	return {
		navigateTo: navigateTo,
		navigateLeft: navigateLeft,
		navigateRight: navigateRight
	};

})();
}
