/*
 *  Written by: Joseph Thibeault
 *  Contact: jthibeault@ironhorsewebsolutions.com
 *  Company: Iron Horse Web & Media Solutions.
 *  Created: 5/7/08
 *  Dependencies: Mootools v1.11 (mootools.net)
 *  Description: Quick and lightweight slideshow display
 *
 *  Liscence: MIT Style Liscence, please retain this copyright notice.
 */

var _slide_time = 7; // in seconds
var _transition_time = 1; // in seconds
var _path = '/assets/templates/sacredheart/images/slides';  // Path to image files with no forward slash at the end.
var _id_of_gallery_container = 'slideshow'; // Id of the slideshow container (the div that the images will be injected into).
var _intended_image_width = 585; // Integer of the intended width of the slide image
var _intended_image_height = 348; // Integer of the intended height of the slide image

// Array of image names with extensions separated by commas
var _images = ['s1.jpg', 's11.jpg', 's12.jpg', 's13.jpg', 's14.jpg', 's15.jpg'];


// --------------------------------------------
// DO NOT EDIT BELOW THIS LINE
// --------------------------------------------
var images = new Array;
var loadedImages = new Array;
var loading = false;
var interval = _slide_time*1000;
var slideIndex = 1;
var prevIndex = 0;

_images.each(function(el, i) {
	var push = _path+"/"+el;
	images.push(push);
});

function initSlideshow() {
	prevIndex = slideIndex-1;
	loadedImages[slideIndex].setStyle('z-index', 1001);
	if(loadedImages[prevIndex]) {
		loadedImages[prevIndex].setStyles({'z-index': 1000});
		var fxFade = new Fx.Tween(loadedImages[prevIndex], {duration: 1000}).start('opacity', 0);
	} else if (prevIndex < 0) {
		loadedImages[loadedImages.length-1].setStyles({'z-index': 1000});
		var fxFade = new Fx.Tween(loadedImages[loadedImages.length-1], {duration: 1000}).start('opacity', 0);
	}
	var fx = new Fx.Tween(loadedImages[slideIndex], {duration: 1000}).start('opacity', 1);
	if(slideIndex < loadedImages.length-1) {
		slideIndex++;
	} else {
		slideIndex = 0;
	}
}

window.addEvent('load', function() {
	if($(_id_of_gallery_container)) {
		var gallery = $(_id_of_gallery_container);
		// Preload images
		new Asset.images(images, {
			onProgress: function(i) {
				loading = true;
				loadedImages[i] = this;
				var topMargin = (gallery.getSize().y / 2) - 15;
				percent = ((i/images.length)*100).round();
				var status = '<div style="font-size: 0.9em; font-weight: bold; margin: '+topMargin+'px 15px 0 15px; text-align: center;">Loading '+percent+'%</div>';
				gallery.empty();
				gallery.innerHTML = status;
				this.setStyles({
					'position': 'absolute',
					'opacity': 0
				});
			},
			onComplete: function() {
				// Initiate slideshow
				loading = false;
				gallery.empty();
				loadedImages.each(function(image, i) {
					image.inject(_id_of_gallery_container).setStyles({'width': _intended_image_width, 'height': _intended_image_height});
				});
				var fx = new Fx.Tween(loadedImages[0], {duration: _transition_time}).start('opacity', 1);
				setInterval("initSlideshow()", interval);
			}
		});
	}
	
	if($defined($$('.ajaxNav')[0])) {
		var links = $$('.ajaxNav ul li a');
		links.each(function(el, i){
			var url = el.getProperty('href');
			el.addEvent('click', function(e) {
				for (j=0;j<links.length;j++) {links[j].setStyles({'color': '#333', 'background': '#e5e5e5', 'margin-top': '0px'});}
				var e = new Event(e).stop();
				$('copy').innerHTML = "<p>Loading...</p>";
				this.setStyles({
					'color': 'white', 
					'background': '#5e8cd0',
					'margin-top': '2px'
				});
				var loadContent = new Request.HTML({
					url: url,
					method: 'get',
					update: $('copy')
				}).post();
			});
			if(i==0) {
				$('copy').innerHTML = "<p>Loading...</p>";
				links[i].setStyles({
					'color': 'white', 
					'background': '#5e8cd0',
					'margin-top': '2px'
				});
				var loadFirst = new Request.HTML({
					url: url,
					method: 'get',
					update: $('copy')
				}).post();
			}
		});
	}
});
