//Slider
var cur = 0;
var next, aktiv, len;
var t = 10000;
var w = 960;
var image_rotate = new Array();
var img_count;
var cur_header = 0;
var next_header;
var animation_stop = false;
var interval = 10000;
var content_height;

function popup (url) {
 fenster = window.open(url, "SBS-Datensysteme", "width=750,height=400,resizable=yes,scrollbars=yes");
 fenster.focus();
}

function slider(fx) {
	var s = t/5;
	next = (cur+1!=len) ? cur+1 : 0;
	if (fx=='fade') {
		$("#slider_"+cur).fadeOut(s);
		$("#slider_"+next).fadeIn(s);
		cur = next;
	} else if (fx=='slide') {
		$("#slider_"+cur).animate({"left": "-"+w+"px"}, s, "swing", function() {
			$(this).css({"z-index":"5"}).hide();
		});
		$("#slider_"+next).css({"z-index":"6", "left":w+"px"}).show().animate({"left": "0px"}, s, "swing", function() {
			cur = next;
		});
	}
}

function emptyJSON(obj) {
	for(var i in obj) return false;
	return true;
}


function switchImg(id,bild) {
	document.getElementById(id).src = bild;
}
function effekt(id, wert) {
	if(wert==0 || wert=='nein' || wert=='no') $('#'+id).slideUp('slow');
	else $('#'+id).slideDown('slow');
}
function defaultText(elem, val1, val2) {
	if(elem.value.replace(/^\s+/,'').replace(/\s+$/,'')==val1) elem.value = val2;	
}

