/**
 * Configuration for site components javascript
 *
 * Configuration array structure:
 * {<component>: {<parameter>: value, <parameter>: value, ... }, ...}
 */
var siteComponentsConfig = {
    'tooltip': {
        'positionby': 'element' //Valid values: mouse (default), element
     },

    'keywords': {
        'elements': ['placeholder-content'],
        'skiptags': ['h1','h2','h3','h4','h5','h6'],
        'usetooltip': true
    }
    
    /*
    // Use this to override the default fontsizes in the fontsize selector
    // (html/lib/fontsize.js)
    
    'fontsize': {
        'sizes': ["10pt", "15pt", "24pt"]
    }
    */
    
};

function slideshowFadeFromTo(current, tileId, action,imageIndex) {
    // Get all slideshow images in this container
	var images  = $$('div#tile'+tileId+' div.slideshow-image');
	//simple way to cast a string to numeric
	current = current-0;
	
	// Find the next image
	if(action == "switch") {
		imageIndex = imageIndex-0;
		next = imageIndex;
		if(current == next) {
			current = next-1;
			if(current < 0) {
				current = images.length - 1;
			}
			else if(current > images.length) {
				current = 0;
			}
		}
	}
	else if(action == 'previous') {
        next = current - 1;
        if(next < 0) {
            next = images.length - 1;
        }
	} else {
	    next = current + 1;
	   
	    if(next >= images.length) {
	       next = 0;
        }
    } 

    var currentImage = images[current];
	var nextImage = images[next];
	
	try {
	    new Effect.Appear(nextImage, {
	        duration: 0.5,
	        afterUpdate: function(effect) {
	            // If this is the first frame, we hide the current
	            // image. We do this here to avoid flicker that
	            // occurs when removing image before effect fades in
	            if(effect.currentFrame == 0) {
	                currentImage.hide();
	            }
	        }
	    });
    } catch (e) {
	    // No scriptaculous - hide/show without effect
	    currentImage.hide();
	    nextImage.show();
    }

    //current = next;
    return next;
}

function Slideshow(slideshow, timeout, speed) {
	this.slides = [];
	var nl = $(slideshow).getElementsByTagName('div');
	for (var i = 0; i < nl.length; i++) {
		if (Element.hasClassName(nl[i], 'slide')) {
			this.slides.push(nl[i]);
		}
	}

	this.timeout = timeout;
	this.current = 0;

	for (var i = 0; i < this.slides.length; i++) {
		this.slides[i].style.zIndex = this.slides.length - i;
	}

	Element.show(slideshow);
	setTimeout((function(){this.next(speed);}).bind(this), this.timeout);
}

Slideshow.prototype = {
	next: function(speed) {
		for (var i = 0; i < this.slides.length; i++) {
			var slide = this.slides[(this.current + i) % this.slides.length];
			slide.style.zIndex = this.slides.length - i;
		}

		Effect.Fade(this.slides[this.current], {
			afterFinish: function(effect) {
				effect.element.style.zIndex = 0;
				Element.show(effect.element);
				Element.setOpacity(effect.element, 1);
			},
			duration: speed
		});

		this.current = (this.current + 1) % this.slides.length;
		setTimeout((function(){this.next(speed);}).bind(this), this.timeout + speed * 1000);
	}
}

Event.observe(window, 'load', function() {
	element = $('rotator');
	if (element != null) {
		element.setStyle({display: 'block'});
	}
});
