function addLoadListener(fn)
{
	// Test for W3C listener method
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	// Alternative (Opera) listener method
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	// Microsoft event listener
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	// Legacy style function load list
	else
	{
		var oldfn = window.onload;
		if (typeof window.onload != 'function')
		{
			window.onload = fn;
		}
		else
		{
			window.onload = function()
			{
				oldfn();
				fn();
			};
		}
	}
}

var photos = [

	["avatar", "After infiltrating the Na'vi natives with his avatar, Neytiri teaches Jake how fly on a winged beast.", "&#8212;Avatar", "http://wingclips.com/cart.php?target=product&product_id=17217&category_id=998"],

	["tosavealife", "A local youth leader, Chris, inspires Jake to look for something greater in his life.", "&#8212;To Save A Life", "http://www.wingclips.com/cart.php?target=product&product_id=17159&category_id=974"],

	["giftedhands", "During an interview at Johns Hopkins University, Ben expresses his passion for the human mind.", "&#8212;Gifted Hands", "http://www.wingclips.com/cart.php?target=product&product_id=17222&category_id=1001"]

/*
	["pirates1", "The Lost Boys wonder why American's celebrate Santa Clause.", "", "cart.php?target=product&product_id=16574&category_id=710"],
	["pirates2", "Jack Sparrow is taken&hellip;to a place not of death, but punishment.", "&#8212;Pirates 3: At World&#8217;s End", "cart.php?target=product&product_id=16574&category_id=710"]
*/

];

	// retrieveComputedStyle
	// Gets styles being applied to element from all potential assets
	function retrieveComputedStyle(element, style_property)
	{
		var computed_style = null;

		if (typeof element.currentStyle != "undefined")
		{
			computed_style = element.currentStyle;
		}
		else
		{
			computed_style = document.defaultView.getComputedStyle(element, null);
		}

		return computed_style[style_property];
	}

	// initThemeMenu function
	// Determines height of the inner container
	function initThemeMenu()
	{
		// define variables
		var menu = "themeMenu";
		var scroller = "themeScroller";
		
		if (typeof document.getElementById == 'undefined') return false;
		if (!document.getElementById(scroller)) return false;

		var themeScroller = document.getElementById(scroller);

		themeScroller.style.top = 0;

		if (retrieveComputedStyle(themeScroller, "position") == "relative")
		{
			var relativeHeight = themeScroller.offsetHeight;

			// Switch element to absolutely positioned; get correct height, then switch back
			themeScroller.style.position = "absolute";
			themeScroller.calculatedHeight = themeScroller.offsetHeight;

			// Set calculatedHeight to largest of the two
			if (relativeHeight > themeScroller.calculatedHeight)
			{
				themeScroller.calculatedHeight = relativeHeight;
			}

			themeScroller.style.position = "relative";
		}
		else
		{
			themeScroller.calculatedHeight = themeScroller.clientHeight;
		}

		return true;
	}

	// scrollUp function
	// Animates the scroller upwards
	function scrollDown(increment)
	{
		// define variables
		var scroller = "themeScroller";
		var themeScroller = document.getElementById(scroller);
		var currTop = parseInt(themeScroller.style.top);
		var parentTop = parseInt(retrieveComputedStyle(themeScroller.parentNode, "top"));
		var parentHeight = parseInt(retrieveComputedStyle(themeScroller.parentNode, "height"));

		if (currTop > (themeScroller.calculatedHeight - parentHeight) * -1)
		{
			themeScroller.style.top = (parseInt(themeScroller.style.top) - increment) + "px";
                     var recursion = function() {scrollDown(increment);};
			themeScroller.timeout = setTimeout(recursion, 50);
		}

		return true;
	}

	// scrollDown function
	// Animates the scroller downwards
	function scrollUp(increment)
	{
		// define variables
		var scroller = "themeScroller";
		var increment = 20;
		var themeScroller = document.getElementById(scroller);
		var currTop = parseInt(themeScroller.style.top);
		var parentTop = parseInt(retrieveComputedStyle(themeScroller.parentNode, "top"));
		var parentHeight = parseInt(retrieveComputedStyle(themeScroller.parentNode, "height"));

		if (currTop < 0)
		{
			themeScroller.style.top = (parseInt(themeScroller.style.top) + increment) + "px";
                     var recursion = function() {scrollUp(increment);};
			themeScroller.timeout = setTimeout(recursion, 50);
		}

		return true;
	}

	// scrollTop function
	// Moves scroller to top
	function scrollerTop()
	{
		// define variables
		var scroller = "themeScroller";
		var themeScroller = document.getElementById(scroller);

		themeScroller.style.top = 0 + "px";

		return false;
	}

	// scrollBottom function
	// Moves scroller to bottom
	function scrollerBottom()
	{
		// define variables
		var scroller = "themeScroller";
		var themeScroller = document.getElementById(scroller);
		var parentHeight = parseInt(retrieveComputedStyle(themeScroller.parentNode, "height"));

		var topPosition = ((themeScroller.calculatedHeight - parentHeight));

		var printer = document.getElementById("printer").innerHTML = themeScroller.calculatedHeight;

		themeScroller.style.top = ((themeScroller.calculatedHeight - parentHeight) * -1);

		return false;
	}

	// stopScroll function
	// Stops the scroller timeout
	function stopScroll()
	{
		// define variables
		var scroller = "themeScroller";
		var themeScroller = document.getElementById(scroller);

		clearTimeout(themeScroller.timeout);

		return true;
	}

