//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;

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

function confirmation() {
	var an = confirm("Möchten Sie die Datei wirklich löschen?");
	return an;
}

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() {
	$(".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();
			});
		}
	});
	

	$("#upload").click(function () {
		var Ergebnis = this.href.search(/\?/);
		target = this.href+((Ergebnis!='-1')?'&':'?')+'ajaxMode=true';
		
		$.fancybox({
			'titlePosition'	: 'inside',
			'titleShow'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'showNavArrows'	: false,
			'href'			: target,
			'type'			: 'iframe',
			'onClosed'		: function() {location.reload();}
		});
		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;
		}
	}
	
	// Karte
	$("#karte .box_marker").each( function() {
		$(this).children(".marker_hover").hover(function(){
			$(this).next(".box_info").stop(true,true).fadeIn(300);
		},
		function() {
			$(this).next(".box_info").stop(true,true).fadeOut(150);
		});
	});
	$("#legende .l_grp").each(function() {
		$(this).click( function () {
			if (!$(this).hasClass('aktiv')) {
				grp = $(this).attr('class').split(" ");
				$(this).parent().children().removeClass("aktiv");
				$(this).addClass("aktiv");
				$("#karte .box_marker").css("display","none");
				$("#karte ."+grp[1]).css("display","block");
			}
		});
		/*
		$(this).toggle(function() {
			grp = $(this).attr('class').split(" ");
			//$(this).css("background-color","#F8F8F8");
			$(this).removeClass("aktiv");
			$("#karte ."+grp[1]).css("display","none");
		},
		function() {
			grp = $(this).attr('class').split(" ");
			//$(this).css("background-color","#EFEFEF");
			$(this).addClass("aktiv");
			$("#karte ."+grp[1]).css("display","block");
		});*/
	});
	
	//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);
	
	//EventsBox
	$('#cal_monthyear').change(function() {
		if($('.cal_body').length!=0) {
			$('.cal_body').append('<div class="cal_load_bg">&nbsp;</div><div class="cal_loader">&nbsp;</div>');
			$('.cal_load_bg').css({'opacity':0.8});
			var period = $(this).val();
			var content_sprache = $('#cal_sprache').val();
			$('.cal_body').load('/ajax/datepicker.php', {period:period,content_sprache:content_sprache});
		}
		return false;
	});

	
	//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);
				}
			}
		});
	});
});

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;
}

$(window).load(function() {
	if ($('#map_canvas').length!=0) {
		$.getScript(GMap.GMapScriptURL);
	}
});
