$(document).ready(function(){

var height = 700;
if($(window).height() > height) height = $(window).height();
$("#content").css("min-height", height);	

$("#indeinembundesland button").hide();
$("#indeinembundesland select").change(function(){
	$("#indeinembundesland form").submit();
});



toolTip();



//CHANGE SCHULRECHT INPUT WORDS
var words = [
	" Sitzenbleiben ", 
	" Leistungsbeurteilung ", 
	" Fehlstunden ", 
	" Schularbeiten ", 
	" Mitbestimmung " 
	];

var randIndex = Math.ceil(Math.random()*words.length);
replaceWordInSitzenbleibenInputField(words, randIndex, 0);

var jetzt = new Date();
var ajaxTime = jetzt.getTime();



//FOTOS STARTSEITE
$("a[rel=fotos]").fancybox({
	'transitionIn'		: 'none',
	'transitionOut'		: 'none',
	'titlePosition' 	: 'over',
	'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
		return '<span id="fancybox-title-over">Foto ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
	}
});

$("a[class=fancy_fotos]").fancybox({
	'transitionIn'		: 'none',
	'transitionOut'		: 'none',
	'titlePosition' 	: 'over',
	'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
		return '<span id="fancybox-title-over">Foto ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
	}
});


$("a.fancy").fancybox();


$("a.fancyAjax").fancybox({
	'titlePosition'		: 'inside',
	'transitionIn'		: 'none',
	'transitionOut'		: 'none', 
	ajax : {
		type	: "POST", 
		data	: "contentOnly=true"
	}

});




//FOTO SLIDESHOW MEDIASEITE


//AJAXBOX STARTSEITE
$("#schulrechtsfrage button").hide();
$("#schulrechtsfrage input").keyup(function() {
		var form = $(this).parent().parent();
		var searchterm = form.find("input").val();
		var searchname = form.find("input").attr("name");
		var submitname = form.find("button").attr("name");
		var submitterm = "true";
		var action = form.attr("action");

		//nur 10 Aufrufe/Sekunde erlauben		
		var curTime = new Date();
		
		if(searchterm != "" && (curTime - ajaxTime) > 200) {
			form.find(".loading").show();
			var dataString = searchname+"="+searchterm+"&"+submitname+"="+submitterm;
			
			$.ajax({
				url: action+"/smallAjax",
				cache: false, 
				data: dataString, 
				type: "POST", 
				success: function(html)
				{
					form.nextAll().remove();
					form.find(".loading").hide();
					form.parent().append(html);
					form.nextAll().hide();
					form.nextAll().fadeIn();
					
					var curTime = new Date();
					ajaxTime = curTime.getTime();
		  		}
			});	
		}
		else {
			form.nextAll().remove();
		}
	});




//SHOP SLIDE UP
$("h3.shopcat + ul").hide();
$("h3.shopcat").each(function(){
	$(this).html('<a href="javascript:;">'+$(this).html()+'</a>')
	});
$("h3.shopcat").click(function(){
	$("h3.shopcat + ul").fadeOut("fast");
	$(this).next().toggle();
	});








//BRANDING AJAX
//reloadImage(10000);



//AJAXBOX AJAX
$("form.ajaxsearch button").hide();
$("form.ajaxsearch input").keyup(function() {

		var form = $(this).parent().parent();

		var searchterm = form.find("input").val();
		var searchname = form.find("input").attr("name");
		var submitname = form.find("button").attr("name");
		var submitterm = "true";
		var action = form.attr("action");
		
		//nur 10 Aufrufe/Sekunde erlauben		
		var curTime = new Date();
		
		if(searchterm != "" && (curTime - ajaxTime) > 200) {
			form.find(".loading").show();
			var dataString = searchname+"="+searchterm+"&"+submitname+"="+submitterm;
			
			$.ajax({
				url: action+"/ajax",
				cache: false, 
				data: dataString, 
				type: "POST", 
				success: function(html)
				{
					form.nextAll().remove();
					form.find(".loading").hide();
					form.parent().append(html);
					form.nextAll().hide();
					form.nextAll().fadeIn();
					
					var curTime = new Date();
					ajaxTime = curTime.getTime();
		  		}
			});
			
		}
	});
	





if($("#navigation ol li ul li.active").size() == 0) {

	/* Das Submen ein/ausblenden */
	/*$("#navigation ol li ul").hide();

	$("#navigation").hoverIntent({    
		over: slideSubNavigationDown, // function = onMouseOver callback (REQUIRED)    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: slideSubNavigationUp // function = onMouseOut callback (REQUIRED)    
	});		
	
	$("#navigation ol li a").mouseenter(function(event){
		var timeoutId = setTimeout( function() {
			$("#navigation ol li ul").slideDown("fast");
		}, 200);
		 $("#navigation ol li a").data('timeoutId', timeoutId);
	});
	
	$("#navigation ol li a > *").mouseenter(function(event){
		var timeoutId = setTimeout( function() {
			$("#navigation ol li ul").slideDown("fast");
		}, 200);
		 $("#navigation ol li a").data('timeoutId', timeoutId);
	});
	
	$("#navigation ol li a").mouseout(function(event){
		clearTimeout($(this).data('timeoutId'));
	});*/
}

function slideSubNavigationDown() { /* Wir brauchen nur die outFunktion, um die overFunktion kmmert sich wer anders */ }
function slideSubNavigationUp() { $("#navigation ol li ul").slideUp("fast"); }


function reloadImage(time) {
	var timeoutId = setTimeout( function() {
		$.get('randumbrand', function(data) {
			$('#branding img').fadeOut(500);
			var timeoutId = setTimeout( function() {
				$('#branding').html(data);
			}, 500);
		});
		reloadImage(30000);
	}, time);
}

function replaceWordInSitzenbleibenInputField(words, index, time) {
	var timeoutId = setTimeout( function() {
		
		if(index >= words.length) index = 0;
		var curName = words[index];
		$("#schulrechtsfrage input").attr("value", curName);
		
		$("#schulrechtsfrage input").focus(function(){
			if($("#schulrechtsfrage input").attr("value") == curName)
				$("#schulrechtsfrage input").attr("value", "");
		});
		
		$("#schulrechtsfrage input").blur(function(){
			if($("#schulrechtsfrage input").attr("value") == "")
				$("#schulrechtsfrage input").attr("value", curName);
		});
		index++;
		
		//replaceWordInSitzenbleibenInputField(words, index, 30000);
	}, time);
}

});