$(document).ready(function() {
	//content height
	content_height = $("#content").height();
	$(".img").fancybox({
		'titlePosition'	: 'over',
		'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over"><span class="page">Bild '+(currentIndex + 1)+' / '+currentArray.length+'</span>'+title+'&nbsp;</span>';
		},
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'onComplete'	: function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").show();
			}, function() {
				$("#fancybox-title").hide();
			});
		}
	});
	$("form.wk_list").submit(function() {
		var obj = this;
		var href = $(obj).attr('action');
		target = href+((href.search(/\?/)!='-1')?'&':'?')+'ajaxMode=true&'+$(obj).serialize();
		$.fancybox({
			'titleShow'	: false,
			'width'		: 600,
			'height'	: 495,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'showNavArrows'	: false,
			'href'			: target,
			'orig'			: obj,
			'type'			: 'iframe'
		});
		
		return false;
	});

	$(".wk").click(function () {
		var Ergebnis = this.href.search(/\?/);
		target = this.href+((Ergebnis!='-1')?'&':'?')+'ajaxMode=true';
		$.fancybox({
			'titleShow'	: false,
			'width'		: 600,
			'height'	: 495,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'showNavArrows'	: false,
			'href'			: target,
			'type'			: 'iframe'
		});
		return false;
	});
	
	$(".vid").each(function() {
		this.href = this.href.replace(new RegExp("watch\\?v=", "i"), 'v/');
	});
	$(".vid").fancybox({
		'padding'	: 0,
		'autoScale'	: false,
		'title'		: this.title,
		'titleShow'	: false,
		'width'		: 680,
		'height'	: 495,
		'type'		: 'swf',
		'swf'		: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
		}
	});
	if (window.location.hash!='') {
		var hash = window.location.hash.replace(/#vid_/, "");
		if ($("#v_"+hash).length!=0) {
			$("#v_"+hash).trigger('click');
			return false;
		}
	}
	

	//rennkalender slider
	$("#rk_slider").hover(
		function() {
			$(this).children("#rk_wrapper").stop(true).animate({width:290},600,"swing",function(){$(this).css('overflow','visible');});
		},
		function() {
			$(this).children("#rk_wrapper").css("overflow","hidden").stop(true).animate({width:0},200,'swing');
	});
	kal_bind_hover();

	//Header Slider
	$("#rota_wrapper img").each( function(i) {
		if (i>0) {
			$(this).css("display","none");
		}
		image_rotate[i] = $(this);
	});
	img_count = image_rotate.length;
	if (img_count>1) { $("#rota_wrapper #slider_left").css("display","block").click(function(){animation("prev");});}
	if (img_count>1) { $("#rota_wrapper #slider_right").css("display","block").click(function(){animation("next");});}
	$("#rota_wrapper #slider_left").hover(
		function() {
			animation_stop = true;
		},
		function() {
			t=setTimeout("animation_go()",interval);
	});
	$("#rota_wrapper #slider_right").hover(
		function() {
			animation_stop = true;
		},
		function() {
			t=setTimeout("animation_go()",interval);
	});
	if (img_count>1) t=setTimeout("animation('auto')",interval);
	

	
	//Veranstaltungen Suche
	$(function() {
		var dates = $( "#von, #bis" ).datepicker({
			dateFormat: 'dd.mm.yy',
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "von" ? "minDate" : "maxDate";
					instance = $( this ).data( "datepicker" );
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			},
			beforeShow: function(input, inst) {
				if (this.id == "von") {
					var jetzt = new Date();
					$(this).datepicker("option", "minDate", jetzt);
				}
			}
		});
	});
	
	/* fahrer */
	
	$("#t_ue .t_bild").each( function(i) {
		if (i==0) {
			$(this).addClass('aktiv');
		} else {
			$(this).css('opacity','0.5');
		}
		// Hover
		$(this).hover ( function(){
			if($(this).hasClass('aktiv') == false) {
				$(this).stop(true,true).css('cursor','pointer').animate({"opacity":"1"});
			}
		},
		function() {
			if($(this).hasClass('aktiv') == false) {
				$(this).stop(true,true).css('cursor','default').animate({"opacity":"0.5"});
			}
		});
		// click
		$(this).click( function() {
			new_e = $(this).index();
			old_e = $(this).parent().find(".aktiv").index();
			if (new_e!=old_e) $(this).parent().find(".aktiv").animate({"opacity":"0.5"});
			$(this).parent().find(".t_bild").removeClass('aktiv');
			$(this).addClass('aktiv');
			$(this).parent().parent().children('#t_detail').find('.t_d').removeClass('aktiv');
			$(this).parent().parent().children('#t_detail').find('.t_d').eq(i).addClass('aktiv');
		});
	});
	
	/* HAUPTNAVI */
	//alert($("#navi ul li:has(div)").children().attr('class'));
	$("#navi ul li:has(div)").each( function(){
		$(this).children("div.sub").css("display","none");
		$(this).children("div.sub").children("ul").css({"display":"none","left":"-955px"});
		$(this).hover(function() {
			$(this).children('.navi_button').children('.under').stop(true,true).fadeIn(300);
			if (!$(this).children("div").children("a").hasClass('aktiv')) {
				$(this).children('div.sub').queue("fx", []).css("display","block");
				$(this).children('div.sub').children('ul').queue("fx", []).css({"display":"block","left":"-1305px"}).animate({'left':'-175px'},400);
			}
		},function(){
			$(this).children('.navi_button').children('.under').stop(true,true).fadeOut(200);
			if (!$(this).children("div").children("a").hasClass('aktiv')) {
				$(this).children('div.sub').children('ul').delay(200).animate({'left':'1305px'},400);
				$(this).children('div.sub').delay(500).hide(1);
			}
		});
	});
	
	/* GB */
	$(".gb_eintrag").hover( function() {
		$(".wrapper_eintrag .eintrag_tt").eq($(this).index()).css("display","block");
		var offs = $(this).offset().left;
		$(this).mousemove( function(e) {
			var height = $(".wrapper_eintrag .eintrag_tt").eq($(this).index()).outerHeight();
			$(".wrapper_eintrag .eintrag_tt").eq($(this).index()).css({"left":(e.pageX-offs+80)+'px',"top":(e.pageY-(height/2)-30)+'px'});
		});
	},function () {
		$(".wrapper_eintrag .eintrag_tt").eq($(this).index()).css("display","none");
	});
	
	$("input#name").focus(function() {
		if ($(this).val()==kontakt_name) $(this).val('');
	}).blur(function() {
		if ($(this).val()=="") $(this).val(kontakt_name);
	});
	$("input#strasse").focus(function() {
		if ($(this).val()==kontakt_strasse) $(this).val('');
	}).blur(function() {
		if ($(this).val()=="") $(this).val(kontakt_strasse);
	});
	$("input#ort").focus(function() {
		if ($(this).val()==kontakt_ort) $(this).val('');
	}).blur(function() {
		if ($(this).val()=="") $(this).val(kontakt_ort);
	});
	$("input#plz").focus(function() {
		if ($(this).val()==kontakt_plz) $(this).val('');
	}).blur(function() {
		if ($(this).val()=="") $(this).val(kontakt_plz);
	});
	$("input#mail").focus(function() {
		if ($(this).val()==kontakt_email) $(this).val('');
	}).blur(function() {
		if ($(this).val()=="") $(this).val(kontakt_email);
	});
	$("textarea#entry").focus(function() {
		if ($(this).val()=='Gästebucheintrag') $(this).val('');
	}).blur(function() {
		if ($(this).val()=="") $(this).val('Gästebucheintrag');
	});
	$("textarea#info").focus(function() {
		if ($(this).val()==kontakt_info) $(this).val('');
	}).blur(function() {
		if ($(this).val()=="") $(this).val(kontakt_info);
	});
	$(function() {
		$( "#nl_anab" ).buttonset();
	});
	$(function() {
		$( "#nl_anab #new" ).button({
            icons: {
                primary: "ui-icon-check"
            },
            text: 'anmelden'
        });
		$( "#nl_anab #delete" ).button({
            icons: {
                primary: "ui-icon-check"
            },
            text: 'anmelden'
        });
	});
	/* Newsletter */
	$(".nl_wrapper").each(function() {
		var ele = $(this);
		var height = $(this).children(".nl_body").outerHeight();
		$(this).children(".nl_headline").click(function() {
			if ($(ele).outerHeight()==36) {
				$("#nl_archiv .nl_wrapper").animate({"height":"36px"},300);
				$(ele).animate({"height":(height+36)+"px"},500);
			}
		});
	});
	

});