if (typeof com == 'undefined')
{
	com = new Object();
}

if (typeof com.wingclips == 'undefined')
{
	com.wingclips = new Object();
}

com.wingclips.Slideshow = function(photoID, photoBgID, quoteID, sourceID, linkID)
{	
	this.photo = document.getElementById(photoID);
	this.photoBg = document.getElementById(photoBgID);
	this.quote = document.getElementById(quoteID);
	this.source = document.getElementById(sourceID);
	this.link = document.getElementById(linkID);
	this.timer = 5; // in seconds
	this.transitionTimer = 1; // in seconds
	this.count = 1;
	this.fps = 20;
	
	this.cueNextSlide = function()
	{
		this.next = new Image();
		
		this.next.onerror = function()
		{
			alert('Failed to load next image');
		};
		
		that = this; // Create variable accessible to object methods
		
		this.next.onload = function()
		{
			/*that.quote.innerHTML = '&#8220;' + photos[that.count][1] + '&#8221;';*/

			that.quote.innerHTML = photos[that.count][1];
			
			that.source.innerHTML = photos[that.count][2];
			
			that.link.setAttribute("href", photos[that.count][3]);
		
			that.initTransition();
		};
		
		this.next.src = '/assets/img/slideshow/' + photos[this.count][0] + '.jpg';
	};
	
	this.initTransition = function()
	{
		// Determine number of steps for animation
		this.steps = this.transitionTimer * this.fps;
		
		// Determine which opacity method is supported
		if (typeof this.photo.style.opacity != 'undefined')
		{
			window.otype = 'w3c';
		}
		else if (typeof this.photo.style.MozOpacity != 'undefined')
		{
			window.otype = 'moz';
		}
		else if (typeof this.photo.style.KhtmlOpacity != 'undefined')
		{
			window.otype = "khtml";
		}
		else if (typeof this.photo.filters == 'object')
		{
			window.otype = (this.photo.filters.length > 0 && typeof this.photo.filters.alpha == 'object' && typeof this.photo.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			window.otype = 'none';
		}
		
		// Determine whether top photo will fade in or out (binary)
		this.value = this.count % 2;
		
		if (this.value == 0)
		{
			// Top photo should be changed
			this.targetvisibility = true;
			this.changePhoto = this.photo;
		}
		else
		{
			// Bottom photo should be changed
			this.targetvisibility = false;
			this.changePhoto = this.photoBg;
		}

		this.changePhoto.src = this.next.src;
		this.changePhoto.alt = photos[this.count][1];
		//this.changePhoto.width = this.next.width;
		//this.changePhoto.height = this.next.height;
		
		this.transitionSlides();

		// Increase photo count until it equals number of photos; then reset
		if (++this.count == photos.length)
		{
			this.count = 0;
		}
	};

	this.transitionSlides = function()
	{
		// this.value will be set to 0 if target visibility is visible (true)
		// In that case, increase by 1, divide by number of animation steps, and reassign
		// If target visibility is hidden (false), this.value will be set to 1
		// In that case, reduce by 1, divide by number of steps, and reassign
		this.value += (this.targetvisibility ? 1 : -1) / this.steps;
		
		// Defines ranges and resets targetvisibility to "maximum" in either case
		if (this.targetvisibility ? this.value > 1 : this.value < 0)
		{
			this.value = this.targetvisibility ? 1 : 0;
		}
		
		switch(window.otype)
		{
			case 'ie':
				this.photo.filters.alpha.opacity = this.value * 100;
				break;
			
			case 'khtml':
				this.photo.style.KhtmlOpacity = this.value;
				break;
			
			case 'moz':
				this.photo.style.MozOpacity = (this.value == 1 ? 0.9999999 : this.value);
				break;
				
			default:
				this.photo.style.opacity = (this.value == 1 ? 0.9999999 : this.value);
		}
		
		that = this;
		
		if (this.targetvisibility ? this.value < 1 : this.value > 0)
		{
			var repeat = function()
			{
				that.transitionSlides();
			};
			setTimeout(repeat, 1000 / this.fps);
		}
		else
		{
			var repeat = function()
			{
				that.cueNextSlide();
			};
			window.setTimeout(repeat, that.timer * 1000);
		}
	};
}

function startSlideshow()
{
	var photoID = 'photo';
	var photoBgID = 'photoBg';
	var quoteID = 'quote';
	var sourceID = 'source';
	var linkID = 'btn-view';

	if (typeof document.getElementById == 'undefined') return false;
	if (!document.getElementById(photoID)) return false;
	if (!document.getElementById(photoBgID)) return false;
	if (!document.getElementById(quoteID)) return false;
	if (!document.getElementById(sourceID)) return false;
	if (!document.getElementById(linkID)) return false;

	var homeFeature = new com.wingclips.Slideshow(photoID, photoBgID, quoteID, sourceID, linkID);
	
	var repeat = function()
	{
		homeFeature.cueNextSlide();
	};
	window.setTimeout(repeat, homeFeature.timer * 1000);
}
addLoadListener(startSlideshow);
