/*global Json:false, $:false, Fx:false, base_ajax:false*/

function ImageSlideshow(obj, podID, options) {
	var nextIndex = 0, 
		timer = 0, 
		currentSlide = -1, 
		opacityChange, 
		durationSpeed = 600, 
		jsonObj = Json.evaluate(obj),
		slides = jsonObj.slides,
		currentImageLoaded = 1,
		classNames = options.classNames ? options.classNames : '',
		that = this;
		
	options.delay = options.delay ? options.delay * 1000 : 6000;
	options.buttons = options.buttons ? parseInt(options.buttons) : 0;
	
	that.init = function () {
		var buttons, appenda, timeout, a, i, atags, wrapper;
		// if show buttons is selected and there is more than one slide
		if (parseInt(options.buttons) === 1 && slides.length > 1) {
			
			wrapper = $("isWrapper" + podID);
			
			timeout = function () {
				clearTimeout(timer);
				//function 
				nextIndex = this.rel;
				that.next_slide();
			};
			
			if ($ES('.buttons', wrapper).length) {
				buttons = $E('.buttons', wrapper);
				atags = $ES('.buttons a');
				appenda = false;
			} else {
				appenda = true;
				buttons = document.createElement("div");
				buttons.className = "buttons";
				wrapper.appendChild(buttons);
			}
				
			for (i = 1; i <= slides.length; i += 1) {
				if (appenda) {
					a = document.createElement("a");
					a.innerHTML = i;
				} else {
					a = atags[i - 1];
				}
				a.rel = i - 1;
				a.href = "javascript:void1();";
				a.id = "slideshow_" + podID + "_" + i;
				a.onclick = timeout;
				if (appenda) {
					buttons.appendChild(a);
				}
			}
		}
		
		if (slides.length > 1) {
			that.preload_images();
		}
		that.next_slide();
	};
	
	that.next_slide = function () {
		that.show_slide(nextIndex);
	};
	
	that.show_slide = function (index) {
		index = parseInt(index, 10);
		//alert(index);
		if (currentSlide > -1) {
			clearTimeout(timer);
			
			if ($(slides[currentSlide].divID).innerHTML === "") {
				that.load_content_into_slidearea(currentSlide);
			}
			
			//alert(currentSlide);
			opacityChange = new Fx.Style(slides[currentSlide].divID, 'opacity', {duration: durationSpeed});
			opacityChange.start(0).chain(function () {
				$(slides[currentSlide].divID).style.display = "none";
				if (options.buttons === 1) {
					$("slideshow_" + podID + "_" + (currentSlide + 1)).className = "";
				}
				opacityChange = new Fx.Style(slides[index].divID, 'opacity', {duration: durationSpeed});
				opacityChange.set(0);
				$(slides[index].divID).style.display = "block";
				opacityChange.start(1).chain(function () {
					if (slides.length > 1) {
						timer = setTimeout(that.next_slide, options.delay);
						if (options.buttons === 1) {
							$("slideshow_" + podID + "_" + (index + 1)).className = "current";
						
						}
						
						nextIndex = (index + 1 < slides.length ? index + 1 : 0);
						//alert("nextIndex1=" + nextIndex);
						currentSlide = index;
					}
				});
			});
		} else {	// if the first time, just fade in the new slides
			opacityChange = new Fx.Style(slides[0].divID, 'opacity', {duration: durationSpeed});
			opacityChange.set(0);
			$(slides[0].divID).style.display = "block";
			opacityChange.start(1).chain(function () {
				if (slides.length > 1) {
					timer = setTimeout(that.next_slide, options.delay);
					if (options.buttons === 1) {
						//alert("1 should be current");
						$("slideshow_" + podID + "_1").className = "current";
						
					}
					
					nextIndex = (index + 1 < slides.length ? index + 1 : 0);
					//alert("nextIndex2=" + nextIndex);
					currentSlide = index;
				}
				
			});
		}
	};
	
	that.load_content_into_slidearea = function (slideIndex) {
		//$(slides[slideIndex].divID).style.display = "none";
		base_ajax(slides[slideIndex].divID, "/includes/modules/assets/controllers/Images/fn-slideshow.php", "mode=displaySlide&imageID=" + slides[slideIndex].id + "&classNames=" + classNames, "");
	};
	
	that.preload_images = function () {
		var img = new Image();
		
		if (currentImageLoaded < slides.length) {
			img.onload = function () {
				//alert("Image " + currentImageLoaded + " loaded");
				// once the image loades, load the content into the slide area
				that.load_content_into_slidearea(currentImageLoaded);
				// load the next image
				currentImageLoaded += 1;
				that.preload_images();
			};
			
			img.src = slides[currentImageLoaded].image;
		}
	};
	
	that.randOrd = function () {
		return (Math.round(Math.random()) - 0.5);
	};
	
	// run the constructor
	that.init();
}

function void1() {}