//Kalender nachladen
function switch_kal(k) {
	$.ajax({
		url:rel_pfad+'module/kalender.php?kalender='+k,
		success: function(data) {
			$('#rk_inhalt').html(data);
			kal_bind_hover();
		},
		beforeSend: function() {
			$("#ajax_loader").css("display","block");
		},
		complete: function() {
			$("#ajax_loader").css("display","none");
		}
	});
	return false;
}
//Kalender bind Hoverelemente
function kal_bind_hover() {
	$(".kalender").hover(
		function() {
			var k_id = $(this).attr('id').substring(8);
			$("#kal_mon_i_"+k_id).show();
		},
		function() {
			var k_id = $(this).attr('id').substring(8);
			$("#kal_mon_i_"+k_id).hide();
	});
	$(".kalender .event,.kalender .events").hover(
		function() {
			var k_id = $(this).attr('id').substring(4);
			var position = $(this).position();
			var info_height = $("#kal_tt_"+k_id).outerHeight();
			$("#kal_tt_"+k_id).css({"top":(position.top-info_height-10)+"px","left":(position.left+50)+"px"}).show();
			$(".kal_info_"+k_id.substring(5)).css('color','#ff0000');
		},
		function() {
			var k_id = $(this).attr('id').substring(4);
			$("#kal_tt_"+k_id).hide();
			$(".kal_info_"+k_id.substring(5)).css('color','#fff');
	});
	/*
	$(".kal_wrapper .kalender .rot-verlauf").click( function() {
		$.ajax({
			url:rel_pfad+'module/kalender.php?day='+$(this).attr('id'),
			success: function(data) {
				$('#day_info').html(data);
			},
			beforeSend: function() {
				$("#ajax_loader").css("display","block");
			},
			complete: function() {
				$("#ajax_loader").css("display","none");
			}
		});
		return false;
	});*/
}


