$(document).ready(function() {
	if ($('#backgroundContainer img').length > 0){
		var imageCount = $('#backgroundContainer img').length;
		var index = 0;
		var timeOut = 0;
		var first = false;
		var sssf = false;
		
		function setNextBackgroundImage() {
			if (window.location.href.indexOf('sssf') > 0 && !sssf)
			{
				sssf = true;
				first = true;
				$('div.home-emo img:first').attr('src', $('#backgroundContainer img').eq(index).attr('src'));
			}
			else
			{
				if (!first)
				{
					first = true;
					timeOut = setTimeout(setNextBackgroundImage, 10000);
					return;
				}
				if (index < imageCount - 1)
					index++;
				else
					index = 0;
				$('div.home-emo img:first').fadeTo(1000, 0, function() { 
					$('div.home-emo img:first').attr('src', $('#backgroundContainer img').eq(index).attr('src'));
					$('div.home-emo img:first').fadeTo(1000, 1);
				});
			}
			timeOut = setTimeout(setNextBackgroundImage, 8000);
		}
		
		setNextBackgroundImage();
	}
	$('div.home-emo div.teaser a.open').click(function(e) {
		e.preventDefault();
		$(this).parent().parent().hide();
		$(this).parent().parent().next('div.teaser-open').show();
	});
	
	$('div.home-emo div.teaser-open a.close').click(function(e) {
		e.preventDefault();
		$(this).parent().parent().hide();
		$(this).parent().parent().prev('div.teaser').show();
	});
	
	var camCount = $('div.teaser3-open div.livecam img').length;
	var index = 0;
	$('div.teaser3-open div.livecam img').hide();
	$('div.teaser3-open div.livecam img:first').show();
	
	$('div.teaser3-open div.livecam .next').click(function(e) {
		e.preventDefault();
		$('div.teaser3-open div.livecam img').hide();
		if (index < camCount -1)
			index++;
		else
			index = 0;
		$('div.teaser3-open div.livecam img').eq(index).show();
		$('div.teaser3-open span.fett').html($('div.teaser3-open div.livecam img').eq(index).attr('alt'));
	});
	
	$('div.teaser3-open div.livecam .back').click(function(e) {
		e.preventDefault();
		$('div.teaser3-open div.livecam img').hide();
		if (index > 0)
			index--;
		else
			index = camCount -1;
		$('div.teaser3-open div.livecam img').eq(index).show();
		$('div.teaser3-open span.fett').html($('div.teaser3-open div.livecam img').eq(index).attr('alt'));
	});
});

// galleries
$(document).ready(function() {
    function getQueryStringValue(argument) {
        retVal = '';
        queryString = window.location.search.substring(1);
        values = queryString.split("&");
        for (i=0; i < values.length;i++) {
            value = values[i].split("=");
            if (value[0] == argument) {
                return value[1];
            }
        }
        return retVal;
    }
    
    var index = 0;
    var page = getQueryStringValue('page');
       
    $('ul.list-galerie')
        .find('a, span, img')
        .each(function() {
            $(this).css('display', 'block');
        })

    $('ul.list-galerie a').click(function(e) {
        e.preventDefault();
        $('div.galerie-detail img:first').attr('src', $(this).attr('href'));
        $('ul.list-galerie a').show();
        $('ul.list-galerie span').hide().removeClass('active');
        $('div.galerie-legend div.text').hide();
        $(this).hide();
        $(this).next('span').show().addClass('active');

        index = $('ul.list-galerie a').index(this);
        
        if (index == 0 && (page == '1' || page == ''))
            $('div.galerie-detail a.back').hide();
        if (index < 9 && (index == $('ul.list-galerie a').length - 1))
            $('div.galerie-detail a.next').hide();
    });

    $('ul.list-galerie a').first().click();

    $('div.galerie-detail a.next').click(function(e) {
        e.preventDefault();
        if (index == $('ul.list-galerie a').length - 1)
        {
            page++;
            if (page < 2)
                page = 2;
            var url ='http://' + window.location.host + window.location.pathname + '?page=' + page; 
            window.location = url;
        }
        else
            $('ul.list-galerie .active').parent().next('li').children('a').click();
    });

    $('div.galerie-detail a.back').click(function(e) {
        e.preventDefault();
        if (index == 0)
        {
            page--;
            var url ='http://' + window.location.host + window.location.pathname + '?page=' + page + '&gb=1'; 
            window.location = url;
        }
        else
            $('ul.list-galerie .active').parent().prev('li').children('a').click();
    });
    
    $('div.galerie-legend a.link-download').click(function(e) {
        e.preventDefault();
        var url = '/flumserberg/custompages/downloadimage.aspx?url=' + $('div.galerie-detail img:first').attr('src');
        window.open(url);
    });
    
    if (index == 0 && (page == '1' || page == ''))
        $('div.galerie-detail a.back').hide();
    if (index < 9 && (index == $('ul.list-galerie a').length - 1))
        $('div.galerie-detail a.next').hide();
});

