$(document).ready(function() {
	slideShow();
	$(".inhalt").hide();
	$(".inhalt-1").show();
	$('.titel').each (function (i) {
		$(this).click(function(){
			$('.inhalt-'+(i)).slideToggle("500");
		});
	});
	//$(".inhalte").prepend('<div class="zoomtext"><span class="zoomtitel">Schrift:</span><a href="" class="klein">A</a><a href="" class="normal aktzoom">A</a><a href="" class="gross">A</a></div>');
	//$(".klein").click( function () { $('.kontainer').css('font-size', '12px'); } );
	//$(".normal").click( function () { $('.kontainer').css('font-size', '13px');} );		
	//$(".gross").click( function () { $('.kontainer').css('font-size', '14px');} );
	
});

function init()  {
	var pfad = "http://" + window.location.hostname + window.location.pathname + "#";
	var aktK = aktN = aktG = "";
	switch (get_cookie("page_size")) {
	case '12px':
		aktK = "aktzoom"
		break;
	case '13px':
		aktN = "aktzoom"
		break;
	case '14px':
		aktG = "aktzoom"
		break;
	default:
		aktN = "aktzoom"
		break;
	}
	$(".inhalte").prepend("<div class=\"zoomtext\"><span class=\"zoomtitel\">Schrift:</span><a href=\"" + pfad + "\" class=\"klein " + aktK + "\" onclick=\"$('.kontainer').css('font-size', '12px'); set_cookie('page_size', '12px', 30);\">A</a><a href=\"" + pfad + "\" class=\"normal " + aktN + "\" onclick=\"$('.kontainer').css('font-size', '13px'); set_cookie('page_size', '13px', 30);\">A</a><a href=\"" + pfad + "\" class=\"gross " + aktG + "\" onclick=\"$('.kontainer').css('font-size', '14px'); set_cookie('page_size', '14px', 30);\">A</a></div>");
	
	if(get_cookie("page_size") != null){
		$('.kontainer').css('font-size', get_cookie("page_size"));
	}else{
		$('.kontainer').css('font-size', '13px');  
	}
	$(".zoomtext a").click( 
	  function () {
		  $(".zoomtext a").removeClass('aktzoom');
		  $(this).addClass('aktzoom');
	  }
	);
}

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

function get_cookie(name) {
    var name_eq = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(name_eq) == 0) return c.substring(name_eq.length,c.length);
    }
    return null;
}

function slideShow() {
	//append a LI item to the UL list for displaying 
	$steuerung = '';
	$('.slideshow li').each (function (i) {
		var akt = '';
		if(i == 0) akt = 'class="akt"';
		$steuerung += '<a ' + akt +' rel=' + i + ' href="http://' + window.location.hostname + window.location.pathname + '#">' + (i+1) + '</a>';
	});
	$('ul.slideshow').append('<li id="slideshow-"><div class="slideshow--container"><div class="steuerung">' + $steuerung + '</div><h4></h4><p></p></div></li>');
	
	$('.steuerung a').each (function (i) {
		$(this).click(function(){
			wechsel(i);
			$(".steuerung a").removeClass();
			$(this).addClass('akt');
		});
	});

	//Set the opacity of all images to 0 & display first
	$('ul.slideshow li').css({opacity: 0.0});
	$('ul.slideshow li:first').css({opacity: 1.0});
	
	//Get the  of the first image from REL attribute and display it
	$('#slideshow- h4').html($('ul.slideshow img:first').attr('title'));
	$('#slideshow- p').html($('ul.slideshow img:first').attr('alt'));
		
	//Display Text 
	$('#slideshow-').css({opacity: 0.80});
}

function wechsel(ziel) {
//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ($('ul.slideshow li').eq(ziel));

	if (next.find('img').attr('title') != current.find('img').attr('title')) {
		//Get next image 
		var title = next.find('img').attr('title');	
		var desc = next.find('img').attr('alt');	
	
		//Set the fade in effect for the next image, show class has higher z-index
		next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1500);
		
		//Hide the  first, and then set and display the 
		$('#slideshow-').slideToggle(400, function () { 
			$('#slideshow- h4').html(title); 
			$('#slideshow- p').html(desc); 
			$('#slideshow-').slideToggle(750);
		});		
	
		//Hide the current image
		current.animate({opacity: 0.0}, 1500).removeClass('show');
	}
}
