﻿// Slideshow auf der Startseite

var stage_isScrolling = false;
var stage_picWidth = 773;
var stage_slider;
var stage_bubbles;
var stage_picCount;
var stage_destPic;
var stage_delay = 0.5;
var stage_userAction = false;
var stage_autoScrollDelay = 7000;
var stage_autoScrollHalt = 30000;

function stage_position() {
	var left = stage_slider.getLeft(true);
	var result = -Math.round(left / stage_picWidth);
	return result;
}


function __stage_scrollBy(dir) {
	var n = stage_position() + dir;
	if (n < 0) {
		stage_slider.setLeft(-(stage_picCount * stage_picWidth))
		__stage_scrollTo(stage_picCount - 1);
	} else {
		__stage_scrollTo(n);
	}
}

function stage_scrollBy(dir) {
	stage_userAction = true;
	__stage_scrollBy(dir);
}

function stage_setBubble(i) {
	for (var c = stage_bubbles.first(); null != c; c = c.next()) {
		if (i <= 0) {
			c.radioClass('selected');
			break;
		}
		--i;
	}
}


function stage_scrollDone() {
	if (stage_position() == stage_picCount) {
		stage_slider.setLeft(0);
	}
	stage_isScrolling = false;
}


function __stage_scrollTo(pos) {
	if (!stage_isScrolling) {
		stage_isScrolling = true;
		stage_slider.animate({ left: { to: -(stage_picWidth * pos)} }, stage_delay, stage_scrollDone, 'easeOut', 'run');
		window.setTimeout('stage_setBubble(' + pos % stage_picCount + ')', stage_delay * 500);	
	}
}


function stage_scrollTo(pos) {
	stage_userAction = true;
	__stage_scrollTo(pos);
}


function stage_autoScroll() {
	if (stage_userAction) {
		stage_userAction = false;
		setTimeout('stage_autoScroll()', stage_autoScrollHalt);
	} else {
		__stage_scrollBy(1);
		setTimeout('stage_autoScroll()', stage_autoScrollDelay);
	}

}



var stage_content = [];

function stage_init() {

	var pic_width = 773;
	
	var src = Ext.get('startpage-stage-content');
	if (null == src) {
		alert('kann \'startpage-stage-content\' nicht finden');
		return;
	}

	var dst = Ext.get('startpage-stage-slider');
	if (null == dst) {
		alert('kann \'startpage-stage-slider\' nicht finden');
		return;
	}
	stage_slider = dst;

	/*
	for (var child = src.first(); null != child; child = child.next()) {
	stage_content.push(child);
	dst.appendChild(child.dom.cloneNode(true));
	}
	if (stage_content.length == 0) {
	alert('keine Inhalte in \'startpage-stage-content\'');
	return;
	}
	stage_content.push(stage_content[0]);
	dst.appendChild(stage_content[0].dom.cloneNode(true));
	*/

	var count = 0;
	for (var child = dst.first(); null != child; child = child.next()) {
		++count;
	}
	dst.setWidth(count * pic_width);

	stage_picCount = count - 1;

	var bubble_container = Ext.get('startpage-stage-bubble-container');
	stage_bubbles = bubble_container;

	for (var i = 0; i < count - 1; ++i) {
		var a = bubble_container.createChild({ 'tag': 'a', 'class': 'startpage-stage-pic-bubble', 'href': 'javascript:stage_scrollTo(' + i + ')' });
		a.createChild({ 'tag': 'img', 'src': '/Sites/Usedom/img/spacer.gif', 'width': '9', 'height': '10' });
	}
	bubble_container.first().radioClass('selected');

	setTimeout('stage_autoScroll()', stage_autoScrollDelay);
	
}




