/*************************************/
/* Slideshow 4 Ciurans :: By UniRing */
/*************************************/

/* Object creation */
slideShow = {};

/* Initialization & image post-loading */
document.observe("dom:loaded", function() {
	var container = $('slideshow-container');
	for(var x=1;x<slideShow.images.length;x++) {
		var img = '';
		img += '<div id="slideshow-image'+x+'" style="position:absolute; display:none;">';
		img += '<a id="collection-gallery-photo'+x+'" href="'+slideShow.images[x].link+'" title="'+slideShow.images[x].coleccion+'">';
		img += '<img src="'+slideShow.images[x].file+'" alt="'+slideShow.images[x].alt+'" title="'+slideShow.images[x].alt+'"/>';
		img += '</a>';
		img += '</div>';
		container.innerHTML += img;
	}
	if (Prototype.Browser.IE) {
		$('slideshow-image0').appear({ afterFinish: function() {
			$('collection-gallery-name').innerHTML = slideShow.images[0].coleccion;
			$('collection-gallery-desc').innerHTML = '<strong>'+slideShow.images[0].serie+'</strong> - '+slideShow.images[0].desc;
			$('collection-gallery-name-container').show();
			$('collection-gallery-desc-container').show();
			slideShow.unlockNav();
		}});
	} else {
		slideShow.lockNav();
		$('slideshow-image0').appear({ afterFinish: function() {
			$('collection-gallery-name').innerHTML = slideShow.images[0].coleccion;
			$('collection-gallery-desc').innerHTML = '<strong>'+slideShow.images[0].serie+'</strong> - '+slideShow.images[0].desc;
			Effect.BlindRight('collection-gallery-name-container',{ duration: 0.5, afterFinish: function() {
				Effect.BlindRight('collection-gallery-desc-container',{ duration: 0.5, afterFinish: function() {
					slideShow.unlockNav();
				}});
			}});
		}});
	}
	slideShow.current = 0;
});

slideShow.loadFirstImage = function() {
	var container = $('slideshow-container');
	var img = '';
		img += '<div id="slideshow-image0" style="position:absolute; display:none;">';
		img += '<a id="collection-gallery-photo0" class="button-collection-gallery" href="'+slideShow.images[0].link+'" title="'+slideShow.images[0].coleccion+'">';
		img += '<img src="'+slideShow.images[0].file+'" alt="'+slideShow.images[0].alt+'" title="'+slideShow.images[0].alt+'"/>';
		img += '</a>';
		img += '</div>';
	container.innerHTML += img;
};

/* Slide to the next image */
slideShow.next = function() {
	slideShow.switchImage(slideShow.current+1);
};

/* Slide to the previous image */
slideShow.prev = function() {
	slideShow.switchImage(slideShow.current-1);
};

/* Switch to another image from the images array */
slideShow.switchImage = function(to) {
	if (!slideShow.locked) {
		slideShow.lockNav();
		if (to >= slideShow.images.length) to = 0;
		if (to < 0) to = slideShow.images.length-1;
		if (Prototype.Browser.IE) {
			$('collection-gallery-name-container').hide();
			$('collection-gallery-desc-container').hide();
			$('collection-gallery-name').innerHTML = slideShow.images[to].coleccion;
			$('collection-gallery-desc').innerHTML = '<strong>'+slideShow.images[to].serie+'</strong> - '+slideShow.images[to].desc;
			$('slideshow-image'+slideShow.current).fade({duration: 1.3});
			$('slideshow-image'+to).appear({duration: 1.3, afterFinish: function() {
				slideShow.current = to;
				$('collection-gallery-name-container').show();
				$('collection-gallery-desc-container').show();
				slideShow.unlockNav();
			}});
		} else {
			Effect.BlindLeft('collection-gallery-desc-container',{ duration: 0.3, afterFinish: function() {
				Effect.BlindLeft('collection-gallery-name-container',{ duration: 0.3, afterFinish: function() {
					$('collection-gallery-name').innerHTML = slideShow.images[to].coleccion;
					$('collection-gallery-desc').innerHTML = '<strong>'+slideShow.images[to].serie+'</strong> - '+slideShow.images[to].desc;
					$('slideshow-image'+slideShow.current).fade({duration: 1.3});
					$('slideshow-image'+to).appear({duration: 1.3, afterFinish: function() {
						slideShow.current = to;
						Effect.BlindRight('collection-gallery-name-container',{ duration: 0.3, afterFinish: function() {
							Effect.BlindRight('collection-gallery-desc-container',{ duration: 0.3, afterFinish: function() {
								slideShow.unlockNav();
							}});
						}});
					}});
				}});
			}});
		}
	}
};

slideShow.lockNav = function() {
	$('prev-collection-gallery').fade({duration: 0, to: 0.01});
	$('next-collection-gallery').fade({duration: 0, to: 0.01});
	slideShow.locked = true;
	slideShow.disableAutoSwitch();
};

slideShow.unlockNav = function() {
	$('prev-collection-gallery').appear({duration: 0});
	$('next-collection-gallery').appear({duration: 0});
	slideShow.locked = false;
	slideShow.enableAutoSwitch();
};

/* Enables the automatic image switch */
slideShow.enableAutoSwitch = function() {
	slideShow.autoSwitchTimer = setInterval(slideShow.next,10000);
};

/* Disables the automatic image switch */
slideShow.disableAutoSwitch = function() {
	if (slideShow.autoSwitchTimer) {
		clearInterval(slideShow.autoSwitchTimer);
	}
};

/* Resets the automatic image switch timer */
slideShow.resetAutoSwitch = function() {
	slideShow.disableAutoSwitch();
	slideShow.enableAutoSwitch();
};
