var aktiv = false;
var fading = false;
var root = 'http://agora2-tourism.net/';

function slideshow_start()
{
	if (aktiv) {
		return;
	}
	
	if (document.location != root) {
		var r = get_cookie('image');
		
		if (r != null) {
			$('#bild0').attr('src', image_path(r));
			$('#bild1').attr('src', image_label_path(r));
		}
		
		aktiv = setInterval(slideshow, 20000);
	} else {
		$('#bild0').attr('src', image_path(3));
		$('#bild1').attr('src', image_label_path(3));
		aktiv = setInterval(slideshow, 20000);
		set_cookie('image', String(3));
	}
}


function slideshow()
{
	var r = Math.floor(Math.random() * 10);
	var image = image_path(r);
	var label = image_label_path(r);
	
	crossfade(document.getElementById('bild0'), image, 2, 'slideshow');
	
	$('#bild1').fadeOut(1000, function() {
		$('#bild1').attr('src', label);
		$('#bild1').fadeIn(1000);
	});
	
	set_cookie('image', String(r));
}


function move_up(new_margin)
{
	document.getElementById('wrapper').style.top = new_margin + 'px';
	document.getElementById('top').innerHTML = '<img src="/images/move_down.png" alt="move_down" onclick="move(\'down\')" />';
	move('up');
	if (aktiv) clearInterval(aktiv); /* Slideshow wird beendet */
}


function move_down(new_margin)
{
	document.getElementById('wrapper').style.top = new_margin + 'px';
	document.getElementById('top').innerHTML = '<img src="/images/move_up.png" alt="move_up" onclick="move(\'up\')" />';
	move('down');
	if (new_margin >155) /* Wenn das DIV#top heruntergefahren wurde, wird die Slideshow wieder gestartet */
		slideshow_start();
}


function move(direction)
{
	current_margin = document.getElementById('wrapper').offsetTop;
	
	if (direction == 'up') {
		if (current_margin > 10) {
			if (! fading) {
				fading = true;
				$('div#country-label').fadeOut('slow', function() {
					fading = false;
				});
			}
			
			setTimeout("move_up(current_margin - 20)",20);
		} else {
			set_cookie('position', 'up');
		}
	}
	
	if (direction == 'down') {
		if (current_margin <= 155) {
			if (! fading) {
				fading = true;
				$('div#country-label').fadeIn('slow', function() {
					fading = false;
				});
			}
			
			setTimeout("move_down(current_margin + 20)",20);
		} else {
			set_cookie('position', 'down');
		}
	}
}


$(document).ready(function()
{
	if (get_cookie('position') == "up") {
		var offset = document.getElementById('wrapper').offsetTop;
		while (offset > 10) {
			offset -= 20;
		}
		move_up(offset);
		$('div#country-label').hide();
	}
});


function set_cookie(name, value)
{
	var date = new Date();
	date = new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365);
	document.cookie = name + '=' + value + ';expires=' + date.toGMTString() + ';path=/;';
}


function get_cookie(name)
{
	var cookie = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
	
	if (cookie) {
		return (unescape(cookie[2]));
	} else {
		return null;
	}
}


function image_path(r)
{
	return '/images/header_images/' + r + '.jpg';
}


function image_label_path(r)
{
	return '/images/header_images/' + r + '_Label.png';
}

