//Anzahl der Elemente der Slideshow
var current_num_object;
var num_objects;
var GET;

function scroll_to(position) {

	$('html, body').animate({ 
	   scrollTop: position}, 
	   1000, 
	   "easeOutQuint"
	);	
}

function get_cookie(name) {
	var cookiename = 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(cookiename) == 0) return c.substring(cookiename.length,c.length);
	}
	return null;
}

function set_cookie(name, value) {

	ablauf = new Date();
	infuenfTagen = ablauf.getTime() + (5 * 24 * 60 * 60 * 1000);
	ablauf.setTime(infuenfTagen);

	document.cookie = name + "=" + value + "; expires=" + ablauf.toGMTString() + "; path=/";
}

function get_GET() {
   var GET = new Array();
   if(location.search.length > 0) {
      var get_param_str = location.search.substring(1, location.search.length);
      var get_params = get_param_str.split("&");
      for(i = 0; i < get_params.length; i++) {
         var key_value = get_params[i].split("=");
         if(key_value.length == 2) {
            var key = key_value[0];
            var value = key_value[1];
            GET[key] = value;
         }
      }
   }
   return(GET);
}

// Call this function to refresh new .fancybox placed ankers
function fancybox_image() {

	$("a.fancyzoom").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition'	: 'over',
		'overlayOpacity' : 0.8,
		'overlayColor': '#000',
		'hideOnContentClick': true
	});
}

function accordion() {
	
	$(".accordion_button > .right").unbind("click");
	
	$(".accordion_button > .right").bind("click", function() {
		
		$('.accordion_content').slideUp('normal');
		
		if ($(this).parent().next().is(":hidden")) {
			
			$(this).parent().next().slideDown('normal');
		}
		else {
			$(this).parent().next().slideUp('normal');
		} 
	});
}

function accordion_hide_all() {
	
	$('.accordion_content').hide();
}

function jump_to_article() {
	
	if (GET["jump_to"] != null) {
		
		//-10 cause of padding
		scroll_to($("#" + GET["jump_to"]).parent().parent().position().top-10);
		$("#" + GET["jump_to"]).parent().parent().parent().next().slideDown('normal');
	}
}

function slide_show_mouselistener() {

	$("#keyvisual").bind("mouseover", function() {
		
		slide_show_automat("stop");
	});
	$("#control_left").bind("mouseover", function() {
		
		slide_show_automat("stop");
	});
	$("#control_right").bind("mouseover", function() {
		
		slide_show_automat("stop");
	});
	$("#keyvisual").bind("mouseout", function() {
		
		slide_show_automat("start");
	});
}

function slide_show_move(type) {		
		
	i=current_num_object;
	
	var start = 1;
	
	width = 1200;
	
	switch (type) {
		
		case "++":
			
			$("#keyvisual .keyvisual_object").each(function() {
				
				
				if (start == 3) {
					
					$(this).fadeIn();
				}
				else {
					$(this).fadeOut();
				}
				$(this).animate({"left": "-=" + width + "px"}, {queue:false}, "slow");
				
				if (start == 1) {
						
					$(".keyvisual_object").last().after('<div class="keyvisual_object" style="left: ' + width*(num_objects-2) + 'px; display: none;">' + $(this).html() + '</div>');
					$(this).remove();
				}
				
				
				start++;
				
			});
			break;
			
		case "--":
		
			$("#keyvisual .keyvisual_object").each(function() {
				
				if (start == 1) {
					
					$(this).fadeIn();
				}
				else {
					$(this).fadeOut();
				}
				$(this).animate({"left": "+=" + width + "px"}, {queue:false}, "slow");
				if (start == num_objects) {
					
					$(".keyvisual_object").first().before('<div class="keyvisual_object" style="left: -' + width + 'px; display: none;">' + $(this).html() + '</div>');
					$(this).remove();
				}
				start++;
			});
			$("#current_num_object").attr("value",current_num_object--);
			break;
	}
	fancybox_image();
}

function slide_show_automat(value) {
	
	switch (value) {
		
		case "start":
		
			$("body").everyTime(3000, function() {
				
				slide_show_move("++");
			});
			break;
			
		case "stop":
			
			$("body").stopTime();
			break;
	}
}

function slide_show() {
	
	//Setze erstes Bild nach links, sodass jeweils rechts und links ein Teaser ist (bei mindestens 3 Teasern)
	i = -1;
	
	num_objects = $("#keyvisual .keyvisual_object").length;
	//Just do this, if there is at least one object in slideshow
	if (num_objects > 1) {
		
		$("#keyvisual .keyvisual_object").each(function() {
			
				width = 1200;
				
				object_left = width*i;
				
				$(this).attr("style","left: " + object_left + "px;");
				
				if (object_left == 0) {
					
					$(this).fadeIn();
				}
				else {
					$(this).fadeOut();
				}
				i++;
		});
	}
	num_objects = i+1;
	
	if (num_objects <= 1) {
		//Entferne Buttons nach rechts und links der Slideshow
		$("#control_left").hide();	
		$("#control_right").hide();
	}
	else {
		slide_show_automat("start");
		slide_show_mouselistener();
	}
}

function menue_hover() {

	$("#navi_main li").each(function() {
		
		$(this).mouseenter(function() {
			
			$(this).find("ul").slideDown(200);
			
		}).mouseleave(function() {
			
			$(this).find("ul").slideUp(200);
		});
	});
}

function navi_sub_slide() {
	
	$(function() {
			
		var offsetTop = $("#navi_sub").offsetTop;
		
		var topPadding;
		
		$(window).scroll(function() {
			
			
			if ($(window).scrollTop() == 0) {
				topPadding = 0;
			}
			else {
				topPadding = 20;
			}
			
			if (document.body.scrollHeight - $(window).scrollTop() > 600) {
				
				if ($(window).scrollTop() > offsetTop) {
					
					
					$("#navi_sub").stop().animate({
						marginTop: $(window).scrollTop() - offset,
						paddingTop: topPadding
					});
				} 
				else {
					$("#navi_sub").stop().animate({
						paddingTop: 0,
						marginTop: 0
					});
				}
			}
			else {
				$("#navi_sub").stop().animate({
					paddingTop: 0
				});
			};
		});
	});
}

function gallery_move(controller, direction, amount) {
	
	switch (direction) {
		
		case "--":
			operator = '+=';
			break;
		case "++":
			operator = '-=';
			break;
	}
	
	anzahl = $(controller).parent().next().find("a").length;
	
	var position = 0;		
	
	$(controller).parent().next().find("a").each(function() {
		
		left = $(this).css("left");
		
		if (position == 0) {
			
			if (parseInt(left) == 0 && operator == "-=") {
				
				$(controller).parent().find(".left").show();
			}
			if (parseInt(left) == amount*(-1) && operator == "+=") {
				
				$(controller).parent().find(".left").hide();
			}
		}
		if (position == anzahl-1) {
			
			if (parseInt(left) <= amount && operator == "-=") {
				
				$(controller).parent().find(".right").hide();
			}
			if (parseInt(left) == 0 && operator == "+=") {
				
				$(controller).parent().find(".right").show();
			}
		}
		
		$(this).animate({"left": operator + amount + "px"}, {queue:false}, "slow");
		position++;
	});
}

function ul_sortable() {

	$("#elements").sortable({revert: true, opacity: 0.6, distance: 5});
	$(".children_small").sortable({revert: true, opacity: 0.6, distance: 5});
	$("#navi_sub_body ul").sortable({revert: true, opacity: 0.6, distance: 5});
}
