
var slideshow;

$(init);

function init () {
	
	$('#slideshow').css({display:"block"});
	slideshow = new SlideShow($('#slideshow'));
	
	$('#navim').css({display:"none"});
	$('#main').css({display:"none"});
	$('#scrollarea').css({display:"none"});
	scrollarea.stop();
}

/* SlideShow */
function SlideShow (target) {
	//alert("SlideShow (" + [target] + ")");
	
	var p = this;
	
	this.target = target;
	this.target
		.css({cursor:"pointer"})
		.click(delegate(this, opening));
	
	this.entries = $('li', this.target);
	
	this.selected;
	this.items_min = 0;
	this.items_max = this.entries.length - 1;
	this.timeout;
	
	this.select(0, 0.0);
}

SlideShow.prototype.next = function () {
	//alert("SlideShow.next ()");
	
	this.select(this.selected + 1);
}

SlideShow.prototype.previous = function () {
	//alert("SlideShow.previous ()");
	
	this.select(this.selected - 1);
}

SlideShow.prototype.select = function (value, time) {
	//alert("SlideShow.select (" + [value, time] + ")");
	
	value = this.getSelected(value);
	if (value == this.selected) return;
	this.selected = value;
	
	time = time != undefined ? time : 2000;
	
	clearTimeout(this.timeout);
	
	this.entries.stop().animate({opacity:0.0}, {duration:time, easing:"easeInOutCubic"});
	$(this.entries[this.selected]).stop().animate({opacity:1.0}, {duration:time, complete:delegate(this, this.shown)});
	
}

SlideShow.prototype.shown = function () {
	//alert("SlideShow.shown (" + [] + ")");
	
	if (this.selected >= this.items_max) {
		this.timeout = setTimeout(opening, 2 * 1000);
	}
	else {
		this.timeout = setTimeout(delegate(this, this.next), 2 * 1000);
	}
}

SlideShow.prototype.stop = function () {
	//alert("SlideShow.stop (" + [] + ")");
	
	clearTimeout(this.timeout);
}

SlideShow.prototype.getSelected = function (value) {
	//alert("SlideShow.getSelected (" + [time] + ")");
	
	if (value < this.items_min) value = value + this.items_max + 1;
	if (value > this.items_max) value = value - this.items_max - 1;
	
	return value;
}


/* Opening */
function opening () {
	
	if (typeof document.documentElement.style.maxHeight == "undefined") {
		$('#body').height(491); // IE6
	}
	else {
		$('#body').height(440);
	}
	
	$('#slideshow').animate({left:865, width:0}, {duration:1000, easing:"easeInOutCubic", complete:delegate(this, openingEnd)});
	
	$('#navim')
		.delay(1200)
		.css({display:"block", marginTop:-491})
		.animate({marginTop:0}, {duration:1200, easing:"easeInOutCubic"});
	
	$('#main')
		.delay(2400)
		.css({display:"block", marginTop:501})
		.animate({marginTop:0}, {duration:1200, easing:"easeInOutCubic"});
	
	$('#scrollarea')
		.delay(3600)
		.css({display:"block", marginTop:501})
		.animate({marginTop:0}, {duration:1200, easing:"easeInOutCubic"});
}

function openingEnd () {
	
	slideshow.stop();
	$('#slideshow').remove();
}








