var animRunning = true;
var animCount = 0;
var animDuration = 5000;

$(document).ready(function(){
	//Lightbox init
	Shadowbox.init();
	
	//Menu init
	$('ul#menu').superfish();
	
	//Font size
	styleOnload();
	
	//contentswitcher
	if($('.contentswitcher')){
		$('.contentswitcher').each(function(){
			var element =  this;
			var count = $(this).find('ul.switchernav li a').length;
			var height = Math.floor(280/count);
			var padding = Math.floor((height-15)/2);
			height -= padding+2;
			
			//Navigationselemente auf die richtige Groesse setzen
			$(element).find('ul.switchernav li a').each(function(index, value){
				$(this).css({'padding-top': padding, 'height': height});
				
				if(index == 0){
					$(this).addClass('first');
				}
				if(index == $(element).find('ul.switchernav li a').length-1){
					$(this).addClass('last');
				}
				
				$(this).mouseenter(function(){
					animRunning = false;
					
					var a = this;
					
					$(element).find('li.current').removeClass('current');
					$(a).parent().addClass('current');
					
					$(element).find('img').stop().fadeOut('slow', function(){ $(this).css({'display': 'none', 'opacity': ''}); });
					var position = getSiblingId(a);
					/*if(position == 0){
						position = $(a).parent().parent().children().length-1;
					}else{
						position--;
					}*/
					
					var images = $(element).find('img');
					$(images[position]).stop().css({'display': 'none', 'opacity': ''}).fadeIn('slow');
				});
			});
			
			var images = $(element).find('img');
			$(images[0]).css({'display': 'block'});
			var lis = $(element).find('ul.switchernav li');
			$(lis[0]).addClass('current');
			
			//animation starten
			setTimeout(function(){switchContent(element);}, animDuration);
		});
	}
});
$(window).unload( function (){
	styleOnunload();
});

function getSiblingId(elm) {
	return $(elm).parent().parent().children().length - $(elm).parent().nextAll().length -1;
}

function switchContent(element){
	if(animRunning){
		var current = animCount;
		animCount++;
		var count = $(element).find('ul.switchernav li a').length;
		if(animCount >= count){
			animCount = 0;
		}
		var images = $(element).find('img');
		var lis = $(element).find('ul.switchernav li');
		$(images[current]).fadeOut('slow');
		$(images[animCount]).fadeIn('slow');
		$(lis[current]).removeClass('current');
		$(lis[animCount]).addClass('current');
		
		console.log(element);
		
		setTimeout(function(){switchContent(element)}, animDuration);
	}
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = 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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function styleOnload() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  
  var cookie = readCookie("fontSize");
  var fontsize = cookie ? cookie : 100;
  var body=document.getElementsByTagName('body')[0];
  body.style.fontSize = fontsize + '%';
  createCookie("fontSize", fontsize, 365);
}

function styleOnunload() {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

function makeFontBigger(bigger){
	var cookie = readCookie("fontSize");
	var fontsize = cookie ? cookie : 100;
	
	if(bigger){
		fontsize = Number(fontsize) + 10;
	}else{
		fontsize = Number(fontsize) - 10;
	}
	
	var body=document.getElementsByTagName('body')[0];
	body.style.fontSize = fontsize + '%';
	
	createCookie("fontSize", fontsize, 365);
	
	return false;
}

function resetValues(){
	setActiveStyleSheet('default');
	
	var body=document.getElementsByTagName('body')[0];
	body.style.fontSize = '100%';
	createCookie("fontSize", 100, 365);
	
	return false;
}