function close_day() {
	$("#day_info").html('');
	return false;
}

$(window).load( function() {
	var akt_hoehe = 0;
	$('#t_detail .t_d').each( function() {
		new_hoehe = $(this).height();
		akt_hoehe = (new_hoehe>akt_hoehe)?(new_hoehe):akt_hoehe;
	});
	$('#t_detail').height(akt_hoehe);
	$(".nl_wrapper:eq(0)").children(".nl_headline").trigger('click');
	show_start();
});

function show_start() {
	$("#facebook").fadeIn(400);
	if (startanzeige==1) {
		$("#content").css('background','none');
		$("#rk_slider").fadeIn(400);
		switch_kal(0);
	} else {
		$("#rk_slider").fadeOut(50);
		/*$("#facebook").fadeOut(50);*/
		$("#content").css("min-height","580px");
	}
	content_height = $("#content").height();
	$("#content_placeholder").css({"height":(614-content_height)+"px"});

}


function animation(art){
	if (animation_stop == false || art != "auto" ) {
		if (art == "next" || art == "auto" ) next_header = (cur_header<(img_count-1))? cur_header+1 : 0;
		else if (art == "prev") next_header = (cur_header>0)? cur_header-1 : (img_count-1);
		//$(image_rotate[cur]).css("display","none");
		//$(image_rotate[next]).css("display","block");
		$(image_rotate[cur_header]).stop(true,true).fadeOut(2000);
		$(image_rotate[next_header]).stop(true,true).fadeIn(2000);
		cur_header = next_header;
	}
	if (art == "auto") t=setTimeout("animation('auto')",interval);
}
function animation_go() {
	animation_stop = false;
}


$(function() {
	//scrollpane parts
	var scrollWrapper = $( ".scroll-wrapper" ),
		scrollContent = $( ".scroll-content" );
	var hoehe = scrollWrapper.height();
	var diff = scrollContent.height()-scrollWrapper.height();
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		orientation: 'vertical',
		value: hoehe,
		min: 0,
		max: hoehe,
		slide: function( event, ui ) {
			if ( scrollContent.height() > scrollWrapper.height() ) {
				var faktor = (hoehe-ui.value)/hoehe;
				scrollContent.css({top:'-'+(Math.round(diff*faktor))+'px'});
			} else {
				scrollContent.css( "top", 0 );
			}
		}
	});
});

var fSlide = {
	elem:			[],				//elements which should be animated
	galW:			0,				//width of the Silder
	galH:			0,
	queue:			null,			//holds the current animated Items
	interval:		40,				//in ms
	step:			0,				//current Speed in pixel
	slow:			0,				//slow Speed
	fast:			15,				//fast Speed
	direction:		'forward', 		//current & default direction
	frameRate:		25,				// 
	orientation:	'horizontal',	//horizontal,vertical
	show_menu:		0,		 		//horizontal,vertical

	init: function (el,orientation,show_menu) {
		if (orientation!=null) {this.orientation = orientation;}
		if (show_menu!=null || show_menu=="") {this.show_menu = show_menu;}
		if (el.lenght!=0) {
			this.interval = Math.floor(1000 / this.frameRate);

			var gal = el.find('.gal ul');
			//init width;
			gal.find('li').each( function() {
				if (fSlide.orientation=='horizontal') {
					fSlide.galW = fSlide.galW+$(this).outerWidth();
				} else {
					fSlide.galH = fSlide.galH+$(this).outerHeight();
				}
			});
			if ((el.width() < this.galW && this.orientation=="horizontal") || (el.height() < this.galH && this.orientation!="horizontal") ) { //viewport smaller than items in it > start animation
				el.css({'height':el.outerHeight()+'px'});
				gal.clone().insertAfter(gal);
				this.elem = el.find('.gal ul');
				this.elem.each( function (i) {
					if (fSlide.orientation=='horizontal') {
						$(this).css({'width':fSlide.galW+'px', 'position': 'absolute', 'top': '0px', 'left': ((fSlide.direction=='lft')?'-':'')+(fSlide.galW*i)+'px'});
					} else {
						$(this).css({'height':fSlide.galH+'px', 'position': 'absolute', 'top': ((fSlide.direction=='lft')?'-':'')+(fSlide.galH*i)+'px', 'left':'0px'});
					}
				});
				
				//init controls and bind events
				if (this.show_menu==1) {
					el.append('<div class="menu"><div class="prev tools"><div class="arrow tools"><span>&lt;</span></div></div><div class="next tools"><div class="arrow tools"><span>&gt;</span></div></div></div>');
					el.find('.menu').fadeIn('slow');
					
					el.find('.menu > .tools').hover(
						function () {
							fSlide.step=fSlide.fast;
							if ($(this).hasClass('prev')) {
								fSlide.moveForward();
							} else {
								fSlide.moveBack();
							}
						}, 
						function () { fSlide.step=fSlide.slow; }
					);
				}
				
				this.elem.hover(
					function () { fSlide.moveStop(); }, 
					function () { fSlide.moveResume(); }
				);
				
				//start animation
				this.moveResume();
			}
		}
	},
	
	moveResume: function () {
		if (this.direction=='forward') {
			this.moveForward();
		} else if (this.direction=='back') {
			this.moveBack();
		}
	},
	
	moveForward: function () {
		this.moveStop();
		this.direction='forward';
		this.queue = window.setInterval("fSlide.slide('forward')",this.interval);
	},
	
	moveBack: function () {
		this.moveStop();
		this.direction='back';
		this.queue = window.setInterval("fSlide.slide('back')",this.interval);
	},
	
	moveStop: function () {
		window.clearInterval(this.queue);
	},
	
	slide: function (d) {
		if (d=='back') {
			if (this.orientation=="horizontal") {
				this.elem.css({'left': '+='+fSlide.step+'px'});
			} else {
				this.elem.css({'top': '+='+fSlide.step+'px'});
			}
		} else {
			if (this.orientation=="horizontal") {
				this.elem.css({'left': '-='+fSlide.step+'px'});
			} else {
				this.elem.css({'top': '-='+fSlide.step+'px'});
			}
		}
		this.elem.each( function () {
			if (fSlide.orientation=="horizontal") {
				var curPos = parseInt($(this).css('left'));
				if (d=='forward' && curPos+fSlide.galW<=0) {
					var diff = fSlide.galW+(curPos+fSlide.galW);
					$(this).css({'left': diff+'px'})
				} else  if (d=='back' && curPos-fSlide.galW>=0) {
					var diff = fSlide.galW-(curPos-fSlide.galW);
					$(this).css({'left': '-'+diff+'px'})
				}
			} else {
				var curPos = parseInt($(this).css('top'));
				if (d=='forward' && curPos+fSlide.galH<=0) {
					var diff = fSlide.galH+(curPos+fSlide.galH);
					$(this).css({'top': diff+'px'})
				} else  if (d=='back' && curPos-fSlide.galH>=0) {
					var diff = fSlide.galH-(curPos-fSlide.galH);
					$(this).css({'top': '-'+diff+'px'})
				}
			}
		});
	}
}

$(document).ready(function() {
	fSlide.init($('#footer .slider'),'horizontal',1);
});

function show_wk(show) {
	if (show==true) {
		$("#snavi .wk_show").removeClass("hidden");
	} else {
		$("#snavi .wk_show").addClass("hidden");
	}
}


